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