xref: /openbsd-src/usr.bin/cvs/client.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: client.c,v 1.121 2009/04/04 11:29:57 joris 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/param.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 = cvs_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 			cvs_buf_putc(bp, ' ');
145 		else
146 			first++;
147 		cvs_buf_puts(bp, cvs_responses[i].name);
148 	}
149 
150 	cvs_buf_putc(bp, '\0');
151 	d = cvs_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 	xfree(entry);
194 }
195 
196 void
197 cvs_client_connect_to_server(void)
198 {
199 	struct cvs_var *vp;
200 	char *cmd, *argv[9], *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++] = current_cvsroot->cr_host;
245 		argv[argc++] = cmd;
246 		argv[argc++] = "server";
247 		argv[argc] = NULL;
248 
249 		cvs_log(LP_TRACE, "connecting to server %s",
250 		    current_cvsroot->cr_host);
251 
252 		execvp(argv[0], argv);
253 		fatal("cvs_client_connect: failed to execute cvs server");
254 	default:
255 		break;
256 	}
257 
258 	close(ifd[0]);
259 	close(ofd[1]);
260 
261 	if ((current_cvsroot->cr_srvin = fdopen(ifd[1], "w")) == NULL)
262 		fatal("cvs_client_connect: %s", strerror(errno));
263 	if ((current_cvsroot->cr_srvout = fdopen(ofd[0], "r")) == NULL)
264 		fatal("cvs_client_connect: %s", strerror(errno));
265 
266 	setvbuf(current_cvsroot->cr_srvin, NULL,_IOLBF, 0);
267 	setvbuf(current_cvsroot->cr_srvout, NULL, _IOLBF, 0);
268 
269 	cvs_client_initlog();
270 
271 	if (cvs_cmdop != CVS_OP_INIT)
272 		cvs_client_send_request("Root %s", current_cvsroot->cr_dir);
273 
274 	resp = client_get_supported_responses();
275 	cvs_client_send_request("Valid-responses %s", resp);
276 	xfree(resp);
277 
278 	cvs_client_send_request("valid-requests");
279 	cvs_client_get_responses();
280 
281 	cvs_client_send_request("UseUnchanged");
282 
283 	if (cvs_nolog == 1)
284 		cvs_client_send_request("Global_option -l");
285 
286 	if (cvs_noexec == 1)
287 		cvs_client_send_request("Global_option -n");
288 
289 	switch (verbosity) {
290 	case 0:
291 		cvs_client_send_request("Global_option -Q");
292 		break;
293 	case 1:
294 		/* Be quiet. This is the default in OpenCVS. */
295 		cvs_client_send_request("Global_option -q");
296 		break;
297 	default:
298 		break;
299 	}
300 
301 	if (cvs_readonly == 1)
302 		cvs_client_send_request("Global_option -r");
303 
304 	if (cvs_trace == 1)
305 		cvs_client_send_request("Global_option -t");
306 
307 	/* XXX: If 'Set' is supported? */
308 	TAILQ_FOREACH(vp, &cvs_variables, cv_link)
309 		cvs_client_send_request("Set %s=%s", vp->cv_name, vp->cv_val);
310 }
311 
312 void
313 cvs_client_send_request(char *fmt, ...)
314 {
315 	int i;
316 	va_list ap;
317 	char *data, *s;
318 	struct cvs_req *req;
319 
320 	va_start(ap, fmt);
321 	i = vasprintf(&data, fmt, ap);
322 	va_end(ap);
323 	if (i == -1)
324 		fatal("cvs_client_send_request: could not allocate memory");
325 
326 	if ((s = strchr(data, ' ')) != NULL)
327 		*s = '\0';
328 
329 	req = cvs_remote_get_request_info(data);
330 	if (req == NULL)
331 		fatal("'%s' is an unknown request", data);
332 
333 	if (req->supported != 1)
334 		fatal("remote cvs server does not support '%s'", data);
335 
336 	if (s != NULL)
337 		*s = ' ';
338 
339 	cvs_log(LP_TRACE, "%s", data);
340 
341 	cvs_remote_output(data);
342 	xfree(data);
343 }
344 
345 void
346 cvs_client_read_response(void)
347 {
348 	char *cmd, *data;
349 	struct cvs_resp *resp;
350 
351 	cmd = cvs_remote_input();
352 	if ((data = strchr(cmd, ' ')) != NULL)
353 		(*data++) = '\0';
354 
355 	resp = cvs_remote_get_response_info(cmd);
356 	if (resp == NULL)
357 		fatal("response '%s' is not supported by our client", cmd);
358 
359 	if (resp->hdlr == NULL)
360 		fatal("opencvs client does not support '%s'", cmd);
361 
362 	(*resp->hdlr)(data);
363 
364 	xfree(cmd);
365 }
366 
367 void
368 cvs_client_get_responses(void)
369 {
370 	while (end_of_response != 1)
371 		cvs_client_read_response();
372 
373 	end_of_response = 0;
374 }
375 
376 void
377 cvs_client_send_logmsg(char *msg)
378 {
379 	char *buf, *p, *q;
380 
381 	(void)xasprintf(&buf, "%s\n", msg);
382 
383 	cvs_client_send_request("Argument -m");
384 	if ((p = strchr(buf, '\n')) != NULL)
385 		*p++ = '\0';
386 	cvs_client_send_request("Argument %s", buf);
387 	for (q = p; p != NULL; q = p) {
388 		if ((p = strchr(q, '\n')) != NULL) {
389 			*p++ = '\0';
390 			cvs_client_send_request("Argumentx %s", q);
391 		}
392 	}
393 
394 	xfree(buf);
395 }
396 
397 void
398 cvs_client_senddir(const char *dir)
399 {
400 	struct stat st;
401 	int nb;
402 	char *d, *date, fpath[MAXPATHLEN], repo[MAXPATHLEN], *tag;
403 
404 	d = NULL;
405 
406 	if (lastdir != NULL && !strcmp(dir, lastdir))
407 		return;
408 
409 	cvs_get_repository_path(dir, repo, MAXPATHLEN);
410 
411 	if (cvs_cmdop != CVS_OP_RLOG)
412 		cvs_client_send_request("Directory %s\n%s", dir, repo);
413 
414 	(void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
415 	    dir, CVS_PATH_STATICENTRIES);
416 
417 	if (stat(fpath, &st) == 0 && (st.st_mode & (S_IRUSR|S_IRGRP|S_IROTH)))
418 		cvs_client_send_request("Static-directory");
419 
420 	d = xstrdup(dir);
421 	cvs_parse_tagfile(d, &tag, &date, &nb);
422 
423 	if (tag != NULL || date != NULL) {
424 		char buf[128];
425 
426 		if (tag != NULL && nb != 0) {
427 			if (strlcpy(buf, "N", sizeof(buf)) >= sizeof(buf))
428 				fatal("cvs_client_senddir: truncation");
429 		} else if (tag != NULL) {
430 			if (strlcpy(buf, "T", sizeof(buf)) >= sizeof(buf))
431 				fatal("cvs_client_senddir: truncation");
432 		} else {
433 			if (strlcpy(buf, "D", sizeof(buf)) >= sizeof(buf))
434 				fatal("cvs_client_senddir: truncation");
435 		}
436 
437 		if (strlcat(buf, tag ? tag : date, sizeof(buf)) >= sizeof(buf))
438 			fatal("cvs_client_senddir: truncation");
439 
440 		cvs_client_send_request("Sticky %s", buf);
441 
442 		if (tag != NULL)
443 			xfree(tag);
444 		if (date != NULL)
445 			xfree(date);
446 	}
447 	if (d != NULL)
448 		xfree(d);
449 
450 	if (lastdir != NULL)
451 		xfree(lastdir);
452 	lastdir = xstrdup(dir);
453 }
454 
455 void
456 cvs_client_sendfile(struct cvs_file *cf)
457 {
458 	size_t len;
459 	struct tm datetm;
460 	char rev[CVS_REV_BUFSZ], timebuf[CVS_TIME_BUFSZ], sticky[CVS_REV_BUFSZ];
461 
462 	if (cf->file_type != CVS_FILE)
463 		return;
464 
465 	cvs_client_senddir(cf->file_wd);
466 	cvs_remote_classify_file(cf);
467 
468 	if (cf->file_type == CVS_DIR)
469 		return;
470 
471 	if (cf->file_ent != NULL && cvs_cmdop != CVS_OP_IMPORT) {
472 		if (cf->file_status == FILE_ADDED) {
473 			len = strlcpy(rev, "0", sizeof(rev));
474 			if (len >= sizeof(rev))
475 				fatal("cvs_client_sendfile: truncation");
476 
477 			len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
478 			if (len >= sizeof(timebuf))
479 				fatal("cvs_client_sendfile: truncation");
480 
481 			len = strlcat(timebuf, cf->file_name, sizeof(timebuf));
482 			if (len >= sizeof(timebuf))
483 				fatal("cvs_client_sendfile: truncation");
484 		} else {
485 			rcsnum_tostr(cf->file_ent->ce_rev, rev, sizeof(rev));
486 			ctime_r(&cf->file_ent->ce_mtime, timebuf);
487 		}
488 
489 		if (cf->file_ent->ce_conflict == NULL) {
490 			timebuf[strcspn(timebuf, "\n")] = '\0';
491 		} else {
492 			len = strlcpy(timebuf, cf->file_ent->ce_conflict,
493 			    sizeof(timebuf));
494 			if (len >= sizeof(timebuf))
495 				fatal("cvs_client_sendfile: truncation");
496 			len = strlcat(timebuf, "+=", sizeof(timebuf));
497 			if (len >= sizeof(timebuf))
498 				fatal("cvs_client_sendfile: truncation");
499 		}
500 
501 		sticky[0] = '\0';
502 		if (cf->file_ent->ce_tag != NULL) {
503 			(void)xsnprintf(sticky, sizeof(sticky), "T%s",
504 			    cf->file_ent->ce_tag);
505 		} else if (cf->file_ent->ce_date != -1) {
506 			gmtime_r(&(cf->file_ent->ce_date), &datetm);
507 			(void)strftime(sticky, sizeof(sticky),
508 			    "D"CVS_DATE_FMT, &datetm);
509 		}
510 
511 		cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
512 		    cf->file_name, (cf->file_status == FILE_REMOVED) ? "-" : "",
513 		    rev, timebuf, cf->file_ent->ce_opts ?
514 		    cf->file_ent->ce_opts : "", sticky);
515 	}
516 
517 	if (cvs_cmdop == CVS_OP_ADD)
518 		cf->file_status = FILE_MODIFIED;
519 
520 	switch (cf->file_status) {
521 	case FILE_UNKNOWN:
522 		if (cf->file_flags & FILE_ON_DISK)
523 			cvs_client_send_request("Questionable %s",
524 			    cf->file_name);
525 		break;
526 	case FILE_ADDED:
527 	case FILE_MODIFIED:
528 		cvs_client_send_request("Modified %s", cf->file_name);
529 		cvs_remote_send_file(cf->file_path, cf->fd);
530 		break;
531 	case FILE_UPTODATE:
532 		cvs_client_send_request("Unchanged %s", cf->file_name);
533 		break;
534 	}
535 }
536 
537 void
538 cvs_client_send_files(char **argv, int argc)
539 {
540 	int i;
541 
542 	for (i = 0; i < argc; i++)
543 		cvs_client_send_request("Argument %s", argv[i]);
544 }
545 
546 void
547 cvs_client_ok(char *data)
548 {
549 	end_of_response = 1;
550 	server_response = SERVER_OK;
551 }
552 
553 void
554 cvs_client_error(char *data)
555 {
556 	end_of_response = 1;
557 	server_response = SERVER_ERROR;
558 }
559 
560 void
561 cvs_client_validreq(char *data)
562 {
563 	int i;
564 	char *sp, *ep;
565 	struct cvs_req *req;
566 
567 	if ((sp = data) == NULL)
568 		fatal("Missing argument for Valid-requests");
569 
570 	do {
571 		if ((ep = strchr(sp, ' ')) != NULL)
572 			*ep = '\0';
573 
574 		req = cvs_remote_get_request_info(sp);
575 		if (req != NULL)
576 			req->supported = 1;
577 
578 		if (ep != NULL)
579 			sp = ep + 1;
580 	} while (ep != NULL);
581 
582 	for (i = 0; cvs_requests[i].supported != -1; i++) {
583 		req = &cvs_requests[i];
584 		if ((req->flags & REQ_NEEDED) &&
585 		    req->supported != 1) {
586 			fatal("server does not support required '%s'",
587 			    req->name);
588 		}
589 	}
590 }
591 
592 void
593 cvs_client_e(char *data)
594 {
595 	if (data == NULL)
596 		fatal("Missing argument for E");
597 
598 	fprintf(stderr, "%s\n", data);
599 }
600 
601 void
602 cvs_client_m(char *data)
603 {
604 	if (data == NULL)
605 		fatal("Missing argument for M");
606 
607 	puts(data);
608 }
609 
610 void
611 cvs_client_checkedin(char *data)
612 {
613 	CVSENTRIES *entlist;
614 	struct cvs_ent *ent, *newent;
615 	size_t len;
616 	struct tm datetm;
617 	char *dir, *e, *entry, rev[CVS_REV_BUFSZ];
618 	char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ];
619 
620 	if (data == NULL)
621 		fatal("Missing argument for Checked-in");
622 
623 	dir = cvs_remote_input();
624 	e = cvs_remote_input();
625 	xfree(dir);
626 
627 	entlist = cvs_ent_open(data);
628 	newent = cvs_ent_parse(e);
629 	ent = cvs_ent_get(entlist, newent->ce_name);
630 	xfree(e);
631 
632 	rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
633 
634 	sticky[0] = '\0';
635 	if (ent == NULL) {
636 		len = strlcpy(rev, "0", sizeof(rev));
637 		if (len >= sizeof(rev))
638 			fatal("cvs_client_sendfile: truncation");
639 
640 		len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
641 		if (len >= sizeof(timebuf))
642 			fatal("cvs_client_sendfile: truncation");
643 
644 		len = strlcat(timebuf, newent->ce_name, sizeof(timebuf));
645 		if (len >= sizeof(timebuf))
646 			fatal("cvs_client_sendfile: truncation");
647 	} else {
648 		gmtime_r(&ent->ce_mtime, &datetm);
649 		asctime_r(&datetm, timebuf);
650 		timebuf[strcspn(timebuf, "\n")] = '\0';
651 
652 		if (newent->ce_tag != NULL) {
653 			(void)xsnprintf(sticky, sizeof(sticky), "T%s",
654 			    newent->ce_tag);
655 		} else if (newent->ce_date != -1) {
656 			gmtime_r(&(newent->ce_date), &datetm);
657 			(void)strftime(sticky, sizeof(sticky),
658 			    "D"CVS_DATE_FMT, &datetm);
659 		}
660 
661 		cvs_ent_free(ent);
662 	}
663 
664 	entry = xmalloc(CVS_ENT_MAXLINELEN);
665 	cvs_ent_line_str(newent->ce_name, rev, timebuf,
666 	    newent->ce_opts ? newent->ce_opts : "", sticky, 0,
667 	    newent->ce_status == CVS_ENT_REMOVED ? 1 : 0,
668 	    entry, CVS_ENT_MAXLINELEN);
669 
670 	cvs_ent_free(newent);
671 	cvs_ent_add(entlist, entry);
672 
673 	xfree(entry);
674 }
675 
676 void
677 cvs_client_updated(char *data)
678 {
679 	int fd;
680 	time_t now;
681 	mode_t fmode;
682 	size_t flen;
683 	CVSENTRIES *ent;
684 	struct cvs_ent *e;
685 	const char *errstr;
686 	struct tm datetm;
687 	struct timeval tv[2];
688 	char repo[MAXPATHLEN], *entry;
689 	char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
690 	char *en, *mode, *len, *rpath, *p;
691 	char sticky[CVS_ENT_MAXLINELEN], fpath[MAXPATHLEN];
692 
693 	if (data == NULL)
694 		fatal("Missing argument for Updated");
695 
696 	rpath = cvs_remote_input();
697 	en = cvs_remote_input();
698 	mode = cvs_remote_input();
699 	len = cvs_remote_input();
700 
701 	client_check_directory(data, rpath);
702 	cvs_get_repository_path(".", repo, MAXPATHLEN);
703 
704 	STRIP_SLASH(repo);
705 
706 	if (strlen(repo) + 1 > strlen(rpath))
707 		fatal("received a repository path that is too short");
708 
709 	p = strrchr(rpath, '/');
710 	if (p == NULL)
711 		fatal("malicious repository path from server");
712 
713 	(void)xsnprintf(fpath, sizeof(fpath), "%s/%s", data, p);
714 
715 	flen = strtonum(len, 0, INT_MAX, &errstr);
716 	if (errstr != NULL)
717 		fatal("cvs_client_updated: %s: %s", len, errstr);
718 	xfree(len);
719 
720 	cvs_strtomode(mode, &fmode);
721 	xfree(mode);
722 	fmode &= ~cvs_umask;
723 
724 	time(&now);
725 	gmtime_r(&now, &datetm);
726 	asctime_r(&datetm, timebuf);
727 	timebuf[strcspn(timebuf, "\n")] = '\0';
728 
729 	e = cvs_ent_parse(en);
730 	xfree(en);
731 
732 	sticky[0] = '\0';
733 	if (e->ce_tag != NULL) {
734 		(void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
735 	} else if (e->ce_date != -1) {
736 		gmtime_r(&(e->ce_date), &datetm);
737 		(void)strftime(sticky, sizeof(sticky),
738 		    "D"CVS_DATE_FMT, &datetm);
739 	}
740 
741 	rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
742 
743 	entry = xmalloc(CVS_ENT_MAXLINELEN);
744 	cvs_ent_line_str(e->ce_name, revbuf, timebuf,
745 	    e->ce_opts ? e->ce_opts : "", sticky, 0, 0,
746 	    entry, CVS_ENT_MAXLINELEN);
747 
748 	cvs_ent_free(e);
749 
750 	if (cvs_cmdop != CVS_OP_EXPORT) {
751 		ent = cvs_ent_open(data);
752 		cvs_ent_add(ent, entry);
753 	}
754 
755 	xfree(entry);
756 
757 	(void)unlink(fpath);
758 	if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
759 		fatal("cvs_client_updated: open: %s: %s",
760 		    fpath, strerror(errno));
761 
762 	cvs_remote_receive_file(fd, flen);
763 
764 	tv[0].tv_sec = now;
765 	tv[0].tv_usec = 0;
766 	tv[1] = tv[0];
767 
768 	if (futimes(fd, tv) == -1)
769 		fatal("cvs_client_updated: futimes: %s", strerror(errno));
770 
771 	if (fchmod(fd, fmode) == -1)
772 		fatal("cvs_client_updated: fchmod: %s", strerror(errno));
773 
774 	(void)close(fd);
775 
776 	xfree(rpath);
777 }
778 
779 void
780 cvs_client_merged(char *data)
781 {
782 	int fd;
783 	time_t now;
784 	mode_t fmode;
785 	size_t flen;
786 	CVSENTRIES *ent;
787 	const char *errstr;
788 	struct timeval tv[2];
789 	struct tm datetm;
790 	char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
791 	char *len, *fpath, *wdir;
792 
793 	if (data == NULL)
794 		fatal("Missing argument for Merged");
795 
796 	rpath = cvs_remote_input();
797 	entry = cvs_remote_input();
798 	mode = cvs_remote_input();
799 	len = cvs_remote_input();
800 
801 	client_check_directory(data, rpath);
802 
803 	repo = xmalloc(MAXPATHLEN);
804 	cvs_get_repository_path(".", repo, MAXPATHLEN);
805 
806 	STRIP_SLASH(repo);
807 
808 	if (strlen(repo) + 1 > strlen(rpath))
809 		fatal("received a repository path that is too short");
810 
811 	fpath = rpath + strlen(repo) + 1;
812 	if ((wdir = dirname(fpath)) == NULL)
813 		fatal("cvs_client_merged: dirname: %s", strerror(errno));
814 	xfree(repo);
815 
816 	flen = strtonum(len, 0, INT_MAX, &errstr);
817 	if (errstr != NULL)
818 		fatal("cvs_client_merged: %s: %s", len, errstr);
819 	xfree(len);
820 
821 	cvs_strtomode(mode, &fmode);
822 	xfree(mode);
823 	fmode &= ~cvs_umask;
824 
825 	time(&now);
826 	gmtime_r(&now, &datetm);
827 	asctime_r(&datetm, timebuf);
828 	timebuf[strcspn(timebuf, "\n")] = '\0';
829 
830 	ent = cvs_ent_open(wdir);
831 	cvs_ent_add(ent, entry);
832 	xfree(entry);
833 
834 	(void)unlink(fpath);
835 	if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
836 		fatal("cvs_client_merged: open: %s: %s",
837 		    fpath, strerror(errno));
838 
839 	cvs_remote_receive_file(fd, flen);
840 
841 	tv[0].tv_sec = now;
842 	tv[0].tv_usec = 0;
843 	tv[1] = tv[0];
844 
845 	if (futimes(fd, tv) == -1)
846 		fatal("cvs_client_merged: futimes: %s", strerror(errno));
847 
848 	if (fchmod(fd, fmode) == -1)
849 		fatal("cvs_client_merged: fchmod: %s", strerror(errno));
850 
851 	(void)close(fd);
852 
853 	xfree(rpath);
854 }
855 
856 void
857 cvs_client_removed(char *data)
858 {
859 	CVSENTRIES *entlist;
860 	char *rpath, *filename, fpath[MAXPATHLEN];
861 
862 	if (data == NULL)
863 		fatal("Missing argument for Removed");
864 
865 	rpath = cvs_remote_input();
866 	if ((filename = strrchr(rpath, '/')) == NULL)
867 		fatal("bad rpath in cvs_client_removed: %s", rpath);
868 	filename++;
869 
870 	entlist = cvs_ent_open(data);
871 	cvs_ent_remove(entlist, filename);
872 
873 	(void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", data, filename);
874 	(void)unlink(fpath);
875 
876 	xfree(rpath);
877 }
878 
879 void
880 cvs_client_remove_entry(char *data)
881 {
882 	CVSENTRIES *entlist;
883 	char *filename, *rpath;
884 
885 	if (data == NULL)
886 		fatal("Missing argument for Remove-entry");
887 
888 	rpath = cvs_remote_input();
889 	if ((filename = strrchr(rpath, '/')) == NULL)
890 		fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
891 	filename++;
892 
893 	entlist = cvs_ent_open(data);
894 	cvs_ent_remove(entlist, filename);
895 
896 	xfree(rpath);
897 }
898 
899 void
900 cvs_client_set_static_directory(char *data)
901 {
902 	FILE *fp;
903 	char *dir, fpath[MAXPATHLEN];
904 
905 	if (data == NULL)
906 		fatal("Missing argument for Set-static-directory");
907 
908 	STRIP_SLASH(data);
909 
910 	dir = cvs_remote_input();
911 	xfree(dir);
912 
913 	if (cvs_cmdop == CVS_OP_EXPORT)
914 		return;
915 
916 	(void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
917 	    data, CVS_PATH_STATICENTRIES);
918 
919 	if ((fp = fopen(fpath, "w+")) == NULL) {
920 		cvs_log(LP_ERRNO, "%s", fpath);
921 		return;
922 	}
923 	(void)fclose(fp);
924 }
925 
926 void
927 cvs_client_clear_static_directory(char *data)
928 {
929 	char *dir, fpath[MAXPATHLEN];
930 
931 	if (data == NULL)
932 		fatal("Missing argument for Clear-static-directory");
933 
934 	STRIP_SLASH(data);
935 
936 	dir = cvs_remote_input();
937 	xfree(dir);
938 
939 	if (cvs_cmdop == CVS_OP_EXPORT)
940 		return;
941 
942 	(void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
943 	    data, CVS_PATH_STATICENTRIES);
944 
945 	(void)cvs_unlink(fpath);
946 }
947 
948 void
949 cvs_client_set_sticky(char *data)
950 {
951 	FILE *fp;
952 	char *dir, *tag, tagpath[MAXPATHLEN];
953 
954 	if (data == NULL)
955 		fatal("Missing argument for Set-sticky");
956 
957 	STRIP_SLASH(data);
958 
959 	dir = cvs_remote_input();
960 	tag = cvs_remote_input();
961 
962 	if (cvs_cmdop == CVS_OP_EXPORT)
963 		goto out;
964 
965 	client_check_directory(data, dir);
966 
967 	(void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
968 
969 	if ((fp = fopen(tagpath, "w+")) == NULL) {
970 		cvs_log(LP_ERRNO, "%s", tagpath);
971 		goto out;
972 	}
973 
974 	(void)fprintf(fp, "%s\n", tag);
975 	(void)fclose(fp);
976 out:
977 	xfree(tag);
978 	xfree(dir);
979 }
980 
981 void
982 cvs_client_clear_sticky(char *data)
983 {
984 	char *dir, tagpath[MAXPATHLEN];
985 
986 	if (data == NULL)
987 		fatal("Missing argument for Clear-sticky");
988 
989 	STRIP_SLASH(data);
990 
991 	dir = cvs_remote_input();
992 
993 	if (cvs_cmdop == CVS_OP_EXPORT) {
994 		xfree(dir);
995 		return;
996 	}
997 
998 	client_check_directory(data, dir);
999 
1000 	(void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1001 	(void)unlink(tagpath);
1002 
1003 	xfree(dir);
1004 }
1005 
1006 
1007 /*
1008  * cvs_client_initlog()
1009  *
1010  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1011  * set.  In this case, the variable's value is used as a path to which the
1012  * appropriate suffix is added (".in" for client input and ".out" for server
1013  * output).
1014  */
1015 static void
1016 cvs_client_initlog(void)
1017 {
1018 	u_int i;
1019 	char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
1020 	char rpath[MAXPATHLEN], timebuf[CVS_TIME_BUFSZ], *s;
1021 	struct stat st;
1022 	time_t now;
1023 	struct passwd *pwd;
1024 
1025 	/* avoid doing it more than once */
1026 	if (cvs_client_logon)
1027 		return;
1028 
1029 	if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
1030 		return;
1031 
1032 	envdup = xstrdup(env);
1033 	if ((s = strchr(envdup, '%')) != NULL)
1034 		*s = '\0';
1035 
1036 	if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
1037 		fatal("cvs_client_initlog: truncation");
1038 
1039 	if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
1040 		fatal("cvs_client_initlog: truncation");
1041 
1042 	xfree(envdup);
1043 
1044 	s = buf;
1045 	while ((s = strchr(s, '%')) != NULL) {
1046 		s++;
1047 		switch (*s) {
1048 		case 'c':
1049 			if (strlcpy(fpath, cmdp->cmd_name, sizeof(fpath)) >=
1050 			    sizeof(fpath))
1051 				fatal("cvs_client_initlog: truncation");
1052 			break;
1053 		case 'd':
1054 			time(&now);
1055 			ctime_r(&now, timebuf);
1056 			timebuf[strcspn(timebuf, "\n")] = '\0';
1057 			if (strlcpy(fpath, timebuf, sizeof(fpath)) >=
1058 			    sizeof(fpath))
1059 				fatal("cvs_client_initlog: truncation");
1060 			break;
1061 		case 'p':
1062 			(void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1063 			break;
1064 		case 'u':
1065 			if ((pwd = getpwuid(getuid())) != NULL) {
1066 				if (strlcpy(fpath, pwd->pw_name,
1067 				    sizeof(fpath)) >= sizeof(fpath))
1068 					fatal("cvs_client_initlog: truncation");
1069 			} else {
1070 				fpath[0] = '\0';
1071 			}
1072 			endpwent();
1073 			break;
1074 		default:
1075 			fpath[0] = '\0';
1076 			break;
1077 		}
1078 
1079 		if (fpath[0] != '\0') {
1080 			if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
1081 				fatal("cvs_client_initlog: truncation");
1082 
1083 			if (strlcat(rpath, fpath, sizeof(rpath))
1084 			    >= sizeof(rpath))
1085 				fatal("cvs_client_initlog: truncation");
1086 		}
1087 	}
1088 
1089 	for (i = 0; i < UINT_MAX; i++) {
1090 		(void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1091 
1092 		if (stat(fpath, &st) != -1)
1093 			continue;
1094 
1095 		if (errno != ENOENT)
1096 			fatal("cvs_client_initlog() stat failed '%s'",
1097 			    strerror(errno));
1098 
1099 		break;
1100 	}
1101 
1102 	if ((cvs_client_inlog_fd = open(fpath,
1103 	    O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1104 		fatal("cvs_client_initlog: open `%s': %s",
1105 		    fpath, strerror(errno));
1106 	}
1107 
1108 	for (i = 0; i < UINT_MAX; i++) {
1109 		(void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1110 
1111 		if (stat(fpath, &st) != -1)
1112 			continue;
1113 
1114 		if (errno != ENOENT)
1115 			fatal("cvs_client_initlog() stat failed '%s'",
1116 			    strerror(errno));
1117 
1118 		break;
1119 	}
1120 
1121 	if ((cvs_client_outlog_fd = open(fpath,
1122 	    O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1123 		fatal("cvs_client_initlog: open `%s': %s",
1124 		    fpath, strerror(errno));
1125 	}
1126 
1127 	cvs_client_logon = 1;
1128 }
1129