Annotation of src/usr.bin/cvs/proto.c, Revision 1.96
1.96 ! deraadt 1: /* $OpenBSD: proto.c,v 1.95 2006/04/11 07:52:34 ray 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.92 joris 55: static void 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");
1.96 ! deraadt 187: else if (root->cr_method == CVS_METHOD_KSERVER ||
! 188: root->cr_method == CVS_METHOD_GSERVER ||
! 189: root->cr_method == CVS_METHOD_EXT ||
! 190: root->cr_method == CVS_METHOD_FORK)
1.82 joris 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.96 ! deraadt 211: if (dup2(infd[0], STDIN_FILENO) == -1 ||
! 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.94 ray 407: vrstr = 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.94 ray 477: vrstr = 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) {
1.95 ray 523: if (ret == -1)
1.82 joris 524: fatal("cvs_sendfile: read error on '%s'", path);
1.1 jfb 525:
1.82 joris 526: cvs_sendraw(root, buf, (size_t)ret);
1.1 jfb 527: }
528:
529: (void)close(fd);
530: }
531:
532:
533: /*
534: * cvs_recvfile()
535: *
536: * Receive the mode and size of a file followed the file's contents and
537: * create or update the file whose path is <path> with the received
538: * information.
539: */
1.14 jfb 540: BUF*
541: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 542: {
543: size_t len;
544: ssize_t ret;
545: off_t fsz, cnt;
546: char buf[4096], *ep;
1.14 jfb 547: BUF *fbuf;
548:
549: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
1.1 jfb 550:
1.83 joris 551: cvs_getln(root, buf, sizeof(buf));
552: cvs_strtomode(buf, mode);
1.1 jfb 553:
1.83 joris 554: cvs_getln(root, buf, sizeof(buf));
1.1 jfb 555:
556: fsz = (off_t)strtol(buf, &ep, 10);
1.83 joris 557: if (*ep != '\0')
558: fatal("parse error in file size transmission");
1.1 jfb 559:
560: cnt = 0;
561: do {
562: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 563: if (len == 0)
564: break;
1.13 jfb 565: ret = cvs_recvraw(root, buf, len);
1.83 joris 566: cvs_buf_append(fbuf, buf, (size_t)ret);
1.1 jfb 567: cnt += (off_t)ret;
568: } while (cnt < fsz);
569:
1.14 jfb 570: return (fbuf);
1.1 jfb 571: }
1.12 jfb 572:
573: /*
574: * cvs_sendreq()
575: *
576: * Send a request to the server of type <rid>, with optional arguments
577: * contained in <arg>, which should not be terminated by a newline.
578: * Returns 0 on success, or -1 on failure.
579: */
1.82 joris 580: void
1.13 jfb 581: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 582: {
1.88 xsa 583: int ret;
1.13 jfb 584: struct cvs_req *req;
1.12 jfb 585:
1.82 joris 586: if (root->cr_srvin == NULL)
587: fatal("cannot send request %u: Not connected", rid);
1.12 jfb 588:
1.13 jfb 589: req = cvs_req_getbyid(rid);
1.82 joris 590: if (req == NULL)
591: fatal("unsupported request type %u", rid);
1.12 jfb 592:
1.18 jfb 593: /* is this request supported by the server? */
594: if (!CVS_GETVR(root, req->req_id)) {
1.57 joris 595: if (rid == CVS_REQ_VERSION) {
1.82 joris 596: cvs_sendreq(root, CVS_REQ_NOOP, arg);
1.57 joris 597: } else {
598: cvs_log(LP_WARN,
599: "remote end does not support request `%s'",
600: req->req_str);
601: }
1.84 reyk 602:
1.82 joris 603: return;
1.18 jfb 604: }
605:
1.88 xsa 606: if (strlcpy(cvs_proto_buf, req->req_str, sizeof(cvs_proto_buf)) >=
607: sizeof(cvs_proto_buf) ||
608: strlcat(cvs_proto_buf, (arg == NULL) ? "" : " ",
609: sizeof(cvs_proto_buf)) >= sizeof(cvs_proto_buf) ||
610: strlcat(cvs_proto_buf, (arg == NULL) ? "" : arg,
1.89 joris 611: sizeof(cvs_proto_buf)) >= sizeof(cvs_proto_buf) ||
612: strlcat(cvs_proto_buf, "\n",
1.88 xsa 613: sizeof(cvs_proto_buf)) >= sizeof(cvs_proto_buf))
614: fatal("cvs_sendreq: overflow when creating proto buffer");
1.12 jfb 615:
616: if (cvs_server_inlog != NULL)
1.13 jfb 617: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 618:
1.13 jfb 619: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.82 joris 620: if (ret == EOF)
621: fatal("failed to send request to server");
1.17 jfb 622:
623: if (rid == CVS_REQ_VERSION)
624: cvs_version_sent = 1;
1.12 jfb 625:
1.13 jfb 626: if (req->req_flags & CVS_REQF_RESP)
1.82 joris 627: cvs_getresp(root);
1.12 jfb 628: }
629:
630:
631: /*
632: * cvs_getresp()
633: *
634: * Get a response from the server. This call will actually read and handle
635: * responses from the server until one of the response handlers returns
1.37 david 636: * non-zero (either an error occurred or the end of the response was reached).
1.12 jfb 637: * Returns the number of handled commands on success, or -1 on failure.
638: */
1.82 joris 639: void
1.13 jfb 640: cvs_getresp(struct cvsroot *root)
1.12 jfb 641: {
1.82 joris 642: int ret;
1.13 jfb 643: size_t len;
1.12 jfb 644:
645: do {
646: /* wait for incoming data */
1.74 xsa 647: if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12 jfb 648: root->cr_srvout) == NULL) {
649: if (feof(root->cr_srvout))
1.82 joris 650: return;
651: fatal("failed to read response from server");
1.12 jfb 652: }
653:
654: if (cvs_server_outlog != NULL)
1.13 jfb 655: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 656:
1.13 jfb 657: if ((len = strlen(cvs_proto_buf)) != 0) {
1.42 joris 658: /* if len - 1 != '\n' the line is truncated */
1.43 joris 659: if (cvs_proto_buf[len - 1] == '\n')
1.13 jfb 660: cvs_proto_buf[--len] = '\0';
1.12 jfb 661: }
662:
1.13 jfb 663: ret = cvs_resp_handle(root, cvs_proto_buf);
1.12 jfb 664: } while (ret == 0);
665: }
666:
667:
668: /*
1.13 jfb 669: * cvs_getln()
670: *
1.18 jfb 671: * Get a line from the remote end and store it in <lbuf>. The terminating
1.13 jfb 672: * newline character is stripped from the result.
673: */
1.83 joris 674: void
1.13 jfb 675: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
676: {
677: size_t rlen;
1.18 jfb 678: FILE *in;
1.13 jfb 679:
1.18 jfb 680: if (cvs_cmdop == CVS_OP_SERVER)
681: in = stdin;
682: else
683: in = root->cr_srvout;
684:
1.74 xsa 685: if (fgets(lbuf, (int)len, in) == NULL) {
1.18 jfb 686: if (ferror(in)) {
1.83 joris 687: fatal("cvs_getln: error reading server: %s",
688: strerror(errno));
1.13 jfb 689: }
690:
1.18 jfb 691: if (feof(in))
1.13 jfb 692: *lbuf = '\0';
693: }
694:
695: if (cvs_server_outlog != NULL)
696: fputs(lbuf, cvs_server_outlog);
697:
698: rlen = strlen(lbuf);
1.96 ! deraadt 699: if (rlen > 0 && lbuf[rlen - 1] == '\n')
1.13 jfb 700: lbuf[--rlen] = '\0';
701: }
702:
1.19 jfb 703:
1.13 jfb 704: /*
1.12 jfb 705: * cvs_sendresp()
706: *
1.19 jfb 707: * Send a response of type <rid> to the client, with optional arguments
1.12 jfb 708: * contained in <arg>, which should not be terminated by a newline.
709: */
1.87 xsa 710: void
1.12 jfb 711: cvs_sendresp(u_int rid, const char *arg)
712: {
713: int ret;
1.19 jfb 714: struct cvs_resp *resp;
1.12 jfb 715:
1.90 joris 716: if ((resp = cvs_resp_getbyid(rid)) == NULL)
1.87 xsa 717: fatal("unsupported response type %u", rid);
1.12 jfb 718:
1.19 jfb 719: ret = fputs(resp->resp_str, stdout);
1.12 jfb 720: if (ret == EOF) {
721: cvs_log(LP_ERRNO, "failed to send response to client");
1.30 deraadt 722: } else {
1.19 jfb 723: if (arg != NULL) {
724: putc(' ', stdout);
725: fputs(arg, stdout);
726: }
1.12 jfb 727: putc('\n', stdout);
728: }
729: }
730:
731:
732: /*
733: * cvs_sendln()
734: *
1.18 jfb 735: * Send a single line <line> string to the remote end. The line is sent as is,
1.12 jfb 736: * without any modifications.
737: */
1.82 joris 738: void
1.12 jfb 739: cvs_sendln(struct cvsroot *root, const char *line)
740: {
741: int nl;
742: size_t len;
1.18 jfb 743: FILE *out;
744:
745: if (cvs_cmdop == CVS_OP_SERVER)
746: out = stdout;
747: else
748: out = root->cr_srvin;
1.12 jfb 749:
750: nl = 0;
751: len = strlen(line);
752:
1.96 ! deraadt 753: if (len > 0 && line[len - 1] != '\n')
1.12 jfb 754: nl = 1;
755:
756: if (cvs_server_inlog != NULL) {
757: fputs(line, cvs_server_inlog);
758: if (nl)
1.55 jfb 759: putc('\n', cvs_server_inlog);
1.12 jfb 760: }
1.18 jfb 761: fputs(line, out);
1.12 jfb 762: if (nl)
1.55 jfb 763: putc('\n', out);
1.12 jfb 764: }
765:
766:
767: /*
1.13 jfb 768: * cvs_sendraw()
1.12 jfb 769: *
770: * Send the first <len> bytes from the buffer <src> to the server.
771: */
1.82 joris 772: void
1.13 jfb 773: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12 jfb 774: {
1.18 jfb 775: FILE *out;
776:
777: if (cvs_cmdop == CVS_OP_SERVER)
778: out = stdout;
779: else
780: out = root->cr_srvin;
781:
1.12 jfb 782: if (cvs_server_inlog != NULL)
783: fwrite(src, sizeof(char), len, cvs_server_inlog);
1.82 joris 784: if (fwrite(src, sizeof(char), len, out) < len)
785: fatal("failed to send data");
1.12 jfb 786: }
787:
788:
789: /*
1.13 jfb 790: * cvs_recvraw()
1.12 jfb 791: *
792: * Receive the first <len> bytes from the buffer <src> to the server.
793: */
1.83 joris 794: size_t
1.13 jfb 795: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 796: {
797: size_t ret;
1.18 jfb 798: FILE *in;
799:
800: if (cvs_cmdop == CVS_OP_SERVER)
801: in = stdin;
802: else
803: in = root->cr_srvout;
1.12 jfb 804:
1.18 jfb 805: ret = fread(dst, sizeof(char), len, in);
1.83 joris 806: if (ret == 0) {
807: if (ferror(in)) {
808: fatal("cvs_recvraw: error reading from server: %s",
809: strerror(errno));
810: }
811: } else {
812: if (cvs_server_outlog != NULL)
813: fwrite(dst, sizeof(char), len, cvs_server_outlog);
814: }
815:
816: return (ret);
1.12 jfb 817: }
818:
819:
820: /*
1.13 jfb 821: * cvs_senddir()
1.12 jfb 822: *
1.32 jfb 823: * Send a `Directory' request along with the 2 paths that follow it. If
824: * the directory info to be sent is the same as the last info sent, the
825: * call does nothing and simply returns without an error.
1.12 jfb 826: */
1.82 joris 827: void
1.31 tedu 828: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 829: {
1.60 xsa 830: size_t len;
1.28 jfb 831: char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12 jfb 832:
1.47 jfb 833: if (dir->cf_type != DT_DIR)
1.82 joris 834: fatal("cvs_senddir(): cf_type != DT_DIR: %d", dir->cf_type);
1.47 jfb 835:
1.32 jfb 836: cvs_file_getpath(dir, lbuf, sizeof(lbuf));
1.45 joris 837: if (strcmp(lbuf, cvs_lastdir) == 0 && cvs_cmdop != CVS_OP_CHECKOUT)
1.82 joris 838: return;
1.32 jfb 839:
1.82 joris 840: if (dir->cf_repo == NULL) {
841: len = strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
842: if (len >= sizeof(rbuf))
843: fatal("cvs_senddir: path truncation");
844: } else {
1.60 xsa 845: len = cvs_path_cat(root->cr_dir, dir->cf_repo, rbuf,
846: sizeof(rbuf));
847: if (len >= sizeof(rbuf))
1.82 joris 848: fatal("cvs_senddir: path truncation");
1.51 xsa 849: }
1.12 jfb 850:
1.82 joris 851: cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf);
852: cvs_sendln(root, rbuf);
1.28 jfb 853:
1.82 joris 854: len = strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
855: if (len >= sizeof(lbuf))
856: fatal("path truncation in cvs_senddir");
1.12 jfb 857: }
858:
859:
860: /*
1.13 jfb 861: * cvs_sendarg()
1.12 jfb 862: *
863: * Send the argument <arg> to the server. The argument <append> is used to
864: * determine if the argument should be simply appended to the last argument
865: * sent or if it should be created as a new argument (0).
866: */
1.82 joris 867: void
1.13 jfb 868: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12 jfb 869: {
1.96 ! deraadt 870: cvs_sendreq(root, append == 0 ? CVS_REQ_ARGUMENT :
! 871: CVS_REQ_ARGUMENTX, arg);
1.12 jfb 872: }
873:
874:
875: /*
1.13 jfb 876: * cvs_sendentry()
1.12 jfb 877: *
878: * Send an `Entry' request to the server along with the mandatory fields from
879: * the CVS entry <ent> (which are the name and revision).
880: */
1.82 joris 881: void
1.47 jfb 882: cvs_sendentry(struct cvsroot *root, const CVSFILE *file)
1.12 jfb 883: {
1.77 xsa 884: char ebuf[CVS_ENT_MAXLINELEN], numbuf[64];
1.12 jfb 885:
1.82 joris 886: if (file->cf_type != DT_REG)
887: fatal("cvs_sendentry: cf_type != DT_REG: %d", file->cf_type);
1.48 joris 888:
889: /* don't send Entry for unknown files */
890: if (file->cf_cvstat == CVS_FST_UNKNOWN)
1.82 joris 891: return;
1.47 jfb 892:
1.88 xsa 893: if (strlcpy(ebuf, "/", sizeof(ebuf)) >= sizeof(ebuf) ||
894: strlcat(ebuf, file->cf_name, sizeof(ebuf)) >= sizeof(ebuf) ||
895: strlcat(ebuf, "/", sizeof(ebuf)) >= sizeof(ebuf) ||
896: strlcat(ebuf, (file->cf_cvstat == CVS_FST_REMOVED) ? "-" : "",
897: sizeof(ebuf)) >= sizeof(ebuf) ||
898: strlcat(ebuf, rcsnum_tostr(file->cf_lrev, numbuf, sizeof(numbuf)),
899: sizeof(ebuf)) >= sizeof(ebuf) ||
900: strlcat(ebuf, "///", sizeof(ebuf)) >= sizeof(ebuf))
1.82 joris 901: fatal("cvs_sendentry: overflow when creating entry buffer");
1.12 jfb 902:
1.82 joris 903: cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12 jfb 904: }
905:
906:
907: /*
1.13 jfb 908: * cvs_initlog()
1.12 jfb 909: *
910: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
911: * set. In this case, the variable's value is used as a path to which the
912: * appropriate suffix is added (".in" for server input and ".out" for server
913: * output.
914: * Returns 0 on success, or -1 on failure.
915: */
1.92 joris 916: static void
1.13 jfb 917: cvs_initlog(void)
1.12 jfb 918: {
1.52 joris 919: int l;
920: u_int i;
921: char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
922: char rpath[MAXPATHLEN], *s;
923: struct stat st;
924: time_t now;
925: struct passwd *pwd;
1.12 jfb 926:
1.13 jfb 927: /* avoid doing it more than once */
928: if (cvs_server_logon)
1.92 joris 929: return;
1.13 jfb 930:
1.12 jfb 931: env = getenv("CVS_CLIENT_LOG");
932: if (env == NULL)
1.92 joris 933: return;
1.12 jfb 934:
1.80 joris 935: envdup = xstrdup(env);
1.52 joris 936: if ((s = strchr(envdup, '%')) != NULL)
937: *s = '\0';
938:
1.93 joris 939: if (strlcpy(buf, env, sizeof(buf)) >= sizeof(buf))
940: fatal("string truncation in cvs_initlog");
941:
942: if (strlcpy(rpath, envdup, sizeof(rpath)) >= sizeof(rpath))
943: fatal("string truncation in cvs_initlog");
944:
1.80 joris 945: xfree(envdup);
1.52 joris 946:
947: s = buf;
948: while ((s = strchr(s, '%')) != NULL) {
1.91 deraadt 949: s++;
1.52 joris 950: switch (*s) {
951: case 'c':
1.93 joris 952: if (strlcpy(fpath, cvs_command, sizeof(fpath)) >=
953: sizeof(fpath))
954: fatal("string truncation in cvs_initlog");
1.52 joris 955: break;
956: case 'd':
957: time(&now);
1.93 joris 958: if (strlcpy(fpath, ctime(&now), sizeof(fpath)) >=
959: sizeof(fpath))
960: fatal("string truncation in cvs_initlog");
1.52 joris 961: break;
962: case 'p':
963: snprintf(fpath, sizeof(fpath), "%d", getpid());
964: break;
965: case 'u':
1.93 joris 966: if ((pwd = getpwuid(getuid())) != NULL) {
967: if (strlcpy(fpath, pwd->pw_name,
968: sizeof(fpath)) >= sizeof(fpath))
969: fatal("truncation in cvs_initlog");
970: } else {
1.52 joris 971: fpath[0] = '\0';
1.93 joris 972: }
1.52 joris 973: endpwent();
974: break;
975: default:
976: fpath[0] = '\0';
977: break;
1.58 joris 978: }
1.52 joris 979:
980: if (fpath[0] != '\0') {
1.93 joris 981: if (strlcat(rpath, "-", sizeof(rpath)) >= sizeof(rpath))
982: fatal("string truncation cvs_initlog");
983:
984: if (strlcat(rpath, fpath, sizeof(rpath))
985: >= sizeof(rpath))
986: fatal("string truncation in cvs_initlog");
1.52 joris 987: }
988: }
989:
990: for (i = 0; i < UINT_MAX; i++) {
991: l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1.92 joris 992: if (l == -1 || l >= (int)sizeof(fpath))
993: fatal("overflow in cvs_initlog()");
1.52 joris 994:
995: if (stat(fpath, &st) != -1)
996: continue;
997:
998: if (errno != ENOENT)
1.93 joris 999: fatal("cvs_initlog() stat failed '%s'",
1000: strerror(errno));
1.52 joris 1001:
1002: break;
1003: }
1004:
1.12 jfb 1005: cvs_server_inlog = fopen(fpath, "w");
1.92 joris 1006: if (cvs_server_inlog == NULL)
1007: fatal("failed to open server input log `%s'", fpath);
1.12 jfb 1008:
1.52 joris 1009: for (i = 0; i < UINT_MAX; i++) {
1010: l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1.92 joris 1011: if (l == -1 || l >= (int)sizeof(fpath))
1012: fatal("overflow in cvs_initlog()");
1.52 joris 1013:
1014: if (stat(fpath, &st) != -1)
1015: continue;
1016:
1017: if (errno != ENOENT)
1.93 joris 1018: fatal("cvs_initlog() stat failed '%s'",
1019: strerror(errno));
1.52 joris 1020:
1021: break;
1022: }
1023:
1.12 jfb 1024: cvs_server_outlog = fopen(fpath, "w");
1.92 joris 1025: if (cvs_server_outlog == NULL)
1026: fatal("failed to open server output log `%s'", fpath);
1.12 jfb 1027:
1028: /* make the streams line-buffered */
1.75 xsa 1029: setvbuf(cvs_server_inlog, NULL, _IOLBF, (size_t)0);
1030: setvbuf(cvs_server_outlog, NULL, _IOLBF, (size_t)0);
1.12 jfb 1031:
1.13 jfb 1032: cvs_server_logon = 1;
1.12 jfb 1033: }