Annotation of src/usr.bin/cvs/proto.c, Revision 1.51
1.51 ! xsa 1: /* $OpenBSD: proto.c,v 1.50 2005/05/09 19:24:07 joris 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:
54: #include "buf.h"
55: #include "cvs.h"
56: #include "log.h"
1.14 jfb 57: #include "file.h"
1.13 jfb 58: #include "proto.h"
1.1 jfb 59:
60:
1.11 jfb 61: /* request flags */
62: #define CVS_REQF_RESP 0x01
63:
64:
1.13 jfb 65: static int cvs_initlog (void);
1.1 jfb 66:
1.18 jfb 67: struct cvs_req cvs_requests[] = {
68: { CVS_REQ_DIRECTORY, "Directory", 0 },
69: { CVS_REQ_MAXDOTDOT, "Max-dotdot", 0 },
70: { CVS_REQ_STATICDIR, "Static-directory", 0 },
71: { CVS_REQ_STICKY, "Sticky", 0 },
72: { CVS_REQ_ENTRY, "Entry", 0 },
73: { CVS_REQ_ENTRYEXTRA, "EntryExtra", 0 },
74: { CVS_REQ_CHECKINTIME, "Checkin-time", 0 },
75: { CVS_REQ_MODIFIED, "Modified", 0 },
76: { CVS_REQ_ISMODIFIED, "Is-modified", 0 },
77: { CVS_REQ_UNCHANGED, "Unchanged", 0 },
78: { CVS_REQ_USEUNCHANGED, "UseUnchanged", 0 },
79: { CVS_REQ_NOTIFY, "Notify", 0 },
80: { CVS_REQ_NOTIFYUSER, "NotifyUser", 0 },
81: { CVS_REQ_QUESTIONABLE, "Questionable", 0 },
82: { CVS_REQ_CASE, "Case", 0 },
83: { CVS_REQ_UTF8, "Utf8", 0 },
84: { CVS_REQ_ARGUMENT, "Argument", 0 },
85: { CVS_REQ_ARGUMENTX, "Argumentx", 0 },
86: { CVS_REQ_GLOBALOPT, "Global_option", 0 },
87: { CVS_REQ_GZIPSTREAM, "Gzip-stream", 0 },
88: { CVS_REQ_READCVSRC2, "read-cvsrc2", 0 },
89: { CVS_REQ_READWRAP, "read-cvswrappers", 0 },
90: { CVS_REQ_READIGNORE, "read-cvsignore", 0 },
91: { CVS_REQ_ERRIFREADER, "Error-If-Reader", 0 },
92: { CVS_REQ_VALIDRCSOPT, "Valid-RcsOptions", 0 },
93: { CVS_REQ_SET, "Set", 0 },
94: { CVS_REQ_XPANDMOD, "expand-modules", CVS_REQF_RESP },
1.26 jfb 95: { CVS_REQ_LOG, "log", CVS_REQF_RESP },
1.18 jfb 96: { CVS_REQ_CO, "co", CVS_REQF_RESP },
97: { CVS_REQ_EXPORT, "export", CVS_REQF_RESP },
1.26 jfb 98: { CVS_REQ_ANNOTATE, "annotate", CVS_REQF_RESP },
99: { CVS_REQ_RDIFF, "rdiff", CVS_REQF_RESP },
1.18 jfb 100: { CVS_REQ_RTAG, "rtag", CVS_REQF_RESP },
101: { CVS_REQ_INIT, "init", CVS_REQF_RESP },
102: { CVS_REQ_STATUS, "status", CVS_REQF_RESP },
103: { CVS_REQ_UPDATE, "update", CVS_REQF_RESP },
1.26 jfb 104: { CVS_REQ_HISTORY, "history", CVS_REQF_RESP },
1.18 jfb 105: { CVS_REQ_IMPORT, "import", CVS_REQF_RESP },
106: { CVS_REQ_ADD, "add", CVS_REQF_RESP },
107: { CVS_REQ_REMOVE, "remove", CVS_REQF_RESP },
108: { CVS_REQ_RELEASE, "release", CVS_REQF_RESP },
109: { CVS_REQ_ROOT, "Root", 0 },
110: { CVS_REQ_VALIDRESP, "Valid-responses", 0 },
111: { CVS_REQ_VALIDREQ, "valid-requests", CVS_REQF_RESP },
112: { CVS_REQ_VERSION, "version", CVS_REQF_RESP },
113: { CVS_REQ_NOOP, "noop", CVS_REQF_RESP },
114: { CVS_REQ_DIFF, "diff", CVS_REQF_RESP },
1.29 jfb 115: { CVS_REQ_CI, "ci", CVS_REQF_RESP },
1.35 jfb 116: { CVS_REQ_TAG, "tag", CVS_REQF_RESP },
1.41 joris 117: { CVS_REQ_ADMIN, "admin", 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");
1.46 xsa 262: exit(1);
1.13 jfb 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.13 jfb 282: /* make the streams line-buffered */
283: (void)setvbuf(root->cr_srvin, NULL, _IOLBF, 0);
284: (void)setvbuf(root->cr_srvout, NULL, _IOLBF, 0);
285:
286: cvs_initlog();
287:
288: /*
289: * Send the server the list of valid responses, then ask for valid
290: * requests.
291: */
292:
1.40 jfb 293: if ((vresp = cvs_resp_getvalid()) == NULL) {
1.13 jfb 294: cvs_log(LP_ERR, "can't generate list of valid responses");
295: return (-1);
296: }
297:
298: if (cvs_sendreq(root, CVS_REQ_VALIDRESP, vresp) < 0) {
1.40 jfb 299: cvs_log(LP_ERR, "failed to get valid responses");
300: free(vresp);
301: return (-1);
1.13 jfb 302: }
303: free(vresp);
304:
305: if (cvs_sendreq(root, CVS_REQ_VALIDREQ, NULL) < 0) {
306: cvs_log(LP_ERR, "failed to get valid requests from server");
307: return (-1);
308: }
309:
1.40 jfb 310: /* don't fail if this request doesn't work */
1.17 jfb 311: if (cvs_sendreq(root, CVS_REQ_VERSION, NULL) < 0)
1.40 jfb 312: cvs_log(LP_WARN, "failed to get remote version");
1.17 jfb 313:
1.13 jfb 314: /* now share our global options with the server */
1.40 jfb 315: if ((verbosity == 1) &&
316: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-q") < 0))
317: return (-1);
318: else if ((verbosity == 0) &&
319: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-Q") < 0))
320: return (-1);
321:
322: if (cvs_nolog && (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-l") < 0))
323: return (-1);
324: if (cvs_readonly && (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-r") < 0))
325: return (-1);
326: if (cvs_trace && (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-t") < 0))
327: return (-1);
1.13 jfb 328:
1.22 jfb 329: /* now send the CVSROOT to the server unless it's an init */
330: if ((cvs_cmdop != CVS_OP_INIT) &&
331: (cvs_sendreq(root, CVS_REQ_ROOT, root->cr_dir) < 0))
1.13 jfb 332: return (-1);
333:
334: /* not sure why, but we have to send this */
335: if (cvs_sendreq(root, CVS_REQ_USEUNCHANGED, NULL) < 0)
336: return (-1);
337:
338: cvs_log(LP_DEBUG, "connected to %s", root->cr_host);
339:
1.33 jfb 340: root->cr_flags |= CVS_ROOT_CONNECTED;
341:
1.13 jfb 342: return (0);
343: }
344:
345:
346: /*
347: * cvs_disconnect()
348: *
349: * Disconnect from the cvs server.
350: */
351: void
352: cvs_disconnect(struct cvsroot *root)
353: {
1.33 jfb 354: if (!(root->cr_flags & CVS_ROOT_CONNECTED))
355: return;
356:
1.13 jfb 357: cvs_log(LP_DEBUG, "closing connection to %s", root->cr_host);
358: if (root->cr_srvin != NULL) {
359: (void)fclose(root->cr_srvin);
360: root->cr_srvin = NULL;
361: }
362: if (root->cr_srvout != NULL) {
363: (void)fclose(root->cr_srvout);
364: root->cr_srvout = NULL;
365: }
1.33 jfb 366:
367: root->cr_flags &= ~CVS_ROOT_CONNECTED;
1.13 jfb 368: }
369:
1.1 jfb 370:
371: /*
372: * cvs_req_getbyid()
373: *
374: */
1.13 jfb 375: struct cvs_req*
1.1 jfb 376: cvs_req_getbyid(int reqid)
377: {
378: u_int i;
379:
380: for (i = 0; i < CVS_NBREQ; i++)
381: if (cvs_requests[i].req_id == reqid)
1.13 jfb 382: return &(cvs_requests[i]);
1.31 tedu 383:
1.1 jfb 384: return (NULL);
385: }
386:
387:
388: /*
389: * cvs_req_getbyname()
390: */
1.13 jfb 391: struct cvs_req*
1.1 jfb 392: cvs_req_getbyname(const char *rname)
393: {
394: u_int i;
395:
396: for (i = 0; i < CVS_NBREQ; i++)
397: if (strcmp(cvs_requests[i].req_str, rname) == 0)
1.13 jfb 398: return &(cvs_requests[i]);
1.1 jfb 399:
1.13 jfb 400: return (NULL);
1.1 jfb 401: }
402:
403:
404: /*
405: * cvs_req_getvalid()
406: *
407: * Build a space-separated list of all the requests that this protocol
408: * implementation supports.
409: */
410: char*
411: cvs_req_getvalid(void)
412: {
413: u_int i;
414: size_t len;
415: char *vrstr;
416: BUF *buf;
417:
418: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
419: if (buf == NULL)
420: return (NULL);
421:
422: cvs_buf_set(buf, cvs_requests[0].req_str,
423: strlen(cvs_requests[0].req_str), 0);
424:
425: for (i = 1; i < CVS_NBREQ; i++) {
426: if ((cvs_buf_putc(buf, ' ') < 0) ||
427: (cvs_buf_append(buf, cvs_requests[i].req_str,
428: strlen(cvs_requests[i].req_str)) < 0)) {
429: cvs_buf_free(buf);
430: return (NULL);
431: }
432: }
433:
434: /* NUL-terminate */
435: if (cvs_buf_putc(buf, '\0') < 0) {
436: cvs_buf_free(buf);
437: return (NULL);
438: }
439:
440: len = cvs_buf_size(buf);
441: vrstr = (char *)malloc(len);
1.27 jfb 442: if (vrstr == NULL) {
443: cvs_buf_free(buf);
444: return (NULL);
445: }
1.1 jfb 446:
447: cvs_buf_copy(buf, 0, vrstr, len);
448: cvs_buf_free(buf);
449:
450: return (vrstr);
451: }
452:
453:
454: /*
455: * cvs_resp_getbyid()
456: *
457: */
1.13 jfb 458: struct cvs_resp*
1.1 jfb 459: cvs_resp_getbyid(int respid)
460: {
461: u_int i;
462:
1.44 tedu 463: for (i = 0; i < CVS_NBRESP; i++)
1.13 jfb 464: if (cvs_responses[i].resp_id == (u_int)respid)
465: return &(cvs_responses[i]);
1.31 tedu 466:
1.1 jfb 467: return (NULL);
468: }
469:
470:
471: /*
472: * cvs_resp_getbyname()
473: */
1.13 jfb 474: struct cvs_resp*
1.1 jfb 475: cvs_resp_getbyname(const char *rname)
476: {
477: u_int i;
478:
1.44 tedu 479: for (i = 0; i < CVS_NBRESP; i++)
1.1 jfb 480: if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13 jfb 481: return &(cvs_responses[i]);
1.1 jfb 482:
1.13 jfb 483: return (NULL);
1.1 jfb 484: }
485:
486:
487: /*
488: * cvs_resp_getvalid()
489: *
490: * Build a space-separated list of all the responses that this protocol
491: * implementation supports.
492: */
493: char*
494: cvs_resp_getvalid(void)
495: {
496: u_int i;
497: size_t len;
498: char *vrstr;
499: BUF *buf;
500:
501: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
502: if (buf == NULL)
503: return (NULL);
504:
505: cvs_buf_set(buf, cvs_responses[0].resp_str,
506: strlen(cvs_responses[0].resp_str), 0);
507:
508: for (i = 1; i < CVS_NBRESP; i++) {
509: if ((cvs_buf_putc(buf, ' ') < 0) ||
510: (cvs_buf_append(buf, cvs_responses[i].resp_str,
511: strlen(cvs_responses[i].resp_str)) < 0)) {
512: cvs_buf_free(buf);
513: return (NULL);
514: }
515: }
516:
517: /* NUL-terminate */
518: if (cvs_buf_putc(buf, '\0') < 0) {
519: cvs_buf_free(buf);
520: return (NULL);
521: }
522:
523: len = cvs_buf_size(buf);
524: vrstr = (char *)malloc(len);
1.27 jfb 525: if (vrstr == NULL) {
526: cvs_buf_free(buf);
527: return (NULL);
528: }
1.1 jfb 529:
530: cvs_buf_copy(buf, 0, vrstr, len);
531: cvs_buf_free(buf);
532:
533: return (vrstr);
534: }
535:
536:
537: /*
538: * cvs_sendfile()
539: *
540: * Send the mode and size of a file followed by the file's contents.
541: * Returns 0 on success, or -1 on failure.
542: */
543: int
1.13 jfb 544: cvs_sendfile(struct cvsroot *root, const char *path)
1.1 jfb 545: {
1.51 ! xsa 546: int fd, l;
1.1 jfb 547: ssize_t ret;
548: char buf[4096];
549: struct stat st;
550:
1.38 jfb 551: cvs_log(LP_TRACE, "Sending file `%s' to server", basename(path));
552:
1.1 jfb 553: if (stat(path, &st) == -1) {
554: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
555: return (-1);
556: }
557:
1.24 jfb 558: if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
559: return (-1);
560:
1.1 jfb 561: fd = open(path, O_RDONLY, 0);
562: if (fd == -1) {
1.38 jfb 563: cvs_log(LP_ERRNO, "failed to open `%s'", path);
1.1 jfb 564: return (-1);
565: }
566:
1.38 jfb 567: if (cvs_sendln(root, buf) < 0) {
568: (void)close(fd);
569: return (-1);
570: }
1.51 ! xsa 571: l = snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
! 572: if (l == -1 || l >= (int)sizeof(buf))
! 573: return (-1);
! 574:
1.38 jfb 575: if (cvs_sendln(root, buf) < 0) {
576: (void)close(fd);
577: return (-1);
578: }
1.1 jfb 579:
580: while ((ret = read(fd, buf, sizeof(buf))) != 0) {
581: if (ret == -1) {
1.24 jfb 582: (void)close(fd);
1.1 jfb 583: cvs_log(LP_ERRNO, "failed to read file `%s'", path);
584: return (-1);
585: }
586:
1.38 jfb 587: if (cvs_sendraw(root, buf, (size_t)ret) < 0) {
588: (void)close(fd);
589: return (-1);
590: }
1.1 jfb 591: }
592:
593: (void)close(fd);
594: return (0);
595: }
596:
597:
598: /*
599: * cvs_recvfile()
600: *
601: * Receive the mode and size of a file followed the file's contents and
602: * create or update the file whose path is <path> with the received
603: * information.
604: */
1.14 jfb 605: BUF*
606: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 607: {
608: size_t len;
609: ssize_t ret;
610: off_t fsz, cnt;
611: char buf[4096], *ep;
1.14 jfb 612: BUF *fbuf;
613:
614: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
615: if (fbuf == NULL)
616: return (NULL);
1.1 jfb 617:
1.13 jfb 618: if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14 jfb 619: (cvs_strtomode(buf, mode) < 0)) {
1.44 tedu 620: cvs_buf_free(fbuf);
1.14 jfb 621: return (NULL);
1.1 jfb 622: }
623:
1.50 joris 624: if (cvs_getln(root, buf, sizeof(buf)) < 0) {
625: cvs_buf_free(fbuf);
626: return (NULL);
627: }
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.44 tedu 632: cvs_buf_free(fbuf);
1.14 jfb 633: return (NULL);
1.1 jfb 634: }
635:
636: cnt = 0;
637: do {
638: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 639: if (len == 0)
640: break;
1.13 jfb 641: ret = cvs_recvraw(root, buf, len);
1.1 jfb 642: if (ret == -1) {
1.14 jfb 643: cvs_buf_free(fbuf);
644: return (NULL);
1.1 jfb 645: }
646:
1.14 jfb 647: if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
648: cvs_log(LP_ERR,
649: "failed to append received file data");
650: cvs_buf_free(fbuf);
651: return (NULL);
1.1 jfb 652: }
653:
654: cnt += (off_t)ret;
655: } while (cnt < fsz);
656:
1.14 jfb 657: return (fbuf);
1.1 jfb 658: }
1.12 jfb 659:
660:
661: /*
662: * cvs_sendreq()
663: *
664: * Send a request to the server of type <rid>, with optional arguments
665: * contained in <arg>, which should not be terminated by a newline.
666: * Returns 0 on success, or -1 on failure.
667: */
668: int
1.13 jfb 669: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 670: {
1.51 ! xsa 671: int ret, l;
1.13 jfb 672: struct cvs_req *req;
1.12 jfb 673:
674: if (root->cr_srvin == NULL) {
1.36 jfb 675: cvs_log(LP_ERR, "cannot send request %u: Not connected", rid);
1.12 jfb 676: return (-1);
677: }
678:
1.13 jfb 679: req = cvs_req_getbyid(rid);
680: if (req == NULL) {
1.12 jfb 681: cvs_log(LP_ERR, "unsupported request type %u", rid);
682: return (-1);
683: }
684:
1.18 jfb 685: /* is this request supported by the server? */
686: if (!CVS_GETVR(root, req->req_id)) {
1.25 jfb 687: cvs_log(LP_WARN, "remote end does not support request `%s'",
1.18 jfb 688: req->req_str);
689: return (-1);
690: }
691:
1.51 ! xsa 692: l = snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
1.13 jfb 693: req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.51 ! xsa 694: if (l == -1 || l >= (int)sizeof(cvs_proto_buf))
! 695: return (-1);
1.12 jfb 696:
697: if (cvs_server_inlog != NULL)
1.13 jfb 698: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 699:
1.13 jfb 700: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12 jfb 701: if (ret == EOF) {
702: cvs_log(LP_ERRNO, "failed to send request to server");
703: return (-1);
704: }
1.17 jfb 705:
706: if (rid == CVS_REQ_VERSION)
707: cvs_version_sent = 1;
1.12 jfb 708:
1.13 jfb 709: if (req->req_flags & CVS_REQF_RESP)
1.12 jfb 710: ret = cvs_getresp(root);
711:
1.13 jfb 712: return (ret);
1.12 jfb 713: }
714:
715:
716: /*
717: * cvs_getresp()
718: *
719: * Get a response from the server. This call will actually read and handle
720: * responses from the server until one of the response handlers returns
1.37 david 721: * non-zero (either an error occurred or the end of the response was reached).
1.12 jfb 722: * Returns the number of handled commands on success, or -1 on failure.
723: */
724: int
1.13 jfb 725: cvs_getresp(struct cvsroot *root)
1.12 jfb 726: {
1.13 jfb 727: int nbcmd, ret;
728: size_t len;
1.12 jfb 729:
730: nbcmd = 0;
731:
732: do {
733: /* wait for incoming data */
1.13 jfb 734: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 735: root->cr_srvout) == NULL) {
736: if (feof(root->cr_srvout))
737: return (0);
738: cvs_log(LP_ERRNO,
739: "failed to read response from server");
740: return (-1);
741: }
742:
743: if (cvs_server_outlog != NULL)
1.13 jfb 744: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 745:
1.13 jfb 746: if ((len = strlen(cvs_proto_buf)) != 0) {
1.42 joris 747: /* if len - 1 != '\n' the line is truncated */
1.43 joris 748: if (cvs_proto_buf[len - 1] == '\n')
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.38 jfb 925: cvs_log(LP_ERR, "failed to send data");
1.12 jfb 926: return (-1);
927: }
928:
929: return (0);
930: }
931:
932:
933: /*
1.13 jfb 934: * cvs_recvraw()
1.12 jfb 935: *
936: * Receive the first <len> bytes from the buffer <src> to the server.
937: */
938: ssize_t
1.13 jfb 939: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 940: {
941: size_t ret;
1.18 jfb 942: FILE *in;
943:
944: if (cvs_cmdop == CVS_OP_SERVER)
945: in = stdin;
946: else
947: in = root->cr_srvout;
1.12 jfb 948:
1.18 jfb 949: ret = fread(dst, sizeof(char), len, in);
1.12 jfb 950: if (ret == 0)
951: return (-1);
952: if (cvs_server_outlog != NULL)
953: fwrite(dst, sizeof(char), len, cvs_server_outlog);
954: return (ssize_t)ret;
955: }
956:
957:
958: /*
1.13 jfb 959: * cvs_senddir()
1.12 jfb 960: *
1.32 jfb 961: * Send a `Directory' request along with the 2 paths that follow it. If
962: * the directory info to be sent is the same as the last info sent, the
963: * call does nothing and simply returns without an error.
1.12 jfb 964: */
965: int
1.31 tedu 966: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 967: {
1.51 ! xsa 968: int l;
1.28 jfb 969: char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12 jfb 970:
1.47 jfb 971: if (dir->cf_type != DT_DIR)
972: return (-1);
973:
1.32 jfb 974: cvs_file_getpath(dir, lbuf, sizeof(lbuf));
1.45 joris 975: if (strcmp(lbuf, cvs_lastdir) == 0 && cvs_cmdop != CVS_OP_CHECKOUT)
1.32 jfb 976: return (0);
977:
1.47 jfb 978: if (dir->cf_repo == NULL)
1.28 jfb 979: strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
1.51 ! xsa 980: else {
! 981: l = snprintf(rbuf, sizeof(rbuf), "%s/%s", root->cr_dir,
1.47 jfb 982: dir->cf_repo);
1.51 ! xsa 983: if (l == -1 || l >= (int)sizeof(rbuf)) {
! 984: errno = ENAMETOOLONG;
! 985: cvs_log(LP_ERRNO, "%s", rbuf);
! 986: return (-1);
! 987: }
! 988: }
1.12 jfb 989:
1.28 jfb 990:
991: if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf) < 0) ||
992: (cvs_sendln(root, rbuf) < 0))
1.12 jfb 993: return (-1);
1.32 jfb 994:
995: strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
1.12 jfb 996:
997: return (0);
998: }
999:
1000:
1001: /*
1.13 jfb 1002: * cvs_sendarg()
1.12 jfb 1003: *
1004: * Send the argument <arg> to the server. The argument <append> is used to
1005: * determine if the argument should be simply appended to the last argument
1006: * sent or if it should be created as a new argument (0).
1007: */
1008: int
1.13 jfb 1009: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12 jfb 1010: {
1.13 jfb 1011: return cvs_sendreq(root, ((append == 0) ?
1012: CVS_REQ_ARGUMENT : CVS_REQ_ARGUMENTX), arg);
1.12 jfb 1013: }
1014:
1015:
1016: /*
1.13 jfb 1017: * cvs_sendentry()
1.12 jfb 1018: *
1019: * Send an `Entry' request to the server along with the mandatory fields from
1020: * the CVS entry <ent> (which are the name and revision).
1021: */
1022: int
1.47 jfb 1023: cvs_sendentry(struct cvsroot *root, const CVSFILE *file)
1.12 jfb 1024: {
1.51 ! xsa 1025: int l;
1.12 jfb 1026: char ebuf[128], numbuf[64];
1027:
1.47 jfb 1028: if (file->cf_type != DT_REG) {
1029: cvs_log(LP_ERR, "attempt to send Entry for non-regular file");
1030: return (-1);
1031: }
1.48 joris 1032:
1033: /* don't send Entry for unknown files */
1034: if (file->cf_cvstat == CVS_FST_UNKNOWN)
1035: return (0);
1.47 jfb 1036:
1.51 ! xsa 1037: l = snprintf(ebuf, sizeof(ebuf), "/%s/%s%s///", file->cf_name,
1.49 joris 1038: (file->cf_cvstat == CVS_FST_REMOVED) ? "-" : "",
1.47 jfb 1039: rcsnum_tostr(file->cf_lrev, numbuf, sizeof(numbuf)));
1.51 ! xsa 1040: if (l == -1 || l >= (int)sizeof(ebuf)) {
! 1041: errno = ENAMETOOLONG;
! 1042: cvs_log(LP_ERRNO, "%s", ebuf);
! 1043: return (-1);
! 1044: }
1.12 jfb 1045:
1.13 jfb 1046: return cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12 jfb 1047: }
1048:
1049:
1050: /*
1.13 jfb 1051: * cvs_initlog()
1.12 jfb 1052: *
1053: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1054: * set. In this case, the variable's value is used as a path to which the
1055: * appropriate suffix is added (".in" for server input and ".out" for server
1056: * output.
1057: * Returns 0 on success, or -1 on failure.
1058: */
1059: static int
1.13 jfb 1060: cvs_initlog(void)
1.12 jfb 1061: {
1062: char *env, fpath[MAXPATHLEN];
1063:
1.13 jfb 1064: /* avoid doing it more than once */
1065: if (cvs_server_logon)
1066: return (0);
1067:
1.12 jfb 1068: env = getenv("CVS_CLIENT_LOG");
1069: if (env == NULL)
1070: return (0);
1071:
1072: strlcpy(fpath, env, sizeof(fpath));
1073: strlcat(fpath, ".in", sizeof(fpath));
1074: cvs_server_inlog = fopen(fpath, "w");
1075: if (cvs_server_inlog == NULL) {
1076: cvs_log(LP_ERRNO, "failed to open server input log `%s'",
1077: fpath);
1078: return (-1);
1079: }
1080:
1081: strlcpy(fpath, env, sizeof(fpath));
1082: strlcat(fpath, ".out", sizeof(fpath));
1083: cvs_server_outlog = fopen(fpath, "w");
1084: if (cvs_server_outlog == NULL) {
1085: cvs_log(LP_ERRNO, "failed to open server output log `%s'",
1086: fpath);
1087: return (-1);
1088: }
1089:
1090: /* make the streams line-buffered */
1091: setvbuf(cvs_server_inlog, NULL, _IOLBF, 0);
1092: setvbuf(cvs_server_outlog, NULL, _IOLBF, 0);
1093:
1.13 jfb 1094: cvs_server_logon = 1;
1.12 jfb 1095:
1096: return (0);
1097: }