GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/cvs/client.c Lines: 0 549 0.0 %
Date: 2017-11-13 Branches: 0 321 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: client.c,v 1.127 2017/08/21 16:45:13 millert Exp $	*/
2
/*
3
 * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4
 *
5
 * Permission to use, copy, modify, and distribute this software for any
6
 * purpose with or without fee is hereby granted, provided that the above
7
 * copyright notice and this permission notice appear in all copies.
8
 *
9
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
 */
17
18
#include <sys/types.h>
19
#include <sys/dirent.h>
20
#include <sys/stat.h>
21
#include <sys/time.h>
22
23
#include <errno.h>
24
#include <fcntl.h>
25
#include <libgen.h>
26
#include <limits.h>
27
#include <pwd.h>
28
#include <stdlib.h>
29
#include <string.h>
30
#include <time.h>
31
#include <unistd.h>
32
33
#include "cvs.h"
34
#include "remote.h"
35
36
struct cvs_req cvs_requests[] = {
37
	/* this is what our client will use, the server should support it */
38
	{ "Root",		1,	cvs_server_root, REQ_NEEDED },
39
	{ "Valid-responses",	1,	cvs_server_validresp, REQ_NEEDED },
40
	{ "valid-requests",	1,	cvs_server_validreq, REQ_NEEDED },
41
	{ "Directory",		0,	cvs_server_directory, REQ_NEEDED },
42
	{ "Static-directory",	0,	cvs_server_static_directory,
43
	    REQ_NEEDED | REQ_NEEDDIR },
44
	{ "Sticky",		0,	cvs_server_sticky,
45
	    REQ_NEEDED | REQ_NEEDDIR },
46
	{ "Entry",		0,	cvs_server_entry,
47
	    REQ_NEEDED | REQ_NEEDDIR },
48
	{ "Modified",		0,	cvs_server_modified,
49
	    REQ_NEEDED | REQ_NEEDDIR },
50
	{ "UseUnchanged",	0,	cvs_server_useunchanged, REQ_NEEDED },
51
	{ "Unchanged",		0,	cvs_server_unchanged,
52
	    REQ_NEEDED | REQ_NEEDDIR },
53
	{ "Questionable",	0,	cvs_server_questionable, REQ_NEEDED },
54
	{ "Argument",		0,	cvs_server_argument, REQ_NEEDED },
55
	{ "Argumentx",		0,	cvs_server_argumentx, REQ_NEEDED },
56
	{ "Global_option",	0,	cvs_server_globalopt, REQ_NEEDED },
57
	{ "Set",		0,	cvs_server_set, REQ_NEEDED },
58
	{ "expand-modules",	0,	cvs_server_exp_modules, 0 },
59
60
	/*
61
	 * used to tell the server what is going on in our
62
	 * working copy, unsupported until we are told otherwise
63
	 */
64
	{ "Max-dotdot",			0,	NULL, 0 },
65
	{ "Checkin-prog",		0,	NULL, 0 },
66
	{ "Update-prog",		0,	NULL, 0 },
67
	{ "Kopt",			0,	NULL, 0 },
68
	{ "Checkin-time",		0,	NULL, 0 },
69
	{ "Is-modified",		0,	NULL, 0 },
70
	{ "Notify",			0,	NULL, 0 },
71
	{ "Case",			0,	NULL, 0 },
72
	{ "Gzip-stream",		0,	NULL, 0 },
73
	{ "wrapper-sendme-rcsOptions",	0,	NULL, 0 },
74
	{ "Kerberos-encrypt",		0,	NULL, 0 },
75
	{ "Gssapi-encrypt",		0,	NULL, 0 },
76
	{ "Gssapi-authenticate",	0,	NULL, 0 },
77
78
	/* commands that might be supported */
79
	{ "ci",			0,	cvs_server_commit,	REQ_NEEDDIR },
80
	{ "co",			0,	cvs_server_checkout,	REQ_NEEDDIR },
81
	{ "update",		0,	cvs_server_update,	REQ_NEEDDIR },
82
	{ "diff",		0,	cvs_server_diff,	REQ_NEEDDIR },
83
	{ "log",		0,	cvs_server_log,		REQ_NEEDDIR },
84
	{ "rlog",		0,	cvs_server_rlog, 0 },
85
	{ "add",		0,	cvs_server_add,		REQ_NEEDDIR },
86
	{ "remove",		0,	cvs_server_remove,	REQ_NEEDDIR },
87
	{ "update-patches",	0,	cvs_server_update_patches, 0 },
88
	{ "gzip-file-contents",	0,	NULL, 0 },
89
	{ "status",		0,	cvs_server_status,	REQ_NEEDDIR },
90
	{ "rdiff",		0,	cvs_server_rdiff, 0 },
91
	{ "tag",		0,	cvs_server_tag,		REQ_NEEDDIR },
92
	{ "rtag",		0,	cvs_server_rtag, 0 },
93
	{ "import",		0,	cvs_server_import,	REQ_NEEDDIR },
94
	{ "admin",		0,	cvs_server_admin,	REQ_NEEDDIR },
95
	{ "export",		0,	cvs_server_export,	REQ_NEEDDIR },
96
	{ "history",		0,	NULL, 0 },
97
	{ "release",		0,	cvs_server_release,	REQ_NEEDDIR },
98
	{ "watch-on",		0,	NULL, 0 },
99
	{ "watch-off",		0,	NULL, 0 },
100
	{ "watch-add",		0,	NULL, 0 },
101
	{ "watch-remove",	0,	NULL, 0 },
102
	{ "watchers",		0,	NULL, 0 },
103
	{ "editors",		0,	NULL, 0 },
104
	{ "init",		0,	cvs_server_init, 0 },
105
	{ "annotate",		0,	cvs_server_annotate,	REQ_NEEDDIR },
106
	{ "rannotate",		0,	cvs_server_rannotate, 0 },
107
	{ "noop",		0,	NULL, 0 },
108
	{ "version",		0,	cvs_server_version, 0 },
109
	{ "",			-1,	NULL, 0 }
110
};
111
112
static void	 client_check_directory(char *, char *);
113
static char	*client_get_supported_responses(void);
114
static char	*lastdir = NULL;
115
static int	 end_of_response = 0;
116
117
static void	cvs_client_initlog(void);
118
119
/*
120
 * File descriptors for protocol logging when the CVS_CLIENT_LOG environment
121
 * variable is set.
122
 */
123
static int	cvs_client_logon = 0;
124
int	cvs_client_inlog_fd = -1;
125
int	cvs_client_outlog_fd = -1;
126
127
128
int server_response = SERVER_OK;
129
130
static char *
131
client_get_supported_responses(void)
132
{
133
	BUF *bp;
134
	char *d;
135
	int i, first;
136
137
	first = 0;
138
	bp = buf_alloc(512);
139
	for (i = 0; cvs_responses[i].supported != -1; i++) {
140
		if (cvs_responses[i].hdlr == NULL)
141
			continue;
142
143
		if (first != 0)
144
			buf_putc(bp, ' ');
145
		else
146
			first++;
147
		buf_puts(bp, cvs_responses[i].name);
148
	}
149
150
	buf_putc(bp, '\0');
151
	d = buf_release(bp);
152
	return (d);
153
}
154
155
static void
156
client_check_directory(char *data, char *repository)
157
{
158
	CVSENTRIES *entlist;
159
	char *entry, *parent, *base, *p;
160
161
	STRIP_SLASH(data);
162
163
	/* first directory we get is our module root */
164
	if (module_repo_root == NULL && checkout_target_dir != NULL) {
165
		p = repository + strlen(current_cvsroot->cr_dir) + 1;
166
		module_repo_root = xstrdup(p);
167
		p = strrchr(module_repo_root, '/');
168
		if (p != NULL)
169
			*p = '\0';
170
	}
171
172
	cvs_mkpath(data, NULL);
173
174
	if (cvs_cmdop == CVS_OP_EXPORT)
175
		return;
176
177
	if ((base = basename(data)) == NULL)
178
		fatal("client_check_directory: overflow");
179
180
	if ((parent = dirname(data)) == NULL)
181
		fatal("client_check_directory: overflow");
182
183
	if (!strcmp(parent, "."))
184
		return;
185
186
	entry = xmalloc(CVS_ENT_MAXLINELEN);
187
	cvs_ent_line_str(base, NULL, NULL, NULL, NULL, 1, 0, entry,
188
	    CVS_ENT_MAXLINELEN);
189
190
	entlist = cvs_ent_open(parent);
191
	cvs_ent_add(entlist, entry);
192
193
	free(entry);
194
}
195
196
void
197
cvs_client_connect_to_server(void)
198
{
199
	struct cvs_var *vp;
200
	char *cmd, *argv[10], *resp;
201
	int ifd[2], ofd[2], argc;
202
203
	if (cvs_server_active == 1)
204
		fatal("cvs_client_connect: I was already connected to server");
205
206
	switch (current_cvsroot->cr_method) {
207
	case CVS_METHOD_PSERVER:
208
	case CVS_METHOD_KSERVER:
209
	case CVS_METHOD_GSERVER:
210
	case CVS_METHOD_FORK:
211
		fatal("the specified connection method is not supported");
212
	default:
213
		break;
214
	}
215
216
	if (pipe(ifd) == -1)
217
		fatal("cvs_client_connect: %s", strerror(errno));
218
	if (pipe(ofd) == -1)
219
		fatal("cvs_client_connect: %s", strerror(errno));
220
221
	switch (fork()) {
222
	case -1:
223
		fatal("cvs_client_connect: fork failed: %s", strerror(errno));
224
	case 0:
225
		if (dup2(ifd[0], STDIN_FILENO) == -1)
226
			fatal("cvs_client_connect: %s", strerror(errno));
227
		if (dup2(ofd[1], STDOUT_FILENO) == -1)
228
			fatal("cvs_client_connect: %s", strerror(errno));
229
230
		close(ifd[1]);
231
		close(ofd[0]);
232
233
		if ((cmd = getenv("CVS_SERVER")) == NULL)
234
			cmd = CVS_SERVER_DEFAULT;
235
236
		argc = 0;
237
		argv[argc++] = cvs_rsh;
238
239
		if (current_cvsroot->cr_user != NULL) {
240
			argv[argc++] = "-l";
241
			argv[argc++] = current_cvsroot->cr_user;
242
		}
243
244
		argv[argc++] = "--";
245
		argv[argc++] = current_cvsroot->cr_host;
246
		argv[argc++] = cmd;
247
		argv[argc++] = "server";
248
		argv[argc] = NULL;
249
250
		cvs_log(LP_TRACE, "connecting to server %s",
251
		    current_cvsroot->cr_host);
252
253
		execvp(argv[0], argv);
254
		fatal("cvs_client_connect: failed to execute cvs server");
255
	default:
256
		break;
257
	}
258
259
	close(ifd[0]);
260
	close(ofd[1]);
261
262
	if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL)
263
		fatal("cvs_client_connect: %s", strerror(errno));
264
	if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL)
265
		fatal("cvs_client_connect: %s", strerror(errno));
266
267
	setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0);
268
	setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0);
269
270
	cvs_client_initlog();
271
272
	if (cvs_cmdop != CVS_OP_INIT)
273
		cvs_client_send_request("Root %s", current_cvsroot->cr_dir);
274
275
	resp = client_get_supported_responses();
276
	cvs_client_send_request("Valid-responses %s", resp);
277
	free(resp);
278
279
	cvs_client_send_request("valid-requests");
280
	cvs_client_get_responses();
281
282
	cvs_client_send_request("UseUnchanged");
283
284
	if (cvs_nolog == 1)
285
		cvs_client_send_request("Global_option -l");
286
287
	if (cvs_noexec == 1)
288
		cvs_client_send_request("Global_option -n");
289
290
	switch (verbosity) {
291
	case 0:
292
		cvs_client_send_request("Global_option -Q");
293
		break;
294
	case 1:
295
		/* Be quiet. This is the default in OpenCVS. */
296
		cvs_client_send_request("Global_option -q");
297
		break;
298
	default:
299
		break;
300
	}
301
302
	if (cvs_readonly == 1)
303
		cvs_client_send_request("Global_option -r");
304
305
	if (cvs_trace == 1)
306
		cvs_client_send_request("Global_option -t");
307
308
	/* XXX: If 'Set' is supported? */
309
	TAILQ_FOREACH(vp, &cvs_variables, cv_link)
310
		cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
311
}
312
313
void
314
cvs_client_send_request(char *fmt, ...)
315
{
316
	int i;
317
	va_list ap;
318
	char *data, *s;
319
	struct cvs_req *req;
320
321
	va_start(ap, fmt);
322
	i = vasprintf(&data, fmt, ap);
323
	va_end(ap);
324
	if (i == -1)
325
		fatal("cvs_client_send_request: could not allocate memory");
326
327
	if ((s = strchr(data, ' ')) != NULL)
328
		*s = '\0';
329
330
	req = cvs_remote_get_request_info(data);
331
	if (req == NULL)
332
		fatal("'%s' is an unknown request", data);
333
334
	if (req->supported != 1)
335
		fatal("remote cvs server does not support '%s'", data);
336
337
	if (s != NULL)
338
		*s = ' ';
339
340
	cvs_log(LP_TRACE, "%s", data);
341
342
	cvs_remote_output(data);
343
	free(data);
344
}
345
346
void
347
cvs_client_read_response(void)
348
{
349
	char *cmd, *data;
350
	struct cvs_resp *resp;
351
352
	cmd = cvs_remote_input();
353
	if ((data = strchr(cmd, ' ')) != NULL)
354
		(*data++) = '\0';
355
356
	resp = cvs_remote_get_response_info(cmd);
357
	if (resp == NULL)
358
		fatal("response '%s' is not supported by our client", cmd);
359
360
	if (resp->hdlr == NULL)
361
		fatal("opencvs client does not support '%s'", cmd);
362
363
	(*resp->hdlr)(data);
364
365
	free(cmd);
366
}
367
368
void
369
cvs_client_get_responses(void)
370
{
371
	while (end_of_response != 1)
372
		cvs_client_read_response();
373
374
	end_of_response = 0;
375
}
376
377
void
378
cvs_client_send_logmsg(char *msg)
379
{
380
	char *buf, *p, *q;
381
382
	(void)xasprintf(&buf, "%s\n", msg);
383
384
	cvs_client_send_request("Argument -m");
385
	if ((p = strchr(buf, '\n')) != NULL)
386
		*p++ = '\0';
387
	cvs_client_send_request("Argument %s", buf);
388
	for (q = p; p != NULL; q = p) {
389
		if ((p = strchr(q, '\n')) != NULL) {
390
			*p++ = '\0';
391
			cvs_client_send_request("Argumentx %s", q);
392
		}
393
	}
394
395
	free(buf);
396
}
397
398
void
399
cvs_client_senddir(const char *dir)
400
{
401
	struct stat st;
402
	int nb;
403
	char *d, *date, fpath[PATH_MAX], repo[PATH_MAX], *tag;
404
405
	d = NULL;
406
407
	if (lastdir != NULL && !strcmp(dir, lastdir))
408
		return;
409
410
	cvs_get_repository_path(dir, repo, PATH_MAX);
411
412
	if (cvs_cmdop != CVS_OP_RLOG)
413
		cvs_client_send_request("Directory %s\n%s", dir, repo);
414
415
	(void)xsnprintf(fpath, PATH_MAX, "%s/%s",
416
	    dir, CVS_PATH_STATICENTRIES);
417
418
	if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
419
		cvs_client_send_request("Static-directory");
420
421
	d = xstrdup(dir);
422
	cvs_parse_tagfile(d, &tag, &date, &nb);
423
424
	if (tag != NULL || date != NULL) {
425
		char buf[128];
426
427
		if (tag != NULL && nb != 0) {
428
			if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf))
429
				fatal("cvs_client_senddir: truncation");
430
		} else if (tag != NULL) {
431
			if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf))
432
				fatal("cvs_client_senddir: truncation");
433
		} else {
434
			if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf))
435
				fatal("cvs_client_senddir: truncation");
436
		}
437
438
		if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf))
439
			fatal("cvs_client_senddir: truncation");
440
441
		cvs_client_send_request("Sticky %s", buf);
442
443
		free(tag);
444
		free(date);
445
	}
446
	free(d);
447
	free(lastdir);
448
	lastdir = xstrdup(dir);
449
}
450
451
void
452
cvs_client_sendfile(struct cvs_file *cf)
453
{
454
	size_t len;
455
	struct tm datetm;
456
	char rev[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ], sticky[CVS_REV_BUFSZ];
457
458
	cvs_client_senddir(cf->file_wd);
459
	cvs_remote_classify_file(cf);
460
461
	if (cf->file_type != CVS_FILE)
462
		return;
463
464
	if (cf->file_ent != NULL && cvs_cmdop != CVS_OP_IMPORT) {
465
		if (cf->file_status == FILE_ADDED) {
466
			len = strlcpy(rev, "0", sizeof(rev));
467
			if (len >= sizeof(rev))
468
				fatal("cvs_client_sendfile: truncation");
469
470
			len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
471
			if (len >= sizeof(timebuf))
472
				fatal("cvs_client_sendfile: truncation");
473
474
			len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
475
			if (len >= sizeof(timebuf))
476
				fatal("cvs_client_sendfile: truncation");
477
		} else {
478
			rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
479
			ctime_r(&cf->file_ent->ce_mtime, timebuf);
480
		}
481
482
		if (cf->file_ent->ce_conflict == NULL) {
483
			timebuf[strcspn(timebuf, "\n")] = '\0';
484
		} else {
485
			len = strlcpy(timebuf, cf->file_ent->ce_conflict,
486
			    sizeof(timebuf));
487
			if (len >= sizeof(timebuf))
488
				fatal("cvs_client_sendfile: truncation");
489
			len = strlcat(timebuf, "+=", sizeof(timebuf));
490
			if (len >= sizeof(timebuf))
491
				fatal("cvs_client_sendfile: truncation");
492
		}
493
494
		sticky[0] = '\0';
495
		if (cf->file_ent->ce_tag != NULL) {
496
			(void)xsnprintf(sticky, sizeof(sticky), "T%s",
497
			    cf->file_ent->ce_tag);
498
		} else if (cf->file_ent->ce_date != -1) {
499
			gmtime_r(&(cf->file_ent->ce_date), &datetm);
500
			(void)strftime(sticky, sizeof(sticky),
501
			    "D"CVS_DATE_FMT, &datetm);
502
		}
503
504
		cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
505
		    cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
506
		    rev, timebuf, cf->file_ent->ce_opts ?
507
		    cf->file_ent->ce_opts : "", sticky);
508
	}
509
510
	if (cvs_cmdop == CVS_OP_ADD)
511
		cf->file_status = FILE_MODIFIED;
512
513
	switch (cf->file_status) {
514
	case FILE_UNKNOWN:
515
		if (cf->file_flags & FILE_ON_DISK)
516
			cvs_client_send_request("Questionable %s",
517
			    cf->file_name);
518
		break;
519
	case FILE_ADDED:
520
		if (backup_local_changes)	/* for update -C */
521
			cvs_backup_file(cf);
522
523
		cvs_client_send_request("Modified %s", cf->file_name);
524
		cvs_remote_send_file(cf->file_path, cf->fd);
525
		break;
526
	case FILE_MODIFIED:
527
		if (backup_local_changes) {	/* for update -C */
528
			cvs_backup_file(cf);
529
			cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
530
			    cf->file_name, "", rev, timebuf,
531
			    cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "",
532
			    sticky);
533
			break;
534
		}
535
536
		cvs_client_send_request("Modified %s", cf->file_name);
537
		cvs_remote_send_file(cf->file_path, cf->fd);
538
		break;
539
	case FILE_UPTODATE:
540
		cvs_client_send_request("Unchanged %s", cf->file_name);
541
		break;
542
	}
543
}
544
545
void
546
cvs_client_send_files(char **argv, int argc)
547
{
548
	int i;
549
550
	for (i = 0; i < argc; i++)
551
		cvs_client_send_request("Argument %s", argv[i]);
552
}
553
554
void
555
cvs_client_ok(char *data)
556
{
557
	end_of_response = 1;
558
	server_response = SERVER_OK;
559
}
560
561
void
562
cvs_client_error(char *data)
563
{
564
	end_of_response = 1;
565
	server_response = SERVER_ERROR;
566
}
567
568
void
569
cvs_client_validreq(char *data)
570
{
571
	int i;
572
	char *sp, *ep;
573
	struct cvs_req *req;
574
575
	if ((sp = data) == NULL)
576
		fatal("Missing argument for Valid-requests");
577
578
	do {
579
		if ((ep = strchr(sp, ' ')) != NULL)
580
			*ep = '\0';
581
582
		req = cvs_remote_get_request_info(sp);
583
		if (req != NULL)
584
			req->supported = 1;
585
586
		if (ep != NULL)
587
			sp = ep + 1;
588
	} while (ep != NULL);
589
590
	for (i = 0; cvs_requests[i].supported != -1; i++) {
591
		req = &cvs_requests[i];
592
		if ((req->flags & REQ_NEEDED) &&
593
		    req->supported != 1) {
594
			fatal("server does not support required '%s'",
595
			    req->name);
596
		}
597
	}
598
}
599
600
void
601
cvs_client_e(char *data)
602
{
603
	if (data == NULL)
604
		fatal("Missing argument for E");
605
606
	fprintf(stderr, "%s\n", data);
607
}
608
609
void
610
cvs_client_m(char *data)
611
{
612
	if (data == NULL)
613
		fatal("Missing argument for M");
614
615
	puts(data);
616
}
617
618
void
619
cvs_client_checkedin(char *data)
620
{
621
	CVSENTRIES *entlist;
622
	struct cvs_ent *ent, *newent;
623
	size_t len;
624
	struct tm datetm;
625
	char *dir, *e, *entry, rev[CVS_REV_BUFSZ];
626
	char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ];
627
628
	if (data == NULL)
629
		fatal("Missing argument for Checked-in");
630
631
	dir = cvs_remote_input();
632
	e = cvs_remote_input();
633
	free(dir);
634
635
	entlist = cvs_ent_open(data);
636
	newent = cvs_ent_parse(e);
637
	ent = cvs_ent_get(entlist, newent->ce_name);
638
	free(e);
639
640
	rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
641
642
	sticky[0] = '\0';
643
	if (ent == NULL) {
644
		len = strlcpy(rev, "0", sizeof(rev));
645
		if (len >= sizeof(rev))
646
			fatal("cvs_client_sendfile: truncation");
647
648
		len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
649
		if (len >= sizeof(timebuf))
650
			fatal("cvs_client_sendfile: truncation");
651
652
		len = strlcat(timebuf, newent->ce_name, sizeof(timebuf));
653
		if (len >= sizeof(timebuf))
654
			fatal("cvs_client_sendfile: truncation");
655
	} else {
656
		gmtime_r(&ent->ce_mtime, &datetm);
657
		asctime_r(&datetm, timebuf);
658
		timebuf[strcspn(timebuf, "\n")] = '\0';
659
660
		if (newent->ce_tag != NULL) {
661
			(void)xsnprintf(sticky, sizeof(sticky), "T%s",
662
			    newent->ce_tag);
663
		} else if (newent->ce_date != -1) {
664
			gmtime_r(&(newent->ce_date), &datetm);
665
			(void)strftime(sticky, sizeof(sticky),
666
			    "D"CVS_DATE_FMT, &datetm);
667
		}
668
669
		cvs_ent_free(ent);
670
	}
671
672
	entry = xmalloc(CVS_ENT_MAXLINELEN);
673
	cvs_ent_line_str(newent->ce_name, rev, timebuf,
674
	    newent->ce_opts ? newent->ce_opts : "", sticky, 0,
675
	    newent->ce_status == CVS_ENT_REMOVED ? 1 : 0,
676
	    entry, CVS_ENT_MAXLINELEN);
677
678
	cvs_ent_free(newent);
679
	cvs_ent_add(entlist, entry);
680
681
	free(entry);
682
}
683
684
void
685
cvs_client_updated(char *data)
686
{
687
	int fd;
688
	time_t now;
689
	mode_t fmode;
690
	size_t flen;
691
	CVSENTRIES *ent;
692
	struct cvs_ent *e;
693
	const char *errstr;
694
	struct tm datetm;
695
	struct timeval tv[2];
696
	char repo[PATH_MAX], *entry;
697
	char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
698
	char *en, *mode, *len, *rpath, *p;
699
	char sticky[CVS_ENT_MAXLINELEN], fpath[PATH_MAX];
700
701
	if (data == NULL)
702
		fatal("Missing argument for Updated");
703
704
	rpath = cvs_remote_input();
705
	en = cvs_remote_input();
706
	mode = cvs_remote_input();
707
	len = cvs_remote_input();
708
709
	client_check_directory(data, rpath);
710
	cvs_get_repository_path(".", repo, PATH_MAX);
711
712
	STRIP_SLASH(repo);
713
714
	if (strlen(repo) + 1 > strlen(rpath))
715
		fatal("received a repository path that is too short");
716
717
	p = strrchr(rpath, '/');
718
	if (p == NULL)
719
		fatal("malicious repository path from server");
720
721
	(void)xsnprintf(fpath, sizeof(fpath), "%s/%s", data, p);
722
723
	flen = strtonum(len, 0, INT_MAX, &errstr);
724
	if (errstr != NULL)
725
		fatal("cvs_client_updated: %s: %s", len, errstr);
726
	free(len);
727
728
	cvs_strtomode(mode, &fmode);
729
	free(mode);
730
	fmode &= ~cvs_umask;
731
732
	time(&now);
733
	gmtime_r(&now, &datetm);
734
	asctime_r(&datetm, timebuf);
735
	timebuf[strcspn(timebuf, "\n")] = '\0';
736
737
	e = cvs_ent_parse(en);
738
	free(en);
739
740
	sticky[0] = '\0';
741
	if (e->ce_tag != NULL) {
742
		(void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
743
	} else if (e->ce_date != -1) {
744
		gmtime_r(&(e->ce_date), &datetm);
745
		(void)strftime(sticky, sizeof(sticky),
746
		    "D"CVS_DATE_FMT, &datetm);
747
	}
748
749
	rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
750
751
	entry = xmalloc(CVS_ENT_MAXLINELEN);
752
	cvs_ent_line_str(e->ce_name, revbuf, timebuf,
753
	    e->ce_opts ? e->ce_opts : "", sticky, 0, 0,
754
	    entry, CVS_ENT_MAXLINELEN);
755
756
	cvs_ent_free(e);
757
758
	if (cvs_cmdop != CVS_OP_EXPORT) {
759
		ent = cvs_ent_open(data);
760
		cvs_ent_add(ent, entry);
761
	}
762
763
	free(entry);
764
765
	(void)unlink(fpath);
766
	if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
767
		fatal("cvs_client_updated: open: %s: %s",
768
		    fpath, strerror(errno));
769
770
	cvs_remote_receive_file(fd, flen);
771
772
	tv[0].tv_sec = now;
773
	tv[0].tv_usec = 0;
774
	tv[1] = tv[0];
775
776
	if (futimes(fd, tv) == -1)
777
		fatal("cvs_client_updated: futimes: %s", strerror(errno));
778
779
	if (fchmod(fd, fmode) == -1)
780
		fatal("cvs_client_updated: fchmod: %s", strerror(errno));
781
782
	(void)close(fd);
783
784
	free(rpath);
785
}
786
787
void
788
cvs_client_merged(char *data)
789
{
790
	int fd;
791
	time_t now;
792
	mode_t fmode;
793
	size_t flen;
794
	CVSENTRIES *ent;
795
	const char *errstr;
796
	struct timeval tv[2];
797
	struct tm datetm;
798
	char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
799
	char *len, *fpath, *wdir;
800
801
	if (data == NULL)
802
		fatal("Missing argument for Merged");
803
804
	rpath = cvs_remote_input();
805
	entry = cvs_remote_input();
806
	mode = cvs_remote_input();
807
	len = cvs_remote_input();
808
809
	client_check_directory(data, rpath);
810
811
	repo = xmalloc(PATH_MAX);
812
	cvs_get_repository_path(".", repo, PATH_MAX);
813
814
	STRIP_SLASH(repo);
815
816
	if (strlen(repo) + 1 > strlen(rpath))
817
		fatal("received a repository path that is too short");
818
819
	fpath = rpath + strlen(repo) + 1;
820
	if ((wdir = dirname(fpath)) == NULL)
821
		fatal("cvs_client_merged: dirname: %s", strerror(errno));
822
	free(repo);
823
824
	flen = strtonum(len, 0, INT_MAX, &errstr);
825
	if (errstr != NULL)
826
		fatal("cvs_client_merged: %s: %s", len, errstr);
827
	free(len);
828
829
	cvs_strtomode(mode, &fmode);
830
	free(mode);
831
	fmode &= ~cvs_umask;
832
833
	time(&now);
834
	gmtime_r(&now, &datetm);
835
	asctime_r(&datetm, timebuf);
836
	timebuf[strcspn(timebuf, "\n")] = '\0';
837
838
	ent = cvs_ent_open(wdir);
839
	cvs_ent_add(ent, entry);
840
	free(entry);
841
842
	(void)unlink(fpath);
843
	if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
844
		fatal("cvs_client_merged: open: %s: %s",
845
		    fpath, strerror(errno));
846
847
	cvs_remote_receive_file(fd, flen);
848
849
	tv[0].tv_sec = now;
850
	tv[0].tv_usec = 0;
851
	tv[1] = tv[0];
852
853
	if (futimes(fd, tv) == -1)
854
		fatal("cvs_client_merged: futimes: %s", strerror(errno));
855
856
	if (fchmod(fd, fmode) == -1)
857
		fatal("cvs_client_merged: fchmod: %s", strerror(errno));
858
859
	(void)close(fd);
860
861
	free(rpath);
862
}
863
864
void
865
cvs_client_removed(char *data)
866
{
867
	CVSENTRIES *entlist;
868
	char *rpath, *filename, fpath[PATH_MAX];
869
870
	if (data == NULL)
871
		fatal("Missing argument for Removed");
872
873
	rpath = cvs_remote_input();
874
	if ((filename = strrchr(rpath, '/')) == NULL)
875
		fatal("bad rpath in cvs_client_removed: %s", rpath);
876
	filename++;
877
878
	entlist = cvs_ent_open(data);
879
	cvs_ent_remove(entlist, filename);
880
881
	(void)xsnprintf(fpath, PATH_MAX, "%s/%s", data, filename);
882
	(void)unlink(fpath);
883
884
	free(rpath);
885
}
886
887
void
888
cvs_client_remove_entry(char *data)
889
{
890
	CVSENTRIES *entlist;
891
	char *filename, *rpath;
892
893
	if (data == NULL)
894
		fatal("Missing argument for Remove-entry");
895
896
	rpath = cvs_remote_input();
897
	if ((filename = strrchr(rpath, '/')) == NULL)
898
		fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
899
	filename++;
900
901
	entlist = cvs_ent_open(data);
902
	cvs_ent_remove(entlist, filename);
903
904
	free(rpath);
905
}
906
907
void
908
cvs_client_set_static_directory(char *data)
909
{
910
	FILE *fp;
911
	char *dir, fpath[PATH_MAX];
912
913
	if (data == NULL)
914
		fatal("Missing argument for Set-static-directory");
915
916
	STRIP_SLASH(data);
917
918
	dir = cvs_remote_input();
919
	free(dir);
920
921
	if (cvs_cmdop == CVS_OP_EXPORT)
922
		return;
923
924
	(void)xsnprintf(fpath, PATH_MAX, "%s/%s",
925
	    data, CVS_PATH_STATICENTRIES);
926
927
	if ((fp = fopen(fpath, "w+")) == NULL) {
928
		cvs_log(LP_ERRNO, "%s", fpath);
929
		return;
930
	}
931
	(void)fclose(fp);
932
}
933
934
void
935
cvs_client_clear_static_directory(char *data)
936
{
937
	char *dir, fpath[PATH_MAX];
938
939
	if (data == NULL)
940
		fatal("Missing argument for Clear-static-directory");
941
942
	STRIP_SLASH(data);
943
944
	dir = cvs_remote_input();
945
	free(dir);
946
947
	if (cvs_cmdop == CVS_OP_EXPORT)
948
		return;
949
950
	(void)xsnprintf(fpath, PATH_MAX, "%s/%s",
951
	    data, CVS_PATH_STATICENTRIES);
952
953
	(void)cvs_unlink(fpath);
954
}
955
956
void
957
cvs_client_set_sticky(char *data)
958
{
959
	FILE *fp;
960
	char *dir, *tag, tagpath[PATH_MAX];
961
962
	if (data == NULL)
963
		fatal("Missing argument for Set-sticky");
964
965
	STRIP_SLASH(data);
966
967
	dir = cvs_remote_input();
968
	tag = cvs_remote_input();
969
970
	if (cvs_cmdop == CVS_OP_EXPORT)
971
		goto out;
972
973
	client_check_directory(data, dir);
974
975
	(void)xsnprintf(tagpath, PATH_MAX, "%s/%s", data, CVS_PATH_TAG);
976
977
	if ((fp = fopen(tagpath, "w+")) == NULL) {
978
		cvs_log(LP_ERRNO, "%s", tagpath);
979
		goto out;
980
	}
981
982
	(void)fprintf(fp, "%s\n", tag);
983
	(void)fclose(fp);
984
out:
985
	free(tag);
986
	free(dir);
987
}
988
989
void
990
cvs_client_clear_sticky(char *data)
991
{
992
	char *dir, tagpath[PATH_MAX];
993
994
	if (data == NULL)
995
		fatal("Missing argument for Clear-sticky");
996
997
	STRIP_SLASH(data);
998
999
	dir = cvs_remote_input();
1000
1001
	if (cvs_cmdop == CVS_OP_EXPORT) {
1002
		free(dir);
1003
		return;
1004
	}
1005
1006
	client_check_directory(data, dir);
1007
1008
	(void)xsnprintf(tagpath, PATH_MAX, "%s/%s", data, CVS_PATH_TAG);
1009
	(void)unlink(tagpath);
1010
1011
	free(dir);
1012
}
1013
1014
1015
/*
1016
 * cvs_client_initlog()
1017
 *
1018
 * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1019
 * set.  In this case, the variable's value is used as a path to which the
1020
 * appropriate suffix is added (".in" for client input and ".out" for server
1021
 * output).
1022
 */
1023
static void
1024
cvs_client_initlog(void)
1025
{
1026
	u_int i;
1027
	char *env, *envdup, buf[PATH_MAX], fpath[PATH_MAX];
1028
	char rpath[PATH_MAX], timebuf[CVS_TIME_BUFSZ], *s;
1029
	struct stat st;
1030
	time_t now;
1031
	struct passwd *pwd;
1032
1033
	/* avoid doing it more than once */
1034
	if (cvs_client_logon)
1035
		return;
1036
1037
	if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
1038
		return;
1039
1040
	envdup = xstrdup(env);
1041
	if ((s = strchr(envdup, '%')) != NULL)
1042
		*s = '\0';
1043
1044
	if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
1045
		fatal("cvs_client_initlog: truncation");
1046
1047
	if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
1048
		fatal("cvs_client_initlog: truncation");
1049
1050
	free(envdup);
1051
1052
	s = buf;
1053
	while ((s = strchr(s, '%')) != NULL) {
1054
		s++;
1055
		switch (*s) {
1056
		case 'c':
1057
			if (strlcpy(fpath, cmdp->cmd_name, sizeof(fpath)) >=
1058
			    sizeof(fpath))
1059
				fatal("cvs_client_initlog: truncation");
1060
			break;
1061
		case 'd':
1062
			time(&now);
1063
			ctime_r(&now, timebuf);
1064
			timebuf[strcspn(timebuf, "\n")] = '\0';
1065
			if (strlcpy(fpath, timebuf, sizeof(fpath)) >=
1066
			    sizeof(fpath))
1067
				fatal("cvs_client_initlog: truncation");
1068
			break;
1069
		case 'p':
1070
			(void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1071
			break;
1072
		case 'u':
1073
			if ((pwd = getpwuid(getuid())) != NULL) {
1074
				if (strlcpy(fpath, pwd->pw_name,
1075
				    sizeof(fpath)) >= sizeof(fpath))
1076
					fatal("cvs_client_initlog: truncation");
1077
			} else {
1078
				fpath[0] = '\0';
1079
			}
1080
			endpwent();
1081
			break;
1082
		default:
1083
			fpath[0] = '\0';
1084
			break;
1085
		}
1086
1087
		if (fpath[0] != '\0') {
1088
			if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
1089
				fatal("cvs_client_initlog: truncation");
1090
1091
			if (strlcat(rpath, fpath, sizeof(rpath))
1092
			    >= sizeof(rpath))
1093
				fatal("cvs_client_initlog: truncation");
1094
		}
1095
	}
1096
1097
	for (i = 0; i < UINT_MAX; i++) {
1098
		(void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1099
1100
		if (stat(fpath, &st) != -1)
1101
			continue;
1102
1103
		if (errno != ENOENT)
1104
			fatal("cvs_client_initlog() stat failed '%s'",
1105
			    strerror(errno));
1106
1107
		break;
1108
	}
1109
1110
	if ((cvs_client_inlog_fd = open(fpath,
1111
	    O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1112
		fatal("cvs_client_initlog: open `%s': %s",
1113
		    fpath, strerror(errno));
1114
	}
1115
1116
	for (i = 0; i < UINT_MAX; i++) {
1117
		(void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1118
1119
		if (stat(fpath, &st) != -1)
1120
			continue;
1121
1122
		if (errno != ENOENT)
1123
			fatal("cvs_client_initlog() stat failed '%s'",
1124
			    strerror(errno));
1125
1126
		break;
1127
	}
1128
1129
	if ((cvs_client_outlog_fd = open(fpath,
1130
	    O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1131
		fatal("cvs_client_initlog: open `%s': %s",
1132
		    fpath, strerror(errno));
1133
	}
1134
1135
	cvs_client_logon = 1;
1136
}