Annotation of src/usr.bin/cvs/proto.c, Revision 1.75
1.75 ! xsa 1: /* $OpenBSD: proto.c,v 1.74 2005/08/10 14:49:20 xsa 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:
1.72 joris 32: * http://www.iam.unibe.ch/~til/documentation/cvs/cvsclient_toc.html
33: * http://www.elegosoft.com/cvs/cvsclient_toc.html
1.1 jfb 34: *
35: * The protocol is split up into two parts; the first part is the client side
36: * of things and is composed of all the response handlers, which are all named
37: * with a prefix of "cvs_resp_". The second part is the set of request
38: * handlers used by the server. These handlers process the request and
39: * generate the appropriate response to send back. The prefix for request
40: * handlers is "cvs_req_".
41: *
42: */
43:
44: #include <sys/types.h>
45: #include <sys/stat.h>
46:
1.56 xsa 47: #include <errno.h>
1.1 jfb 48: #include <fcntl.h>
1.56 xsa 49: #include <libgen.h>
50: #include <pwd.h>
1.1 jfb 51: #include <stdio.h>
52: #include <stdlib.h>
1.56 xsa 53: #include <string.h>
1.1 jfb 54: #include <unistd.h>
55:
56: #include "cvs.h"
57: #include "log.h"
1.13 jfb 58: #include "proto.h"
1.1 jfb 59:
60:
1.11 jfb 61: /* request flags */
1.68 xsa 62: #define CVS_REQF_RESP 0x01
1.11 jfb 63:
64:
1.68 xsa 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" },
1.63 joris 134: { CVS_RESP_RMENTRY, "Remove-entry" },
1.18 jfb 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.68 xsa 150: #define CVS_NBREQ (sizeof(cvs_requests)/sizeof(cvs_requests[0]))
151: #define CVS_NBRESP (sizeof(cvs_responses)/sizeof(cvs_responses[0]))
1.17 jfb 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.71 xsa 187: char *argv[16], *cvs_server_cmd, tmsg[1024], *vresp;
1.13 jfb 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;
1.71 xsa 260:
261: if (cvs_trace == 1) {
262: tmsg[0] = '\0';
263: for (argc = 0; argv[argc] != NULL; argc++) {
264: strlcat(tmsg, argv[argc], sizeof(tmsg));
265: strlcat(tmsg, " ", sizeof(tmsg));
266: }
267: }
268: cvs_log(LP_TRACE, "Starting server: %s", tmsg);
1.13 jfb 269:
270: execvp(argv[0], argv);
271: cvs_log(LP_ERRNO, "failed to exec");
1.73 xsa 272: exit(CVS_EX_PROTO);
1.13 jfb 273: }
274:
275: /* we are the parent */
276: (void)close(infd[0]);
277: (void)close(outfd[1]);
1.20 jfb 278: (void)close(errfd[1]);
1.13 jfb 279:
280: root->cr_srvin = fdopen(infd[1], "w");
281: if (root->cr_srvin == NULL) {
282: cvs_log(LP_ERRNO, "failed to create pipe stream");
283: return (-1);
284: }
285:
286: root->cr_srvout = fdopen(outfd[0], "r");
287: if (root->cr_srvout == NULL) {
288: cvs_log(LP_ERRNO, "failed to create pipe stream");
1.20 jfb 289: return (-1);
290: }
291:
1.13 jfb 292: /* make the streams line-buffered */
1.75 ! xsa 293: (void)setvbuf(root->cr_srvin, NULL, _IOLBF, (size_t)0);
! 294: (void)setvbuf(root->cr_srvout, NULL, _IOLBF, (size_t)0);
1.13 jfb 295:
296: cvs_initlog();
297:
298: /*
299: * Send the server the list of valid responses, then ask for valid
300: * requests.
301: */
302:
1.40 jfb 303: if ((vresp = cvs_resp_getvalid()) == NULL) {
1.13 jfb 304: cvs_log(LP_ERR, "can't generate list of valid responses");
305: return (-1);
306: }
307:
308: if (cvs_sendreq(root, CVS_REQ_VALIDRESP, vresp) < 0) {
1.40 jfb 309: cvs_log(LP_ERR, "failed to get valid responses");
310: free(vresp);
311: return (-1);
1.13 jfb 312: }
313: free(vresp);
314:
315: if (cvs_sendreq(root, CVS_REQ_VALIDREQ, NULL) < 0) {
316: cvs_log(LP_ERR, "failed to get valid requests from server");
317: return (-1);
318: }
319:
1.62 xsa 320: /* send the CVSROOT to the server */
321: if (cvs_sendreq(root, CVS_REQ_ROOT, root->cr_dir) < 0)
1.54 joris 322: return (-1);
323:
1.40 jfb 324: /* don't fail if this request doesn't work */
1.17 jfb 325: if (cvs_sendreq(root, CVS_REQ_VERSION, NULL) < 0)
1.40 jfb 326: cvs_log(LP_WARN, "failed to get remote version");
1.17 jfb 327:
1.13 jfb 328: /* now share our global options with the server */
1.61 joris 329: if ((verbosity <= 1) &&
1.40 jfb 330: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-q") < 0))
331: return (-1);
1.61 joris 332: if ((verbosity == 0) &&
1.40 jfb 333: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-Q") < 0))
334: return (-1);
335:
1.66 xsa 336: if ((cvs_noexec == 1) &&
337: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-n") < 0))
1.53 jfb 338: return (-1);
1.66 xsa 339: if ((cvs_nolog == 1) &&
340: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-l") < 0))
1.40 jfb 341: return (-1);
1.66 xsa 342: if ((cvs_readonly == 1) &&
343: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-r") < 0))
1.40 jfb 344: return (-1);
1.66 xsa 345: if ((cvs_trace == 1) &&
346: (cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-t") < 0))
1.13 jfb 347: return (-1);
348:
349: /* not sure why, but we have to send this */
350: if (cvs_sendreq(root, CVS_REQ_USEUNCHANGED, NULL) < 0)
351: return (-1);
352:
353: cvs_log(LP_DEBUG, "connected to %s", root->cr_host);
354:
1.33 jfb 355: root->cr_flags |= CVS_ROOT_CONNECTED;
356:
1.13 jfb 357: return (0);
358: }
359:
360:
361: /*
362: * cvs_disconnect()
363: *
364: * Disconnect from the cvs server.
365: */
366: void
367: cvs_disconnect(struct cvsroot *root)
368: {
1.33 jfb 369: if (!(root->cr_flags & CVS_ROOT_CONNECTED))
370: return;
371:
1.13 jfb 372: cvs_log(LP_DEBUG, "closing connection to %s", root->cr_host);
373: if (root->cr_srvin != NULL) {
374: (void)fclose(root->cr_srvin);
375: root->cr_srvin = NULL;
376: }
377: if (root->cr_srvout != NULL) {
378: (void)fclose(root->cr_srvout);
379: root->cr_srvout = NULL;
380: }
1.33 jfb 381:
382: root->cr_flags &= ~CVS_ROOT_CONNECTED;
1.13 jfb 383: }
384:
1.1 jfb 385:
386: /*
387: * cvs_req_getbyid()
388: *
389: */
1.13 jfb 390: struct cvs_req*
1.1 jfb 391: cvs_req_getbyid(int reqid)
392: {
393: u_int i;
394:
395: for (i = 0; i < CVS_NBREQ; i++)
396: if (cvs_requests[i].req_id == reqid)
1.13 jfb 397: return &(cvs_requests[i]);
1.31 tedu 398:
1.1 jfb 399: return (NULL);
400: }
401:
402:
403: /*
404: * cvs_req_getbyname()
405: */
1.13 jfb 406: struct cvs_req*
1.1 jfb 407: cvs_req_getbyname(const char *rname)
408: {
409: u_int i;
410:
411: for (i = 0; i < CVS_NBREQ; i++)
412: if (strcmp(cvs_requests[i].req_str, rname) == 0)
1.13 jfb 413: return &(cvs_requests[i]);
1.1 jfb 414:
1.13 jfb 415: return (NULL);
1.1 jfb 416: }
417:
418:
419: /*
420: * cvs_req_getvalid()
421: *
422: * Build a space-separated list of all the requests that this protocol
423: * implementation supports.
424: */
1.67 xsa 425: char *
1.1 jfb 426: cvs_req_getvalid(void)
427: {
428: u_int i;
429: size_t len;
430: char *vrstr;
431: BUF *buf;
432:
1.75 ! xsa 433: buf = cvs_buf_alloc((size_t)512, BUF_AUTOEXT);
1.1 jfb 434: if (buf == NULL)
435: return (NULL);
436:
437: cvs_buf_set(buf, cvs_requests[0].req_str,
1.75 ! xsa 438: strlen(cvs_requests[0].req_str), (size_t)0);
1.1 jfb 439:
440: for (i = 1; i < CVS_NBREQ; i++) {
441: if ((cvs_buf_putc(buf, ' ') < 0) ||
442: (cvs_buf_append(buf, cvs_requests[i].req_str,
443: strlen(cvs_requests[i].req_str)) < 0)) {
444: cvs_buf_free(buf);
445: return (NULL);
446: }
447: }
448:
449: /* NUL-terminate */
450: if (cvs_buf_putc(buf, '\0') < 0) {
451: cvs_buf_free(buf);
452: return (NULL);
453: }
454:
1.70 moritz 455: len = cvs_buf_len(buf);
1.1 jfb 456: vrstr = (char *)malloc(len);
1.27 jfb 457: if (vrstr == NULL) {
458: cvs_buf_free(buf);
459: return (NULL);
460: }
1.1 jfb 461:
1.75 ! xsa 462: cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1 jfb 463: cvs_buf_free(buf);
464:
465: return (vrstr);
466: }
467:
468:
469: /*
470: * cvs_resp_getbyid()
471: *
472: */
1.13 jfb 473: struct cvs_resp*
1.1 jfb 474: cvs_resp_getbyid(int respid)
475: {
476: u_int i;
477:
1.44 tedu 478: for (i = 0; i < CVS_NBRESP; i++)
1.13 jfb 479: if (cvs_responses[i].resp_id == (u_int)respid)
480: return &(cvs_responses[i]);
1.31 tedu 481:
1.1 jfb 482: return (NULL);
483: }
484:
485:
486: /*
487: * cvs_resp_getbyname()
488: */
1.67 xsa 489: struct cvs_resp *
1.1 jfb 490: cvs_resp_getbyname(const char *rname)
491: {
492: u_int i;
493:
1.44 tedu 494: for (i = 0; i < CVS_NBRESP; i++)
1.1 jfb 495: if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13 jfb 496: return &(cvs_responses[i]);
1.1 jfb 497:
1.13 jfb 498: return (NULL);
1.1 jfb 499: }
500:
501:
502: /*
503: * cvs_resp_getvalid()
504: *
505: * Build a space-separated list of all the responses that this protocol
506: * implementation supports.
507: */
1.67 xsa 508: char *
1.1 jfb 509: cvs_resp_getvalid(void)
510: {
511: u_int i;
512: size_t len;
513: char *vrstr;
514: BUF *buf;
515:
1.75 ! xsa 516: buf = cvs_buf_alloc((size_t)512, BUF_AUTOEXT);
1.1 jfb 517: if (buf == NULL)
518: return (NULL);
519:
520: cvs_buf_set(buf, cvs_responses[0].resp_str,
1.75 ! xsa 521: strlen(cvs_responses[0].resp_str), (size_t)0);
1.1 jfb 522:
523: for (i = 1; i < CVS_NBRESP; i++) {
524: if ((cvs_buf_putc(buf, ' ') < 0) ||
525: (cvs_buf_append(buf, cvs_responses[i].resp_str,
526: strlen(cvs_responses[i].resp_str)) < 0)) {
527: cvs_buf_free(buf);
528: return (NULL);
529: }
530: }
531:
532: /* NUL-terminate */
533: if (cvs_buf_putc(buf, '\0') < 0) {
534: cvs_buf_free(buf);
535: return (NULL);
536: }
537:
1.70 moritz 538: len = cvs_buf_len(buf);
1.1 jfb 539: vrstr = (char *)malloc(len);
1.27 jfb 540: if (vrstr == NULL) {
541: cvs_buf_free(buf);
542: return (NULL);
543: }
1.1 jfb 544:
1.75 ! xsa 545: cvs_buf_copy(buf, (size_t)0, vrstr, len);
1.1 jfb 546: cvs_buf_free(buf);
547:
548: return (vrstr);
549: }
550:
551:
552: /*
553: * cvs_sendfile()
554: *
555: * Send the mode and size of a file followed by the file's contents.
556: * Returns 0 on success, or -1 on failure.
557: */
558: int
1.13 jfb 559: cvs_sendfile(struct cvsroot *root, const char *path)
1.1 jfb 560: {
1.51 xsa 561: int fd, l;
1.1 jfb 562: ssize_t ret;
563: char buf[4096];
564: struct stat st;
565:
1.38 jfb 566: cvs_log(LP_TRACE, "Sending file `%s' to server", basename(path));
567:
1.1 jfb 568: if (stat(path, &st) == -1) {
569: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
570: return (-1);
571: }
572:
1.24 jfb 573: if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
574: return (-1);
575:
1.1 jfb 576: fd = open(path, O_RDONLY, 0);
577: if (fd == -1) {
1.38 jfb 578: cvs_log(LP_ERRNO, "failed to open `%s'", path);
1.1 jfb 579: return (-1);
580: }
581:
1.38 jfb 582: if (cvs_sendln(root, buf) < 0) {
583: (void)close(fd);
584: return (-1);
585: }
1.51 xsa 586: l = snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
587: if (l == -1 || l >= (int)sizeof(buf))
588: return (-1);
589:
1.38 jfb 590: if (cvs_sendln(root, buf) < 0) {
591: (void)close(fd);
592: return (-1);
593: }
1.1 jfb 594:
595: while ((ret = read(fd, buf, sizeof(buf))) != 0) {
596: if (ret == -1) {
1.24 jfb 597: (void)close(fd);
1.1 jfb 598: cvs_log(LP_ERRNO, "failed to read file `%s'", path);
599: return (-1);
600: }
601:
1.38 jfb 602: if (cvs_sendraw(root, buf, (size_t)ret) < 0) {
603: (void)close(fd);
604: return (-1);
605: }
1.1 jfb 606: }
607:
608: (void)close(fd);
609: return (0);
610: }
611:
612:
613: /*
614: * cvs_recvfile()
615: *
616: * Receive the mode and size of a file followed the file's contents and
617: * create or update the file whose path is <path> with the received
618: * information.
619: */
1.14 jfb 620: BUF*
621: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 622: {
623: size_t len;
624: ssize_t ret;
625: off_t fsz, cnt;
626: char buf[4096], *ep;
1.14 jfb 627: BUF *fbuf;
628:
629: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
630: if (fbuf == NULL)
631: return (NULL);
1.1 jfb 632:
1.13 jfb 633: if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14 jfb 634: (cvs_strtomode(buf, mode) < 0)) {
1.44 tedu 635: cvs_buf_free(fbuf);
1.14 jfb 636: return (NULL);
1.1 jfb 637: }
638:
1.50 joris 639: if (cvs_getln(root, buf, sizeof(buf)) < 0) {
640: cvs_buf_free(fbuf);
641: return (NULL);
642: }
1.1 jfb 643:
644: fsz = (off_t)strtol(buf, &ep, 10);
645: if (*ep != '\0') {
646: cvs_log(LP_ERR, "parse error in file size transmission");
1.44 tedu 647: cvs_buf_free(fbuf);
1.14 jfb 648: return (NULL);
1.1 jfb 649: }
650:
651: cnt = 0;
652: do {
653: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 654: if (len == 0)
655: break;
1.13 jfb 656: ret = cvs_recvraw(root, buf, len);
1.1 jfb 657: if (ret == -1) {
1.14 jfb 658: cvs_buf_free(fbuf);
659: return (NULL);
1.1 jfb 660: }
661:
1.14 jfb 662: if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
663: cvs_log(LP_ERR,
664: "failed to append received file data");
665: cvs_buf_free(fbuf);
666: return (NULL);
1.1 jfb 667: }
668:
669: cnt += (off_t)ret;
670: } while (cnt < fsz);
671:
1.14 jfb 672: return (fbuf);
1.1 jfb 673: }
1.12 jfb 674:
675: /*
676: * cvs_sendreq()
677: *
678: * Send a request to the server of type <rid>, with optional arguments
679: * contained in <arg>, which should not be terminated by a newline.
680: * Returns 0 on success, or -1 on failure.
681: */
682: int
1.13 jfb 683: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 684: {
1.51 xsa 685: int ret, l;
1.13 jfb 686: struct cvs_req *req;
1.12 jfb 687:
688: if (root->cr_srvin == NULL) {
1.36 jfb 689: cvs_log(LP_ERR, "cannot send request %u: Not connected", rid);
1.12 jfb 690: return (-1);
691: }
692:
1.13 jfb 693: req = cvs_req_getbyid(rid);
694: if (req == NULL) {
1.12 jfb 695: cvs_log(LP_ERR, "unsupported request type %u", rid);
696: return (-1);
697: }
698:
1.18 jfb 699: /* is this request supported by the server? */
700: if (!CVS_GETVR(root, req->req_id)) {
1.57 joris 701: if (rid == CVS_REQ_VERSION) {
702: ret = cvs_sendreq(root, CVS_REQ_NOOP, arg);
703: } else {
704: cvs_log(LP_WARN,
705: "remote end does not support request `%s'",
706: req->req_str);
707: ret = -1;
708: }
709: return (ret);
1.18 jfb 710: }
711:
1.51 xsa 712: l = snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
1.13 jfb 713: req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.51 xsa 714: if (l == -1 || l >= (int)sizeof(cvs_proto_buf))
715: return (-1);
1.12 jfb 716:
717: if (cvs_server_inlog != NULL)
1.13 jfb 718: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 719:
1.13 jfb 720: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12 jfb 721: if (ret == EOF) {
722: cvs_log(LP_ERRNO, "failed to send request to server");
723: return (-1);
724: }
1.17 jfb 725:
726: if (rid == CVS_REQ_VERSION)
727: cvs_version_sent = 1;
1.12 jfb 728:
1.13 jfb 729: if (req->req_flags & CVS_REQF_RESP)
1.12 jfb 730: ret = cvs_getresp(root);
731:
1.13 jfb 732: return (ret);
1.12 jfb 733: }
734:
735:
736: /*
737: * cvs_getresp()
738: *
739: * Get a response from the server. This call will actually read and handle
740: * responses from the server until one of the response handlers returns
1.37 david 741: * non-zero (either an error occurred or the end of the response was reached).
1.12 jfb 742: * Returns the number of handled commands on success, or -1 on failure.
743: */
744: int
1.13 jfb 745: cvs_getresp(struct cvsroot *root)
1.12 jfb 746: {
1.13 jfb 747: int nbcmd, ret;
748: size_t len;
1.12 jfb 749:
750: nbcmd = 0;
751:
752: do {
753: /* wait for incoming data */
1.74 xsa 754: if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12 jfb 755: root->cr_srvout) == NULL) {
756: if (feof(root->cr_srvout))
757: return (0);
758: cvs_log(LP_ERRNO,
759: "failed to read response from server");
760: return (-1);
761: }
762:
763: if (cvs_server_outlog != NULL)
1.13 jfb 764: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 765:
1.13 jfb 766: if ((len = strlen(cvs_proto_buf)) != 0) {
1.42 joris 767: /* if len - 1 != '\n' the line is truncated */
1.43 joris 768: if (cvs_proto_buf[len - 1] == '\n')
1.13 jfb 769: cvs_proto_buf[--len] = '\0';
1.12 jfb 770: }
771:
1.13 jfb 772: ret = cvs_resp_handle(root, cvs_proto_buf);
1.12 jfb 773: nbcmd++;
774: } while (ret == 0);
775:
776: if (ret > 0)
777: ret = nbcmd;
778: return (ret);
779: }
780:
781:
782: /*
1.13 jfb 783: * cvs_getln()
784: *
1.18 jfb 785: * Get a line from the remote end and store it in <lbuf>. The terminating
1.13 jfb 786: * newline character is stripped from the result.
1.55 jfb 787: * Returns the length in bytes of the line (not including the NUL byte), or
788: * -1 on failure.
1.13 jfb 789: */
790: int
791: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
792: {
793: size_t rlen;
1.18 jfb 794: FILE *in;
1.13 jfb 795:
1.18 jfb 796: if (cvs_cmdop == CVS_OP_SERVER)
797: in = stdin;
798: else
799: in = root->cr_srvout;
800:
1.74 xsa 801: if (fgets(lbuf, (int)len, in) == NULL) {
1.18 jfb 802: if (ferror(in)) {
803: cvs_log(LP_ERRNO, "failed to read line");
1.13 jfb 804: return (-1);
805: }
806:
1.18 jfb 807: if (feof(in))
1.13 jfb 808: *lbuf = '\0';
809: }
810:
811: if (cvs_server_outlog != NULL)
812: fputs(lbuf, cvs_server_outlog);
813:
814: rlen = strlen(lbuf);
815: if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
816: lbuf[--rlen] = '\0';
817:
1.55 jfb 818: return (rlen);
1.13 jfb 819: }
820:
1.19 jfb 821:
1.13 jfb 822: /*
1.12 jfb 823: * cvs_sendresp()
824: *
1.19 jfb 825: * Send a response of type <rid> to the client, with optional arguments
1.12 jfb 826: * contained in <arg>, which should not be terminated by a newline.
827: * Returns 0 on success, or -1 on failure.
828: */
829: int
830: cvs_sendresp(u_int rid, const char *arg)
831: {
832: int ret;
1.19 jfb 833: struct cvs_resp *resp;
1.12 jfb 834:
835: resp = cvs_resp_getbyid(rid);
1.13 jfb 836: if (resp == NULL) {
1.12 jfb 837: cvs_log(LP_ERR, "unsupported response type %u", rid);
838: return (-1);
839: }
840:
1.19 jfb 841: ret = fputs(resp->resp_str, stdout);
1.12 jfb 842: if (ret == EOF) {
843: cvs_log(LP_ERRNO, "failed to send response to client");
1.30 deraadt 844: } else {
1.19 jfb 845: if (arg != NULL) {
846: putc(' ', stdout);
847: fputs(arg, stdout);
848: }
1.12 jfb 849: putc('\n', stdout);
850: }
851: return (0);
852: }
853:
854:
1.19 jfb 855: #ifdef notyet
1.12 jfb 856: /*
857: * cvs_getreq()
858: *
859: * Get a request from the client.
860: */
861: int
862: cvs_getreq(void)
863: {
864: int nbcmd;
865:
866: nbcmd = 0;
867:
868: do {
869: /* wait for incoming data */
1.74 xsa 870: if (fgets(cvs_proto_buf, (int)sizeof(cvs_proto_buf),
1.12 jfb 871: stdin) == NULL) {
872: if (feof(stdin))
873: return (0);
874: cvs_log(LP_ERRNO,
875: "failed to read request from client");
876: return (-1);
877: }
878:
1.13 jfb 879: if ((len = strlen(cvs_proto_buf)) != 0) {
880: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 881: /* truncated line */
1.30 deraadt 882: } else
1.13 jfb 883: cvs_proto_buf[--len] = '\0';
1.12 jfb 884: }
885:
1.13 jfb 886: ret = cvs_resp_handle(cvs_proto_buf);
1.12 jfb 887: } while (ret == 0);
888: }
889: #endif
890:
891:
892: /*
893: * cvs_sendln()
894: *
1.18 jfb 895: * Send a single line <line> string to the remote end. The line is sent as is,
1.12 jfb 896: * without any modifications.
897: * Returns 0 on success, or -1 on failure.
898: */
899: int
900: cvs_sendln(struct cvsroot *root, const char *line)
901: {
902: int nl;
903: size_t len;
1.18 jfb 904: FILE *out;
905:
906: if (cvs_cmdop == CVS_OP_SERVER)
907: out = stdout;
908: else
909: out = root->cr_srvin;
1.12 jfb 910:
911: nl = 0;
912: len = strlen(line);
913:
914: if ((len > 0) && (line[len - 1] != '\n'))
915: nl = 1;
916:
917: if (cvs_server_inlog != NULL) {
918: fputs(line, cvs_server_inlog);
919: if (nl)
1.55 jfb 920: putc('\n', cvs_server_inlog);
1.12 jfb 921: }
1.18 jfb 922: fputs(line, out);
1.12 jfb 923: if (nl)
1.55 jfb 924: putc('\n', out);
1.12 jfb 925: return (0);
926: }
927:
928:
929: /*
1.13 jfb 930: * cvs_sendraw()
1.12 jfb 931: *
932: * Send the first <len> bytes from the buffer <src> to the server.
933: */
934: int
1.13 jfb 935: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12 jfb 936: {
1.18 jfb 937: FILE *out;
938:
939: if (cvs_cmdop == CVS_OP_SERVER)
940: out = stdout;
941: else
942: out = root->cr_srvin;
943:
1.12 jfb 944: if (cvs_server_inlog != NULL)
945: fwrite(src, sizeof(char), len, cvs_server_inlog);
1.18 jfb 946: if (fwrite(src, sizeof(char), len, out) < len) {
1.38 jfb 947: cvs_log(LP_ERR, "failed to send data");
1.12 jfb 948: return (-1);
949: }
950:
951: return (0);
952: }
953:
954:
955: /*
1.13 jfb 956: * cvs_recvraw()
1.12 jfb 957: *
958: * Receive the first <len> bytes from the buffer <src> to the server.
959: */
960: ssize_t
1.13 jfb 961: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 962: {
963: size_t ret;
1.18 jfb 964: FILE *in;
965:
966: if (cvs_cmdop == CVS_OP_SERVER)
967: in = stdin;
968: else
969: in = root->cr_srvout;
1.12 jfb 970:
1.18 jfb 971: ret = fread(dst, sizeof(char), len, in);
1.12 jfb 972: if (ret == 0)
973: return (-1);
974: if (cvs_server_outlog != NULL)
975: fwrite(dst, sizeof(char), len, cvs_server_outlog);
976: return (ssize_t)ret;
977: }
978:
979:
980: /*
1.13 jfb 981: * cvs_senddir()
1.12 jfb 982: *
1.32 jfb 983: * Send a `Directory' request along with the 2 paths that follow it. If
984: * the directory info to be sent is the same as the last info sent, the
985: * call does nothing and simply returns without an error.
1.12 jfb 986: */
987: int
1.31 tedu 988: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 989: {
1.60 xsa 990: size_t len;
1.28 jfb 991: char lbuf[MAXPATHLEN], rbuf[MAXPATHLEN];
1.12 jfb 992:
1.47 jfb 993: if (dir->cf_type != DT_DIR)
994: return (-1);
995:
1.32 jfb 996: cvs_file_getpath(dir, lbuf, sizeof(lbuf));
1.45 joris 997: if (strcmp(lbuf, cvs_lastdir) == 0 && cvs_cmdop != CVS_OP_CHECKOUT)
1.32 jfb 998: return (0);
999:
1.47 jfb 1000: if (dir->cf_repo == NULL)
1.28 jfb 1001: strlcpy(rbuf, root->cr_dir, sizeof(rbuf));
1.51 xsa 1002: else {
1.60 xsa 1003: len = cvs_path_cat(root->cr_dir, dir->cf_repo, rbuf,
1004: sizeof(rbuf));
1005: if (len >= sizeof(rbuf))
1.51 xsa 1006: return (-1);
1007: }
1.12 jfb 1008:
1.28 jfb 1009:
1010: if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, lbuf) < 0) ||
1011: (cvs_sendln(root, rbuf) < 0))
1.12 jfb 1012: return (-1);
1.32 jfb 1013:
1014: strlcpy(cvs_lastdir, lbuf, sizeof(cvs_lastdir));
1.12 jfb 1015:
1016: return (0);
1017: }
1018:
1019:
1020: /*
1.13 jfb 1021: * cvs_sendarg()
1.12 jfb 1022: *
1023: * Send the argument <arg> to the server. The argument <append> is used to
1024: * determine if the argument should be simply appended to the last argument
1025: * sent or if it should be created as a new argument (0).
1026: */
1027: int
1.13 jfb 1028: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12 jfb 1029: {
1.13 jfb 1030: return cvs_sendreq(root, ((append == 0) ?
1031: CVS_REQ_ARGUMENT : CVS_REQ_ARGUMENTX), arg);
1.12 jfb 1032: }
1033:
1034:
1035: /*
1.13 jfb 1036: * cvs_sendentry()
1.12 jfb 1037: *
1038: * Send an `Entry' request to the server along with the mandatory fields from
1039: * the CVS entry <ent> (which are the name and revision).
1040: */
1041: int
1.47 jfb 1042: cvs_sendentry(struct cvsroot *root, const CVSFILE *file)
1.12 jfb 1043: {
1.51 xsa 1044: int l;
1.12 jfb 1045: char ebuf[128], numbuf[64];
1046:
1.47 jfb 1047: if (file->cf_type != DT_REG) {
1048: cvs_log(LP_ERR, "attempt to send Entry for non-regular file");
1049: return (-1);
1050: }
1.48 joris 1051:
1052: /* don't send Entry for unknown files */
1053: if (file->cf_cvstat == CVS_FST_UNKNOWN)
1054: return (0);
1.47 jfb 1055:
1.51 xsa 1056: l = snprintf(ebuf, sizeof(ebuf), "/%s/%s%s///", file->cf_name,
1.49 joris 1057: (file->cf_cvstat == CVS_FST_REMOVED) ? "-" : "",
1.47 jfb 1058: rcsnum_tostr(file->cf_lrev, numbuf, sizeof(numbuf)));
1.51 xsa 1059: if (l == -1 || l >= (int)sizeof(ebuf)) {
1060: errno = ENAMETOOLONG;
1061: cvs_log(LP_ERRNO, "%s", ebuf);
1062: return (-1);
1063: }
1.12 jfb 1064:
1.13 jfb 1065: return cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12 jfb 1066: }
1067:
1068:
1069: /*
1.13 jfb 1070: * cvs_initlog()
1.12 jfb 1071: *
1072: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1073: * set. In this case, the variable's value is used as a path to which the
1074: * appropriate suffix is added (".in" for server input and ".out" for server
1075: * output.
1076: * Returns 0 on success, or -1 on failure.
1077: */
1078: static int
1.13 jfb 1079: cvs_initlog(void)
1.12 jfb 1080: {
1.52 joris 1081: int l;
1082: u_int i;
1083: char *env, *envdup, buf[MAXPATHLEN], fpath[MAXPATHLEN];
1084: char rpath[MAXPATHLEN], *s;
1085: struct stat st;
1086: time_t now;
1087: struct passwd *pwd;
1.12 jfb 1088:
1.13 jfb 1089: /* avoid doing it more than once */
1090: if (cvs_server_logon)
1091: return (0);
1092:
1.12 jfb 1093: env = getenv("CVS_CLIENT_LOG");
1094: if (env == NULL)
1095: return (0);
1096:
1.52 joris 1097: if ((envdup = strdup(env)) == NULL)
1098: return (-1);
1099:
1100: if ((s = strchr(envdup, '%')) != NULL)
1101: *s = '\0';
1102:
1103: strlcpy(buf, env, sizeof(buf));
1104: strlcpy(rpath, envdup, sizeof(rpath));
1.69 moritz 1105: free(envdup);
1.52 joris 1106:
1107: s = buf;
1108: while ((s = strchr(s, '%')) != NULL) {
1109: *s++;
1110: switch (*s) {
1111: case 'c':
1112: strlcpy(fpath, cvs_command, sizeof(fpath));
1113: break;
1114: case 'd':
1115: time(&now);
1.59 niallo 1116: strlcpy(fpath, ctime(&now), sizeof(fpath));
1.52 joris 1117: break;
1118: case 'p':
1119: snprintf(fpath, sizeof(fpath), "%d", getpid());
1120: break;
1121: case 'u':
1122: if ((pwd = getpwuid(getuid())) != NULL)
1123: strlcpy(fpath, pwd->pw_name, sizeof(fpath));
1124: else
1125: fpath[0] = '\0';
1126: endpwent();
1127: break;
1128: default:
1129: fpath[0] = '\0';
1130: break;
1.58 joris 1131: }
1.52 joris 1132:
1133: if (fpath[0] != '\0') {
1134: strlcat(rpath, "-", sizeof(rpath));
1135: strlcat(rpath, fpath, sizeof(rpath));
1136: }
1137: }
1138:
1139: for (i = 0; i < UINT_MAX; i++) {
1140: l = snprintf(fpath, sizeof(fpath), "%s-%d.in", rpath, i);
1141: if (l == -1 || l >= (int)sizeof(fpath)) {
1142: errno = ENAMETOOLONG;
1143: cvs_log(LP_ERRNO, "%s", fpath);
1144: return (-1);
1145: }
1146:
1147: if (stat(fpath, &st) != -1)
1148: continue;
1149:
1150: if (errno != ENOENT)
1151: return (-1);
1152:
1153: break;
1154: }
1155:
1.12 jfb 1156: cvs_server_inlog = fopen(fpath, "w");
1157: if (cvs_server_inlog == NULL) {
1158: cvs_log(LP_ERRNO, "failed to open server input log `%s'",
1159: fpath);
1160: return (-1);
1161: }
1162:
1.52 joris 1163: for (i = 0; i < UINT_MAX; i++) {
1164: l = snprintf(fpath, sizeof(fpath), "%s-%d.out", rpath, i);
1165: if (l == -1 || l >= (int)sizeof(fpath)) {
1166: errno = ENAMETOOLONG;
1167: cvs_log(LP_ERRNO, "%s", fpath);
1168: return (-1);
1169: }
1170:
1171: if (stat(fpath, &st) != -1)
1172: continue;
1173:
1174: if (errno != ENOENT)
1175: return (-1);
1176:
1177: break;
1178: }
1179:
1.12 jfb 1180: cvs_server_outlog = fopen(fpath, "w");
1181: if (cvs_server_outlog == NULL) {
1182: cvs_log(LP_ERRNO, "failed to open server output log `%s'",
1183: fpath);
1184: return (-1);
1185: }
1186:
1187: /* make the streams line-buffered */
1.75 ! xsa 1188: setvbuf(cvs_server_inlog, NULL, _IOLBF, (size_t)0);
! 1189: setvbuf(cvs_server_outlog, NULL, _IOLBF, (size_t)0);
1.12 jfb 1190:
1.13 jfb 1191: cvs_server_logon = 1;
1.12 jfb 1192:
1193: return (0);
1194: }