Annotation of src/usr.bin/cvs/proto.c, Revision 1.39
1.39 ! jfb 1: /* $OpenBSD: proto.c,v 1.38 2005/01/14 20:54:48 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>
1.38 jfb 49: #include <libgen.h>
1.1 jfb 50: #include <stdlib.h>
51: #include <unistd.h>
52: #include <string.h>
53: #include <sysexits.h>
54:
55: #include "buf.h"
56: #include "cvs.h"
57: #include "log.h"
1.14 jfb 58: #include "file.h"
1.13 jfb 59: #include "proto.h"
1.1 jfb 60:
61:
1.11 jfb 62: /* request flags */
63: #define CVS_REQF_RESP 0x01
64:
65:
1.13 jfb 66: static int cvs_initlog (void);
1.1 jfb 67:
1.18 jfb 68: struct cvs_req cvs_requests[] = {
69: { CVS_REQ_DIRECTORY, "Directory", 0 },
70: { CVS_REQ_MAXDOTDOT, "Max-dotdot", 0 },
71: { CVS_REQ_STATICDIR, "Static-directory", 0 },
72: { CVS_REQ_STICKY, "Sticky", 0 },
73: { CVS_REQ_ENTRY, "Entry", 0 },
74: { CVS_REQ_ENTRYEXTRA, "EntryExtra", 0 },
75: { CVS_REQ_CHECKINTIME, "Checkin-time", 0 },
76: { CVS_REQ_MODIFIED, "Modified", 0 },
77: { CVS_REQ_ISMODIFIED, "Is-modified", 0 },
78: { CVS_REQ_UNCHANGED, "Unchanged", 0 },
79: { CVS_REQ_USEUNCHANGED, "UseUnchanged", 0 },
80: { CVS_REQ_NOTIFY, "Notify", 0 },
81: { CVS_REQ_NOTIFYUSER, "NotifyUser", 0 },
82: { CVS_REQ_QUESTIONABLE, "Questionable", 0 },
83: { CVS_REQ_CASE, "Case", 0 },
84: { CVS_REQ_UTF8, "Utf8", 0 },
85: { CVS_REQ_ARGUMENT, "Argument", 0 },
86: { CVS_REQ_ARGUMENTX, "Argumentx", 0 },
87: { CVS_REQ_GLOBALOPT, "Global_option", 0 },
88: { CVS_REQ_GZIPSTREAM, "Gzip-stream", 0 },
89: { CVS_REQ_READCVSRC2, "read-cvsrc2", 0 },
90: { CVS_REQ_READWRAP, "read-cvswrappers", 0 },
91: { CVS_REQ_READIGNORE, "read-cvsignore", 0 },
92: { CVS_REQ_ERRIFREADER, "Error-If-Reader", 0 },
93: { CVS_REQ_VALIDRCSOPT, "Valid-RcsOptions", 0 },
94: { CVS_REQ_SET, "Set", 0 },
95: { CVS_REQ_XPANDMOD, "expand-modules", CVS_REQF_RESP },
1.26 jfb 96: { CVS_REQ_LOG, "log", CVS_REQF_RESP },
1.18 jfb 97: { CVS_REQ_CO, "co", CVS_REQF_RESP },
98: { CVS_REQ_EXPORT, "export", CVS_REQF_RESP },
1.26 jfb 99: { CVS_REQ_ANNOTATE, "annotate", CVS_REQF_RESP },
100: { CVS_REQ_RDIFF, "rdiff", CVS_REQF_RESP },
1.18 jfb 101: { CVS_REQ_RTAG, "rtag", CVS_REQF_RESP },
102: { CVS_REQ_INIT, "init", CVS_REQF_RESP },
103: { CVS_REQ_STATUS, "status", CVS_REQF_RESP },
104: { CVS_REQ_UPDATE, "update", CVS_REQF_RESP },
1.26 jfb 105: { CVS_REQ_HISTORY, "history", CVS_REQF_RESP },
1.18 jfb 106: { CVS_REQ_IMPORT, "import", CVS_REQF_RESP },
107: { CVS_REQ_ADD, "add", CVS_REQF_RESP },
108: { CVS_REQ_REMOVE, "remove", CVS_REQF_RESP },
109: { CVS_REQ_RELEASE, "release", CVS_REQF_RESP },
110: { CVS_REQ_ROOT, "Root", 0 },
111: { CVS_REQ_VALIDRESP, "Valid-responses", 0 },
112: { CVS_REQ_VALIDREQ, "valid-requests", CVS_REQF_RESP },
113: { CVS_REQ_VERSION, "version", CVS_REQF_RESP },
114: { CVS_REQ_NOOP, "noop", CVS_REQF_RESP },
115: { CVS_REQ_DIFF, "diff", CVS_REQF_RESP },
1.29 jfb 116: { CVS_REQ_CI, "ci", CVS_REQF_RESP },
1.35 jfb 117: { CVS_REQ_TAG, "tag", CVS_REQF_RESP },
1.1 jfb 118: };
119:
1.18 jfb 120: struct cvs_resp cvs_responses[] = {
121: { CVS_RESP_OK, "ok" },
122: { CVS_RESP_ERROR, "error" },
123: { CVS_RESP_VALIDREQ, "Valid-requests" },
124: { CVS_RESP_M, "M" },
125: { CVS_RESP_MBINARY, "Mbinary" },
126: { CVS_RESP_MT, "MT" },
127: { CVS_RESP_E, "E" },
128: { CVS_RESP_F, "F" },
129: { CVS_RESP_CREATED, "Created" },
130: { CVS_RESP_UPDATED, "Updated" },
131: { CVS_RESP_UPDEXIST, "Update-existing" },
132: { CVS_RESP_MERGED, "Merged" },
133: { CVS_RESP_REMOVED, "Removed" },
134: { CVS_RESP_CKSUM, "Checksum" },
135: { CVS_RESP_CLRSTATDIR, "Clear-static-directory" },
136: { CVS_RESP_SETSTATDIR, "Set-static-directory" },
137: { CVS_RESP_NEWENTRY, "New-entry" },
138: { CVS_RESP_CHECKEDIN, "Checked-in" },
139: { CVS_RESP_MODE, "Mode" },
140: { CVS_RESP_MODTIME, "Mod-time" },
141: { CVS_RESP_MODXPAND, "Module-expansion" },
142: { CVS_RESP_SETSTICKY, "Set-sticky" },
143: { CVS_RESP_CLRSTICKY, "Clear-sticky" },
144: { CVS_RESP_RCSDIFF, "Rcs-diff" },
145: { CVS_RESP_TEMPLATE, "Template" },
1.34 jfb 146: { CVS_RESP_COPYFILE, "Copy-file" },
1.1 jfb 147: };
148:
1.17 jfb 149: #define CVS_NBREQ (sizeof(cvs_requests)/sizeof(cvs_requests[0]))
150: #define CVS_NBRESP (sizeof(cvs_responses)/sizeof(cvs_responses[0]))
151:
152: /* hack to receive the remote version without outputting it */
1.18 jfb 153: u_int cvs_version_sent = 0;
1.17 jfb 154:
1.13 jfb 155: static char cvs_proto_buf[4096];
156:
157: /*
1.37 david 158: * Output files for protocol logging when the CVS_CLIENT_LOG environment
1.13 jfb 159: * variable is set.
160: */
161: static int cvs_server_logon = 0;
162: static FILE *cvs_server_inlog = NULL;
163: static FILE *cvs_server_outlog = NULL;
164:
1.28 jfb 165: static pid_t cvs_subproc_pid;
166:
1.32 jfb 167: /* last directory sent with cvs_senddir() */
168: static char cvs_lastdir[MAXPATHLEN] = "";
169:
1.13 jfb 170:
171: /*
172: * cvs_connect()
173: *
174: * Open a client connection to the cvs server whose address is given in
175: * the <root> variable. The method used to connect depends on the
176: * setting of the CVS_RSH variable.
1.17 jfb 177: * Once the connection has been established, we first send the list of
178: * responses we support and request the list of supported requests from the
179: * server. Then, a version request is sent and various global flags are sent.
1.13 jfb 180: * Returns 0 on success, or -1 on failure.
181: */
182: int
183: cvs_connect(struct cvsroot *root)
184: {
1.20 jfb 185: int argc, infd[2], outfd[2], errfd[2];
1.13 jfb 186: char *argv[16], *cvs_server_cmd, *vresp;
187:
1.36 jfb 188: if (root->cr_method == CVS_METHOD_PSERVER) {
189: cvs_log(LP_ERR, "no pserver support due to security issues");
190: return (-1);
191: } else if ((root->cr_method == CVS_METHOD_KSERVER) ||
192: (root->cr_method == CVS_METHOD_GSERVER) ||
193: (root->cr_method == CVS_METHOD_EXT) ||
194: (root->cr_method == CVS_METHOD_FORK)) {
195: cvs_log(LP_ERR, "connection method not supported yet");
196: return (-1);
197: }
1.33 jfb 198:
199: if (root->cr_flags & CVS_ROOT_CONNECTED) {
200: cvs_log(LP_NOTICE, "already connected to CVSROOT");
201: return (0);
202: }
203:
1.13 jfb 204: if (pipe(infd) == -1) {
205: cvs_log(LP_ERRNO,
206: "failed to create input pipe for client connection");
207: return (-1);
208: }
209:
210: if (pipe(outfd) == -1) {
211: cvs_log(LP_ERRNO,
212: "failed to create output pipe for client connection");
213: (void)close(infd[0]);
214: (void)close(infd[1]);
215: return (-1);
216: }
217:
1.20 jfb 218: if (pipe(errfd) == -1) {
219: cvs_log(LP_ERRNO,
220: "failed to create error pipe for client connection");
221: (void)close(infd[0]);
222: (void)close(infd[1]);
223: (void)close(outfd[0]);
224: (void)close(outfd[1]);
225: return (-1);
226: }
227:
1.28 jfb 228: cvs_subproc_pid = fork();
229: if (cvs_subproc_pid == -1) {
1.13 jfb 230: cvs_log(LP_ERRNO, "failed to fork for cvs server connection");
231: return (-1);
1.30 deraadt 232: } else if (cvs_subproc_pid == 0) {
1.13 jfb 233: if ((dup2(infd[0], STDIN_FILENO) == -1) ||
1.23 jfb 234: (dup2(outfd[1], STDOUT_FILENO) == -1)) {
1.13 jfb 235: cvs_log(LP_ERRNO,
236: "failed to setup standard streams for cvs server");
237: return (-1);
238: }
239: (void)close(infd[1]);
240: (void)close(outfd[0]);
1.20 jfb 241: (void)close(errfd[0]);
1.13 jfb 242:
243: argc = 0;
244: argv[argc++] = cvs_rsh;
245:
246: if (root->cr_user != NULL) {
247: argv[argc++] = "-l";
248: argv[argc++] = root->cr_user;
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: char*
501: cvs_resp_getvalid(void)
502: {
503: u_int i;
504: size_t len;
505: char *vrstr;
506: BUF *buf;
507:
508: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
509: if (buf == NULL)
510: return (NULL);
511:
512: cvs_buf_set(buf, cvs_responses[0].resp_str,
513: strlen(cvs_responses[0].resp_str), 0);
514:
515: for (i = 1; i < CVS_NBRESP; i++) {
516: if ((cvs_buf_putc(buf, ' ') < 0) ||
517: (cvs_buf_append(buf, cvs_responses[i].resp_str,
518: strlen(cvs_responses[i].resp_str)) < 0)) {
519: cvs_buf_free(buf);
520: return (NULL);
521: }
522: }
523:
524: /* NUL-terminate */
525: if (cvs_buf_putc(buf, '\0') < 0) {
526: cvs_buf_free(buf);
527: return (NULL);
528: }
529:
530: len = cvs_buf_size(buf);
531: vrstr = (char *)malloc(len);
1.27 jfb 532: if (vrstr == NULL) {
533: cvs_buf_free(buf);
534: return (NULL);
535: }
1.1 jfb 536:
537: cvs_buf_copy(buf, 0, vrstr, len);
538: cvs_buf_free(buf);
539:
540: return (vrstr);
541: }
542:
543:
544: /*
545: * cvs_sendfile()
546: *
547: * Send the mode and size of a file followed by the file's contents.
548: * Returns 0 on success, or -1 on failure.
549: */
550: int
1.13 jfb 551: cvs_sendfile(struct cvsroot *root, const char *path)
1.1 jfb 552: {
553: int fd;
554: ssize_t ret;
555: char buf[4096];
556: struct stat st;
557:
1.38 jfb 558: cvs_log(LP_TRACE, "Sending file `%s' to server", basename(path));
559:
1.1 jfb 560: if (stat(path, &st) == -1) {
561: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
562: return (-1);
563: }
564:
1.24 jfb 565: if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
566: return (-1);
567:
1.1 jfb 568: fd = open(path, O_RDONLY, 0);
569: if (fd == -1) {
1.38 jfb 570: cvs_log(LP_ERRNO, "failed to open `%s'", path);
1.1 jfb 571: return (-1);
572: }
573:
1.38 jfb 574: if (cvs_sendln(root, buf) < 0) {
575: (void)close(fd);
576: return (-1);
577: }
1.1 jfb 578: snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
1.38 jfb 579: if (cvs_sendln(root, buf) < 0) {
580: (void)close(fd);
581: return (-1);
582: }
1.1 jfb 583:
584: while ((ret = read(fd, buf, sizeof(buf))) != 0) {
585: if (ret == -1) {
1.24 jfb 586: (void)close(fd);
1.1 jfb 587: cvs_log(LP_ERRNO, "failed to read file `%s'", path);
588: return (-1);
589: }
590:
1.38 jfb 591: if (cvs_sendraw(root, buf, (size_t)ret) < 0) {
592: (void)close(fd);
593: return (-1);
594: }
1.1 jfb 595: }
596:
597: (void)close(fd);
598: return (0);
599: }
600:
601:
602: /*
603: * cvs_recvfile()
604: *
605: * Receive the mode and size of a file followed the file's contents and
606: * create or update the file whose path is <path> with the received
607: * information.
608: */
1.14 jfb 609: BUF*
610: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 611: {
612: size_t len;
613: ssize_t ret;
614: off_t fsz, cnt;
615: char buf[4096], *ep;
1.14 jfb 616: BUF *fbuf;
617:
618: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
619: if (fbuf == NULL)
620: return (NULL);
1.1 jfb 621:
1.13 jfb 622: if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14 jfb 623: (cvs_strtomode(buf, mode) < 0)) {
624: return (NULL);
1.1 jfb 625: }
626:
1.13 jfb 627: cvs_getln(root, buf, sizeof(buf));
1.1 jfb 628:
629: fsz = (off_t)strtol(buf, &ep, 10);
630: if (*ep != '\0') {
631: cvs_log(LP_ERR, "parse error in file size transmission");
1.14 jfb 632: return (NULL);
1.1 jfb 633: }
634:
635: cnt = 0;
636: do {
637: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 638: if (len == 0)
639: break;
1.13 jfb 640: ret = cvs_recvraw(root, buf, len);
1.1 jfb 641: if (ret == -1) {
1.14 jfb 642: cvs_buf_free(fbuf);
643: return (NULL);
1.1 jfb 644: }
645:
1.14 jfb 646: if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
647: cvs_log(LP_ERR,
648: "failed to append received file data");
649: cvs_buf_free(fbuf);
650: return (NULL);
1.1 jfb 651: }
652:
653: cnt += (off_t)ret;
654: } while (cnt < fsz);
655:
1.14 jfb 656: return (fbuf);
1.1 jfb 657: }
1.12 jfb 658:
659:
660: /*
661: * cvs_sendreq()
662: *
663: * Send a request to the server of type <rid>, with optional arguments
664: * contained in <arg>, which should not be terminated by a newline.
665: * Returns 0 on success, or -1 on failure.
666: */
667: int
1.13 jfb 668: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 669: {
670: int ret;
1.13 jfb 671: struct cvs_req *req;
1.12 jfb 672:
673: if (root->cr_srvin == NULL) {
1.36 jfb 674: cvs_log(LP_ERR, "cannot send request %u: Not connected", rid);
1.12 jfb 675: return (-1);
676: }
677:
1.13 jfb 678: req = cvs_req_getbyid(rid);
679: if (req == NULL) {
1.12 jfb 680: cvs_log(LP_ERR, "unsupported request type %u", rid);
681: return (-1);
682: }
683:
1.18 jfb 684: /* is this request supported by the server? */
685: if (!CVS_GETVR(root, req->req_id)) {
1.25 jfb 686: cvs_log(LP_WARN, "remote end does not support request `%s'",
1.18 jfb 687: req->req_str);
688: return (-1);
689: }
690:
1.13 jfb 691: snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
692: req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.12 jfb 693:
694: if (cvs_server_inlog != NULL)
1.13 jfb 695: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 696:
1.13 jfb 697: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12 jfb 698: if (ret == EOF) {
699: cvs_log(LP_ERRNO, "failed to send request to server");
700: return (-1);
701: }
1.17 jfb 702:
703: if (rid == CVS_REQ_VERSION)
704: cvs_version_sent = 1;
1.12 jfb 705:
1.13 jfb 706: if (req->req_flags & CVS_REQF_RESP)
1.12 jfb 707: ret = cvs_getresp(root);
708:
1.13 jfb 709: return (ret);
1.12 jfb 710: }
711:
712:
713: /*
714: * cvs_getresp()
715: *
716: * Get a response from the server. This call will actually read and handle
717: * responses from the server until one of the response handlers returns
1.37 david 718: * non-zero (either an error occurred or the end of the response was reached).
1.12 jfb 719: * Returns the number of handled commands on success, or -1 on failure.
720: */
721: int
1.13 jfb 722: cvs_getresp(struct cvsroot *root)
1.12 jfb 723: {
1.13 jfb 724: int nbcmd, ret;
725: size_t len;
1.12 jfb 726:
727: nbcmd = 0;
728:
729: do {
730: /* wait for incoming data */
1.13 jfb 731: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 732: root->cr_srvout) == NULL) {
733: if (feof(root->cr_srvout))
734: return (0);
735: cvs_log(LP_ERRNO,
736: "failed to read response from server");
737: return (-1);
738: }
739:
740: if (cvs_server_outlog != NULL)
1.13 jfb 741: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 742:
1.13 jfb 743: if ((len = strlen(cvs_proto_buf)) != 0) {
744: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 745: /* truncated line */
1.30 deraadt 746: } else
1.13 jfb 747: cvs_proto_buf[--len] = '\0';
1.12 jfb 748: }
749:
1.13 jfb 750: ret = cvs_resp_handle(root, cvs_proto_buf);
1.12 jfb 751: nbcmd++;
752: } while (ret == 0);
753:
754: if (ret > 0)
755: ret = nbcmd;
756: return (ret);
757: }
758:
759:
760: /*
1.13 jfb 761: * cvs_getln()
762: *
1.18 jfb 763: * Get a line from the remote end and store it in <lbuf>. The terminating
1.13 jfb 764: * newline character is stripped from the result.
765: */
766: int
767: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
768: {
769: size_t rlen;
1.18 jfb 770: FILE *in;
1.13 jfb 771:
1.18 jfb 772: if (cvs_cmdop == CVS_OP_SERVER)
773: in = stdin;
774: else
775: in = root->cr_srvout;
776:
777: if (fgets(lbuf, len, in) == NULL) {
778: if (ferror(in)) {
779: cvs_log(LP_ERRNO, "failed to read line");
1.13 jfb 780: return (-1);
781: }
782:
1.18 jfb 783: if (feof(in))
1.13 jfb 784: *lbuf = '\0';
785: }
786:
787: if (cvs_server_outlog != NULL)
788: fputs(lbuf, cvs_server_outlog);
789:
790: rlen = strlen(lbuf);
791: if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
792: lbuf[--rlen] = '\0';
793:
794: return (0);
795: }
796:
1.19 jfb 797:
1.13 jfb 798: /*
1.12 jfb 799: * cvs_sendresp()
800: *
1.19 jfb 801: * Send a response of type <rid> to the client, with optional arguments
1.12 jfb 802: * contained in <arg>, which should not be terminated by a newline.
803: * Returns 0 on success, or -1 on failure.
804: */
805: int
806: cvs_sendresp(u_int rid, const char *arg)
807: {
808: int ret;
1.19 jfb 809: struct cvs_resp *resp;
1.12 jfb 810:
811: resp = cvs_resp_getbyid(rid);
1.13 jfb 812: if (resp == NULL) {
1.12 jfb 813: cvs_log(LP_ERR, "unsupported response type %u", rid);
814: return (-1);
815: }
816:
1.19 jfb 817: ret = fputs(resp->resp_str, stdout);
1.12 jfb 818: if (ret == EOF) {
819: cvs_log(LP_ERRNO, "failed to send response to client");
1.30 deraadt 820: } else {
1.19 jfb 821: if (arg != NULL) {
822: putc(' ', stdout);
823: fputs(arg, stdout);
824: }
1.12 jfb 825: putc('\n', stdout);
826: }
827: return (0);
828: }
829:
830:
1.19 jfb 831: #ifdef notyet
1.12 jfb 832: /*
833: * cvs_getreq()
834: *
835: * Get a request from the client.
836: */
837: int
838: cvs_getreq(void)
839: {
840: int nbcmd;
841:
842: nbcmd = 0;
843:
844: do {
845: /* wait for incoming data */
1.13 jfb 846: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 847: stdin) == NULL) {
848: if (feof(stdin))
849: return (0);
850: cvs_log(LP_ERRNO,
851: "failed to read request from client");
852: return (-1);
853: }
854:
1.13 jfb 855: if ((len = strlen(cvs_proto_buf)) != 0) {
856: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 857: /* truncated line */
1.30 deraadt 858: } else
1.13 jfb 859: cvs_proto_buf[--len] = '\0';
1.12 jfb 860: }
861:
1.13 jfb 862: ret = cvs_resp_handle(cvs_proto_buf);
1.12 jfb 863: } while (ret == 0);
864: }
865: #endif
866:
867:
868: /*
869: * cvs_sendln()
870: *
1.18 jfb 871: * Send a single line <line> string to the remote end. The line is sent as is,
1.12 jfb 872: * without any modifications.
873: * Returns 0 on success, or -1 on failure.
874: */
875: int
876: cvs_sendln(struct cvsroot *root, const char *line)
877: {
878: int nl;
879: size_t len;
1.18 jfb 880: FILE *out;
881:
882: if (cvs_cmdop == CVS_OP_SERVER)
883: out = stdout;
884: else
885: out = root->cr_srvin;
1.12 jfb 886:
887: nl = 0;
888: len = strlen(line);
889:
890: if ((len > 0) && (line[len - 1] != '\n'))
891: nl = 1;
892:
893: if (cvs_server_inlog != NULL) {
894: fputs(line, cvs_server_inlog);
895: if (nl)
896: fputc('\n', cvs_server_inlog);
897: }
1.18 jfb 898: fputs(line, out);
1.12 jfb 899: if (nl)
1.18 jfb 900: fputc('\n', out);
1.12 jfb 901: return (0);
902: }
903:
904:
905: /*
1.13 jfb 906: * cvs_sendraw()
1.12 jfb 907: *
908: * Send the first <len> bytes from the buffer <src> to the server.
909: */
910: int
1.13 jfb 911: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12 jfb 912: {
1.18 jfb 913: FILE *out;
914:
915: if (cvs_cmdop == CVS_OP_SERVER)
916: out = stdout;
917: else
918: out = root->cr_srvin;
919:
1.12 jfb 920: if (cvs_server_inlog != NULL)
921: fwrite(src, sizeof(char), len, cvs_server_inlog);
1.18 jfb 922: if (fwrite(src, sizeof(char), len, out) < len) {
1.38 jfb 923: cvs_log(LP_ERR, "failed to send data");
1.12 jfb 924: return (-1);
925: }
926:
927: return (0);
928: }
929:
930:
931: /*
1.13 jfb 932: * cvs_recvraw()
1.12 jfb 933: *
934: * Receive the first <len> bytes from the buffer <src> to the server.
935: */
936: ssize_t
1.13 jfb 937: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 938: {
939: size_t ret;
1.18 jfb 940: FILE *in;
941:
942: if (cvs_cmdop == CVS_OP_SERVER)
943: in = stdin;
944: else
945: in = root->cr_srvout;
1.12 jfb 946:
1.18 jfb 947: ret = fread(dst, sizeof(char), len, in);
1.12 jfb 948: if (ret == 0)
949: return (-1);
950: if (cvs_server_outlog != NULL)
951: fwrite(dst, sizeof(char), len, cvs_server_outlog);
952: return (ssize_t)ret;
953: }
954:
955:
956: /*
1.13 jfb 957: * cvs_senddir()
1.12 jfb 958: *
1.32 jfb 959: * Send a `Directory' request along with the 2 paths that follow it. If
960: * the directory info to be sent is the same as the last info sent, the
961: * call does nothing and simply returns without an error.
1.12 jfb 962: */
963: int
1.31 tedu 964: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 965: {
1.28 jfb 966: char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12 jfb 967:
1.32 jfb 968: cvs_file_getpath(dir, lbuf, sizeof(lbuf));
969: if (strcmp(lbuf, cvs_lastdir) == 0)
970: return (0);
971:
1.13 jfb 972: if (dir->cf_ddat->cd_repo == NULL)
1.28 jfb 973: strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
1.13 jfb 974: else
1.28 jfb 975: snprintf(rbuf, sizeof(rbuf), "%s/%s", root->cr_dir,
1.13 jfb 976: dir->cf_ddat->cd_repo);
1.12 jfb 977:
1.28 jfb 978:
979: if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf) < 0) ||
980: (cvs_sendln(root, rbuf) < 0))
1.12 jfb 981: return (-1);
1.32 jfb 982:
983: strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
1.12 jfb 984:
985: return (0);
986: }
987:
988:
989: /*
1.13 jfb 990: * cvs_sendarg()
1.12 jfb 991: *
992: * Send the argument <arg> to the server. The argument <append> is used to
993: * determine if the argument should be simply appended to the last argument
994: * sent or if it should be created as a new argument (0).
995: */
996: int
1.13 jfb 997: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12 jfb 998: {
1.13 jfb 999: return cvs_sendreq(root, ((append == 0) ?
1000: CVS_REQ_ARGUMENT : CVS_REQ_ARGUMENTX), arg);
1.12 jfb 1001: }
1002:
1003:
1004: /*
1.13 jfb 1005: * cvs_sendentry()
1.12 jfb 1006: *
1007: * Send an `Entry' request to the server along with the mandatory fields from
1008: * the CVS entry <ent> (which are the name and revision).
1009: */
1010: int
1.13 jfb 1011: cvs_sendentry(struct cvsroot *root, const struct cvs_ent *ent)
1.12 jfb 1012: {
1013: char ebuf[128], numbuf[64];
1014:
1015: snprintf(ebuf, sizeof(ebuf), "/%s/%s///", ent->ce_name,
1016: rcsnum_tostr(ent->ce_rev, numbuf, sizeof(numbuf)));
1017:
1.13 jfb 1018: return cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12 jfb 1019: }
1020:
1021:
1022: /*
1.13 jfb 1023: * cvs_initlog()
1.12 jfb 1024: *
1025: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1026: * set. In this case, the variable's value is used as a path to which the
1027: * appropriate suffix is added (".in" for server input and ".out" for server
1028: * output.
1029: * Returns 0 on success, or -1 on failure.
1030: */
1031: static int
1.13 jfb 1032: cvs_initlog(void)
1.12 jfb 1033: {
1034: char *env, fpath[MAXPATHLEN];
1035:
1.13 jfb 1036: /* avoid doing it more than once */
1037: if (cvs_server_logon)
1038: return (0);
1039:
1.12 jfb 1040: env = getenv("CVS_CLIENT_LOG");
1041: if (env == NULL)
1042: return (0);
1043:
1044: strlcpy(fpath, env, sizeof(fpath));
1045: strlcat(fpath, ".in", sizeof(fpath));
1046: cvs_server_inlog = fopen(fpath, "w");
1047: if (cvs_server_inlog == NULL) {
1048: cvs_log(LP_ERRNO, "failed to open server input log `%s'",
1049: fpath);
1050: return (-1);
1051: }
1052:
1053: strlcpy(fpath, env, sizeof(fpath));
1054: strlcat(fpath, ".out", sizeof(fpath));
1055: cvs_server_outlog = fopen(fpath, "w");
1056: if (cvs_server_outlog == NULL) {
1057: cvs_log(LP_ERRNO, "failed to open server output log `%s'",
1058: fpath);
1059: return (-1);
1060: }
1061:
1062: /* make the streams line-buffered */
1063: setvbuf(cvs_server_inlog, NULL, _IOLBF, 0);
1064: setvbuf(cvs_server_outlog, NULL, _IOLBF, 0);
1065:
1.13 jfb 1066: cvs_server_logon = 1;
1.12 jfb 1067:
1068: return (0);
1069: }