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