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