Annotation of src/usr.bin/cvs/proto.c, Revision 1.30
1.30 ! deraadt 1: /* $OpenBSD: proto.c,v 1.29 2004/12/02 19:23:44 jfb 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>
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.29 jfb 132: { CVS_REQ_CI, "ci", 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:
1.28 jfb 181: static pid_t cvs_subproc_pid;
182:
1.13 jfb 183:
184: /*
185: * cvs_connect()
186: *
187: * Open a client connection to the cvs server whose address is given in
188: * the <root> variable. The method used to connect depends on the
189: * setting of the CVS_RSH variable.
1.17 jfb 190: * Once the connection has been established, we first send the list of
191: * responses we support and request the list of supported requests from the
192: * server. Then, a version request is sent and various global flags are sent.
1.13 jfb 193: * Returns 0 on success, or -1 on failure.
194: */
195:
196: int
197: cvs_connect(struct cvsroot *root)
198: {
1.20 jfb 199: int argc, infd[2], outfd[2], errfd[2];
1.13 jfb 200: char *argv[16], *cvs_server_cmd, *vresp;
201:
202: if (pipe(infd) == -1) {
203: cvs_log(LP_ERRNO,
204: "failed to create input pipe for client connection");
205: return (-1);
206: }
207:
208: if (pipe(outfd) == -1) {
209: cvs_log(LP_ERRNO,
210: "failed to create output pipe for client connection");
211: (void)close(infd[0]);
212: (void)close(infd[1]);
213: return (-1);
214: }
215:
1.20 jfb 216: if (pipe(errfd) == -1) {
217: cvs_log(LP_ERRNO,
218: "failed to create error pipe for client connection");
219: (void)close(infd[0]);
220: (void)close(infd[1]);
221: (void)close(outfd[0]);
222: (void)close(outfd[1]);
223: return (-1);
224: }
225:
1.28 jfb 226: cvs_subproc_pid = fork();
227: if (cvs_subproc_pid == -1) {
1.13 jfb 228: cvs_log(LP_ERRNO, "failed to fork for cvs server connection");
229: return (-1);
1.30 ! deraadt 230: } else if (cvs_subproc_pid == 0) {
1.13 jfb 231: if ((dup2(infd[0], STDIN_FILENO) == -1) ||
1.23 jfb 232: (dup2(outfd[1], STDOUT_FILENO) == -1)) {
1.13 jfb 233: cvs_log(LP_ERRNO,
234: "failed to setup standard streams for cvs server");
235: return (-1);
236: }
237: (void)close(infd[1]);
238: (void)close(outfd[0]);
1.20 jfb 239: (void)close(errfd[0]);
1.13 jfb 240:
241: argc = 0;
242: argv[argc++] = cvs_rsh;
243:
244: if (root->cr_user != NULL) {
245: argv[argc++] = "-l";
246: argv[argc++] = root->cr_user;
247: }
248:
249:
250: cvs_server_cmd = getenv("CVS_SERVER");
251: if (cvs_server_cmd == NULL)
1.20 jfb 252: cvs_server_cmd = CVS_SERVER_DEFAULT;
1.13 jfb 253:
254: argv[argc++] = root->cr_host;
255: argv[argc++] = cvs_server_cmd;
256: argv[argc++] = "server";
257: argv[argc] = NULL;
258:
259: execvp(argv[0], argv);
260: cvs_log(LP_ERRNO, "failed to exec");
261: exit(EX_OSERR);
262: }
263:
264: /* we are the parent */
265: (void)close(infd[0]);
266: (void)close(outfd[1]);
1.20 jfb 267: (void)close(errfd[1]);
1.13 jfb 268:
269: root->cr_srvin = fdopen(infd[1], "w");
270: if (root->cr_srvin == NULL) {
271: cvs_log(LP_ERRNO, "failed to create pipe stream");
272: return (-1);
273: }
274:
275: root->cr_srvout = fdopen(outfd[0], "r");
276: if (root->cr_srvout == NULL) {
277: cvs_log(LP_ERRNO, "failed to create pipe stream");
1.20 jfb 278: return (-1);
279: }
280:
1.21 jfb 281: #if 0
1.20 jfb 282: root->cr_srverr = fdopen(errfd[0], "r");
283: if (root->cr_srverr == NULL) {
284: cvs_log(LP_ERR, "failed to create pipe stream");
1.13 jfb 285: return (-1);
286: }
1.21 jfb 287: #endif
1.13 jfb 288:
289: /* make the streams line-buffered */
290: (void)setvbuf(root->cr_srvin, NULL, _IOLBF, 0);
291: (void)setvbuf(root->cr_srvout, NULL, _IOLBF, 0);
292:
293: cvs_initlog();
294:
295: /*
296: * Send the server the list of valid responses, then ask for valid
297: * requests.
298: */
299:
300: vresp = cvs_resp_getvalid();
301: if (vresp == NULL) {
302: cvs_log(LP_ERR, "can't generate list of valid responses");
303: return (-1);
304: }
305:
306: if (cvs_sendreq(root, CVS_REQ_VALIDRESP, vresp) < 0) {
307: }
308: free(vresp);
309:
310: if (cvs_sendreq(root, CVS_REQ_VALIDREQ, NULL) < 0) {
311: cvs_log(LP_ERR, "failed to get valid requests from server");
312: return (-1);
313: }
314:
1.17 jfb 315: if (cvs_sendreq(root, CVS_REQ_VERSION, NULL) < 0)
316: cvs_log(LP_ERR, "failed to get remote version");
317:
1.13 jfb 318: /* now share our global options with the server */
319: if (verbosity == 1)
320: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-q");
321: else if (verbosity == 0)
322: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-Q");
323:
324: if (cvs_nolog)
325: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-l");
326: if (cvs_readonly)
327: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-r");
328: if (cvs_trace)
329: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-t");
330:
1.22 jfb 331: /* now send the CVSROOT to the server unless it's an init */
332: if ((cvs_cmdop != CVS_OP_INIT) &&
333: (cvs_sendreq(root, CVS_REQ_ROOT, root->cr_dir) < 0))
1.13 jfb 334: return (-1);
335:
336: /* not sure why, but we have to send this */
337: if (cvs_sendreq(root, CVS_REQ_USEUNCHANGED, NULL) < 0)
338: return (-1);
339:
340: #ifdef CVS_ZLIB
341: /* if compression was requested, initialize it */
342: #endif
343:
344: cvs_log(LP_DEBUG, "connected to %s", root->cr_host);
345:
346: return (0);
347: }
348:
349:
350: /*
351: * cvs_disconnect()
352: *
353: * Disconnect from the cvs server.
354: */
355:
356: void
357: cvs_disconnect(struct cvsroot *root)
358: {
359: cvs_log(LP_DEBUG, "closing connection to %s", root->cr_host);
360: if (root->cr_srvin != NULL) {
361: (void)fclose(root->cr_srvin);
362: root->cr_srvin = NULL;
363: }
364: if (root->cr_srvout != NULL) {
365: (void)fclose(root->cr_srvout);
366: root->cr_srvout = NULL;
367: }
368: }
369:
1.1 jfb 370:
371: /*
372: * cvs_req_getbyid()
373: *
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.1 jfb 384: return (NULL);
385: }
386:
387:
388: /*
389: * cvs_req_getbyname()
390: */
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:
412: char*
413: cvs_req_getvalid(void)
414: {
415: u_int i;
416: size_t len;
417: char *vrstr;
418: BUF *buf;
419:
420: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
421: if (buf == NULL)
422: return (NULL);
423:
424: cvs_buf_set(buf, cvs_requests[0].req_str,
425: strlen(cvs_requests[0].req_str), 0);
426:
427: for (i = 1; i < CVS_NBREQ; i++) {
428: if ((cvs_buf_putc(buf, ' ') < 0) ||
429: (cvs_buf_append(buf, cvs_requests[i].req_str,
430: strlen(cvs_requests[i].req_str)) < 0)) {
431: cvs_buf_free(buf);
432: return (NULL);
433: }
434: }
435:
436: /* NUL-terminate */
437: if (cvs_buf_putc(buf, '\0') < 0) {
438: cvs_buf_free(buf);
439: return (NULL);
440: }
441:
442: len = cvs_buf_size(buf);
443: vrstr = (char *)malloc(len);
1.27 jfb 444: if (vrstr == NULL) {
445: cvs_buf_free(buf);
446: return (NULL);
447: }
1.1 jfb 448:
449: cvs_buf_copy(buf, 0, vrstr, len);
450: cvs_buf_free(buf);
451:
452: return (vrstr);
453: }
454:
455:
456: /*
457: * cvs_resp_getbyid()
458: *
459: */
460:
1.13 jfb 461: struct cvs_resp*
1.1 jfb 462: cvs_resp_getbyid(int respid)
463: {
464: u_int i;
465:
466: for (i = 0; i < CVS_NBREQ; i++)
1.13 jfb 467: if (cvs_responses[i].resp_id == (u_int)respid)
468: return &(cvs_responses[i]);
1.1 jfb 469: return (NULL);
470: }
471:
472:
473: /*
474: * cvs_resp_getbyname()
475: */
476:
1.13 jfb 477: struct cvs_resp*
1.1 jfb 478: cvs_resp_getbyname(const char *rname)
479: {
480: u_int i;
481:
482: for (i = 0; i < CVS_NBREQ; i++)
483: if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13 jfb 484: return &(cvs_responses[i]);
1.1 jfb 485:
1.13 jfb 486: return (NULL);
1.1 jfb 487: }
488:
489:
490: /*
491: * cvs_resp_getvalid()
492: *
493: * Build a space-separated list of all the responses that this protocol
494: * implementation supports.
495: */
496:
497: char*
498: cvs_resp_getvalid(void)
499: {
500: u_int i;
501: size_t len;
502: char *vrstr;
503: BUF *buf;
504:
505: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
506: if (buf == NULL)
507: return (NULL);
508:
509: cvs_buf_set(buf, cvs_responses[0].resp_str,
510: strlen(cvs_responses[0].resp_str), 0);
511:
512: for (i = 1; i < CVS_NBRESP; i++) {
513: if ((cvs_buf_putc(buf, ' ') < 0) ||
514: (cvs_buf_append(buf, cvs_responses[i].resp_str,
515: strlen(cvs_responses[i].resp_str)) < 0)) {
516: cvs_buf_free(buf);
517: return (NULL);
518: }
519: }
520:
521: /* NUL-terminate */
522: if (cvs_buf_putc(buf, '\0') < 0) {
523: cvs_buf_free(buf);
524: return (NULL);
525: }
526:
527: len = cvs_buf_size(buf);
528: vrstr = (char *)malloc(len);
1.27 jfb 529: if (vrstr == NULL) {
530: cvs_buf_free(buf);
531: return (NULL);
532: }
1.1 jfb 533:
534: cvs_buf_copy(buf, 0, vrstr, len);
535: cvs_buf_free(buf);
536:
537: return (vrstr);
538: }
539:
540:
541: /*
542: * cvs_sendfile()
543: *
544: * Send the mode and size of a file followed by the file's contents.
545: * Returns 0 on success, or -1 on failure.
546: */
547:
548: int
1.13 jfb 549: cvs_sendfile(struct cvsroot *root, const char *path)
1.1 jfb 550: {
551: int fd;
552: ssize_t ret;
553: char buf[4096];
554: struct stat st;
555:
556: if (stat(path, &st) == -1) {
557: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
558: return (-1);
559: }
560:
1.24 jfb 561: if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
562: return (-1);
563:
1.1 jfb 564: fd = open(path, O_RDONLY, 0);
565: if (fd == -1) {
566: return (-1);
567: }
568:
1.13 jfb 569: cvs_sendln(root, buf);
1.1 jfb 570: snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
1.13 jfb 571: cvs_sendln(root, buf);
1.1 jfb 572:
573: while ((ret = read(fd, buf, sizeof(buf))) != 0) {
574: if (ret == -1) {
1.24 jfb 575: (void)close(fd);
1.1 jfb 576: cvs_log(LP_ERRNO, "failed to read file `%s'", path);
577: return (-1);
578: }
579:
1.13 jfb 580: cvs_sendraw(root, buf, (size_t)ret);
1.1 jfb 581:
582: }
583:
584: (void)close(fd);
585:
586: return (0);
587: }
588:
589:
590: /*
591: * cvs_recvfile()
592: *
593: * Receive the mode and size of a file followed the file's contents and
594: * create or update the file whose path is <path> with the received
595: * information.
596: */
597:
1.14 jfb 598: BUF*
599: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 600: {
601: size_t len;
602: ssize_t ret;
603: off_t fsz, cnt;
604: char buf[4096], *ep;
1.14 jfb 605: BUF *fbuf;
606:
607: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
608: if (fbuf == NULL)
609: return (NULL);
1.1 jfb 610:
1.13 jfb 611: if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14 jfb 612: (cvs_strtomode(buf, mode) < 0)) {
613: return (NULL);
1.1 jfb 614: }
615:
1.13 jfb 616: cvs_getln(root, buf, sizeof(buf));
1.1 jfb 617:
618: fsz = (off_t)strtol(buf, &ep, 10);
619: if (*ep != '\0') {
620: cvs_log(LP_ERR, "parse error in file size transmission");
1.14 jfb 621: return (NULL);
1.1 jfb 622: }
623:
624: cnt = 0;
625: do {
626: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 627: if (len == 0)
628: break;
1.13 jfb 629: ret = cvs_recvraw(root, buf, len);
1.1 jfb 630: if (ret == -1) {
1.14 jfb 631: cvs_buf_free(fbuf);
632: return (NULL);
1.1 jfb 633: }
634:
1.14 jfb 635: if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
636: cvs_log(LP_ERR,
637: "failed to append received file data");
638: cvs_buf_free(fbuf);
639: return (NULL);
1.1 jfb 640: }
641:
642: cnt += (off_t)ret;
643: } while (cnt < fsz);
644:
1.14 jfb 645: return (fbuf);
1.1 jfb 646: }
1.12 jfb 647:
648:
649: /*
650: * cvs_sendreq()
651: *
652: * Send a request to the server of type <rid>, with optional arguments
653: * contained in <arg>, which should not be terminated by a newline.
654: * Returns 0 on success, or -1 on failure.
655: */
656:
657: int
1.13 jfb 658: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 659: {
660: int ret;
1.13 jfb 661: struct cvs_req *req;
1.12 jfb 662:
663: if (root->cr_srvin == NULL) {
664: cvs_log(LP_ERR, "cannot send request: Not connected");
665: return (-1);
666: }
667:
1.13 jfb 668: req = cvs_req_getbyid(rid);
669: if (req == NULL) {
1.12 jfb 670: cvs_log(LP_ERR, "unsupported request type %u", rid);
671: return (-1);
672: }
673:
1.18 jfb 674: /* is this request supported by the server? */
675: if (!CVS_GETVR(root, req->req_id)) {
1.25 jfb 676: cvs_log(LP_WARN, "remote end does not support request `%s'",
1.18 jfb 677: req->req_str);
678: return (-1);
679: }
680:
1.13 jfb 681: snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
682: req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.12 jfb 683:
684: if (cvs_server_inlog != NULL)
1.13 jfb 685: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 686:
1.13 jfb 687: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12 jfb 688: if (ret == EOF) {
689: cvs_log(LP_ERRNO, "failed to send request to server");
690: return (-1);
691: }
1.17 jfb 692:
693: if (rid == CVS_REQ_VERSION)
694: cvs_version_sent = 1;
1.12 jfb 695:
1.13 jfb 696: if (req->req_flags & CVS_REQF_RESP)
1.12 jfb 697: ret = cvs_getresp(root);
698:
1.13 jfb 699: return (ret);
1.12 jfb 700: }
701:
702:
703: /*
704: * cvs_getresp()
705: *
706: * Get a response from the server. This call will actually read and handle
707: * responses from the server until one of the response handlers returns
708: * non-zero (either an error occured or the end of the response was reached).
709: * Returns the number of handled commands on success, or -1 on failure.
710: */
711:
712: int
1.13 jfb 713: cvs_getresp(struct cvsroot *root)
1.12 jfb 714: {
1.13 jfb 715: int nbcmd, ret;
716: size_t len;
1.12 jfb 717:
718: nbcmd = 0;
719:
720: do {
721: /* wait for incoming data */
1.13 jfb 722: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 723: root->cr_srvout) == NULL) {
724: if (feof(root->cr_srvout))
725: return (0);
726: cvs_log(LP_ERRNO,
727: "failed to read response from server");
728: return (-1);
729: }
730:
731: if (cvs_server_outlog != NULL)
1.13 jfb 732: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 733:
1.13 jfb 734: if ((len = strlen(cvs_proto_buf)) != 0) {
735: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 736: /* truncated line */
1.30 ! deraadt 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");
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:
831: int
832: cvs_getreq(void)
833: {
834: int nbcmd;
835:
836: nbcmd = 0;
837:
838: do {
839: /* wait for incoming data */
1.13 jfb 840: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 841: stdin) == NULL) {
842: if (feof(stdin))
843: return (0);
844: cvs_log(LP_ERRNO,
845: "failed to read request from client");
846: return (-1);
847: }
848:
1.13 jfb 849: if ((len = strlen(cvs_proto_buf)) != 0) {
850: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 851: /* truncated line */
1.30 ! deraadt 852: } else
1.13 jfb 853: cvs_proto_buf[--len] = '\0';
1.12 jfb 854: }
855:
1.13 jfb 856: ret = cvs_resp_handle(cvs_proto_buf);
1.12 jfb 857: } while (ret == 0);
858: }
859: #endif
860:
861:
862: /*
863: * cvs_sendln()
864: *
1.18 jfb 865: * Send a single line <line> string to the remote end. The line is sent as is,
1.12 jfb 866: * without any modifications.
867: * Returns 0 on success, or -1 on failure.
868: */
869:
870: int
871: cvs_sendln(struct cvsroot *root, const char *line)
872: {
873: int nl;
874: size_t len;
1.18 jfb 875: FILE *out;
876:
877: if (cvs_cmdop == CVS_OP_SERVER)
878: out = stdout;
879: else
880: out = root->cr_srvin;
1.12 jfb 881:
882: nl = 0;
883: len = strlen(line);
884:
885: if ((len > 0) && (line[len - 1] != '\n'))
886: nl = 1;
887:
888: if (cvs_server_inlog != NULL) {
889: fputs(line, cvs_server_inlog);
890: if (nl)
891: fputc('\n', cvs_server_inlog);
892: }
1.18 jfb 893: fputs(line, out);
1.12 jfb 894: if (nl)
1.18 jfb 895: fputc('\n', out);
1.12 jfb 896: return (0);
897: }
898:
899:
900: /*
1.13 jfb 901: * cvs_sendraw()
1.12 jfb 902: *
903: * Send the first <len> bytes from the buffer <src> to the server.
904: */
905:
906: int
1.13 jfb 907: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12 jfb 908: {
1.18 jfb 909: FILE *out;
910:
911: if (cvs_cmdop == CVS_OP_SERVER)
912: out = stdout;
913: else
914: out = root->cr_srvin;
915:
1.12 jfb 916: if (cvs_server_inlog != NULL)
917: fwrite(src, sizeof(char), len, cvs_server_inlog);
1.18 jfb 918: if (fwrite(src, sizeof(char), len, out) < len) {
1.12 jfb 919: return (-1);
920: }
921:
922: return (0);
923: }
924:
925:
926: /*
1.13 jfb 927: * cvs_recvraw()
1.12 jfb 928: *
929: * Receive the first <len> bytes from the buffer <src> to the server.
930: */
931:
932: ssize_t
1.13 jfb 933: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 934: {
935: size_t ret;
1.18 jfb 936: FILE *in;
937:
938: if (cvs_cmdop == CVS_OP_SERVER)
939: in = stdin;
940: else
941: in = root->cr_srvout;
1.12 jfb 942:
1.18 jfb 943: ret = fread(dst, sizeof(char), len, in);
1.12 jfb 944: if (ret == 0)
945: return (-1);
946: if (cvs_server_outlog != NULL)
947: fwrite(dst, sizeof(char), len, cvs_server_outlog);
948: return (ssize_t)ret;
949: }
950:
951:
952: /*
1.13 jfb 953: * cvs_senddir()
1.12 jfb 954: *
955: * Send a `Directory' request along with the 2 paths that follow it.
956: */
957:
958: int
1.13 jfb 959: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 960: {
1.28 jfb 961: char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12 jfb 962:
1.13 jfb 963: if (dir->cf_ddat->cd_repo == NULL)
1.28 jfb 964: strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
1.13 jfb 965: else
1.28 jfb 966: snprintf(rbuf, sizeof(rbuf), "%s/%s", root->cr_dir,
1.13 jfb 967: dir->cf_ddat->cd_repo);
1.12 jfb 968:
1.28 jfb 969: cvs_file_getpath(dir, lbuf, sizeof(lbuf));
970:
971: if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf) < 0) ||
972: (cvs_sendln(root, rbuf) < 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: }