xref: /openbsd-src/usr.bin/cvs/client.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: client.c,v 1.123 2010/09/29 18:14:52 nicm 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 = 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 	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 		if (backup_local_changes)	/* for update -C */
528 			cvs_backup_file(cf);
529 
530 		cvs_client_send_request("Modified %s", cf->file_name);
531 		cvs_remote_send_file(cf->file_path, cf->fd);
532 		break;
533 	case FILE_MODIFIED:
534 		if (backup_local_changes) {	/* for update -C */
535 			cvs_backup_file(cf);
536 			cvs_client_send_request("Entry /%s/%s%s/%s/%s/%s",
537 			    cf->file_name, "", rev, timebuf,
538 			    cf->file_ent->ce_opts ? cf->file_ent->ce_opts : "",
539 			    sticky);
540 			break;
541 		}
542 
543 		cvs_client_send_request("Modified %s", cf->file_name);
544 		cvs_remote_send_file(cf->file_path, cf->fd);
545 		break;
546 	case FILE_UPTODATE:
547 		cvs_client_send_request("Unchanged %s", cf->file_name);
548 		break;
549 	}
550 }
551 
552 void
553 cvs_client_send_files(char **argv, int argc)
554 {
555 	int i;
556 
557 	for (i = 0; i < argc; i++)
558 		cvs_client_send_request("Argument %s", argv[i]);
559 }
560 
561 void
562 cvs_client_ok(char *data)
563 {
564 	end_of_response = 1;
565 	server_response = SERVER_OK;
566 }
567 
568 void
569 cvs_client_error(char *data)
570 {
571 	end_of_response = 1;
572 	server_response = SERVER_ERROR;
573 }
574 
575 void
576 cvs_client_validreq(char *data)
577 {
578 	int i;
579 	char *sp, *ep;
580 	struct cvs_req *req;
581 
582 	if ((sp = data) == NULL)
583 		fatal("Missing argument for Valid-requests");
584 
585 	do {
586 		if ((ep = strchr(sp, ' ')) != NULL)
587 			*ep = '\0';
588 
589 		req = cvs_remote_get_request_info(sp);
590 		if (req != NULL)
591 			req->supported = 1;
592 
593 		if (ep != NULL)
594 			sp = ep + 1;
595 	} while (ep != NULL);
596 
597 	for (i = 0; cvs_requests[i].supported != -1; i++) {
598 		req = &cvs_requests[i];
599 		if ((req->flags & REQ_NEEDED) &&
600 		    req->supported != 1) {
601 			fatal("server does not support required '%s'",
602 			    req->name);
603 		}
604 	}
605 }
606 
607 void
608 cvs_client_e(char *data)
609 {
610 	if (data == NULL)
611 		fatal("Missing argument for E");
612 
613 	fprintf(stderr, "%s\n", data);
614 }
615 
616 void
617 cvs_client_m(char *data)
618 {
619 	if (data == NULL)
620 		fatal("Missing argument for M");
621 
622 	puts(data);
623 }
624 
625 void
626 cvs_client_checkedin(char *data)
627 {
628 	CVSENTRIES *entlist;
629 	struct cvs_ent *ent, *newent;
630 	size_t len;
631 	struct tm datetm;
632 	char *dir, *e, *entry, rev[CVS_REV_BUFSZ];
633 	char sticky[CVS_ENT_MAXLINELEN], timebuf[CVS_TIME_BUFSZ];
634 
635 	if (data == NULL)
636 		fatal("Missing argument for Checked-in");
637 
638 	dir = cvs_remote_input();
639 	e = cvs_remote_input();
640 	xfree(dir);
641 
642 	entlist = cvs_ent_open(data);
643 	newent = cvs_ent_parse(e);
644 	ent = cvs_ent_get(entlist, newent->ce_name);
645 	xfree(e);
646 
647 	rcsnum_tostr(newent->ce_rev, rev, sizeof(rev));
648 
649 	sticky[0] = '\0';
650 	if (ent == NULL) {
651 		len = strlcpy(rev, "0", sizeof(rev));
652 		if (len >= sizeof(rev))
653 			fatal("cvs_client_sendfile: truncation");
654 
655 		len = strlcpy(timebuf, "Initial ", sizeof(timebuf));
656 		if (len >= sizeof(timebuf))
657 			fatal("cvs_client_sendfile: truncation");
658 
659 		len = strlcat(timebuf, newent->ce_name, sizeof(timebuf));
660 		if (len >= sizeof(timebuf))
661 			fatal("cvs_client_sendfile: truncation");
662 	} else {
663 		gmtime_r(&ent->ce_mtime, &datetm);
664 		asctime_r(&datetm, timebuf);
665 		timebuf[strcspn(timebuf, "\n")] = '\0';
666 
667 		if (newent->ce_tag != NULL) {
668 			(void)xsnprintf(sticky, sizeof(sticky), "T%s",
669 			    newent->ce_tag);
670 		} else if (newent->ce_date != -1) {
671 			gmtime_r(&(newent->ce_date), &datetm);
672 			(void)strftime(sticky, sizeof(sticky),
673 			    "D"CVS_DATE_FMT, &datetm);
674 		}
675 
676 		cvs_ent_free(ent);
677 	}
678 
679 	entry = xmalloc(CVS_ENT_MAXLINELEN);
680 	cvs_ent_line_str(newent->ce_name, rev, timebuf,
681 	    newent->ce_opts ? newent->ce_opts : "", sticky, 0,
682 	    newent->ce_status == CVS_ENT_REMOVED ? 1 : 0,
683 	    entry, CVS_ENT_MAXLINELEN);
684 
685 	cvs_ent_free(newent);
686 	cvs_ent_add(entlist, entry);
687 
688 	xfree(entry);
689 }
690 
691 void
692 cvs_client_updated(char *data)
693 {
694 	int fd;
695 	time_t now;
696 	mode_t fmode;
697 	size_t flen;
698 	CVSENTRIES *ent;
699 	struct cvs_ent *e;
700 	const char *errstr;
701 	struct tm datetm;
702 	struct timeval tv[2];
703 	char repo[MAXPATHLEN], *entry;
704 	char timebuf[CVS_TIME_BUFSZ], revbuf[CVS_REV_BUFSZ];
705 	char *en, *mode, *len, *rpath, *p;
706 	char sticky[CVS_ENT_MAXLINELEN], fpath[MAXPATHLEN];
707 
708 	if (data == NULL)
709 		fatal("Missing argument for Updated");
710 
711 	rpath = cvs_remote_input();
712 	en = cvs_remote_input();
713 	mode = cvs_remote_input();
714 	len = cvs_remote_input();
715 
716 	client_check_directory(data, rpath);
717 	cvs_get_repository_path(".", repo, MAXPATHLEN);
718 
719 	STRIP_SLASH(repo);
720 
721 	if (strlen(repo) + 1 > strlen(rpath))
722 		fatal("received a repository path that is too short");
723 
724 	p = strrchr(rpath, '/');
725 	if (p == NULL)
726 		fatal("malicious repository path from server");
727 
728 	(void)xsnprintf(fpath, sizeof(fpath), "%s/%s", data, p);
729 
730 	flen = strtonum(len, 0, INT_MAX, &errstr);
731 	if (errstr != NULL)
732 		fatal("cvs_client_updated: %s: %s", len, errstr);
733 	xfree(len);
734 
735 	cvs_strtomode(mode, &fmode);
736 	xfree(mode);
737 	fmode &= ~cvs_umask;
738 
739 	time(&now);
740 	gmtime_r(&now, &datetm);
741 	asctime_r(&datetm, timebuf);
742 	timebuf[strcspn(timebuf, "\n")] = '\0';
743 
744 	e = cvs_ent_parse(en);
745 	xfree(en);
746 
747 	sticky[0] = '\0';
748 	if (e->ce_tag != NULL) {
749 		(void)xsnprintf(sticky, sizeof(sticky), "T%s", e->ce_tag);
750 	} else if (e->ce_date != -1) {
751 		gmtime_r(&(e->ce_date), &datetm);
752 		(void)strftime(sticky, sizeof(sticky),
753 		    "D"CVS_DATE_FMT, &datetm);
754 	}
755 
756 	rcsnum_tostr(e->ce_rev, revbuf, sizeof(revbuf));
757 
758 	entry = xmalloc(CVS_ENT_MAXLINELEN);
759 	cvs_ent_line_str(e->ce_name, revbuf, timebuf,
760 	    e->ce_opts ? e->ce_opts : "", sticky, 0, 0,
761 	    entry, CVS_ENT_MAXLINELEN);
762 
763 	cvs_ent_free(e);
764 
765 	if (cvs_cmdop != CVS_OP_EXPORT) {
766 		ent = cvs_ent_open(data);
767 		cvs_ent_add(ent, entry);
768 	}
769 
770 	xfree(entry);
771 
772 	(void)unlink(fpath);
773 	if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
774 		fatal("cvs_client_updated: open: %s: %s",
775 		    fpath, strerror(errno));
776 
777 	cvs_remote_receive_file(fd, flen);
778 
779 	tv[0].tv_sec = now;
780 	tv[0].tv_usec = 0;
781 	tv[1] = tv[0];
782 
783 	if (futimes(fd, tv) == -1)
784 		fatal("cvs_client_updated: futimes: %s", strerror(errno));
785 
786 	if (fchmod(fd, fmode) == -1)
787 		fatal("cvs_client_updated: fchmod: %s", strerror(errno));
788 
789 	(void)close(fd);
790 
791 	xfree(rpath);
792 }
793 
794 void
795 cvs_client_merged(char *data)
796 {
797 	int fd;
798 	time_t now;
799 	mode_t fmode;
800 	size_t flen;
801 	CVSENTRIES *ent;
802 	const char *errstr;
803 	struct timeval tv[2];
804 	struct tm datetm;
805 	char timebuf[CVS_TIME_BUFSZ], *repo, *rpath, *entry, *mode;
806 	char *len, *fpath, *wdir;
807 
808 	if (data == NULL)
809 		fatal("Missing argument for Merged");
810 
811 	rpath = cvs_remote_input();
812 	entry = cvs_remote_input();
813 	mode = cvs_remote_input();
814 	len = cvs_remote_input();
815 
816 	client_check_directory(data, rpath);
817 
818 	repo = xmalloc(MAXPATHLEN);
819 	cvs_get_repository_path(".", repo, MAXPATHLEN);
820 
821 	STRIP_SLASH(repo);
822 
823 	if (strlen(repo) + 1 > strlen(rpath))
824 		fatal("received a repository path that is too short");
825 
826 	fpath = rpath + strlen(repo) + 1;
827 	if ((wdir = dirname(fpath)) == NULL)
828 		fatal("cvs_client_merged: dirname: %s", strerror(errno));
829 	xfree(repo);
830 
831 	flen = strtonum(len, 0, INT_MAX, &errstr);
832 	if (errstr != NULL)
833 		fatal("cvs_client_merged: %s: %s", len, errstr);
834 	xfree(len);
835 
836 	cvs_strtomode(mode, &fmode);
837 	xfree(mode);
838 	fmode &= ~cvs_umask;
839 
840 	time(&now);
841 	gmtime_r(&now, &datetm);
842 	asctime_r(&datetm, timebuf);
843 	timebuf[strcspn(timebuf, "\n")] = '\0';
844 
845 	ent = cvs_ent_open(wdir);
846 	cvs_ent_add(ent, entry);
847 	xfree(entry);
848 
849 	(void)unlink(fpath);
850 	if ((fd = open(fpath, O_CREAT | O_WRONLY | O_TRUNC)) == -1)
851 		fatal("cvs_client_merged: open: %s: %s",
852 		    fpath, strerror(errno));
853 
854 	cvs_remote_receive_file(fd, flen);
855 
856 	tv[0].tv_sec = now;
857 	tv[0].tv_usec = 0;
858 	tv[1] = tv[0];
859 
860 	if (futimes(fd, tv) == -1)
861 		fatal("cvs_client_merged: futimes: %s", strerror(errno));
862 
863 	if (fchmod(fd, fmode) == -1)
864 		fatal("cvs_client_merged: fchmod: %s", strerror(errno));
865 
866 	(void)close(fd);
867 
868 	xfree(rpath);
869 }
870 
871 void
872 cvs_client_removed(char *data)
873 {
874 	CVSENTRIES *entlist;
875 	char *rpath, *filename, fpath[MAXPATHLEN];
876 
877 	if (data == NULL)
878 		fatal("Missing argument for Removed");
879 
880 	rpath = cvs_remote_input();
881 	if ((filename = strrchr(rpath, '/')) == NULL)
882 		fatal("bad rpath in cvs_client_removed: %s", rpath);
883 	filename++;
884 
885 	entlist = cvs_ent_open(data);
886 	cvs_ent_remove(entlist, filename);
887 
888 	(void)xsnprintf(fpath, MAXPATHLEN, "%s/%s", data, filename);
889 	(void)unlink(fpath);
890 
891 	xfree(rpath);
892 }
893 
894 void
895 cvs_client_remove_entry(char *data)
896 {
897 	CVSENTRIES *entlist;
898 	char *filename, *rpath;
899 
900 	if (data == NULL)
901 		fatal("Missing argument for Remove-entry");
902 
903 	rpath = cvs_remote_input();
904 	if ((filename = strrchr(rpath, '/')) == NULL)
905 		fatal("bad rpath in cvs_client_remove_entry: %s", rpath);
906 	filename++;
907 
908 	entlist = cvs_ent_open(data);
909 	cvs_ent_remove(entlist, filename);
910 
911 	xfree(rpath);
912 }
913 
914 void
915 cvs_client_set_static_directory(char *data)
916 {
917 	FILE *fp;
918 	char *dir, fpath[MAXPATHLEN];
919 
920 	if (data == NULL)
921 		fatal("Missing argument for Set-static-directory");
922 
923 	STRIP_SLASH(data);
924 
925 	dir = cvs_remote_input();
926 	xfree(dir);
927 
928 	if (cvs_cmdop == CVS_OP_EXPORT)
929 		return;
930 
931 	(void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
932 	    data, CVS_PATH_STATICENTRIES);
933 
934 	if ((fp = fopen(fpath, "w+")) == NULL) {
935 		cvs_log(LP_ERRNO, "%s", fpath);
936 		return;
937 	}
938 	(void)fclose(fp);
939 }
940 
941 void
942 cvs_client_clear_static_directory(char *data)
943 {
944 	char *dir, fpath[MAXPATHLEN];
945 
946 	if (data == NULL)
947 		fatal("Missing argument for Clear-static-directory");
948 
949 	STRIP_SLASH(data);
950 
951 	dir = cvs_remote_input();
952 	xfree(dir);
953 
954 	if (cvs_cmdop == CVS_OP_EXPORT)
955 		return;
956 
957 	(void)xsnprintf(fpath, MAXPATHLEN, "%s/%s",
958 	    data, CVS_PATH_STATICENTRIES);
959 
960 	(void)cvs_unlink(fpath);
961 }
962 
963 void
964 cvs_client_set_sticky(char *data)
965 {
966 	FILE *fp;
967 	char *dir, *tag, tagpath[MAXPATHLEN];
968 
969 	if (data == NULL)
970 		fatal("Missing argument for Set-sticky");
971 
972 	STRIP_SLASH(data);
973 
974 	dir = cvs_remote_input();
975 	tag = cvs_remote_input();
976 
977 	if (cvs_cmdop == CVS_OP_EXPORT)
978 		goto out;
979 
980 	client_check_directory(data, dir);
981 
982 	(void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
983 
984 	if ((fp = fopen(tagpath, "w+")) == NULL) {
985 		cvs_log(LP_ERRNO, "%s", tagpath);
986 		goto out;
987 	}
988 
989 	(void)fprintf(fp, "%s\n", tag);
990 	(void)fclose(fp);
991 out:
992 	xfree(tag);
993 	xfree(dir);
994 }
995 
996 void
997 cvs_client_clear_sticky(char *data)
998 {
999 	char *dir, tagpath[MAXPATHLEN];
1000 
1001 	if (data == NULL)
1002 		fatal("Missing argument for Clear-sticky");
1003 
1004 	STRIP_SLASH(data);
1005 
1006 	dir = cvs_remote_input();
1007 
1008 	if (cvs_cmdop == CVS_OP_EXPORT) {
1009 		xfree(dir);
1010 		return;
1011 	}
1012 
1013 	client_check_directory(data, dir);
1014 
1015 	(void)xsnprintf(tagpath, MAXPATHLEN, "%s/%s", data, CVS_PATH_TAG);
1016 	(void)unlink(tagpath);
1017 
1018 	xfree(dir);
1019 }
1020 
1021 
1022 /*
1023  * cvs_client_initlog()
1024  *
1025  * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1026  * set.  In this case, the variable's value is used as a path to which the
1027  * appropriate suffix is added (".in" for client input and ".out" for server
1028  * output).
1029  */
1030 static void
1031 cvs_client_initlog(void)
1032 {
1033 	u_int i;
1034 	char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
1035 	char rpath[MAXPATHLEN], timebuf[CVS_TIME_BUFSZ], *s;
1036 	struct stat st;
1037 	time_t now;
1038 	struct passwd *pwd;
1039 
1040 	/* avoid doing it more than once */
1041 	if (cvs_client_logon)
1042 		return;
1043 
1044 	if ((env = getenv("CVS_CLIENT_LOG")) == NULL)
1045 		return;
1046 
1047 	envdup = xstrdup(env);
1048 	if ((s = strchr(envdup, '%')) != NULL)
1049 		*s = '\0';
1050 
1051 	if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
1052 		fatal("cvs_client_initlog: truncation");
1053 
1054 	if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
1055 		fatal("cvs_client_initlog: truncation");
1056 
1057 	xfree(envdup);
1058 
1059 	s = buf;
1060 	while ((s = strchr(s, '%')) != NULL) {
1061 		s++;
1062 		switch (*s) {
1063 		case 'c':
1064 			if (strlcpy(fpath, cmdp->cmd_name, sizeof(fpath)) >=
1065 			    sizeof(fpath))
1066 				fatal("cvs_client_initlog: truncation");
1067 			break;
1068 		case 'd':
1069 			time(&now);
1070 			ctime_r(&now, timebuf);
1071 			timebuf[strcspn(timebuf, "\n")] = '\0';
1072 			if (strlcpy(fpath, timebuf, sizeof(fpath)) >=
1073 			    sizeof(fpath))
1074 				fatal("cvs_client_initlog: truncation");
1075 			break;
1076 		case 'p':
1077 			(void)xsnprintf(fpath, sizeof(fpath), "%d", getpid());
1078 			break;
1079 		case 'u':
1080 			if ((pwd = getpwuid(getuid())) != NULL) {
1081 				if (strlcpy(fpath, pwd->pw_name,
1082 				    sizeof(fpath)) >= sizeof(fpath))
1083 					fatal("cvs_client_initlog: truncation");
1084 			} else {
1085 				fpath[0] = '\0';
1086 			}
1087 			endpwent();
1088 			break;
1089 		default:
1090 			fpath[0] = '\0';
1091 			break;
1092 		}
1093 
1094 		if (fpath[0] != '\0') {
1095 			if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
1096 				fatal("cvs_client_initlog: truncation");
1097 
1098 			if (strlcat(rpath, fpath, sizeof(rpath))
1099 			    >= sizeof(rpath))
1100 				fatal("cvs_client_initlog: truncation");
1101 		}
1102 	}
1103 
1104 	for (i = 0; i < UINT_MAX; i++) {
1105 		(void)xsnprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1106 
1107 		if (stat(fpath, &st) != -1)
1108 			continue;
1109 
1110 		if (errno != ENOENT)
1111 			fatal("cvs_client_initlog() stat failed '%s'",
1112 			    strerror(errno));
1113 
1114 		break;
1115 	}
1116 
1117 	if ((cvs_client_inlog_fd = open(fpath,
1118 	    O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1119 		fatal("cvs_client_initlog: open `%s': %s",
1120 		    fpath, strerror(errno));
1121 	}
1122 
1123 	for (i = 0; i < UINT_MAX; i++) {
1124 		(void)xsnprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1125 
1126 		if (stat(fpath, &st) != -1)
1127 			continue;
1128 
1129 		if (errno != ENOENT)
1130 			fatal("cvs_client_initlog() stat failed '%s'",
1131 			    strerror(errno));
1132 
1133 		break;
1134 	}
1135 
1136 	if ((cvs_client_outlog_fd = open(fpath,
1137 	    O_RDWR | O_CREAT | O_TRUNC, 0644)) == -1) {
1138 		fatal("cvs_client_initlog: open `%s': %s",
1139 		    fpath, strerror(errno));
1140 	}
1141 
1142 	cvs_client_logon = 1;
1143 }
1144