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