Annotation of src/usr.bin/cvs/proto.c, Revision 1.26
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);
443:
444: cvs_buf_copy(buf, 0, vrstr, len);
445:
446: cvs_buf_free(buf);
447:
448: return (vrstr);
449: }
450:
451:
452: /*
453: * cvs_resp_getbyid()
454: *
455: */
456:
1.13 jfb 457: struct cvs_resp*
1.1 jfb 458: cvs_resp_getbyid(int respid)
459: {
460: u_int i;
461:
462: for (i = 0; i < CVS_NBREQ; i++)
1.13 jfb 463: if (cvs_responses[i].resp_id == (u_int)respid)
464: return &(cvs_responses[i]);
1.1 jfb 465: return (NULL);
466: }
467:
468:
469: /*
470: * cvs_resp_getbyname()
471: */
472:
1.13 jfb 473: struct cvs_resp*
1.1 jfb 474: cvs_resp_getbyname(const char *rname)
475: {
476: u_int i;
477:
478: for (i = 0; i < CVS_NBREQ; i++)
479: if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13 jfb 480: return &(cvs_responses[i]);
1.1 jfb 481:
1.13 jfb 482: return (NULL);
1.1 jfb 483: }
484:
485:
486: /*
487: * cvs_resp_getvalid()
488: *
489: * Build a space-separated list of all the responses that this protocol
490: * implementation supports.
491: */
492:
493: char*
494: cvs_resp_getvalid(void)
495: {
496: u_int i;
497: size_t len;
498: char *vrstr;
499: BUF *buf;
500:
501: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
502: if (buf == NULL)
503: return (NULL);
504:
505: cvs_buf_set(buf, cvs_responses[0].resp_str,
506: strlen(cvs_responses[0].resp_str), 0);
507:
508: for (i = 1; i < CVS_NBRESP; i++) {
509: if ((cvs_buf_putc(buf, ' ') < 0) ||
510: (cvs_buf_append(buf, cvs_responses[i].resp_str,
511: strlen(cvs_responses[i].resp_str)) < 0)) {
512: cvs_buf_free(buf);
513: return (NULL);
514: }
515: }
516:
517: /* NUL-terminate */
518: if (cvs_buf_putc(buf, '\0') < 0) {
519: cvs_buf_free(buf);
520: return (NULL);
521: }
522:
523: len = cvs_buf_size(buf);
524: vrstr = (char *)malloc(len);
525:
526: cvs_buf_copy(buf, 0, vrstr, len);
527: cvs_buf_free(buf);
528:
529: return (vrstr);
530: }
531:
532:
533: /*
534: * cvs_sendfile()
535: *
536: * Send the mode and size of a file followed by the file's contents.
537: * Returns 0 on success, or -1 on failure.
538: */
539:
540: int
1.13 jfb 541: cvs_sendfile(struct cvsroot *root, const char *path)
1.1 jfb 542: {
543: int fd;
544: ssize_t ret;
545: char buf[4096];
546: struct stat st;
547:
548: if (stat(path, &st) == -1) {
549: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
550: return (-1);
551: }
552:
1.24 jfb 553: if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
554: return (-1);
555:
1.1 jfb 556: fd = open(path, O_RDONLY, 0);
557: if (fd == -1) {
558: return (-1);
559: }
560:
1.13 jfb 561: cvs_sendln(root, buf);
1.1 jfb 562: snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
1.13 jfb 563: cvs_sendln(root, buf);
1.1 jfb 564:
565: while ((ret = read(fd, buf, sizeof(buf))) != 0) {
566: if (ret == -1) {
1.24 jfb 567: (void)close(fd);
1.1 jfb 568: cvs_log(LP_ERRNO, "failed to read file `%s'", path);
569: return (-1);
570: }
571:
1.13 jfb 572: cvs_sendraw(root, buf, (size_t)ret);
1.1 jfb 573:
574: }
575:
576: (void)close(fd);
577:
578: return (0);
579: }
580:
581:
582: /*
583: * cvs_recvfile()
584: *
585: * Receive the mode and size of a file followed the file's contents and
586: * create or update the file whose path is <path> with the received
587: * information.
588: */
589:
1.14 jfb 590: BUF*
591: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 592: {
593: size_t len;
594: ssize_t ret;
595: off_t fsz, cnt;
596: char buf[4096], *ep;
1.14 jfb 597: BUF *fbuf;
598:
599: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
600: if (fbuf == NULL)
601: return (NULL);
1.1 jfb 602:
1.13 jfb 603: if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14 jfb 604: (cvs_strtomode(buf, mode) < 0)) {
605: return (NULL);
1.1 jfb 606: }
607:
1.13 jfb 608: cvs_getln(root, buf, sizeof(buf));
1.1 jfb 609:
610: fsz = (off_t)strtol(buf, &ep, 10);
611: if (*ep != '\0') {
612: cvs_log(LP_ERR, "parse error in file size transmission");
1.14 jfb 613: return (NULL);
1.1 jfb 614: }
615:
616: cnt = 0;
617: do {
618: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 619: if (len == 0)
620: break;
1.13 jfb 621: ret = cvs_recvraw(root, buf, len);
1.1 jfb 622: if (ret == -1) {
1.14 jfb 623: cvs_buf_free(fbuf);
624: return (NULL);
1.1 jfb 625: }
626:
1.14 jfb 627: if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
628: cvs_log(LP_ERR,
629: "failed to append received file data");
630: cvs_buf_free(fbuf);
631: return (NULL);
1.1 jfb 632: }
633:
634: cnt += (off_t)ret;
635: } while (cnt < fsz);
636:
1.14 jfb 637: return (fbuf);
1.1 jfb 638: }
1.12 jfb 639:
640:
641: /*
642: * cvs_sendreq()
643: *
644: * Send a request to the server of type <rid>, with optional arguments
645: * contained in <arg>, which should not be terminated by a newline.
646: * Returns 0 on success, or -1 on failure.
647: */
648:
649: int
1.13 jfb 650: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 651: {
652: int ret;
1.13 jfb 653: struct cvs_req *req;
1.12 jfb 654:
655: if (root->cr_srvin == NULL) {
656: cvs_log(LP_ERR, "cannot send request: Not connected");
657: return (-1);
658: }
659:
1.13 jfb 660: req = cvs_req_getbyid(rid);
661: if (req == NULL) {
1.12 jfb 662: cvs_log(LP_ERR, "unsupported request type %u", rid);
663: return (-1);
664: }
665:
1.18 jfb 666: /* is this request supported by the server? */
667: if (!CVS_GETVR(root, req->req_id)) {
1.25 jfb 668: cvs_log(LP_WARN, "remote end does not support request `%s'",
1.18 jfb 669: req->req_str);
670: return (-1);
671: }
672:
1.13 jfb 673: snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
674: req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.12 jfb 675:
676: if (cvs_server_inlog != NULL)
1.13 jfb 677: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 678:
1.13 jfb 679: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12 jfb 680: if (ret == EOF) {
681: cvs_log(LP_ERRNO, "failed to send request to server");
682: return (-1);
683: }
1.17 jfb 684:
685: if (rid == CVS_REQ_VERSION)
686: cvs_version_sent = 1;
1.12 jfb 687:
1.13 jfb 688: if (req->req_flags & CVS_REQF_RESP)
1.12 jfb 689: ret = cvs_getresp(root);
690:
1.13 jfb 691: return (ret);
1.12 jfb 692: }
693:
694:
695: /*
696: * cvs_getresp()
697: *
698: * Get a response from the server. This call will actually read and handle
699: * responses from the server until one of the response handlers returns
700: * non-zero (either an error occured or the end of the response was reached).
701: * Returns the number of handled commands on success, or -1 on failure.
702: */
703:
704: int
1.13 jfb 705: cvs_getresp(struct cvsroot *root)
1.12 jfb 706: {
1.13 jfb 707: int nbcmd, ret;
708: size_t len;
1.12 jfb 709:
710: nbcmd = 0;
711:
712: do {
713: /* wait for incoming data */
1.13 jfb 714: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 715: root->cr_srvout) == NULL) {
716: if (feof(root->cr_srvout))
717: return (0);
718: cvs_log(LP_ERRNO,
719: "failed to read response from server");
720: return (-1);
721: }
722:
723: if (cvs_server_outlog != NULL)
1.13 jfb 724: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 725:
1.13 jfb 726: if ((len = strlen(cvs_proto_buf)) != 0) {
727: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 728: /* truncated line */
729: }
730: else
1.13 jfb 731: cvs_proto_buf[--len] = '\0';
1.12 jfb 732: }
733:
1.13 jfb 734: ret = cvs_resp_handle(root, cvs_proto_buf);
1.12 jfb 735: nbcmd++;
736: } while (ret == 0);
737:
738: if (ret > 0)
739: ret = nbcmd;
740: return (ret);
741: }
742:
743:
744: /*
1.13 jfb 745: * cvs_getln()
746: *
1.18 jfb 747: * Get a line from the remote end and store it in <lbuf>. The terminating
1.13 jfb 748: * newline character is stripped from the result.
749: */
750:
751: int
752: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
753: {
754: size_t rlen;
1.18 jfb 755: FILE *in;
1.13 jfb 756:
1.18 jfb 757: if (cvs_cmdop == CVS_OP_SERVER)
758: in = stdin;
759: else
760: in = root->cr_srvout;
761:
762: if (fgets(lbuf, len, in) == NULL) {
763: if (ferror(in)) {
764: cvs_log(LP_ERRNO, "failed to read line");
1.13 jfb 765: return (-1);
766: }
767:
1.18 jfb 768: if (feof(in))
1.13 jfb 769: *lbuf = '\0';
770: }
771:
772: if (cvs_server_outlog != NULL)
773: fputs(lbuf, cvs_server_outlog);
774:
775: rlen = strlen(lbuf);
776: if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
777: lbuf[--rlen] = '\0';
778:
779: return (0);
780: }
781:
1.19 jfb 782:
1.13 jfb 783: /*
1.12 jfb 784: * cvs_sendresp()
785: *
1.19 jfb 786: * Send a response of type <rid> to the client, with optional arguments
1.12 jfb 787: * contained in <arg>, which should not be terminated by a newline.
788: * Returns 0 on success, or -1 on failure.
789: */
790:
791: int
792: cvs_sendresp(u_int rid, const char *arg)
793: {
794: int ret;
1.19 jfb 795: struct cvs_resp *resp;
1.12 jfb 796:
797: resp = cvs_resp_getbyid(rid);
1.13 jfb 798: if (resp == NULL) {
1.12 jfb 799: cvs_log(LP_ERR, "unsupported response type %u", rid);
800: return (-1);
801: }
802:
1.19 jfb 803: ret = fputs(resp->resp_str, stdout);
1.12 jfb 804: if (ret == EOF) {
805: cvs_log(LP_ERRNO, "failed to send response to client");
806: }
807: else {
1.19 jfb 808: if (arg != NULL) {
809: putc(' ', stdout);
810: fputs(arg, stdout);
811: }
1.12 jfb 812: putc('\n', stdout);
813: }
814: return (0);
815: }
816:
817:
1.19 jfb 818: #ifdef notyet
1.12 jfb 819: /*
820: * cvs_getreq()
821: *
822: * Get a request from the client.
823: */
824:
825: int
826: cvs_getreq(void)
827: {
828: int nbcmd;
829:
830: nbcmd = 0;
831:
832: do {
833: /* wait for incoming data */
1.13 jfb 834: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 835: stdin) == NULL) {
836: if (feof(stdin))
837: return (0);
838: cvs_log(LP_ERRNO,
839: "failed to read request from client");
840: return (-1);
841: }
842:
1.13 jfb 843: if ((len = strlen(cvs_proto_buf)) != 0) {
844: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 845: /* truncated line */
846: }
847: else
1.13 jfb 848: cvs_proto_buf[--len] = '\0';
1.12 jfb 849: }
850:
1.13 jfb 851: ret = cvs_resp_handle(cvs_proto_buf);
1.12 jfb 852: } while (ret == 0);
853: }
854: #endif
855:
856:
857: /*
858: * cvs_sendln()
859: *
1.18 jfb 860: * Send a single line <line> string to the remote end. The line is sent as is,
1.12 jfb 861: * without any modifications.
862: * Returns 0 on success, or -1 on failure.
863: */
864:
865: int
866: cvs_sendln(struct cvsroot *root, const char *line)
867: {
868: int nl;
869: size_t len;
1.18 jfb 870: FILE *out;
871:
872: if (cvs_cmdop == CVS_OP_SERVER)
873: out = stdout;
874: else
875: out = root->cr_srvin;
1.12 jfb 876:
877: nl = 0;
878: len = strlen(line);
879:
880: if ((len > 0) && (line[len - 1] != '\n'))
881: nl = 1;
882:
883: if (cvs_server_inlog != NULL) {
884: fputs(line, cvs_server_inlog);
885: if (nl)
886: fputc('\n', cvs_server_inlog);
887: }
1.18 jfb 888: fputs(line, out);
1.12 jfb 889: if (nl)
1.18 jfb 890: fputc('\n', out);
1.12 jfb 891: return (0);
892: }
893:
894:
895: /*
1.13 jfb 896: * cvs_sendraw()
1.12 jfb 897: *
898: * Send the first <len> bytes from the buffer <src> to the server.
899: */
900:
901: int
1.13 jfb 902: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12 jfb 903: {
1.18 jfb 904: FILE *out;
905:
906: if (cvs_cmdop == CVS_OP_SERVER)
907: out = stdout;
908: else
909: out = root->cr_srvin;
910:
1.12 jfb 911: if (cvs_server_inlog != NULL)
912: fwrite(src, sizeof(char), len, cvs_server_inlog);
1.18 jfb 913: if (fwrite(src, sizeof(char), len, out) < len) {
1.12 jfb 914: return (-1);
915: }
916:
917: return (0);
918: }
919:
920:
921: /*
1.13 jfb 922: * cvs_recvraw()
1.12 jfb 923: *
924: * Receive the first <len> bytes from the buffer <src> to the server.
925: */
926:
927: ssize_t
1.13 jfb 928: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 929: {
930: size_t ret;
1.18 jfb 931: FILE *in;
932:
933: if (cvs_cmdop == CVS_OP_SERVER)
934: in = stdin;
935: else
936: in = root->cr_srvout;
1.12 jfb 937:
1.18 jfb 938: ret = fread(dst, sizeof(char), len, in);
1.12 jfb 939: if (ret == 0)
940: return (-1);
941: if (cvs_server_outlog != NULL)
942: fwrite(dst, sizeof(char), len, cvs_server_outlog);
943: return (ssize_t)ret;
944: }
945:
946:
947: /*
1.13 jfb 948: * cvs_senddir()
1.12 jfb 949: *
950: * Send a `Directory' request along with the 2 paths that follow it.
951: */
952:
953: int
1.13 jfb 954: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 955: {
1.13 jfb 956: char buf[MAXPATHLEN];
1.12 jfb 957:
1.13 jfb 958: if (dir->cf_ddat->cd_repo == NULL)
959: strlcpy(buf, root->cr_dir, sizeof(buf));
960: else
961: snprintf(buf, sizeof(buf), "%s/%s", root->cr_dir,
962: dir->cf_ddat->cd_repo);
1.12 jfb 963:
1.13 jfb 964: if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, dir->cf_path) < 0) ||
965: (cvs_sendln(root, buf) < 0))
1.12 jfb 966: return (-1);
967:
968: return (0);
969: }
970:
971:
972: /*
1.13 jfb 973: * cvs_sendarg()
1.12 jfb 974: *
975: * Send the argument <arg> to the server. The argument <append> is used to
976: * determine if the argument should be simply appended to the last argument
977: * sent or if it should be created as a new argument (0).
978: */
979:
980: int
1.13 jfb 981: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12 jfb 982: {
1.13 jfb 983: return cvs_sendreq(root, ((append == 0) ?
984: CVS_REQ_ARGUMENT : CVS_REQ_ARGUMENTX), arg);
1.12 jfb 985: }
986:
987:
988: /*
1.13 jfb 989: * cvs_sendentry()
1.12 jfb 990: *
991: * Send an `Entry' request to the server along with the mandatory fields from
992: * the CVS entry <ent> (which are the name and revision).
993: */
994:
995: int
1.13 jfb 996: cvs_sendentry(struct cvsroot *root, const struct cvs_ent *ent)
1.12 jfb 997: {
998: char ebuf[128], numbuf[64];
999:
1000: snprintf(ebuf, sizeof(ebuf), "/%s/%s///", ent->ce_name,
1001: rcsnum_tostr(ent->ce_rev, numbuf, sizeof(numbuf)));
1002:
1.13 jfb 1003: return cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12 jfb 1004: }
1005:
1006:
1007: /*
1.13 jfb 1008: * cvs_initlog()
1.12 jfb 1009: *
1010: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1011: * set. In this case, the variable's value is used as a path to which the
1012: * appropriate suffix is added (".in" for server input and ".out" for server
1013: * output.
1014: * Returns 0 on success, or -1 on failure.
1015: */
1016:
1017: static int
1.13 jfb 1018: cvs_initlog(void)
1.12 jfb 1019: {
1020: char *env, fpath[MAXPATHLEN];
1021:
1.13 jfb 1022: /* avoid doing it more than once */
1023: if (cvs_server_logon)
1024: return (0);
1025:
1.12 jfb 1026: env = getenv("CVS_CLIENT_LOG");
1027: if (env == NULL)
1028: return (0);
1029:
1030: strlcpy(fpath, env, sizeof(fpath));
1031: strlcat(fpath, ".in", sizeof(fpath));
1032: cvs_server_inlog = fopen(fpath, "w");
1033: if (cvs_server_inlog == NULL) {
1034: cvs_log(LP_ERRNO, "failed to open server input log `%s'",
1035: fpath);
1036: return (-1);
1037: }
1038:
1039: strlcpy(fpath, env, sizeof(fpath));
1040: strlcat(fpath, ".out", sizeof(fpath));
1041: cvs_server_outlog = fopen(fpath, "w");
1042: if (cvs_server_outlog == NULL) {
1043: cvs_log(LP_ERRNO, "failed to open server output log `%s'",
1044: fpath);
1045: return (-1);
1046: }
1047:
1048: /* make the streams line-buffered */
1049: setvbuf(cvs_server_inlog, NULL, _IOLBF, 0);
1050: setvbuf(cvs_server_outlog, NULL, _IOLBF, 0);
1051:
1.13 jfb 1052: cvs_server_logon = 1;
1.12 jfb 1053:
1054: return (0);
1055: }