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