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