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