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