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