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