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