Annotation of src/usr.bin/cvs/proto.c, Revision 1.54
1.54 ! joris 1: /* $OpenBSD: proto.c,v 1.53 2005/05/24 04:01:03 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>
1.52 joris 53: #include <pwd.h>
1.1 jfb 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.41 joris 118: { CVS_REQ_ADMIN, "admin", CVS_REQF_RESP },
1.1 jfb 119: };
120:
1.18 jfb 121: struct cvs_resp cvs_responses[] = {
122: { CVS_RESP_OK, "ok" },
123: { CVS_RESP_ERROR, "error" },
124: { CVS_RESP_VALIDREQ, "Valid-requests" },
125: { CVS_RESP_M, "M" },
126: { CVS_RESP_MBINARY, "Mbinary" },
127: { CVS_RESP_MT, "MT" },
128: { CVS_RESP_E, "E" },
129: { CVS_RESP_F, "F" },
130: { CVS_RESP_CREATED, "Created" },
131: { CVS_RESP_UPDATED, "Updated" },
132: { CVS_RESP_UPDEXIST, "Update-existing" },
133: { CVS_RESP_MERGED, "Merged" },
134: { CVS_RESP_REMOVED, "Removed" },
135: { CVS_RESP_CKSUM, "Checksum" },
136: { CVS_RESP_CLRSTATDIR, "Clear-static-directory" },
137: { CVS_RESP_SETSTATDIR, "Set-static-directory" },
138: { CVS_RESP_NEWENTRY, "New-entry" },
139: { CVS_RESP_CHECKEDIN, "Checked-in" },
140: { CVS_RESP_MODE, "Mode" },
141: { CVS_RESP_MODTIME, "Mod-time" },
142: { CVS_RESP_MODXPAND, "Module-expansion" },
143: { CVS_RESP_SETSTICKY, "Set-sticky" },
144: { CVS_RESP_CLRSTICKY, "Clear-sticky" },
145: { CVS_RESP_RCSDIFF, "Rcs-diff" },
146: { CVS_RESP_TEMPLATE, "Template" },
1.34 jfb 147: { CVS_RESP_COPYFILE, "Copy-file" },
1.1 jfb 148: };
149:
1.17 jfb 150: #define CVS_NBREQ (sizeof(cvs_requests)/sizeof(cvs_requests[0]))
151: #define CVS_NBRESP (sizeof(cvs_responses)/sizeof(cvs_responses[0]))
152:
153: /* hack to receive the remote version without outputting it */
1.18 jfb 154: u_int cvs_version_sent = 0;
1.17 jfb 155:
1.13 jfb 156: static char cvs_proto_buf[4096];
157:
158: /*
1.37 david 159: * Output files for protocol logging when the CVS_CLIENT_LOG environment
1.13 jfb 160: * variable is set.
161: */
162: static int cvs_server_logon = 0;
163: static FILE *cvs_server_inlog = NULL;
164: static FILE *cvs_server_outlog = NULL;
165:
1.28 jfb 166: static pid_t cvs_subproc_pid;
167:
1.32 jfb 168: /* last directory sent with cvs_senddir() */
169: static char cvs_lastdir[MAXPATHLEN] = "";
170:
1.13 jfb 171:
172: /*
173: * cvs_connect()
174: *
175: * Open a client connection to the cvs server whose address is given in
176: * the <root> variable. The method used to connect depends on the
177: * setting of the CVS_RSH variable.
1.17 jfb 178: * Once the connection has been established, we first send the list of
179: * responses we support and request the list of supported requests from the
180: * server. Then, a version request is sent and various global flags are sent.
1.13 jfb 181: * Returns 0 on success, or -1 on failure.
182: */
183: int
184: cvs_connect(struct cvsroot *root)
185: {
1.20 jfb 186: int argc, infd[2], outfd[2], errfd[2];
1.13 jfb 187: char *argv[16], *cvs_server_cmd, *vresp;
188:
1.36 jfb 189: if (root->cr_method == CVS_METHOD_PSERVER) {
190: cvs_log(LP_ERR, "no pserver support due to security issues");
191: return (-1);
192: } else if ((root->cr_method == CVS_METHOD_KSERVER) ||
193: (root->cr_method == CVS_METHOD_GSERVER) ||
194: (root->cr_method == CVS_METHOD_EXT) ||
195: (root->cr_method == CVS_METHOD_FORK)) {
196: cvs_log(LP_ERR, "connection method not supported yet");
197: return (-1);
198: }
1.33 jfb 199:
200: if (root->cr_flags & CVS_ROOT_CONNECTED) {
201: cvs_log(LP_NOTICE, "already connected to CVSROOT");
202: return (0);
203: }
204:
1.13 jfb 205: if (pipe(infd) == -1) {
206: cvs_log(LP_ERRNO,
207: "failed to create input pipe for client connection");
208: return (-1);
209: }
210:
211: if (pipe(outfd) == -1) {
212: cvs_log(LP_ERRNO,
213: "failed to create output pipe for client connection");
214: (void)close(infd[0]);
215: (void)close(infd[1]);
216: return (-1);
217: }
218:
1.20 jfb 219: if (pipe(errfd) == -1) {
220: cvs_log(LP_ERRNO,
221: "failed to create error pipe for client connection");
222: (void)close(infd[0]);
223: (void)close(infd[1]);
224: (void)close(outfd[0]);
225: (void)close(outfd[1]);
226: return (-1);
227: }
228:
1.28 jfb 229: cvs_subproc_pid = fork();
230: if (cvs_subproc_pid == -1) {
1.13 jfb 231: cvs_log(LP_ERRNO, "failed to fork for cvs server connection");
232: return (-1);
1.30 deraadt 233: } else if (cvs_subproc_pid == 0) {
1.13 jfb 234: if ((dup2(infd[0], STDIN_FILENO) == -1) ||
1.23 jfb 235: (dup2(outfd[1], STDOUT_FILENO) == -1)) {
1.13 jfb 236: cvs_log(LP_ERRNO,
237: "failed to setup standard streams for cvs server");
238: return (-1);
239: }
240: (void)close(infd[1]);
241: (void)close(outfd[0]);
1.20 jfb 242: (void)close(errfd[0]);
1.13 jfb 243:
244: argc = 0;
245: argv[argc++] = cvs_rsh;
246:
247: if (root->cr_user != NULL) {
248: argv[argc++] = "-l";
249: argv[argc++] = root->cr_user;
250: }
251:
252: cvs_server_cmd = getenv("CVS_SERVER");
253: if (cvs_server_cmd == NULL)
1.20 jfb 254: cvs_server_cmd = CVS_SERVER_DEFAULT;
1.13 jfb 255:
256: argv[argc++] = root->cr_host;
257: argv[argc++] = cvs_server_cmd;
258: argv[argc++] = "server";
259: argv[argc] = NULL;
260:
261: execvp(argv[0], argv);
262: cvs_log(LP_ERRNO, "failed to exec");
1.46 xsa 263: exit(1);
1.13 jfb 264: }
265:
266: /* we are the parent */
267: (void)close(infd[0]);
268: (void)close(outfd[1]);
1.20 jfb 269: (void)close(errfd[1]);
1.13 jfb 270:
271: root->cr_srvin = fdopen(infd[1], "w");
272: if (root->cr_srvin == NULL) {
273: cvs_log(LP_ERRNO, "failed to create pipe stream");
274: return (-1);
275: }
276:
277: root->cr_srvout = fdopen(outfd[0], "r");
278: if (root->cr_srvout == NULL) {
279: cvs_log(LP_ERRNO, "failed to create pipe stream");
1.20 jfb 280: return (-1);
281: }
282:
1.13 jfb 283: /* make the streams line-buffered */
284: (void)setvbuf(root->cr_srvin, NULL, _IOLBF, 0);
285: (void)setvbuf(root->cr_srvout, NULL, _IOLBF, 0);
286:
287: cvs_initlog();
288:
289: /*
290: * Send the server the list of valid responses, then ask for valid
291: * requests.
292: */
293:
1.40 jfb 294: if ((vresp = cvs_resp_getvalid()) == NULL) {
1.13 jfb 295: cvs_log(LP_ERR, "can't generate list of valid responses");
296: return (-1);
297: }
298:
299: if (cvs_sendreq(root, CVS_REQ_VALIDRESP, vresp) < 0) {
1.40 jfb 300: cvs_log(LP_ERR, "failed to get valid responses");
301: free(vresp);
302: return (-1);
1.13 jfb 303: }
304: free(vresp);
305:
306: if (cvs_sendreq(root, CVS_REQ_VALIDREQ, NULL) < 0) {
307: cvs_log(LP_ERR, "failed to get valid requests from server");
308: return (-1);
309: }
310:
1.54 ! joris 311: /* send the CVSROOT to the server unless it's an init */
! 312: if ((cvs_cmdop != CVS_OP_INIT) &&
! 313: (cvs_sendreq(root, CVS_REQ_ROOT, root->cr_dir) < 0))
! 314: return (-1);
! 315:
1.40 jfb 316: /* don't fail if this request doesn't work */
1.17 jfb 317: if (cvs_sendreq(root, CVS_REQ_VERSION, NULL) < 0)
1.40 jfb 318: cvs_log(LP_WARN, "failed to get remote version");
1.17 jfb 319:
1.13 jfb 320: /* now share our global options with the server */
1.40 jfb 321: if ((verbosity == 1) &&
322: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-q") < 0))
323: return (-1);
324: else if ((verbosity == 0) &&
325: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-Q") < 0))
326: return (-1);
327:
1.53 jfb 328: if (cvs_noexec && (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-n") < 0))
329: return (-1);
1.40 jfb 330: if (cvs_nolog && (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-l") < 0))
331: return (-1);
332: if (cvs_readonly && (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-r") < 0))
333: return (-1);
334: if (cvs_trace && (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-t") < 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: cvs_log(LP_DEBUG, "connected to %s", root->cr_host);
342:
1.33 jfb 343: root->cr_flags |= CVS_ROOT_CONNECTED;
344:
1.13 jfb 345: return (0);
346: }
347:
348:
349: /*
350: * cvs_disconnect()
351: *
352: * Disconnect from the cvs server.
353: */
354: void
355: cvs_disconnect(struct cvsroot *root)
356: {
1.33 jfb 357: if (!(root->cr_flags & CVS_ROOT_CONNECTED))
358: return;
359:
1.13 jfb 360: cvs_log(LP_DEBUG, "closing connection to %s", root->cr_host);
361: if (root->cr_srvin != NULL) {
362: (void)fclose(root->cr_srvin);
363: root->cr_srvin = NULL;
364: }
365: if (root->cr_srvout != NULL) {
366: (void)fclose(root->cr_srvout);
367: root->cr_srvout = NULL;
368: }
1.33 jfb 369:
370: root->cr_flags &= ~CVS_ROOT_CONNECTED;
1.13 jfb 371: }
372:
1.1 jfb 373:
374: /*
375: * cvs_req_getbyid()
376: *
377: */
1.13 jfb 378: struct cvs_req*
1.1 jfb 379: cvs_req_getbyid(int reqid)
380: {
381: u_int i;
382:
383: for (i = 0; i < CVS_NBREQ; i++)
384: if (cvs_requests[i].req_id == reqid)
1.13 jfb 385: return &(cvs_requests[i]);
1.31 tedu 386:
1.1 jfb 387: return (NULL);
388: }
389:
390:
391: /*
392: * cvs_req_getbyname()
393: */
1.13 jfb 394: struct cvs_req*
1.1 jfb 395: cvs_req_getbyname(const char *rname)
396: {
397: u_int i;
398:
399: for (i = 0; i < CVS_NBREQ; i++)
400: if (strcmp(cvs_requests[i].req_str, rname) == 0)
1.13 jfb 401: return &(cvs_requests[i]);
1.1 jfb 402:
1.13 jfb 403: return (NULL);
1.1 jfb 404: }
405:
406:
407: /*
408: * cvs_req_getvalid()
409: *
410: * Build a space-separated list of all the requests that this protocol
411: * implementation supports.
412: */
413: char*
414: cvs_req_getvalid(void)
415: {
416: u_int i;
417: size_t len;
418: char *vrstr;
419: BUF *buf;
420:
421: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
422: if (buf == NULL)
423: return (NULL);
424:
425: cvs_buf_set(buf, cvs_requests[0].req_str,
426: strlen(cvs_requests[0].req_str), 0);
427:
428: for (i = 1; i < CVS_NBREQ; i++) {
429: if ((cvs_buf_putc(buf, ' ') < 0) ||
430: (cvs_buf_append(buf, cvs_requests[i].req_str,
431: strlen(cvs_requests[i].req_str)) < 0)) {
432: cvs_buf_free(buf);
433: return (NULL);
434: }
435: }
436:
437: /* NUL-terminate */
438: if (cvs_buf_putc(buf, '\0') < 0) {
439: cvs_buf_free(buf);
440: return (NULL);
441: }
442:
443: len = cvs_buf_size(buf);
444: vrstr = (char *)malloc(len);
1.27 jfb 445: if (vrstr == NULL) {
446: cvs_buf_free(buf);
447: return (NULL);
448: }
1.1 jfb 449:
450: cvs_buf_copy(buf, 0, vrstr, len);
451: cvs_buf_free(buf);
452:
453: return (vrstr);
454: }
455:
456:
457: /*
458: * cvs_resp_getbyid()
459: *
460: */
1.13 jfb 461: struct cvs_resp*
1.1 jfb 462: cvs_resp_getbyid(int respid)
463: {
464: u_int i;
465:
1.44 tedu 466: for (i = 0; i < CVS_NBRESP; i++)
1.13 jfb 467: if (cvs_responses[i].resp_id == (u_int)respid)
468: return &(cvs_responses[i]);
1.31 tedu 469:
1.1 jfb 470: return (NULL);
471: }
472:
473:
474: /*
475: * cvs_resp_getbyname()
476: */
1.13 jfb 477: struct cvs_resp*
1.1 jfb 478: cvs_resp_getbyname(const char *rname)
479: {
480: u_int i;
481:
1.44 tedu 482: for (i = 0; i < CVS_NBRESP; i++)
1.1 jfb 483: if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13 jfb 484: return &(cvs_responses[i]);
1.1 jfb 485:
1.13 jfb 486: return (NULL);
1.1 jfb 487: }
488:
489:
490: /*
491: * cvs_resp_getvalid()
492: *
493: * Build a space-separated list of all the responses that this protocol
494: * implementation supports.
495: */
496: char*
497: cvs_resp_getvalid(void)
498: {
499: u_int i;
500: size_t len;
501: char *vrstr;
502: BUF *buf;
503:
504: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
505: if (buf == NULL)
506: return (NULL);
507:
508: cvs_buf_set(buf, cvs_responses[0].resp_str,
509: strlen(cvs_responses[0].resp_str), 0);
510:
511: for (i = 1; i < CVS_NBRESP; i++) {
512: if ((cvs_buf_putc(buf, ' ') < 0) ||
513: (cvs_buf_append(buf, cvs_responses[i].resp_str,
514: strlen(cvs_responses[i].resp_str)) < 0)) {
515: cvs_buf_free(buf);
516: return (NULL);
517: }
518: }
519:
520: /* NUL-terminate */
521: if (cvs_buf_putc(buf, '\0') < 0) {
522: cvs_buf_free(buf);
523: return (NULL);
524: }
525:
526: len = cvs_buf_size(buf);
527: vrstr = (char *)malloc(len);
1.27 jfb 528: if (vrstr == NULL) {
529: cvs_buf_free(buf);
530: return (NULL);
531: }
1.1 jfb 532:
533: cvs_buf_copy(buf, 0, vrstr, len);
534: cvs_buf_free(buf);
535:
536: return (vrstr);
537: }
538:
539:
540: /*
541: * cvs_sendfile()
542: *
543: * Send the mode and size of a file followed by the file's contents.
544: * Returns 0 on success, or -1 on failure.
545: */
546: int
1.13 jfb 547: cvs_sendfile(struct cvsroot *root, const char *path)
1.1 jfb 548: {
1.51 xsa 549: int fd, l;
1.1 jfb 550: ssize_t ret;
551: char buf[4096];
552: struct stat st;
553:
1.38 jfb 554: cvs_log(LP_TRACE, "Sending file `%s' to server", basename(path));
555:
1.1 jfb 556: if (stat(path, &st) == -1) {
557: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
558: return (-1);
559: }
560:
1.24 jfb 561: if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
562: return (-1);
563:
1.1 jfb 564: fd = open(path, O_RDONLY, 0);
565: if (fd == -1) {
1.38 jfb 566: cvs_log(LP_ERRNO, "failed to open `%s'", path);
1.1 jfb 567: return (-1);
568: }
569:
1.38 jfb 570: if (cvs_sendln(root, buf) < 0) {
571: (void)close(fd);
572: return (-1);
573: }
1.51 xsa 574: l = snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
575: if (l == -1 || l >= (int)sizeof(buf))
576: return (-1);
577:
1.38 jfb 578: if (cvs_sendln(root, buf) < 0) {
579: (void)close(fd);
580: return (-1);
581: }
1.1 jfb 582:
583: while ((ret = read(fd, buf, sizeof(buf))) != 0) {
584: if (ret == -1) {
1.24 jfb 585: (void)close(fd);
1.1 jfb 586: cvs_log(LP_ERRNO, "failed to read file `%s'", path);
587: return (-1);
588: }
589:
1.38 jfb 590: if (cvs_sendraw(root, buf, (size_t)ret) < 0) {
591: (void)close(fd);
592: return (-1);
593: }
1.1 jfb 594: }
595:
596: (void)close(fd);
597: return (0);
598: }
599:
600:
601: /*
602: * cvs_recvfile()
603: *
604: * Receive the mode and size of a file followed the file's contents and
605: * create or update the file whose path is <path> with the received
606: * information.
607: */
1.14 jfb 608: BUF*
609: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 610: {
611: size_t len;
612: ssize_t ret;
613: off_t fsz, cnt;
614: char buf[4096], *ep;
1.14 jfb 615: BUF *fbuf;
616:
617: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
618: if (fbuf == NULL)
619: return (NULL);
1.1 jfb 620:
1.13 jfb 621: if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14 jfb 622: (cvs_strtomode(buf, mode) < 0)) {
1.44 tedu 623: cvs_buf_free(fbuf);
1.14 jfb 624: return (NULL);
1.1 jfb 625: }
626:
1.50 joris 627: if (cvs_getln(root, buf, sizeof(buf)) < 0) {
628: cvs_buf_free(fbuf);
629: return (NULL);
630: }
1.1 jfb 631:
632: fsz = (off_t)strtol(buf, &ep, 10);
633: if (*ep != '\0') {
634: cvs_log(LP_ERR, "parse error in file size transmission");
1.44 tedu 635: cvs_buf_free(fbuf);
1.14 jfb 636: return (NULL);
1.1 jfb 637: }
638:
639: cnt = 0;
640: do {
641: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 642: if (len == 0)
643: break;
1.13 jfb 644: ret = cvs_recvraw(root, buf, len);
1.1 jfb 645: if (ret == -1) {
1.14 jfb 646: cvs_buf_free(fbuf);
647: return (NULL);
1.1 jfb 648: }
649:
1.14 jfb 650: if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
651: cvs_log(LP_ERR,
652: "failed to append received file data");
653: cvs_buf_free(fbuf);
654: return (NULL);
1.1 jfb 655: }
656:
657: cnt += (off_t)ret;
658: } while (cnt < fsz);
659:
1.14 jfb 660: return (fbuf);
1.1 jfb 661: }
1.12 jfb 662:
663:
664: /*
665: * cvs_sendreq()
666: *
667: * Send a request to the server of type <rid>, with optional arguments
668: * contained in <arg>, which should not be terminated by a newline.
669: * Returns 0 on success, or -1 on failure.
670: */
671: int
1.13 jfb 672: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 673: {
1.51 xsa 674: int ret, l;
1.13 jfb 675: struct cvs_req *req;
1.12 jfb 676:
677: if (root->cr_srvin == NULL) {
1.36 jfb 678: cvs_log(LP_ERR, "cannot send request %u: Not connected", rid);
1.12 jfb 679: return (-1);
680: }
681:
1.13 jfb 682: req = cvs_req_getbyid(rid);
683: if (req == NULL) {
1.12 jfb 684: cvs_log(LP_ERR, "unsupported request type %u", rid);
685: return (-1);
686: }
687:
1.18 jfb 688: /* is this request supported by the server? */
689: if (!CVS_GETVR(root, req->req_id)) {
1.25 jfb 690: cvs_log(LP_WARN, "remote end does not support request `%s'",
1.18 jfb 691: req->req_str);
692: return (-1);
693: }
694:
1.51 xsa 695: l = snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
1.13 jfb 696: req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.51 xsa 697: if (l == -1 || l >= (int)sizeof(cvs_proto_buf))
698: return (-1);
1.12 jfb 699:
700: if (cvs_server_inlog != NULL)
1.13 jfb 701: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 702:
1.13 jfb 703: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12 jfb 704: if (ret == EOF) {
705: cvs_log(LP_ERRNO, "failed to send request to server");
706: return (-1);
707: }
1.17 jfb 708:
709: if (rid == CVS_REQ_VERSION)
710: cvs_version_sent = 1;
1.12 jfb 711:
1.13 jfb 712: if (req->req_flags & CVS_REQF_RESP)
1.12 jfb 713: ret = cvs_getresp(root);
714:
1.13 jfb 715: return (ret);
1.12 jfb 716: }
717:
718:
719: /*
720: * cvs_getresp()
721: *
722: * Get a response from the server. This call will actually read and handle
723: * responses from the server until one of the response handlers returns
1.37 david 724: * non-zero (either an error occurred or the end of the response was reached).
1.12 jfb 725: * Returns the number of handled commands on success, or -1 on failure.
726: */
727: int
1.13 jfb 728: cvs_getresp(struct cvsroot *root)
1.12 jfb 729: {
1.13 jfb 730: int nbcmd, ret;
731: size_t len;
1.12 jfb 732:
733: nbcmd = 0;
734:
735: do {
736: /* wait for incoming data */
1.13 jfb 737: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 738: root->cr_srvout) == NULL) {
739: if (feof(root->cr_srvout))
740: return (0);
741: cvs_log(LP_ERRNO,
742: "failed to read response from server");
743: return (-1);
744: }
745:
746: if (cvs_server_outlog != NULL)
1.13 jfb 747: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 748:
1.13 jfb 749: if ((len = strlen(cvs_proto_buf)) != 0) {
1.42 joris 750: /* if len - 1 != '\n' the line is truncated */
1.43 joris 751: if (cvs_proto_buf[len - 1] == '\n')
1.13 jfb 752: cvs_proto_buf[--len] = '\0';
1.12 jfb 753: }
754:
1.13 jfb 755: ret = cvs_resp_handle(root, cvs_proto_buf);
1.12 jfb 756: nbcmd++;
757: } while (ret == 0);
758:
759: if (ret > 0)
760: ret = nbcmd;
761: return (ret);
762: }
763:
764:
765: /*
1.13 jfb 766: * cvs_getln()
767: *
1.18 jfb 768: * Get a line from the remote end and store it in <lbuf>. The terminating
1.13 jfb 769: * newline character is stripped from the result.
770: */
771: int
772: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
773: {
774: size_t rlen;
1.18 jfb 775: FILE *in;
1.13 jfb 776:
1.18 jfb 777: if (cvs_cmdop == CVS_OP_SERVER)
778: in = stdin;
779: else
780: in = root->cr_srvout;
781:
782: if (fgets(lbuf, len, in) == NULL) {
783: if (ferror(in)) {
784: cvs_log(LP_ERRNO, "failed to read line");
1.13 jfb 785: return (-1);
786: }
787:
1.18 jfb 788: if (feof(in))
1.13 jfb 789: *lbuf = '\0';
790: }
791:
792: if (cvs_server_outlog != NULL)
793: fputs(lbuf, cvs_server_outlog);
794:
795: rlen = strlen(lbuf);
796: if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
797: lbuf[--rlen] = '\0';
798:
799: return (0);
800: }
801:
1.19 jfb 802:
1.13 jfb 803: /*
1.12 jfb 804: * cvs_sendresp()
805: *
1.19 jfb 806: * Send a response of type <rid> to the client, with optional arguments
1.12 jfb 807: * contained in <arg>, which should not be terminated by a newline.
808: * Returns 0 on success, or -1 on failure.
809: */
810: int
811: cvs_sendresp(u_int rid, const char *arg)
812: {
813: int ret;
1.19 jfb 814: struct cvs_resp *resp;
1.12 jfb 815:
816: resp = cvs_resp_getbyid(rid);
1.13 jfb 817: if (resp == NULL) {
1.12 jfb 818: cvs_log(LP_ERR, "unsupported response type %u", rid);
819: return (-1);
820: }
821:
1.19 jfb 822: ret = fputs(resp->resp_str, stdout);
1.12 jfb 823: if (ret == EOF) {
824: cvs_log(LP_ERRNO, "failed to send response to client");
1.30 deraadt 825: } else {
1.19 jfb 826: if (arg != NULL) {
827: putc(' ', stdout);
828: fputs(arg, stdout);
829: }
1.12 jfb 830: putc('\n', stdout);
831: }
832: return (0);
833: }
834:
835:
1.19 jfb 836: #ifdef notyet
1.12 jfb 837: /*
838: * cvs_getreq()
839: *
840: * Get a request from the client.
841: */
842: int
843: cvs_getreq(void)
844: {
845: int nbcmd;
846:
847: nbcmd = 0;
848:
849: do {
850: /* wait for incoming data */
1.13 jfb 851: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 852: stdin) == NULL) {
853: if (feof(stdin))
854: return (0);
855: cvs_log(LP_ERRNO,
856: "failed to read request from client");
857: return (-1);
858: }
859:
1.13 jfb 860: if ((len = strlen(cvs_proto_buf)) != 0) {
861: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 862: /* truncated line */
1.30 deraadt 863: } else
1.13 jfb 864: cvs_proto_buf[--len] = '\0';
1.12 jfb 865: }
866:
1.13 jfb 867: ret = cvs_resp_handle(cvs_proto_buf);
1.12 jfb 868: } while (ret == 0);
869: }
870: #endif
871:
872:
873: /*
874: * cvs_sendln()
875: *
1.18 jfb 876: * Send a single line <line> string to the remote end. The line is sent as is,
1.12 jfb 877: * without any modifications.
878: * Returns 0 on success, or -1 on failure.
879: */
880: int
881: cvs_sendln(struct cvsroot *root, const char *line)
882: {
883: int nl;
884: size_t len;
1.18 jfb 885: FILE *out;
886:
887: if (cvs_cmdop == CVS_OP_SERVER)
888: out = stdout;
889: else
890: out = root->cr_srvin;
1.12 jfb 891:
892: nl = 0;
893: len = strlen(line);
894:
895: if ((len > 0) && (line[len - 1] != '\n'))
896: nl = 1;
897:
898: if (cvs_server_inlog != NULL) {
899: fputs(line, cvs_server_inlog);
900: if (nl)
901: fputc('\n', cvs_server_inlog);
902: }
1.18 jfb 903: fputs(line, out);
1.12 jfb 904: if (nl)
1.18 jfb 905: fputc('\n', out);
1.12 jfb 906: return (0);
907: }
908:
909:
910: /*
1.13 jfb 911: * cvs_sendraw()
1.12 jfb 912: *
913: * Send the first <len> bytes from the buffer <src> to the server.
914: */
915: int
1.13 jfb 916: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12 jfb 917: {
1.18 jfb 918: FILE *out;
919:
920: if (cvs_cmdop == CVS_OP_SERVER)
921: out = stdout;
922: else
923: out = root->cr_srvin;
924:
1.12 jfb 925: if (cvs_server_inlog != NULL)
926: fwrite(src, sizeof(char), len, cvs_server_inlog);
1.18 jfb 927: if (fwrite(src, sizeof(char), len, out) < len) {
1.38 jfb 928: cvs_log(LP_ERR, "failed to send data");
1.12 jfb 929: return (-1);
930: }
931:
932: return (0);
933: }
934:
935:
936: /*
1.13 jfb 937: * cvs_recvraw()
1.12 jfb 938: *
939: * Receive the first <len> bytes from the buffer <src> to the server.
940: */
941: ssize_t
1.13 jfb 942: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 943: {
944: size_t ret;
1.18 jfb 945: FILE *in;
946:
947: if (cvs_cmdop == CVS_OP_SERVER)
948: in = stdin;
949: else
950: in = root->cr_srvout;
1.12 jfb 951:
1.18 jfb 952: ret = fread(dst, sizeof(char), len, in);
1.12 jfb 953: if (ret == 0)
954: return (-1);
955: if (cvs_server_outlog != NULL)
956: fwrite(dst, sizeof(char), len, cvs_server_outlog);
957: return (ssize_t)ret;
958: }
959:
960:
961: /*
1.13 jfb 962: * cvs_senddir()
1.12 jfb 963: *
1.32 jfb 964: * Send a `Directory' request along with the 2 paths that follow it. If
965: * the directory info to be sent is the same as the last info sent, the
966: * call does nothing and simply returns without an error.
1.12 jfb 967: */
968: int
1.31 tedu 969: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 970: {
1.51 xsa 971: int l;
1.28 jfb 972: char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12 jfb 973:
1.47 jfb 974: if (dir->cf_type != DT_DIR)
975: return (-1);
976:
1.32 jfb 977: cvs_file_getpath(dir, lbuf, sizeof(lbuf));
1.45 joris 978: if (strcmp(lbuf, cvs_lastdir) == 0 && cvs_cmdop != CVS_OP_CHECKOUT)
1.32 jfb 979: return (0);
980:
1.47 jfb 981: if (dir->cf_repo == NULL)
1.28 jfb 982: strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
1.51 xsa 983: else {
984: l = snprintf(rbuf, sizeof(rbuf), "%s/%s", root->cr_dir,
1.47 jfb 985: dir->cf_repo);
1.51 xsa 986: if (l == -1 || l >= (int)sizeof(rbuf)) {
987: errno = ENAMETOOLONG;
988: cvs_log(LP_ERRNO, "%s", rbuf);
989: return (-1);
990: }
991: }
1.12 jfb 992:
1.28 jfb 993:
994: if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf) < 0) ||
995: (cvs_sendln(root, rbuf) < 0))
1.12 jfb 996: return (-1);
1.32 jfb 997:
998: strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
1.12 jfb 999:
1000: return (0);
1001: }
1002:
1003:
1004: /*
1.13 jfb 1005: * cvs_sendarg()
1.12 jfb 1006: *
1007: * Send the argument <arg> to the server. The argument <append> is used to
1008: * determine if the argument should be simply appended to the last argument
1009: * sent or if it should be created as a new argument (0).
1010: */
1011: int
1.13 jfb 1012: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12 jfb 1013: {
1.13 jfb 1014: return cvs_sendreq(root, ((append == 0) ?
1015: CVS_REQ_ARGUMENT : CVS_REQ_ARGUMENTX), arg);
1.12 jfb 1016: }
1017:
1018:
1019: /*
1.13 jfb 1020: * cvs_sendentry()
1.12 jfb 1021: *
1022: * Send an `Entry' request to the server along with the mandatory fields from
1023: * the CVS entry <ent> (which are the name and revision).
1024: */
1025: int
1.47 jfb 1026: cvs_sendentry(struct cvsroot *root, const CVSFILE *file)
1.12 jfb 1027: {
1.51 xsa 1028: int l;
1.12 jfb 1029: char ebuf[128], numbuf[64];
1030:
1.47 jfb 1031: if (file->cf_type != DT_REG) {
1032: cvs_log(LP_ERR, "attempt to send Entry for non-regular file");
1033: return (-1);
1034: }
1.48 joris 1035:
1036: /* don't send Entry for unknown files */
1037: if (file->cf_cvstat == CVS_FST_UNKNOWN)
1038: return (0);
1.47 jfb 1039:
1.51 xsa 1040: l = snprintf(ebuf, sizeof(ebuf), "/%s/%s%s///", file->cf_name,
1.49 joris 1041: (file->cf_cvstat == CVS_FST_REMOVED) ? "-" : "",
1.47 jfb 1042: rcsnum_tostr(file->cf_lrev, numbuf, sizeof(numbuf)));
1.51 xsa 1043: if (l == -1 || l >= (int)sizeof(ebuf)) {
1044: errno = ENAMETOOLONG;
1045: cvs_log(LP_ERRNO, "%s", ebuf);
1046: return (-1);
1047: }
1.12 jfb 1048:
1.13 jfb 1049: return cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12 jfb 1050: }
1051:
1052:
1053: /*
1.13 jfb 1054: * cvs_initlog()
1.12 jfb 1055: *
1056: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1057: * set. In this case, the variable's value is used as a path to which the
1058: * appropriate suffix is added (".in" for server input and ".out" for server
1059: * output.
1060: * Returns 0 on success, or -1 on failure.
1061: */
1062: static int
1.13 jfb 1063: cvs_initlog(void)
1.12 jfb 1064: {
1.52 joris 1065: int l;
1066: u_int i;
1067: char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
1068: char rpath[MAXPATHLEN], *s;
1069: struct stat st;
1070: time_t now;
1071: struct passwd *pwd;
1.12 jfb 1072:
1.13 jfb 1073: /* avoid doing it more than once */
1074: if (cvs_server_logon)
1075: return (0);
1076:
1.12 jfb 1077: env = getenv("CVS_CLIENT_LOG");
1078: if (env == NULL)
1079: return (0);
1080:
1.52 joris 1081: if ((envdup = strdup(env)) == NULL)
1082: return (-1);
1083:
1084: if ((s = strchr(envdup, '%')) != NULL)
1085: *s = '\0';
1086:
1087: strlcpy(buf, env, sizeof(buf));
1088: strlcpy(rpath, envdup, sizeof(rpath));
1089:
1090: s = buf;
1091: while ((s = strchr(s, '%')) != NULL) {
1092: *s++;
1093: switch (*s) {
1094: case 'c':
1095: strlcpy(fpath, cvs_command, sizeof(fpath));
1096: break;
1097: case 'd':
1098: time(&now);
1099: snprintf(fpath, sizeof(fpath), "%s", ctime(&now));
1100: break;
1101: case 'p':
1102: snprintf(fpath, sizeof(fpath), "%d", getpid());
1103: break;
1104: case 'u':
1105: if ((pwd = getpwuid(getuid())) != NULL)
1106: strlcpy(fpath, pwd->pw_name, sizeof(fpath));
1107: else
1108: fpath[0] = '\0';
1109: endpwent();
1110: break;
1111: default:
1112: fpath[0] = '\0';
1113: break;
1114: }
1115:
1116: if (fpath[0] != '\0') {
1117: strlcat(rpath, "-", sizeof(rpath));
1118: strlcat(rpath, fpath, sizeof(rpath));
1119: }
1120: }
1121:
1122: for (i = 0; i < UINT_MAX; i++) {
1123: l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1124: if (l == -1 || l >= (int)sizeof(fpath)) {
1125: errno = ENAMETOOLONG;
1126: cvs_log(LP_ERRNO, "%s", fpath);
1127: return (-1);
1128: }
1129:
1130: if (stat(fpath, &st) != -1)
1131: continue;
1132:
1133: if (errno != ENOENT)
1134: return (-1);
1135:
1136: break;
1137: }
1138:
1.12 jfb 1139: cvs_server_inlog = fopen(fpath, "w");
1140: if (cvs_server_inlog == NULL) {
1141: cvs_log(LP_ERRNO, "failed to open server input log `%s'",
1142: fpath);
1143: return (-1);
1144: }
1145:
1.52 joris 1146: for (i = 0; i < UINT_MAX; i++) {
1147: l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1148: if (l == -1 || l >= (int)sizeof(fpath)) {
1149: errno = ENAMETOOLONG;
1150: cvs_log(LP_ERRNO, "%s", fpath);
1151: return (-1);
1152: }
1153:
1154: if (stat(fpath, &st) != -1)
1155: continue;
1156:
1157: if (errno != ENOENT)
1158: return (-1);
1159:
1160: break;
1161: }
1162:
1.12 jfb 1163: cvs_server_outlog = fopen(fpath, "w");
1164: if (cvs_server_outlog == NULL) {
1165: cvs_log(LP_ERRNO, "failed to open server output log `%s'",
1166: fpath);
1167: return (-1);
1168: }
1169:
1170: /* make the streams line-buffered */
1171: setvbuf(cvs_server_inlog, NULL, _IOLBF, 0);
1172: setvbuf(cvs_server_outlog, NULL, _IOLBF, 0);
1173:
1.13 jfb 1174: cvs_server_logon = 1;
1.12 jfb 1175:
1176: return (0);
1177: }