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