Annotation of src/usr.bin/cvs/proto.c, Revision 1.89
1.89 ! joris 1: /* $OpenBSD: proto.c,v 1.88 2006/01/26 09:05:31 xsa Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26: /*
27: * CVS client/server protocol
28: * ==========================
29: *
30: * The following code implements the CVS client/server protocol, which is
31: * documented at the following URL:
1.72 joris 32: * http://www.iam.unibe.ch/~til/documentation/cvs/cvsclient_toc.html
33: * http://www.elegosoft.com/cvs/cvsclient_toc.html
1.1 jfb 34: *
35: * The protocol is split up into two parts; the first part is the client side
36: * of things and is composed of all the response handlers, which are all named
37: * with a prefix of "cvs_resp_". The second part is the set of request
38: * handlers used by the server. These handlers process the request and
39: * generate the appropriate response to send back. The prefix for request
40: * handlers is "cvs_req_".
41: *
42: */
43:
1.85 xsa 44: #include "includes.h"
1.1 jfb 45:
46: #include "cvs.h"
47: #include "log.h"
1.13 jfb 48: #include "proto.h"
1.1 jfb 49:
50:
1.11 jfb 51: /* request flags */
1.68 xsa 52: #define CVS_REQF_RESP 0x01
1.11 jfb 53:
54:
1.68 xsa 55: static int cvs_initlog(void);
1.1 jfb 56:
1.18 jfb 57: struct cvs_req cvs_requests[] = {
58: { CVS_REQ_DIRECTORY, "Directory", 0 },
59: { CVS_REQ_MAXDOTDOT, "Max-dotdot", 0 },
60: { CVS_REQ_STATICDIR, "Static-directory", 0 },
61: { CVS_REQ_STICKY, "Sticky", 0 },
62: { CVS_REQ_ENTRY, "Entry", 0 },
63: { CVS_REQ_ENTRYEXTRA, "EntryExtra", 0 },
64: { CVS_REQ_CHECKINTIME, "Checkin-time", 0 },
65: { CVS_REQ_MODIFIED, "Modified", 0 },
66: { CVS_REQ_ISMODIFIED, "Is-modified", 0 },
67: { CVS_REQ_UNCHANGED, "Unchanged", 0 },
68: { CVS_REQ_USEUNCHANGED, "UseUnchanged", 0 },
69: { CVS_REQ_NOTIFY, "Notify", 0 },
70: { CVS_REQ_NOTIFYUSER, "NotifyUser", 0 },
71: { CVS_REQ_QUESTIONABLE, "Questionable", 0 },
72: { CVS_REQ_CASE, "Case", 0 },
73: { CVS_REQ_UTF8, "Utf8", 0 },
74: { CVS_REQ_ARGUMENT, "Argument", 0 },
75: { CVS_REQ_ARGUMENTX, "Argumentx", 0 },
76: { CVS_REQ_GLOBALOPT, "Global_option", 0 },
77: { CVS_REQ_GZIPSTREAM, "Gzip-stream", 0 },
78: { CVS_REQ_READCVSRC2, "read-cvsrc2", 0 },
79: { CVS_REQ_READWRAP, "read-cvswrappers", 0 },
80: { CVS_REQ_READIGNORE, "read-cvsignore", 0 },
81: { CVS_REQ_ERRIFREADER, "Error-If-Reader", 0 },
82: { CVS_REQ_VALIDRCSOPT, "Valid-RcsOptions", 0 },
83: { CVS_REQ_SET, "Set", 0 },
84: { CVS_REQ_XPANDMOD, "expand-modules", CVS_REQF_RESP },
1.26 jfb 85: { CVS_REQ_LOG, "log", CVS_REQF_RESP },
1.18 jfb 86: { CVS_REQ_CO, "co", CVS_REQF_RESP },
87: { CVS_REQ_EXPORT, "export", CVS_REQF_RESP },
1.26 jfb 88: { CVS_REQ_ANNOTATE, "annotate", CVS_REQF_RESP },
89: { CVS_REQ_RDIFF, "rdiff", CVS_REQF_RESP },
1.18 jfb 90: { CVS_REQ_RTAG, "rtag", CVS_REQF_RESP },
91: { CVS_REQ_INIT, "init", CVS_REQF_RESP },
92: { CVS_REQ_STATUS, "status", CVS_REQF_RESP },
93: { CVS_REQ_UPDATE, "update", CVS_REQF_RESP },
1.26 jfb 94: { CVS_REQ_HISTORY, "history", CVS_REQF_RESP },
1.18 jfb 95: { CVS_REQ_IMPORT, "import", CVS_REQF_RESP },
96: { CVS_REQ_ADD, "add", CVS_REQF_RESP },
97: { CVS_REQ_REMOVE, "remove", CVS_REQF_RESP },
98: { CVS_REQ_RELEASE, "release", CVS_REQF_RESP },
99: { CVS_REQ_ROOT, "Root", 0 },
100: { CVS_REQ_VALIDRESP, "Valid-responses", 0 },
101: { CVS_REQ_VALIDREQ, "valid-requests", CVS_REQF_RESP },
102: { CVS_REQ_VERSION, "version", CVS_REQF_RESP },
103: { CVS_REQ_NOOP, "noop", CVS_REQF_RESP },
104: { CVS_REQ_DIFF, "diff", CVS_REQF_RESP },
1.29 jfb 105: { CVS_REQ_CI, "ci", CVS_REQF_RESP },
1.35 jfb 106: { CVS_REQ_TAG, "tag", CVS_REQF_RESP },
1.41 joris 107: { CVS_REQ_ADMIN, "admin", CVS_REQF_RESP },
1.76 xsa 108: { CVS_REQ_WATCHERS, "watchers", CVS_REQF_RESP },
1.79 moritz 109: { CVS_REQ_WATCH_ON, "watch-on", CVS_REQF_RESP },
110: { CVS_REQ_WATCH_OFF, "watch-off", CVS_REQF_RESP },
111: { CVS_REQ_WATCH_ADD, "watch-add", CVS_REQF_RESP },
112: { CVS_REQ_WATCH_REMOVE, "watch-remove", CVS_REQF_RESP },
1.78 xsa 113: { CVS_REQ_EDITORS, "editors", CVS_REQF_RESP },
1.1 jfb 114: };
115:
1.18 jfb 116: struct cvs_resp cvs_responses[] = {
117: { CVS_RESP_OK, "ok" },
118: { CVS_RESP_ERROR, "error" },
119: { CVS_RESP_VALIDREQ, "Valid-requests" },
120: { CVS_RESP_M, "M" },
121: { CVS_RESP_MBINARY, "Mbinary" },
122: { CVS_RESP_MT, "MT" },
123: { CVS_RESP_E, "E" },
124: { CVS_RESP_F, "F" },
125: { CVS_RESP_CREATED, "Created" },
126: { CVS_RESP_UPDATED, "Updated" },
127: { CVS_RESP_UPDEXIST, "Update-existing" },
128: { CVS_RESP_MERGED, "Merged" },
129: { CVS_RESP_REMOVED, "Removed" },
1.63 joris 130: { CVS_RESP_RMENTRY, "Remove-entry" },
1.18 jfb 131: { CVS_RESP_CKSUM, "Checksum" },
132: { CVS_RESP_CLRSTATDIR, "Clear-static-directory" },
133: { CVS_RESP_SETSTATDIR, "Set-static-directory" },
134: { CVS_RESP_NEWENTRY, "New-entry" },
135: { CVS_RESP_CHECKEDIN, "Checked-in" },
136: { CVS_RESP_MODE, "Mode" },
137: { CVS_RESP_MODTIME, "Mod-time" },
138: { CVS_RESP_MODXPAND, "Module-expansion" },
139: { CVS_RESP_SETSTICKY, "Set-sticky" },
140: { CVS_RESP_CLRSTICKY, "Clear-sticky" },
141: { CVS_RESP_RCSDIFF, "Rcs-diff" },
142: { CVS_RESP_TEMPLATE, "Template" },
1.34 jfb 143: { CVS_RESP_COPYFILE, "Copy-file" },
1.1 jfb 144: };
145:
1.68 xsa 146: #define CVS_NBREQ (sizeof(cvs_requests)/sizeof(cvs_requests[0]))
147: #define CVS_NBRESP (sizeof(cvs_responses)/sizeof(cvs_responses[0]))
1.17 jfb 148:
149: /* hack to receive the remote version without outputting it */
1.18 jfb 150: u_int cvs_version_sent = 0;
1.17 jfb 151:
1.13 jfb 152: static char cvs_proto_buf[4096];
153:
154: /*
1.37 david 155: * Output files for protocol logging when the CVS_CLIENT_LOG environment
1.13 jfb 156: * variable is set.
157: */
158: static int cvs_server_logon = 0;
159: static FILE *cvs_server_inlog = NULL;
160: static FILE *cvs_server_outlog = NULL;
161:
1.28 jfb 162: static pid_t cvs_subproc_pid;
163:
1.32 jfb 164: /* last directory sent with cvs_senddir() */
165: static char cvs_lastdir[MAXPATHLEN] = "";
166:
1.13 jfb 167:
168: /*
169: * cvs_connect()
170: *
171: * Open a client connection to the cvs server whose address is given in
172: * the <root> variable. The method used to connect depends on the
173: * setting of the CVS_RSH variable.
1.17 jfb 174: * Once the connection has been established, we first send the list of
175: * responses we support and request the list of supported requests from the
176: * server. Then, a version request is sent and various global flags are sent.
1.13 jfb 177: */
1.82 joris 178: void
1.13 jfb 179: cvs_connect(struct cvsroot *root)
180: {
1.82 joris 181: size_t len;
1.20 jfb 182: int argc, infd[2], outfd[2], errfd[2];
1.71 xsa 183: char *argv[16], *cvs_server_cmd, tmsg[1024], *vresp;
1.13 jfb 184:
1.82 joris 185: if (root->cr_method == CVS_METHOD_PSERVER)
186: fatal("no pserver support due to security issues");
187: else if ((root->cr_method == CVS_METHOD_KSERVER) ||
1.36 jfb 188: (root->cr_method == CVS_METHOD_GSERVER) ||
189: (root->cr_method == CVS_METHOD_EXT) ||
1.82 joris 190: (root->cr_method == CVS_METHOD_FORK))
191: fatal("connection method not supported yet");
1.33 jfb 192:
193: if (root->cr_flags & CVS_ROOT_CONNECTED) {
194: cvs_log(LP_NOTICE, "already connected to CVSROOT");
1.82 joris 195: return;
1.33 jfb 196: }
197:
1.82 joris 198: if (pipe(infd) == -1)
199: fatal("failed to create input pipe for client connection");
1.13 jfb 200:
1.82 joris 201: if (pipe(outfd) == -1)
202: fatal("failed to create output pipe for client connection");
1.13 jfb 203:
1.82 joris 204: if (pipe(errfd) == -1)
205: fatal("failed to create error pipe for client connection");
1.20 jfb 206:
1.28 jfb 207: cvs_subproc_pid = fork();
208: if (cvs_subproc_pid == -1) {
1.82 joris 209: fatal("failed to fork for cvs server connection");
1.30 deraadt 210: } else if (cvs_subproc_pid == 0) {
1.13 jfb 211: if ((dup2(infd[0], STDIN_FILENO) == -1) ||
1.82 joris 212: (dup2(outfd[1], STDOUT_FILENO) == -1))
1.84 reyk 213: fatal("failed to setup standard streams "
214: "for cvs server");
1.82 joris 215:
1.13 jfb 216: (void)close(infd[1]);
217: (void)close(outfd[0]);
1.20 jfb 218: (void)close(errfd[0]);
1.13 jfb 219:
220: argc = 0;
221: argv[argc++] = cvs_rsh;
222:
223: if (root->cr_user != NULL) {
224: argv[argc++] = "-l";
225: argv[argc++] = root->cr_user;
226: }
227:
228: cvs_server_cmd = getenv("CVS_SERVER");
229: if (cvs_server_cmd == NULL)
1.20 jfb 230: cvs_server_cmd = CVS_SERVER_DEFAULT;
1.13 jfb 231:
232: argv[argc++] = root->cr_host;
233: argv[argc++] = cvs_server_cmd;
234: argv[argc++] = "server";
235: argv[argc] = NULL;
1.71 xsa 236:
237: if (cvs_trace == 1) {
238: tmsg[0] = '\0';
239: for (argc = 0; argv[argc] != NULL; argc++) {
1.82 joris 240: len = strlcat(tmsg, argv[argc], sizeof(tmsg));
241: if (len >= sizeof(tmsg))
242: fatal("truncation in cvs_connect");
243:
244: len = strlcat(tmsg, " ", sizeof(tmsg));
245: if (len >= sizeof(tmsg))
246: fatal("truncation in cvs_connect");
1.71 xsa 247: }
248: }
1.82 joris 249:
1.71 xsa 250: cvs_log(LP_TRACE, "Starting server: %s", tmsg);
1.13 jfb 251:
252: execvp(argv[0], argv);
1.82 joris 253: fatal("failed to execute cvs server");
1.13 jfb 254: }
255:
256: /* we are the parent */
257: (void)close(infd[0]);
258: (void)close(outfd[1]);
1.20 jfb 259: (void)close(errfd[1]);
1.13 jfb 260:
261: root->cr_srvin = fdopen(infd[1], "w");
1.82 joris 262: if (root->cr_srvin == NULL)
263: fatal("failed to create pipe stream");
1.13 jfb 264:
265: root->cr_srvout = fdopen(outfd[0], "r");
1.82 joris 266: if (root->cr_srvout == NULL)
267: fatal("failed to create pipe stream");
1.20 jfb 268:
1.13 jfb 269: /* make the streams line-buffered */
1.75 xsa 270: (void)setvbuf(root->cr_srvin, NULL, _IOLBF, (size_t)0);
271: (void)setvbuf(root->cr_srvout, NULL, _IOLBF, (size_t)0);
1.13 jfb 272:
273: cvs_initlog();
274:
275: /*
276: * Send the server the list of valid responses, then ask for valid
277: * requests.
278: */
279:
1.82 joris 280: vresp = cvs_resp_getvalid();
281: cvs_sendreq(root, CVS_REQ_VALIDRESP, vresp);
1.80 joris 282: xfree(vresp);
1.13 jfb 283:
1.82 joris 284: cvs_sendreq(root, CVS_REQ_VALIDREQ, NULL);
1.13 jfb 285:
1.62 xsa 286: /* send the CVSROOT to the server */
1.82 joris 287: cvs_sendreq(root, CVS_REQ_ROOT, root->cr_dir);
1.54 joris 288:
1.40 jfb 289: /* don't fail if this request doesn't work */
1.82 joris 290: cvs_sendreq(root, CVS_REQ_VERSION, NULL);
1.17 jfb 291:
1.13 jfb 292: /* now share our global options with the server */
1.82 joris 293: if (verbosity <= 1)
294: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-q");
295: if (verbosity == 0)
296: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-Q");
297:
298: if (cvs_noexec == 1)
299: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-n");
300:
301: if (cvs_nolog == 1)
302: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-l");
303:
304: if (cvs_readonly == 1)
305: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-r");
1.40 jfb 306:
1.82 joris 307: if (cvs_trace == 1)
308: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-t");
1.13 jfb 309:
310: /* not sure why, but we have to send this */
1.82 joris 311: cvs_sendreq(root, CVS_REQ_USEUNCHANGED, NULL);
1.13 jfb 312:
313: cvs_log(LP_DEBUG, "connected to %s", root->cr_host);
314:
1.33 jfb 315: root->cr_flags |= CVS_ROOT_CONNECTED;
1.13 jfb 316: }
317:
318:
319: /*
320: * cvs_disconnect()
321: *
322: * Disconnect from the cvs server.
323: */
324: void
325: cvs_disconnect(struct cvsroot *root)
326: {
1.33 jfb 327: if (!(root->cr_flags & CVS_ROOT_CONNECTED))
328: return;
329:
1.13 jfb 330: cvs_log(LP_DEBUG, "closing connection to %s", root->cr_host);
1.82 joris 331:
1.13 jfb 332: if (root->cr_srvin != NULL) {
333: (void)fclose(root->cr_srvin);
334: root->cr_srvin = NULL;
335: }
336: if (root->cr_srvout != NULL) {
337: (void)fclose(root->cr_srvout);
338: root->cr_srvout = NULL;
339: }
1.33 jfb 340:
341: root->cr_flags &= ~CVS_ROOT_CONNECTED;
1.13 jfb 342: }
343:
1.1 jfb 344:
345: /*
346: * cvs_req_getbyid()
347: *
348: */
1.13 jfb 349: struct cvs_req*
1.1 jfb 350: cvs_req_getbyid(int reqid)
351: {
352: u_int i;
353:
354: for (i = 0; i < CVS_NBREQ; i++)
355: if (cvs_requests[i].req_id == reqid)
1.13 jfb 356: return &(cvs_requests[i]);
1.31 tedu 357:
1.1 jfb 358: return (NULL);
359: }
360:
361:
362: /*
363: * cvs_req_getbyname()
364: */
1.13 jfb 365: struct cvs_req*
1.1 jfb 366: cvs_req_getbyname(const char *rname)
367: {
368: u_int i;
369:
370: for (i = 0; i < CVS_NBREQ; i++)
371: if (strcmp(cvs_requests[i].req_str, rname) == 0)
1.13 jfb 372: return &(cvs_requests[i]);
1.1 jfb 373:
1.13 jfb 374: return (NULL);
1.1 jfb 375: }
376:
377:
378: /*
379: * cvs_req_getvalid()
380: *
381: * Build a space-separated list of all the requests that this protocol
382: * implementation supports.
383: */
1.67 xsa 384: char *
1.1 jfb 385: cvs_req_getvalid(void)
386: {
387: u_int i;
388: size_t len;
389: char *vrstr;
390: BUF *buf;
391:
1.75 xsa 392: buf = cvs_buf_alloc((size_t)512, BUF_AUTOEXT);
1.1 jfb 393:
394: cvs_buf_set(buf, cvs_requests[0].req_str,
1.75 xsa 395: strlen(cvs_requests[0].req_str), (size_t)0);
1.1 jfb 396:
397: for (i = 1; i < CVS_NBREQ; i++) {
1.81 xsa 398: cvs_buf_putc(buf, ' ');
399: cvs_buf_append(buf, cvs_requests[i].req_str,
400: strlen(cvs_requests[i].req_str));
1.1 jfb 401: }
402:
403: /* NUL-terminate */
1.81 xsa 404: cvs_buf_putc(buf, '\0');
1.1 jfb 405:
1.70 moritz 406: len = cvs_buf_len(buf);
1.80 joris 407: vrstr = (char *)xmalloc(len);
1.75 xsa 408: cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1 jfb 409: cvs_buf_free(buf);
410:
411: return (vrstr);
412: }
413:
414:
415: /*
416: * cvs_resp_getbyid()
417: *
418: */
1.13 jfb 419: struct cvs_resp*
1.1 jfb 420: cvs_resp_getbyid(int respid)
421: {
422: u_int i;
423:
1.44 tedu 424: for (i = 0; i < CVS_NBRESP; i++)
1.13 jfb 425: if (cvs_responses[i].resp_id == (u_int)respid)
426: return &(cvs_responses[i]);
1.31 tedu 427:
1.1 jfb 428: return (NULL);
429: }
430:
431:
432: /*
433: * cvs_resp_getbyname()
434: */
1.67 xsa 435: struct cvs_resp *
1.1 jfb 436: cvs_resp_getbyname(const char *rname)
437: {
438: u_int i;
439:
1.44 tedu 440: for (i = 0; i < CVS_NBRESP; i++)
1.1 jfb 441: if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13 jfb 442: return &(cvs_responses[i]);
1.1 jfb 443:
1.13 jfb 444: return (NULL);
1.1 jfb 445: }
446:
447:
448: /*
449: * cvs_resp_getvalid()
450: *
451: * Build a space-separated list of all the responses that this protocol
452: * implementation supports.
453: */
1.67 xsa 454: char *
1.1 jfb 455: cvs_resp_getvalid(void)
456: {
457: u_int i;
458: size_t len;
459: char *vrstr;
460: BUF *buf;
461:
1.75 xsa 462: buf = cvs_buf_alloc((size_t)512, BUF_AUTOEXT);
1.1 jfb 463:
464: cvs_buf_set(buf, cvs_responses[0].resp_str,
1.75 xsa 465: strlen(cvs_responses[0].resp_str), (size_t)0);
1.1 jfb 466:
467: for (i = 1; i < CVS_NBRESP; i++) {
1.81 xsa 468: cvs_buf_putc(buf, ' ');
469: cvs_buf_append(buf, cvs_responses[i].resp_str,
470: strlen(cvs_responses[i].resp_str));
1.1 jfb 471: }
472:
473: /* NUL-terminate */
1.81 xsa 474: cvs_buf_putc(buf, '\0');
1.1 jfb 475:
1.70 moritz 476: len = cvs_buf_len(buf);
1.80 joris 477: vrstr = (char *)xmalloc(len);
1.75 xsa 478: cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1 jfb 479: cvs_buf_free(buf);
480:
481: return (vrstr);
482: }
483:
484:
485: /*
486: * cvs_sendfile()
487: *
488: * Send the mode and size of a file followed by the file's contents.
489: * Returns 0 on success, or -1 on failure.
490: */
1.82 joris 491: void
1.13 jfb 492: cvs_sendfile(struct cvsroot *root, const char *path)
1.1 jfb 493: {
1.51 xsa 494: int fd, l;
1.1 jfb 495: ssize_t ret;
496: char buf[4096];
497: struct stat st;
498:
1.38 jfb 499: cvs_log(LP_TRACE, "Sending file `%s' to server", basename(path));
500:
1.1 jfb 501: if (stat(path, &st) == -1) {
1.82 joris 502: fatal("cvs_sendfile(): stat failed on '%s': %s",
503: path, strerror(errno));
1.1 jfb 504: }
505:
1.82 joris 506: cvs_modetostr(st.st_mode, buf, sizeof(buf));
1.24 jfb 507:
1.1 jfb 508: fd = open(path, O_RDONLY, 0);
509: if (fd == -1) {
1.82 joris 510: fatal("cvs_sendfile(): failed to open '%s': %s",
511: path, strerror(errno));
1.1 jfb 512: }
513:
1.82 joris 514: cvs_sendln(root, buf);
515:
1.51 xsa 516: l = snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
517: if (l == -1 || l >= (int)sizeof(buf))
1.82 joris 518: fatal("overflow in cvs_sendfile");
1.51 xsa 519:
1.82 joris 520: cvs_sendln(root, buf);
1.1 jfb 521:
522: while ((ret = read(fd, buf, sizeof(buf))) != 0) {
523: if (ret == -1) {
1.24 jfb 524: (void)close(fd);
1.82 joris 525: fatal("cvs_sendfile: read error on '%s'", path);
1.1 jfb 526: }
527:
1.82 joris 528: cvs_sendraw(root, buf, (size_t)ret);
1.1 jfb 529: }
530:
531: (void)close(fd);
532: }
533:
534:
535: /*
536: * cvs_recvfile()
537: *
538: * Receive the mode and size of a file followed the file's contents and
539: * create or update the file whose path is <path> with the received
540: * information.
541: */
1.14 jfb 542: BUF*
543: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 544: {
545: size_t len;
546: ssize_t ret;
547: off_t fsz, cnt;
548: char buf[4096], *ep;
1.14 jfb 549: BUF *fbuf;
550:
551: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
1.1 jfb 552:
1.83 joris 553: cvs_getln(root, buf, sizeof(buf));
554: cvs_strtomode(buf, mode);
1.1 jfb 555:
1.83 joris 556: cvs_getln(root, buf, sizeof(buf));
1.1 jfb 557:
558: fsz = (off_t)strtol(buf, &ep, 10);
1.83 joris 559: if (*ep != '\0')
560: fatal("parse error in file size transmission");
1.1 jfb 561:
562: cnt = 0;
563: do {
564: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 565: if (len == 0)
566: break;
1.13 jfb 567: ret = cvs_recvraw(root, buf, len);
1.83 joris 568: cvs_buf_append(fbuf, buf, (size_t)ret);
1.1 jfb 569: cnt += (off_t)ret;
570: } while (cnt < fsz);
571:
1.14 jfb 572: return (fbuf);
1.1 jfb 573: }
1.12 jfb 574:
575: /*
576: * cvs_sendreq()
577: *
578: * Send a request to the server of type <rid>, with optional arguments
579: * contained in <arg>, which should not be terminated by a newline.
580: * Returns 0 on success, or -1 on failure.
581: */
1.82 joris 582: void
1.13 jfb 583: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 584: {
1.88 xsa 585: int ret;
1.13 jfb 586: struct cvs_req *req;
1.12 jfb 587:
1.82 joris 588: if (root->cr_srvin == NULL)
589: fatal("cannot send request %u: Not connected", rid);
1.12 jfb 590:
1.13 jfb 591: req = cvs_req_getbyid(rid);
1.82 joris 592: if (req == NULL)
593: fatal("unsupported request type %u", rid);
1.12 jfb 594:
1.18 jfb 595: /* is this request supported by the server? */
596: if (!CVS_GETVR(root, req->req_id)) {
1.57 joris 597: if (rid == CVS_REQ_VERSION) {
1.82 joris 598: cvs_sendreq(root, CVS_REQ_NOOP, arg);
1.57 joris 599: } else {
600: cvs_log(LP_WARN,
601: "remote end does not support request `%s'",
602: req->req_str);
603: }
1.84 reyk 604:
1.82 joris 605: return;
1.18 jfb 606: }
607:
1.88 xsa 608: if (strlcpy(cvs_proto_buf, req->req_str, sizeof(cvs_proto_buf)) >=
609: sizeof(cvs_proto_buf) ||
610: strlcat(cvs_proto_buf, (arg == NULL) ? "" : " ",
611: sizeof(cvs_proto_buf)) >= sizeof(cvs_proto_buf) ||
612: strlcat(cvs_proto_buf, (arg == NULL) ? "" : arg,
1.89 ! joris 613: sizeof(cvs_proto_buf)) >= sizeof(cvs_proto_buf) ||
! 614: strlcat(cvs_proto_buf, "\n",
1.88 xsa 615: sizeof(cvs_proto_buf)) >= sizeof(cvs_proto_buf))
616: fatal("cvs_sendreq: overflow when creating proto buffer");
1.12 jfb 617:
618: if (cvs_server_inlog != NULL)
1.13 jfb 619: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 620:
1.13 jfb 621: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.82 joris 622: if (ret == EOF)
623: fatal("failed to send request to server");
1.17 jfb 624:
625: if (rid == CVS_REQ_VERSION)
626: cvs_version_sent = 1;
1.12 jfb 627:
1.13 jfb 628: if (req->req_flags & CVS_REQF_RESP)
1.82 joris 629: cvs_getresp(root);
1.12 jfb 630: }
631:
632:
633: /*
634: * cvs_getresp()
635: *
636: * Get a response from the server. This call will actually read and handle
637: * responses from the server until one of the response handlers returns
1.37 david 638: * non-zero (either an error occurred or the end of the response was reached).
1.12 jfb 639: * Returns the number of handled commands on success, or -1 on failure.
640: */
1.82 joris 641: void
1.13 jfb 642: cvs_getresp(struct cvsroot *root)
1.12 jfb 643: {
1.82 joris 644: int ret;
1.13 jfb 645: size_t len;
1.12 jfb 646:
647: do {
648: /* wait for incoming data */
1.74 xsa 649: if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12 jfb 650: root->cr_srvout) == NULL) {
651: if (feof(root->cr_srvout))
1.82 joris 652: return;
653: fatal("failed to read response from server");
1.12 jfb 654: }
655:
656: if (cvs_server_outlog != NULL)
1.13 jfb 657: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 658:
1.13 jfb 659: if ((len = strlen(cvs_proto_buf)) != 0) {
1.42 joris 660: /* if len - 1 != '\n' the line is truncated */
1.43 joris 661: if (cvs_proto_buf[len - 1] == '\n')
1.13 jfb 662: cvs_proto_buf[--len] = '\0';
1.12 jfb 663: }
664:
1.13 jfb 665: ret = cvs_resp_handle(root, cvs_proto_buf);
1.12 jfb 666: } while (ret == 0);
667: }
668:
669:
670: /*
1.13 jfb 671: * cvs_getln()
672: *
1.18 jfb 673: * Get a line from the remote end and store it in <lbuf>. The terminating
1.13 jfb 674: * newline character is stripped from the result.
675: */
1.83 joris 676: void
1.13 jfb 677: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
678: {
679: size_t rlen;
1.18 jfb 680: FILE *in;
1.13 jfb 681:
1.18 jfb 682: if (cvs_cmdop == CVS_OP_SERVER)
683: in = stdin;
684: else
685: in = root->cr_srvout;
686:
1.74 xsa 687: if (fgets(lbuf, (int)len, in) == NULL) {
1.18 jfb 688: if (ferror(in)) {
1.83 joris 689: fatal("cvs_getln: error reading server: %s",
690: strerror(errno));
1.13 jfb 691: }
692:
1.18 jfb 693: if (feof(in))
1.13 jfb 694: *lbuf = '\0';
695: }
696:
697: if (cvs_server_outlog != NULL)
698: fputs(lbuf, cvs_server_outlog);
699:
700: rlen = strlen(lbuf);
701: if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
702: lbuf[--rlen] = '\0';
703: }
704:
1.19 jfb 705:
1.13 jfb 706: /*
1.12 jfb 707: * cvs_sendresp()
708: *
1.19 jfb 709: * Send a response of type <rid> to the client, with optional arguments
1.12 jfb 710: * contained in <arg>, which should not be terminated by a newline.
711: */
1.87 xsa 712: void
1.12 jfb 713: cvs_sendresp(u_int rid, const char *arg)
714: {
715: int ret;
1.19 jfb 716: struct cvs_resp *resp;
1.12 jfb 717:
1.87 xsa 718: if ((resp = cvs_resp_getbyid(rid)) == NULL);
719: fatal("unsupported response type %u", rid);
1.12 jfb 720:
1.19 jfb 721: ret = fputs(resp->resp_str, stdout);
1.12 jfb 722: if (ret == EOF) {
723: cvs_log(LP_ERRNO, "failed to send response to client");
1.30 deraadt 724: } else {
1.19 jfb 725: if (arg != NULL) {
726: putc(' ', stdout);
727: fputs(arg, stdout);
728: }
1.12 jfb 729: putc('\n', stdout);
730: }
731: }
732:
733:
734: /*
735: * cvs_sendln()
736: *
1.18 jfb 737: * Send a single line <line> string to the remote end. The line is sent as is,
1.12 jfb 738: * without any modifications.
739: */
1.82 joris 740: void
1.12 jfb 741: cvs_sendln(struct cvsroot *root, const char *line)
742: {
743: int nl;
744: size_t len;
1.18 jfb 745: FILE *out;
746:
747: if (cvs_cmdop == CVS_OP_SERVER)
748: out = stdout;
749: else
750: out = root->cr_srvin;
1.12 jfb 751:
752: nl = 0;
753: len = strlen(line);
754:
755: if ((len > 0) && (line[len - 1] != '\n'))
756: nl = 1;
757:
758: if (cvs_server_inlog != NULL) {
759: fputs(line, cvs_server_inlog);
760: if (nl)
1.55 jfb 761: putc('\n', cvs_server_inlog);
1.12 jfb 762: }
1.18 jfb 763: fputs(line, out);
1.12 jfb 764: if (nl)
1.55 jfb 765: putc('\n', out);
1.12 jfb 766: }
767:
768:
769: /*
1.13 jfb 770: * cvs_sendraw()
1.12 jfb 771: *
772: * Send the first <len> bytes from the buffer <src> to the server.
773: */
1.82 joris 774: void
1.13 jfb 775: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12 jfb 776: {
1.18 jfb 777: FILE *out;
778:
779: if (cvs_cmdop == CVS_OP_SERVER)
780: out = stdout;
781: else
782: out = root->cr_srvin;
783:
1.12 jfb 784: if (cvs_server_inlog != NULL)
785: fwrite(src, sizeof(char), len, cvs_server_inlog);
1.82 joris 786: if (fwrite(src, sizeof(char), len, out) < len)
787: fatal("failed to send data");
1.12 jfb 788: }
789:
790:
791: /*
1.13 jfb 792: * cvs_recvraw()
1.12 jfb 793: *
794: * Receive the first <len> bytes from the buffer <src> to the server.
795: */
1.83 joris 796: size_t
1.13 jfb 797: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 798: {
799: size_t ret;
1.18 jfb 800: FILE *in;
801:
802: if (cvs_cmdop == CVS_OP_SERVER)
803: in = stdin;
804: else
805: in = root->cr_srvout;
1.12 jfb 806:
1.18 jfb 807: ret = fread(dst, sizeof(char), len, in);
1.83 joris 808: if (ret == 0) {
809: if (ferror(in)) {
810: fatal("cvs_recvraw: error reading from server: %s",
811: strerror(errno));
812: }
813: } else {
814: if (cvs_server_outlog != NULL)
815: fwrite(dst, sizeof(char), len, cvs_server_outlog);
816: }
817:
818: return (ret);
1.12 jfb 819: }
820:
821:
822: /*
1.13 jfb 823: * cvs_senddir()
1.12 jfb 824: *
1.32 jfb 825: * Send a `Directory' request along with the 2 paths that follow it. If
826: * the directory info to be sent is the same as the last info sent, the
827: * call does nothing and simply returns without an error.
1.12 jfb 828: */
1.82 joris 829: void
1.31 tedu 830: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 831: {
1.60 xsa 832: size_t len;
1.28 jfb 833: char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12 jfb 834:
1.47 jfb 835: if (dir->cf_type != DT_DIR)
1.82 joris 836: fatal("cvs_senddir(): cf_type != DT_DIR: %d", dir->cf_type);
1.47 jfb 837:
1.32 jfb 838: cvs_file_getpath(dir, lbuf, sizeof(lbuf));
1.45 joris 839: if (strcmp(lbuf, cvs_lastdir) == 0 && cvs_cmdop != CVS_OP_CHECKOUT)
1.82 joris 840: return;
1.32 jfb 841:
1.82 joris 842: if (dir->cf_repo == NULL) {
843: len = strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
844: if (len >= sizeof(rbuf))
845: fatal("cvs_senddir: path truncation");
846: } else {
1.60 xsa 847: len = cvs_path_cat(root->cr_dir, dir->cf_repo, rbuf,
848: sizeof(rbuf));
849: if (len >= sizeof(rbuf))
1.82 joris 850: fatal("cvs_senddir: path truncation");
1.51 xsa 851: }
1.12 jfb 852:
1.82 joris 853: cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf);
854: cvs_sendln(root, rbuf);
1.28 jfb 855:
1.82 joris 856: len = strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
857: if (len >= sizeof(lbuf))
858: fatal("path truncation in cvs_senddir");
1.12 jfb 859: }
860:
861:
862: /*
1.13 jfb 863: * cvs_sendarg()
1.12 jfb 864: *
865: * Send the argument <arg> to the server. The argument <append> is used to
866: * determine if the argument should be simply appended to the last argument
867: * sent or if it should be created as a new argument (0).
868: */
1.82 joris 869: void
1.13 jfb 870: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12 jfb 871: {
1.82 joris 872: cvs_sendreq(root, ((append == 0) ? CVS_REQ_ARGUMENT :
873: CVS_REQ_ARGUMENTX), arg);
1.12 jfb 874: }
875:
876:
877: /*
1.13 jfb 878: * cvs_sendentry()
1.12 jfb 879: *
880: * Send an `Entry' request to the server along with the mandatory fields from
881: * the CVS entry <ent> (which are the name and revision).
882: */
1.82 joris 883: void
1.47 jfb 884: cvs_sendentry(struct cvsroot *root, const CVSFILE *file)
1.12 jfb 885: {
1.77 xsa 886: char ebuf[CVS_ENT_MAXLINELEN], numbuf[64];
1.12 jfb 887:
1.82 joris 888: if (file->cf_type != DT_REG)
889: fatal("cvs_sendentry: cf_type != DT_REG: %d", file->cf_type);
1.48 joris 890:
891: /* don't send Entry for unknown files */
892: if (file->cf_cvstat == CVS_FST_UNKNOWN)
1.82 joris 893: return;
1.47 jfb 894:
1.88 xsa 895: if (strlcpy(ebuf, "/", sizeof(ebuf)) >= sizeof(ebuf) ||
896: strlcat(ebuf, file->cf_name, sizeof(ebuf)) >= sizeof(ebuf) ||
897: strlcat(ebuf, "/", sizeof(ebuf)) >= sizeof(ebuf) ||
898: strlcat(ebuf, (file->cf_cvstat == CVS_FST_REMOVED) ? "-" : "",
899: sizeof(ebuf)) >= sizeof(ebuf) ||
900: strlcat(ebuf, rcsnum_tostr(file->cf_lrev, numbuf, sizeof(numbuf)),
901: sizeof(ebuf)) >= sizeof(ebuf) ||
902: strlcat(ebuf, "///", sizeof(ebuf)) >= sizeof(ebuf))
1.82 joris 903: fatal("cvs_sendentry: overflow when creating entry buffer");
1.12 jfb 904:
1.82 joris 905: cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12 jfb 906: }
907:
908:
909: /*
1.13 jfb 910: * cvs_initlog()
1.12 jfb 911: *
912: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
913: * set. In this case, the variable's value is used as a path to which the
914: * appropriate suffix is added (".in" for server input and ".out" for server
915: * output.
916: * Returns 0 on success, or -1 on failure.
917: */
918: static int
1.13 jfb 919: cvs_initlog(void)
1.12 jfb 920: {
1.52 joris 921: int l;
922: u_int i;
923: char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
924: char rpath[MAXPATHLEN], *s;
925: struct stat st;
926: time_t now;
927: struct passwd *pwd;
1.12 jfb 928:
1.13 jfb 929: /* avoid doing it more than once */
930: if (cvs_server_logon)
931: return (0);
932:
1.12 jfb 933: env = getenv("CVS_CLIENT_LOG");
934: if (env == NULL)
935: return (0);
936:
1.80 joris 937: envdup = xstrdup(env);
1.52 joris 938: if ((s = strchr(envdup, '%')) != NULL)
939: *s = '\0';
940:
941: strlcpy(buf, env, sizeof(buf));
942: strlcpy(rpath, envdup, sizeof(rpath));
1.80 joris 943: xfree(envdup);
1.52 joris 944:
945: s = buf;
946: while ((s = strchr(s, '%')) != NULL) {
947: *s++;
948: switch (*s) {
949: case 'c':
950: strlcpy(fpath, cvs_command, sizeof(fpath));
951: break;
952: case 'd':
953: time(&now);
1.59 niallo 954: strlcpy(fpath, ctime(&now), sizeof(fpath));
1.52 joris 955: break;
956: case 'p':
957: snprintf(fpath, sizeof(fpath), "%d", getpid());
958: break;
959: case 'u':
960: if ((pwd = getpwuid(getuid())) != NULL)
961: strlcpy(fpath, pwd->pw_name, sizeof(fpath));
962: else
963: fpath[0] = '\0';
964: endpwent();
965: break;
966: default:
967: fpath[0] = '\0';
968: break;
1.58 joris 969: }
1.52 joris 970:
971: if (fpath[0] != '\0') {
972: strlcat(rpath, "-", sizeof(rpath));
973: strlcat(rpath, fpath, sizeof(rpath));
974: }
975: }
976:
977: for (i = 0; i < UINT_MAX; i++) {
978: l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
979: if (l == -1 || l >= (int)sizeof(fpath)) {
980: errno = ENAMETOOLONG;
981: cvs_log(LP_ERRNO, "%s", fpath);
982: return (-1);
983: }
984:
985: if (stat(fpath, &st) != -1)
986: continue;
987:
988: if (errno != ENOENT)
989: return (-1);
990:
991: break;
992: }
993:
1.12 jfb 994: cvs_server_inlog = fopen(fpath, "w");
995: if (cvs_server_inlog == NULL) {
996: cvs_log(LP_ERRNO, "failed to open server input log `%s'",
997: fpath);
998: return (-1);
999: }
1000:
1.52 joris 1001: for (i = 0; i < UINT_MAX; i++) {
1002: l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1003: if (l == -1 || l >= (int)sizeof(fpath)) {
1004: errno = ENAMETOOLONG;
1005: cvs_log(LP_ERRNO, "%s", fpath);
1006: return (-1);
1007: }
1008:
1009: if (stat(fpath, &st) != -1)
1010: continue;
1011:
1012: if (errno != ENOENT)
1013: return (-1);
1014:
1015: break;
1016: }
1017:
1.12 jfb 1018: cvs_server_outlog = fopen(fpath, "w");
1019: if (cvs_server_outlog == NULL) {
1020: cvs_log(LP_ERRNO, "failed to open server output log `%s'",
1021: fpath);
1022: return (-1);
1023: }
1024:
1025: /* make the streams line-buffered */
1.75 xsa 1026: setvbuf(cvs_server_inlog, NULL, _IOLBF, (size_t)0);
1027: setvbuf(cvs_server_outlog, NULL, _IOLBF, (size_t)0);
1.12 jfb 1028:
1.13 jfb 1029: cvs_server_logon = 1;
1.12 jfb 1030:
1031: return (0);
1032: }