Annotation of src/usr.bin/cvs/proto.c, Revision 1.14
1.1 jfb 1: /* $OpenBSD$ */
2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26: /*
27: * CVS client/server protocol
28: * ==========================
29: *
30: * The following code implements the CVS client/server protocol, which is
31: * documented at the following URL:
32: * http://www.loria.fr/~molli/cvs/doc/cvsclient_toc.html
33: *
34: * The protocol is split up into two parts; the first part is the client side
35: * of things and is composed of all the response handlers, which are all named
36: * with a prefix of "cvs_resp_". The second part is the set of request
37: * handlers used by the server. These handlers process the request and
38: * generate the appropriate response to send back. The prefix for request
39: * handlers is "cvs_req_".
40: *
41: */
42:
43: #include <sys/types.h>
44: #include <sys/stat.h>
45:
46: #include <fcntl.h>
47: #include <stdio.h>
48: #include <errno.h>
49: #include <stdlib.h>
50: #include <unistd.h>
51: #include <signal.h>
52: #include <string.h>
53: #include <sysexits.h>
54: #ifdef CVS_ZLIB
55: #include <zlib.h>
56: #endif
57:
58: #include "buf.h"
59: #include "cvs.h"
60: #include "log.h"
1.14 ! jfb 61: #include "file.h"
1.13 jfb 62: #include "proto.h"
1.1 jfb 63:
64:
1.5 jfb 65: #define CVS_MTSTK_MAXDEPTH 16
66:
1.11 jfb 67: /* request flags */
68: #define CVS_REQF_RESP 0x01
69:
70:
1.5 jfb 71:
1.1 jfb 72:
73: extern int verbosity;
74: extern int cvs_compress;
75: extern char *cvs_rsh;
76: extern int cvs_trace;
77: extern int cvs_nolog;
78: extern int cvs_readonly;
79:
80:
81:
1.13 jfb 82: static int cvs_resp_validreq (struct cvsroot *, int, char *);
83: static int cvs_resp_cksum (struct cvsroot *, int, char *);
84: static int cvs_resp_modtime (struct cvsroot *, int, char *);
85: static int cvs_resp_m (struct cvsroot *, int, char *);
86: static int cvs_resp_ok (struct cvsroot *, int, char *);
87: static int cvs_resp_error (struct cvsroot *, int, char *);
88: static int cvs_resp_statdir (struct cvsroot *, int, char *);
89: static int cvs_resp_sticky (struct cvsroot *, int, char *);
90: static int cvs_resp_newentry (struct cvsroot *, int, char *);
91: static int cvs_resp_updated (struct cvsroot *, int, char *);
92: static int cvs_resp_removed (struct cvsroot *, int, char *);
93: static int cvs_resp_mode (struct cvsroot *, int, char *);
94: static int cvs_resp_modxpand (struct cvsroot *, int, char *);
1.14 ! jfb 95: static int cvs_resp_rcsdiff (struct cvsroot *, int, char *);
1.1 jfb 96:
1.13 jfb 97: static int cvs_initlog (void);
1.1 jfb 98:
1.7 jfb 99: static const char *cvs_months[] = {
1.9 jfb 100: "Jan", "Feb", "Mar", "Apr", "May", "Jun",
101: "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
1.7 jfb 102: };
103:
104:
105:
1.1 jfb 106: struct cvs_req {
107: int req_id;
108: char req_str[32];
109: u_int req_flags;
1.13 jfb 110: int (*req_hdlr)(int, char *);
1.1 jfb 111: } cvs_requests[] = {
112: { CVS_REQ_DIRECTORY, "Directory", 0, NULL },
113: { CVS_REQ_MAXDOTDOT, "Max-dotdot", 0, NULL },
114: { CVS_REQ_STATICDIR, "Static-directory", 0, NULL },
115: { CVS_REQ_STICKY, "Sticky", 0, NULL },
116: { CVS_REQ_ENTRY, "Entry", 0, NULL },
117: { CVS_REQ_ENTRYEXTRA, "EntryExtra", 0, NULL },
118: { CVS_REQ_CHECKINTIME, "Checkin-time", 0, NULL },
119: { CVS_REQ_MODIFIED, "Modified", 0, NULL },
120: { CVS_REQ_ISMODIFIED, "Is-modified", 0, NULL },
121: { CVS_REQ_UNCHANGED, "Unchanged", 0, NULL },
122: { CVS_REQ_USEUNCHANGED, "UseUnchanged", 0, NULL },
123: { CVS_REQ_NOTIFY, "Notify", 0, NULL },
124: { CVS_REQ_NOTIFYUSER, "NotifyUser", 0, NULL },
125: { CVS_REQ_QUESTIONABLE, "Questionable", 0, NULL },
126: { CVS_REQ_CASE, "Case", 0, NULL },
127: { CVS_REQ_UTF8, "Utf8", 0, NULL },
128: { CVS_REQ_ARGUMENT, "Argument", 0, NULL },
129: { CVS_REQ_ARGUMENTX, "Argumentx", 0, NULL },
130: { CVS_REQ_GLOBALOPT, "Global_option", 0, NULL },
131: { CVS_REQ_GZIPSTREAM, "Gzip-stream", 0, NULL },
132: { CVS_REQ_READCVSRC2, "read-cvsrc2", 0, NULL },
133: { CVS_REQ_READWRAP, "read-cvswrappers", 0, NULL },
134: { CVS_REQ_READIGNORE, "read-cvsignore", 0, NULL },
135: { CVS_REQ_ERRIFREADER, "Error-If-Reader", 0, NULL },
136: { CVS_REQ_VALIDRCSOPT, "Valid-RcsOptions", 0, NULL },
137: { CVS_REQ_SET, "Set", 0, NULL },
1.11 jfb 138: { CVS_REQ_XPANDMOD, "expand-modules", CVS_REQF_RESP, NULL },
1.1 jfb 139: { CVS_REQ_LOG, "log", 0, NULL },
1.11 jfb 140: { CVS_REQ_CO, "co", CVS_REQF_RESP, NULL },
141: { CVS_REQ_EXPORT, "export", CVS_REQF_RESP, NULL },
1.1 jfb 142: { CVS_REQ_RANNOTATE, "rannotate", 0, NULL },
143: { CVS_REQ_RDIFF, "rdiff", 0, NULL },
144: { CVS_REQ_RLOG, "rlog", 0, NULL },
1.11 jfb 145: { CVS_REQ_RTAG, "rtag", CVS_REQF_RESP, NULL },
146: { CVS_REQ_INIT, "init", CVS_REQF_RESP, NULL },
147: { CVS_REQ_STATUS, "status", CVS_REQF_RESP, NULL },
148: { CVS_REQ_UPDATE, "update", CVS_REQF_RESP, NULL },
1.1 jfb 149: { CVS_REQ_HISTORY, "history", 0, NULL },
1.11 jfb 150: { CVS_REQ_IMPORT, "import", CVS_REQF_RESP, NULL },
151: { CVS_REQ_ADD, "add", CVS_REQF_RESP, NULL },
152: { CVS_REQ_REMOVE, "remove", CVS_REQF_RESP, NULL },
153: { CVS_REQ_RELEASE, "release", CVS_REQF_RESP, NULL },
1.1 jfb 154: { CVS_REQ_ROOT, "Root", 0, NULL },
1.13 jfb 155: { CVS_REQ_VALIDRESP, "Valid-responses", 0, NULL },
1.11 jfb 156: { CVS_REQ_VALIDREQ, "valid-requests", CVS_REQF_RESP, NULL },
157: { CVS_REQ_VERSION, "version", CVS_REQF_RESP, NULL },
158: { CVS_REQ_NOOP, "noop", CVS_REQF_RESP, NULL },
159: { CVS_REQ_DIFF, "diff", CVS_REQF_RESP, NULL },
1.1 jfb 160: };
161:
162:
163: struct cvs_resp {
164: u_int resp_id;
165: char resp_str[32];
1.13 jfb 166: int (*resp_hdlr)(struct cvsroot *, int, char *);
1.1 jfb 167: } cvs_responses[] = {
168: { CVS_RESP_OK, "ok", cvs_resp_ok },
169: { CVS_RESP_ERROR, "error", cvs_resp_error },
170: { CVS_RESP_VALIDREQ, "Valid-requests", cvs_resp_validreq },
171: { CVS_RESP_M, "M", cvs_resp_m },
172: { CVS_RESP_MBINARY, "Mbinary", cvs_resp_m },
173: { CVS_RESP_MT, "MT", cvs_resp_m },
174: { CVS_RESP_E, "E", cvs_resp_m },
175: { CVS_RESP_F, "F", cvs_resp_m },
176: { CVS_RESP_CREATED, "Created", cvs_resp_updated },
177: { CVS_RESP_UPDATED, "Updated", cvs_resp_updated },
178: { CVS_RESP_UPDEXIST, "Update-existing", cvs_resp_updated },
179: { CVS_RESP_REMOVED, "Removed", cvs_resp_removed },
180: { CVS_RESP_MERGED, "Merged", NULL },
181: { CVS_RESP_CKSUM, "Checksum", cvs_resp_cksum },
182: { CVS_RESP_CLRSTATDIR, "Clear-static-directory", cvs_resp_statdir },
183: { CVS_RESP_SETSTATDIR, "Set-static-directory", cvs_resp_statdir },
184: { CVS_RESP_NEWENTRY, "New-entry", cvs_resp_newentry },
185: { CVS_RESP_CHECKEDIN, "Checked-in", cvs_resp_newentry },
186: { CVS_RESP_MODE, "Mode", cvs_resp_mode },
1.4 jfb 187: { CVS_RESP_MODTIME, "Mod-time", cvs_resp_modtime },
1.5 jfb 188: { CVS_RESP_MODXPAND, "Module-expansion", cvs_resp_modxpand },
189: { CVS_RESP_SETSTICKY, "Set-sticky", cvs_resp_sticky },
190: { CVS_RESP_CLRSTICKY, "Clear-sticky", cvs_resp_sticky },
1.14 ! jfb 191: { CVS_RESP_RCSDIFF, "Rcs-diff", cvs_resp_rcsdiff },
1.1 jfb 192: };
193:
194:
1.5 jfb 195: static char *cvs_mt_stack[CVS_MTSTK_MAXDEPTH];
196: static u_int cvs_mtstk_depth = 0;
197:
1.8 jfb 198: static time_t cvs_modtime = 0;
1.7 jfb 199:
1.5 jfb 200:
1.1 jfb 201: #define CVS_NBREQ (sizeof(cvs_requests)/sizeof(cvs_requests[0]))
202: #define CVS_NBRESP (sizeof(cvs_responses)/sizeof(cvs_responses[0]))
203:
204: /* mask of requets supported by server */
205: static u_char cvs_server_validreq[CVS_REQ_MAX + 1];
206:
1.5 jfb 207: char *cvs_fcksum = NULL;
208:
209: mode_t cvs_lastmode = 0;
210:
1.13 jfb 211: static char cvs_proto_buf[4096];
212:
213: /*
214: * Output files for protocol logging when the CVS_CLIENT_LOG enviroment
215: * variable is set.
216: */
217: static int cvs_server_logon = 0;
218: static FILE *cvs_server_inlog = NULL;
219: static FILE *cvs_server_outlog = NULL;
220:
221:
222: /*
223: * cvs_connect()
224: *
225: * Open a client connection to the cvs server whose address is given in
226: * the <root> variable. The method used to connect depends on the
227: * setting of the CVS_RSH variable.
228: * Returns 0 on success, or -1 on failure.
229: */
230:
231: int
232: cvs_connect(struct cvsroot *root)
233: {
234: int argc, infd[2], outfd[2];
235: pid_t pid;
236: char *argv[16], *cvs_server_cmd, *vresp;
237:
238: if (pipe(infd) == -1) {
239: cvs_log(LP_ERRNO,
240: "failed to create input pipe for client connection");
241: return (-1);
242: }
243:
244: if (pipe(outfd) == -1) {
245: cvs_log(LP_ERRNO,
246: "failed to create output pipe for client connection");
247: (void)close(infd[0]);
248: (void)close(infd[1]);
249: return (-1);
250: }
251:
252: pid = fork();
253: if (pid == -1) {
254: cvs_log(LP_ERRNO, "failed to fork for cvs server connection");
255: return (-1);
256: }
257: if (pid == 0) {
258: if ((dup2(infd[0], STDIN_FILENO) == -1) ||
259: (dup2(outfd[1], STDOUT_FILENO) == -1)) {
260: cvs_log(LP_ERRNO,
261: "failed to setup standard streams for cvs server");
262: return (-1);
263: }
264: (void)close(infd[1]);
265: (void)close(outfd[0]);
266:
267: argc = 0;
268: argv[argc++] = cvs_rsh;
269:
270: if (root->cr_user != NULL) {
271: argv[argc++] = "-l";
272: argv[argc++] = root->cr_user;
273: }
274:
275:
276: cvs_server_cmd = getenv("CVS_SERVER");
277: if (cvs_server_cmd == NULL)
278: cvs_server_cmd = "cvs";
279:
280: argv[argc++] = root->cr_host;
281: argv[argc++] = cvs_server_cmd;
282: argv[argc++] = "server";
283: argv[argc] = NULL;
284:
285: execvp(argv[0], argv);
286: cvs_log(LP_ERRNO, "failed to exec");
287: exit(EX_OSERR);
288: }
289:
290: /* we are the parent */
291: (void)close(infd[0]);
292: (void)close(outfd[1]);
293:
294: root->cr_srvin = fdopen(infd[1], "w");
295: if (root->cr_srvin == NULL) {
296: cvs_log(LP_ERRNO, "failed to create pipe stream");
297: return (-1);
298: }
299:
300: root->cr_srvout = fdopen(outfd[0], "r");
301: if (root->cr_srvout == NULL) {
302: cvs_log(LP_ERRNO, "failed to create pipe stream");
303: return (-1);
304: }
305:
306: /* make the streams line-buffered */
307: (void)setvbuf(root->cr_srvin, NULL, _IOLBF, 0);
308: (void)setvbuf(root->cr_srvout, NULL, _IOLBF, 0);
309:
310: cvs_initlog();
311:
312: /*
313: * Send the server the list of valid responses, then ask for valid
314: * requests.
315: */
316:
317: vresp = cvs_resp_getvalid();
318: if (vresp == NULL) {
319: cvs_log(LP_ERR, "can't generate list of valid responses");
320: return (-1);
321: }
322:
323: if (cvs_sendreq(root, CVS_REQ_VALIDRESP, vresp) < 0) {
324: }
325: free(vresp);
326:
327: if (cvs_sendreq(root, CVS_REQ_VALIDREQ, NULL) < 0) {
328: cvs_log(LP_ERR, "failed to get valid requests from server");
329: return (-1);
330: }
331:
332: /* now share our global options with the server */
333: if (verbosity == 1)
334: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-q");
335: else if (verbosity == 0)
336: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-Q");
337:
338: if (cvs_nolog)
339: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-l");
340: if (cvs_readonly)
341: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-r");
342: if (cvs_trace)
343: cvs_sendreq(root, CVS_REQ_GLOBALOPT, "-t");
344:
345: /* now send the CVSROOT to the server */
346: if (cvs_sendreq(root, CVS_REQ_ROOT, root->cr_dir) < 0)
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: #ifdef CVS_ZLIB
354: /* if compression was requested, initialize it */
355: #endif
356:
357: cvs_log(LP_DEBUG, "connected to %s", root->cr_host);
358:
359: return (0);
360: }
361:
362:
363: /*
364: * cvs_disconnect()
365: *
366: * Disconnect from the cvs server.
367: */
368:
369: void
370: cvs_disconnect(struct cvsroot *root)
371: {
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: }
381: }
382:
1.1 jfb 383:
384: /*
385: * cvs_req_getbyid()
386: *
387: */
388:
1.13 jfb 389: struct cvs_req*
1.1 jfb 390: cvs_req_getbyid(int reqid)
391: {
392: u_int i;
393:
394: for (i = 0; i < CVS_NBREQ; i++)
395: if (cvs_requests[i].req_id == reqid)
1.13 jfb 396: return &(cvs_requests[i]);
1.1 jfb 397: return (NULL);
398: }
399:
400:
401: /*
402: * cvs_req_getbyname()
403: */
404:
1.13 jfb 405: struct cvs_req*
1.1 jfb 406: cvs_req_getbyname(const char *rname)
407: {
408: u_int i;
409:
410: for (i = 0; i < CVS_NBREQ; i++)
411: if (strcmp(cvs_requests[i].req_str, rname) == 0)
1.13 jfb 412: return &(cvs_requests[i]);
1.1 jfb 413:
1.13 jfb 414: return (NULL);
1.1 jfb 415: }
416:
417:
418: /*
419: * cvs_req_getvalid()
420: *
421: * Build a space-separated list of all the requests that this protocol
422: * implementation supports.
423: */
424:
425: char*
426: cvs_req_getvalid(void)
427: {
428: u_int i;
429: size_t len;
430: char *vrstr;
431: BUF *buf;
432:
433: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
434: if (buf == NULL)
435: return (NULL);
436:
437: cvs_buf_set(buf, cvs_requests[0].req_str,
438: strlen(cvs_requests[0].req_str), 0);
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:
455: len = cvs_buf_size(buf);
456: vrstr = (char *)malloc(len);
457:
458: cvs_buf_copy(buf, 0, vrstr, len);
459:
460: cvs_buf_free(buf);
461:
462: return (vrstr);
463: }
464:
465:
466: /*
467: * cvs_req_handle()
468: *
469: * Generic request handler dispatcher.
470: */
471:
472: int
473: cvs_req_handle(char *line)
474: {
475: return (0);
476: }
477:
478:
479: /*
480: * cvs_resp_getbyid()
481: *
482: */
483:
1.13 jfb 484: struct cvs_resp*
1.1 jfb 485: cvs_resp_getbyid(int respid)
486: {
487: u_int i;
488:
489: for (i = 0; i < CVS_NBREQ; i++)
1.13 jfb 490: if (cvs_responses[i].resp_id == (u_int)respid)
491: return &(cvs_responses[i]);
1.1 jfb 492: return (NULL);
493: }
494:
495:
496: /*
497: * cvs_resp_getbyname()
498: */
499:
1.13 jfb 500: struct cvs_resp*
1.1 jfb 501: cvs_resp_getbyname(const char *rname)
502: {
503: u_int i;
504:
505: for (i = 0; i < CVS_NBREQ; i++)
506: if (strcmp(cvs_responses[i].resp_str, rname) == 0)
1.13 jfb 507: return &(cvs_responses[i]);
1.1 jfb 508:
1.13 jfb 509: return (NULL);
1.1 jfb 510: }
511:
512:
513: /*
514: * cvs_resp_getvalid()
515: *
516: * Build a space-separated list of all the responses that this protocol
517: * implementation supports.
518: */
519:
520: char*
521: cvs_resp_getvalid(void)
522: {
523: u_int i;
524: size_t len;
525: char *vrstr;
526: BUF *buf;
527:
528: buf = cvs_buf_alloc(512, BUF_AUTOEXT);
529: if (buf == NULL)
530: return (NULL);
531:
532: cvs_buf_set(buf, cvs_responses[0].resp_str,
533: strlen(cvs_responses[0].resp_str), 0);
534:
535: for (i = 1; i < CVS_NBRESP; i++) {
536: if ((cvs_buf_putc(buf, ' ') < 0) ||
537: (cvs_buf_append(buf, cvs_responses[i].resp_str,
538: strlen(cvs_responses[i].resp_str)) < 0)) {
539: cvs_buf_free(buf);
540: return (NULL);
541: }
542: }
543:
544: /* NUL-terminate */
545: if (cvs_buf_putc(buf, '\0') < 0) {
546: cvs_buf_free(buf);
547: return (NULL);
548: }
549:
550: len = cvs_buf_size(buf);
551: vrstr = (char *)malloc(len);
552:
553: cvs_buf_copy(buf, 0, vrstr, len);
554: cvs_buf_free(buf);
555:
556: return (vrstr);
557: }
558:
559:
560: /*
561: * cvs_resp_handle()
562: *
563: * Generic response handler dispatcher. The handler expects the first line
564: * of the command as single argument.
565: * Returns the return value of the command on success, or -1 on failure.
566: */
567:
568: int
1.13 jfb 569: cvs_resp_handle(struct cvsroot *root, char *line)
1.1 jfb 570: {
571: u_int i;
572: char *cp, *cmd;
573:
574: cmd = line;
575:
576: cp = strchr(cmd, ' ');
577: if (cp != NULL)
578: *(cp++) = '\0';
579:
580: for (i = 0; i < CVS_NBRESP; i++) {
581: if (strcmp(cvs_responses[i].resp_str, cmd) == 0)
582: return (*cvs_responses[i].resp_hdlr)
1.13 jfb 583: (root, cvs_responses[i].resp_id, cp);
1.1 jfb 584: }
585:
586: /* unhandled */
587: return (-1);
588: }
589:
590:
591: /*
592: * cvs_resp_validreq()
593: *
594: * Handler for the `Valid-requests' response. The list of valid requests is
595: * split on spaces and each request's entry in the valid request array is set
596: * to 1 to indicate the validity.
597: * Returns 0 on success, or -1 on failure.
598: */
599:
600: static int
1.13 jfb 601: cvs_resp_validreq(struct cvsroot *root, int type, char *line)
1.1 jfb 602: {
603: char *sp, *ep;
1.13 jfb 604: struct cvs_req *req;
1.1 jfb 605:
606: /* parse the requests */
607: sp = line;
608: do {
609: ep = strchr(sp, ' ');
610: if (ep != NULL)
611: *ep = '\0';
612:
1.13 jfb 613: req = cvs_req_getbyname(sp);
614: if (req != NULL)
615: cvs_server_validreq[req->req_id] = 1;
1.1 jfb 616:
617: if (ep != NULL)
618: sp = ep + 1;
619: } while (ep != NULL);
620:
621: return (0);
622: }
623:
624:
625: /*
626: * cvs_resp_m()
627: *
1.4 jfb 628: * Handler for the `M', 'MT', `F' and `E' responses.
1.1 jfb 629: */
630:
631: static int
1.13 jfb 632: cvs_resp_m(struct cvsroot *root, int type, char *line)
1.1 jfb 633: {
1.5 jfb 634: char *cp;
1.1 jfb 635: FILE *stream;
636:
637: stream = NULL;
638:
639: switch (type) {
640: case CVS_RESP_F:
641: fflush(stderr);
642: return (0);
643: case CVS_RESP_M:
644: stream = stdout;
645: break;
646: case CVS_RESP_E:
647: stream = stderr;
648: break;
649: case CVS_RESP_MT:
1.5 jfb 650: if (*line == '+') {
651: if (cvs_mtstk_depth == CVS_MTSTK_MAXDEPTH) {
652: cvs_log(LP_ERR,
653: "MT scope stack has reached max depth");
654: return (-1);
655: }
1.6 jfb 656: cvs_mt_stack[cvs_mtstk_depth] = strdup(line + 1);
657: if (cvs_mt_stack[cvs_mtstk_depth] == NULL)
1.5 jfb 658: return (-1);
1.6 jfb 659: cvs_mtstk_depth++;
1.5 jfb 660: }
661: else if (*line == '-') {
662: if (cvs_mtstk_depth == 0) {
663: cvs_log(LP_ERR, "MT scope stack underflow");
664: return (-1);
665: }
1.6 jfb 666: else if (strcmp(line + 1,
667: cvs_mt_stack[cvs_mtstk_depth - 1]) != 0) {
1.5 jfb 668: cvs_log(LP_ERR, "mismatch in MT scope stack");
669: return (-1);
670: }
671: free(cvs_mt_stack[cvs_mtstk_depth--]);
672: }
673: else {
674: if (strcmp(line, "newline") == 0)
675: putc('\n', stdout);
676: else if (strncmp(line, "fname ", 6) == 0)
677: printf("%s", line + 6);
678: else {
679: /* assume text */
680: cp = strchr(line, ' ');
681: if (cp != NULL)
682: printf("%s", cp + 1);
683: }
684: }
685:
1.6 jfb 686: return (0);
1.1 jfb 687: case CVS_RESP_MBINARY:
1.5 jfb 688: cvs_log(LP_WARN, "Mbinary not supported in client yet");
1.1 jfb 689: break;
690: }
691:
692: fputs(line, stream);
693: fputc('\n', stream);
694:
695: return (0);
696: }
697:
698:
699: /*
700: * cvs_resp_ok()
701: *
702: * Handler for the `ok' response. This handler's job is to
703: */
704:
705: static int
1.13 jfb 706: cvs_resp_ok(struct cvsroot *root, int type, char *line)
1.1 jfb 707: {
708: return (1);
709: }
710:
711:
712: /*
713: * cvs_resp_error()
714: *
715: * Handler for the `error' response. This handler's job is to
716: */
717:
718: static int
1.13 jfb 719: cvs_resp_error(struct cvsroot *root, int type, char *line)
1.1 jfb 720: {
721: return (1);
722: }
723:
724:
725: /*
726: * cvs_resp_statdir()
727: *
728: * Handler for the `Clear-static-directory' and `Set-static-directory'
729: * responses.
730: */
731:
732: static int
1.13 jfb 733: cvs_resp_statdir(struct cvsroot *root, int type, char *line)
1.1 jfb 734: {
735: int fd;
1.6 jfb 736: char rpath[MAXPATHLEN], statpath[MAXPATHLEN];
737:
1.13 jfb 738: cvs_getln(root, rpath, sizeof(rpath));
1.1 jfb 739:
740: snprintf(statpath, sizeof(statpath), "%s/%s", line,
741: CVS_PATH_STATICENTRIES);
742:
743: if ((type == CVS_RESP_CLRSTATDIR) &&
1.5 jfb 744: (unlink(statpath) == -1) && (errno != ENOENT)) {
1.1 jfb 745: cvs_log(LP_ERRNO, "failed to unlink %s file",
746: CVS_PATH_STATICENTRIES);
747: return (-1);
748: }
749: else if (type == CVS_RESP_SETSTATDIR) {
1.5 jfb 750: fd = open(statpath, O_CREAT|O_TRUNC|O_WRONLY, 0400);
1.1 jfb 751: if (fd == -1) {
752: cvs_log(LP_ERRNO, "failed to create %s file",
753: CVS_PATH_STATICENTRIES);
754: return (-1);
755: }
756: (void)close(fd);
1.5 jfb 757:
758: }
759:
760: return (0);
761: }
762:
763: /*
764: * cvs_resp_sticky()
765: *
766: * Handler for the `Clear-sticky' and `Set-sticky' responses.
767: */
768:
769: static int
1.13 jfb 770: cvs_resp_sticky(struct cvsroot *root, int type, char *line)
1.5 jfb 771: {
1.9 jfb 772: size_t len;
1.6 jfb 773: char rpath[MAXPATHLEN];
774: struct stat st;
1.7 jfb 775: CVSFILE *cf;
1.6 jfb 776:
1.9 jfb 777: /* remove trailing slash */
778: len = strlen(line);
779: if ((len > 0) && (line[len - 1] == '/'))
780: line[--len] = '\0';
781:
1.6 jfb 782: /* get the remote path */
1.13 jfb 783: cvs_getln(root, rpath, sizeof(rpath));
1.6 jfb 784:
785: /* if the directory doesn't exist, create it */
786: if (stat(line, &st) == -1) {
1.7 jfb 787: /* attempt to create it */
1.6 jfb 788: if (errno != ENOENT) {
789: cvs_log(LP_ERRNO, "failed to stat %s", line);
790: }
1.7 jfb 791: else {
792: cf = cvs_file_create(line, DT_DIR, 0755);
793: if (cf == NULL)
794: return (-1);
795: cf->cf_ddat->cd_repo = strdup(line);
1.14 ! jfb 796: cf->cf_ddat->cd_root = root;
! 797: root->cr_ref++;
1.7 jfb 798: cvs_mkadmin(cf, 0755);
799:
800: cvs_file_free(cf);
1.6 jfb 801: }
802: }
803:
1.5 jfb 804: if (type == CVS_RESP_CLRSTICKY) {
805: }
806: else if (type == CVS_RESP_SETSTICKY) {
1.1 jfb 807: }
808:
809: return (0);
810: }
811:
812:
813: /*
814: * cvs_resp_newentry()
815: *
816: * Handler for the `New-entry' response and `Checked-in' responses.
817: */
818:
819: static int
1.13 jfb 820: cvs_resp_newentry(struct cvsroot *root, int type, char *line)
1.1 jfb 821: {
822: char entbuf[128], path[MAXPATHLEN];
823: CVSENTRIES *entfile;
824:
1.2 jfb 825: snprintf(path, sizeof(path), "%s/" CVS_PATH_ENTRIES, line);
1.1 jfb 826:
827: /* get the remote path */
1.13 jfb 828: cvs_getln(root, entbuf, sizeof(entbuf));
1.1 jfb 829:
830: /* get the new Entries line */
1.13 jfb 831: if (cvs_getln(root, entbuf, sizeof(entbuf)) < 0)
1.1 jfb 832: return (-1);
833:
1.2 jfb 834: entfile = cvs_ent_open(path, O_WRONLY);
1.1 jfb 835: if (entfile == NULL)
836: return (-1);
1.3 jfb 837: cvs_ent_addln(entfile, entbuf);
1.1 jfb 838: cvs_ent_close(entfile);
839:
840: return (0);
841: }
842:
843:
844: /*
845: * cvs_resp_cksum()
846: *
847: * Handler for the `Checksum' response. We store the checksum received for
848: * the next file in a dynamically-allocated buffer pointed to by <cvs_fcksum>.
849: * Upon next file reception, the handler checks to see if there is a stored
850: * checksum.
851: * The file handler must make sure that the checksums match and free the
852: * checksum buffer once it's done to indicate there is no further checksum.
853: */
854:
855: static int
1.13 jfb 856: cvs_resp_cksum(struct cvsroot *root, int type, char *line)
1.1 jfb 857: {
858: if (cvs_fcksum != NULL) {
859: cvs_log(LP_WARN, "unused checksum");
860: free(cvs_fcksum);
861: }
862:
863: cvs_fcksum = strdup(line);
864: if (cvs_fcksum == NULL) {
865: cvs_log(LP_ERRNO, "failed to copy checksum string");
866: return (-1);
867: }
868:
1.4 jfb 869: return (0);
870: }
871:
872:
873: /*
874: * cvs_resp_modtime()
875: *
876: * Handler for the `Mod-time' file update modifying response. The timestamp
877: * given is used to set the last modification time on the next file that
878: * will be received.
879: */
880:
881: static int
1.13 jfb 882: cvs_resp_modtime(struct cvsroot *root, int type, char *line)
1.4 jfb 883: {
1.8 jfb 884: int i;
885: long off;
886: char sign, mon[8], gmt[8], hr[4], min[4], *ep;
887: struct tm cvs_tm;
888:
889: memset(&cvs_tm, 0, sizeof(cvs_tm));
1.9 jfb 890: sscanf(line, "%d %3s %d %2d:%2d:%2d %5s", &cvs_tm.tm_mday, mon,
1.8 jfb 891: &cvs_tm.tm_year, &cvs_tm.tm_hour, &cvs_tm.tm_min,
892: &cvs_tm.tm_sec, gmt);
893: cvs_tm.tm_year -= 1900;
1.9 jfb 894: cvs_tm.tm_isdst = -1;
1.8 jfb 895:
896: if (*gmt == '-') {
1.9 jfb 897: sscanf(gmt, "%c%2s%2s", &sign, hr, min);
1.8 jfb 898: cvs_tm.tm_gmtoff = strtol(hr, &ep, 10);
899: if ((cvs_tm.tm_gmtoff == LONG_MIN) ||
900: (cvs_tm.tm_gmtoff == LONG_MAX) ||
901: (*ep != '\0')) {
902: cvs_log(LP_ERR,
903: "parse error in GMT hours specification `%s'", hr);
904: cvs_tm.tm_gmtoff = 0;
905: }
906: else {
907: /* get seconds */
908: cvs_tm.tm_gmtoff *= 3600;
1.7 jfb 909:
1.8 jfb 910: /* add the minutes */
911: off = strtol(min, &ep, 10);
912: if ((cvs_tm.tm_gmtoff == LONG_MIN) ||
913: (cvs_tm.tm_gmtoff == LONG_MAX) ||
914: (*ep != '\0')) {
915: cvs_log(LP_ERR,
916: "parse error in GMT minutes "
917: "specification `%s'", min);
918: }
919: else
920: cvs_tm.tm_gmtoff += off * 60;
921: }
922: }
923: if (sign == '-')
924: cvs_tm.tm_gmtoff = -cvs_tm.tm_gmtoff;
1.7 jfb 925:
1.8 jfb 926: for (i = 0; i < (int)(sizeof(cvs_months)/sizeof(cvs_months[0])); i++) {
927: if (strcmp(cvs_months[i], mon) == 0) {
928: cvs_tm.tm_mon = i;
929: break;
930: }
1.7 jfb 931: }
932:
1.8 jfb 933: cvs_modtime = mktime(&cvs_tm);
1.1 jfb 934: return (0);
935: }
936:
937:
938: /*
939: * cvs_resp_updated()
940: *
1.6 jfb 941: * Handler for the `Updated' and `Created' responses.
1.1 jfb 942: */
943:
944: static int
1.13 jfb 945: cvs_resp_updated(struct cvsroot *root, int type, char *line)
1.1 jfb 946: {
1.7 jfb 947: size_t len;
1.14 ! jfb 948: mode_t fmode;
1.7 jfb 949: char tbuf[32], path[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
1.14 ! jfb 950: BUF *fbuf;
1.7 jfb 951: CVSENTRIES *ef;
1.6 jfb 952: struct cvs_ent *ep;
1.1 jfb 953:
1.13 jfb 954: ep = NULL;
955:
1.1 jfb 956: if (type == CVS_RESP_CREATED) {
1.6 jfb 957: /* read the remote path of the file */
1.13 jfb 958: cvs_getln(root, path, sizeof(path));
1.6 jfb 959:
960: /* read the new entry */
1.13 jfb 961: cvs_getln(root, path, sizeof(path));
1.6 jfb 962: ep = cvs_ent_parse(path);
963: if (ep == NULL)
964: return (-1);
1.7 jfb 965:
1.14 ! jfb 966: snprintf(path, sizeof(path), "%s%s", line, ep->ce_name);
! 967:
1.7 jfb 968: /* set the timestamp as the last one received from Mod-time */
1.8 jfb 969: ep->ce_timestamp = ctime_r(&cvs_modtime, tbuf);
1.7 jfb 970: len = strlen(tbuf);
971: if ((len > 0) && (tbuf[len - 1] == '\n'))
972: tbuf[--len] = '\0';
973:
974: ef = cvs_ent_open(line, O_WRONLY);
975: if (ef == NULL)
976: return (-1);
977:
978: cvs_ent_add(ef, ep);
979: cvs_ent_close(ef);
1.1 jfb 980: }
981: else if (type == CVS_RESP_UPDEXIST) {
982: }
983: else if (type == CVS_RESP_UPDATED) {
984: }
985:
1.14 ! jfb 986: fbuf = cvs_recvfile(root, &fmode);
! 987: if (fbuf == NULL)
1.1 jfb 988: return (-1);
1.14 ! jfb 989:
! 990: cvs_buf_write(fbuf, path, fmode);
1.1 jfb 991:
992: /* now see if there is a checksum */
1.6 jfb 993: if (cvs_fcksum != NULL) {
1.14 ! jfb 994: if (cvs_cksum(path, cksum_buf, sizeof(cksum_buf)) < 0) {
1.6 jfb 995: }
996:
997: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
998: cvs_log(LP_ERR, "checksum error on received file");
999: (void)unlink(line);
1000: }
1.1 jfb 1001:
1.6 jfb 1002: free(cvs_fcksum);
1003: cvs_fcksum = NULL;
1.1 jfb 1004: }
1005:
1006: return (0);
1007: }
1008:
1009:
1010: /*
1011: * cvs_resp_removed()
1012: *
1013: * Handler for the `Updated' response.
1014: */
1015:
1016: static int
1.13 jfb 1017: cvs_resp_removed(struct cvsroot *root, int type, char *line)
1.1 jfb 1018: {
1019: return (0);
1020: }
1021:
1022:
1023: /*
1024: * cvs_resp_mode()
1025: *
1026: * Handler for the `Mode' response.
1027: */
1028:
1029: static int
1.13 jfb 1030: cvs_resp_mode(struct cvsroot *root, int type, char *line)
1.1 jfb 1031: {
1032: if (cvs_strtomode(line, &cvs_lastmode) < 0) {
1033: return (-1);
1034: }
1.5 jfb 1035: return (0);
1036: }
1037:
1038:
1039: /*
1040: * cvs_resp_modxpand()
1041: *
1042: * Handler for the `Module-expansion' response.
1043: */
1044:
1045: static int
1.13 jfb 1046: cvs_resp_modxpand(struct cvsroot *root, int type, char *line)
1.5 jfb 1047: {
1.1 jfb 1048: return (0);
1049: }
1050:
1.14 ! jfb 1051: /*
! 1052: * cvs_resp_rcsdiff()
! 1053: *
! 1054: * Handler for the `Rcs-diff' response.
! 1055: */
! 1056:
! 1057: static int
! 1058: cvs_resp_rcsdiff(struct cvsroot *root, int type, char *line)
! 1059: {
! 1060: char file[MAXPATHLEN], buf[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
! 1061: char *fname, *orig, *patch;
! 1062: mode_t fmode;
! 1063: BUF *res, *fcont, *patchbuf;
! 1064: CVSENTRIES *entf;
! 1065: struct cvs_ent *ent;
! 1066:
! 1067: /* get remote path and build local path of file to be patched */
! 1068: cvs_getln(root, buf, sizeof(buf));
! 1069: fname = strrchr(buf, '/');
! 1070: if (fname == NULL)
! 1071: fname = buf;
! 1072: snprintf(file, sizeof(file), "%s%s", line, fname);
! 1073: printf("FILE TO PATCH: %s\n", file);
! 1074:
! 1075: /* get updated entry fields */
! 1076: cvs_getln(root, buf, sizeof(buf));
! 1077: ent = cvs_ent_parse(buf);
! 1078: if (ent == NULL) {
! 1079: return (-1);
! 1080: }
! 1081:
! 1082: patchbuf = cvs_recvfile(root, &fmode);
! 1083: fcont = cvs_buf_load(file, BUF_AUTOEXT);
! 1084: if (fcont == NULL)
! 1085: return (-1);
! 1086:
! 1087: cvs_buf_putc(patchbuf, '\0');
! 1088: cvs_buf_putc(fcont, '\0');
! 1089: orig = cvs_buf_release(fcont);
! 1090: patch = cvs_buf_release(patchbuf);
! 1091:
! 1092: res = rcs_patch(orig, patch);
! 1093: if (res == NULL)
! 1094: return (-1);
! 1095:
! 1096: cvs_buf_write(res, file, fmode);
! 1097:
! 1098: /* now see if there is a checksum */
! 1099: if (cvs_fcksum != NULL) {
! 1100: if (cvs_cksum(file, cksum_buf, sizeof(cksum_buf)) < 0) {
! 1101: }
! 1102:
! 1103: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
! 1104: cvs_log(LP_ERR, "checksum error on received file");
! 1105: (void)unlink(file);
! 1106: }
! 1107:
! 1108: free(cvs_fcksum);
! 1109: cvs_fcksum = NULL;
! 1110: }
! 1111:
! 1112: /* update revision in entries */
! 1113: entf = cvs_ent_open(line, O_WRONLY);
! 1114: if (entf == NULL)
! 1115: return (-1);
! 1116:
! 1117: cvs_ent_close(entf);
! 1118:
! 1119: return (0);
! 1120: }
! 1121:
1.1 jfb 1122:
1123: /*
1124: * cvs_sendfile()
1125: *
1126: * Send the mode and size of a file followed by the file's contents.
1127: * Returns 0 on success, or -1 on failure.
1128: */
1129:
1130: int
1.13 jfb 1131: cvs_sendfile(struct cvsroot *root, const char *path)
1.1 jfb 1132: {
1133: int fd;
1134: ssize_t ret;
1135: char buf[4096];
1136: struct stat st;
1137:
1138: if (stat(path, &st) == -1) {
1139: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
1140: return (-1);
1141: }
1142:
1143: fd = open(path, O_RDONLY, 0);
1144: if (fd == -1) {
1145: return (-1);
1146: }
1147:
1148: if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
1149: return (-1);
1150:
1.13 jfb 1151: cvs_sendln(root, buf);
1.1 jfb 1152: snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
1.13 jfb 1153: cvs_sendln(root, buf);
1.1 jfb 1154:
1155: while ((ret = read(fd, buf, sizeof(buf))) != 0) {
1156: if (ret == -1) {
1157: cvs_log(LP_ERRNO, "failed to read file `%s'", path);
1158: return (-1);
1159: }
1160:
1.13 jfb 1161: cvs_sendraw(root, buf, (size_t)ret);
1.1 jfb 1162:
1163: }
1164:
1165: (void)close(fd);
1166:
1167: return (0);
1168: }
1169:
1170:
1171: /*
1172: * cvs_recvfile()
1173: *
1174: * Receive the mode and size of a file followed the file's contents and
1175: * create or update the file whose path is <path> with the received
1176: * information.
1177: */
1178:
1.14 ! jfb 1179: BUF*
! 1180: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 1181: {
1182: size_t len;
1183: ssize_t ret;
1184: off_t fsz, cnt;
1185: char buf[4096], *ep;
1.14 ! jfb 1186: BUF *fbuf;
! 1187:
! 1188: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
! 1189: if (fbuf == NULL)
! 1190: return (NULL);
1.1 jfb 1191:
1.13 jfb 1192: if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14 ! jfb 1193: (cvs_strtomode(buf, mode) < 0)) {
! 1194: return (NULL);
1.1 jfb 1195: }
1196:
1.13 jfb 1197: cvs_getln(root, buf, sizeof(buf));
1.1 jfb 1198:
1199: fsz = (off_t)strtol(buf, &ep, 10);
1200: if (*ep != '\0') {
1201: cvs_log(LP_ERR, "parse error in file size transmission");
1.14 ! jfb 1202: return (NULL);
1.1 jfb 1203: }
1204:
1205: cnt = 0;
1206: do {
1207: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 1208: if (len == 0)
1209: break;
1.13 jfb 1210: ret = cvs_recvraw(root, buf, len);
1.1 jfb 1211: if (ret == -1) {
1.14 ! jfb 1212: cvs_buf_free(fbuf);
! 1213: return (NULL);
1.1 jfb 1214: }
1215:
1.14 ! jfb 1216: if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
! 1217: cvs_log(LP_ERR,
! 1218: "failed to append received file data");
! 1219: cvs_buf_free(fbuf);
! 1220: return (NULL);
1.1 jfb 1221: }
1222:
1223: cnt += (off_t)ret;
1224: } while (cnt < fsz);
1225:
1.14 ! jfb 1226: return (fbuf);
1.1 jfb 1227: }
1.12 jfb 1228:
1229:
1230: /*
1231: * cvs_sendreq()
1232: *
1233: * Send a request to the server of type <rid>, with optional arguments
1234: * contained in <arg>, which should not be terminated by a newline.
1235: * Returns 0 on success, or -1 on failure.
1236: */
1237:
1238: int
1.13 jfb 1239: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 1240: {
1241: int ret;
1.13 jfb 1242: struct cvs_req *req;
1.12 jfb 1243:
1244: if (root->cr_srvin == NULL) {
1245: cvs_log(LP_ERR, "cannot send request: Not connected");
1246: return (-1);
1247: }
1248:
1.13 jfb 1249: req = cvs_req_getbyid(rid);
1250: if (req == NULL) {
1.12 jfb 1251: cvs_log(LP_ERR, "unsupported request type %u", rid);
1252: return (-1);
1253: }
1254:
1.13 jfb 1255: snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
1256: req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.12 jfb 1257:
1258: if (cvs_server_inlog != NULL)
1.13 jfb 1259: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 1260:
1.13 jfb 1261: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12 jfb 1262: if (ret == EOF) {
1263: cvs_log(LP_ERRNO, "failed to send request to server");
1264: return (-1);
1265: }
1266:
1.13 jfb 1267: if (req->req_flags & CVS_REQF_RESP)
1.12 jfb 1268: ret = cvs_getresp(root);
1269:
1.13 jfb 1270: return (ret);
1.12 jfb 1271: }
1272:
1273:
1274: /*
1275: * cvs_getresp()
1276: *
1277: * Get a response from the server. This call will actually read and handle
1278: * responses from the server until one of the response handlers returns
1279: * non-zero (either an error occured or the end of the response was reached).
1280: * Returns the number of handled commands on success, or -1 on failure.
1281: */
1282:
1283: int
1.13 jfb 1284: cvs_getresp(struct cvsroot *root)
1.12 jfb 1285: {
1.13 jfb 1286: int nbcmd, ret;
1287: size_t len;
1.12 jfb 1288:
1289: nbcmd = 0;
1290:
1291: do {
1292: /* wait for incoming data */
1.13 jfb 1293: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 1294: root->cr_srvout) == NULL) {
1295: if (feof(root->cr_srvout))
1296: return (0);
1297: cvs_log(LP_ERRNO,
1298: "failed to read response from server");
1299: return (-1);
1300: }
1301:
1302: if (cvs_server_outlog != NULL)
1.13 jfb 1303: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 1304:
1.13 jfb 1305: if ((len = strlen(cvs_proto_buf)) != 0) {
1306: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 1307: /* truncated line */
1308: }
1309: else
1.13 jfb 1310: cvs_proto_buf[--len] = '\0';
1.12 jfb 1311: }
1312:
1.13 jfb 1313: ret = cvs_resp_handle(root, cvs_proto_buf);
1.12 jfb 1314: nbcmd++;
1315: } while (ret == 0);
1316:
1317: if (ret > 0)
1318: ret = nbcmd;
1319: return (ret);
1320: }
1321:
1322:
1323: /*
1.13 jfb 1324: * cvs_getln()
1325: *
1326: * Get a line from the server's output and store it in <lbuf>. The terminating
1327: * newline character is stripped from the result.
1328: */
1329:
1330: int
1331: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
1332: {
1333: size_t rlen;
1334:
1335: if (fgets(lbuf, len, root->cr_srvout) == NULL) {
1336: if (ferror(root->cr_srvout)) {
1337: cvs_log(LP_ERRNO, "failed to read line from server");
1338: return (-1);
1339: }
1340:
1341: if (feof(root->cr_srvout))
1342: *lbuf = '\0';
1343: }
1344:
1345: if (cvs_server_outlog != NULL)
1346: fputs(lbuf, cvs_server_outlog);
1347:
1348: rlen = strlen(lbuf);
1349: if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
1350: lbuf[--rlen] = '\0';
1351:
1352: return (0);
1353: }
1354:
1355: #ifdef notyet
1356: /*
1.12 jfb 1357: * cvs_sendresp()
1358: *
1359: * Send a response to the client of type <rid>, with optional arguments
1360: * contained in <arg>, which should not be terminated by a newline.
1361: * Returns 0 on success, or -1 on failure.
1362: */
1363:
1364: int
1365: cvs_sendresp(u_int rid, const char *arg)
1366: {
1367: int ret;
1368: size_t len;
1369: const char *resp;
1370:
1371: resp = cvs_resp_getbyid(rid);
1.13 jfb 1372: if (resp == NULL) {
1.12 jfb 1373: cvs_log(LP_ERR, "unsupported response type %u", rid);
1374: return (-1);
1375: }
1376:
1.13 jfb 1377: snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s %s\n", resp,
1.12 jfb 1378: (arg == NULL) ? "" : arg);
1379:
1380: ret = fputs(resp, stdout);
1381: if (ret == EOF) {
1382: cvs_log(LP_ERRNO, "failed to send response to client");
1383: }
1384: else {
1385: if (arg != NULL)
1386: ret = fprintf(stdout, " %s", arg);
1387: putc('\n', stdout);
1388: }
1389: return (0);
1390: }
1391:
1392:
1393: /*
1394: * cvs_getreq()
1395: *
1396: * Get a request from the client.
1397: */
1398:
1399: int
1400: cvs_getreq(void)
1401: {
1402: int nbcmd;
1403:
1404: nbcmd = 0;
1405:
1406: do {
1407: /* wait for incoming data */
1.13 jfb 1408: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 1409: stdin) == NULL) {
1410: if (feof(stdin))
1411: return (0);
1412: cvs_log(LP_ERRNO,
1413: "failed to read request from client");
1414: return (-1);
1415: }
1416:
1.13 jfb 1417: if ((len = strlen(cvs_proto_buf)) != 0) {
1418: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 1419: /* truncated line */
1420: }
1421: else
1.13 jfb 1422: cvs_proto_buf[--len] = '\0';
1.12 jfb 1423: }
1424:
1.13 jfb 1425: ret = cvs_resp_handle(cvs_proto_buf);
1.12 jfb 1426: } while (ret == 0);
1427: }
1428: #endif
1429:
1430:
1431: /*
1432: * cvs_sendln()
1433: *
1434: * Send a single line <line> string to the server. The line is sent as is,
1435: * without any modifications.
1436: * Returns 0 on success, or -1 on failure.
1437: */
1438:
1439: int
1440: cvs_sendln(struct cvsroot *root, const char *line)
1441: {
1442: int nl;
1443: size_t len;
1444:
1445: nl = 0;
1446: len = strlen(line);
1447:
1448: if ((len > 0) && (line[len - 1] != '\n'))
1449: nl = 1;
1450:
1451: if (cvs_server_inlog != NULL) {
1452: fputs(line, cvs_server_inlog);
1453: if (nl)
1454: fputc('\n', cvs_server_inlog);
1455: }
1456: fputs(line, root->cr_srvin);
1457: if (nl)
1458: fputc('\n', root->cr_srvin);
1459:
1460: return (0);
1461: }
1462:
1463:
1464: /*
1.13 jfb 1465: * cvs_sendraw()
1.12 jfb 1466: *
1467: * Send the first <len> bytes from the buffer <src> to the server.
1468: */
1469:
1470: int
1.13 jfb 1471: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12 jfb 1472: {
1473: if (cvs_server_inlog != NULL)
1474: fwrite(src, sizeof(char), len, cvs_server_inlog);
1.13 jfb 1475: if (fwrite(src, sizeof(char), len, root->cr_srvin) < len) {
1.12 jfb 1476: return (-1);
1477: }
1478:
1479: return (0);
1480: }
1481:
1482:
1483: /*
1.13 jfb 1484: * cvs_recvraw()
1.12 jfb 1485: *
1486: * Receive the first <len> bytes from the buffer <src> to the server.
1487: */
1488:
1489: ssize_t
1.13 jfb 1490: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 1491: {
1492: size_t ret;
1493:
1.13 jfb 1494: ret = fread(dst, sizeof(char), len, root->cr_srvout);
1.12 jfb 1495: if (ret == 0)
1496: return (-1);
1497: if (cvs_server_outlog != NULL)
1498: fwrite(dst, sizeof(char), len, cvs_server_outlog);
1499: return (ssize_t)ret;
1500: }
1501:
1502:
1503: /*
1.13 jfb 1504: * cvs_senddir()
1.12 jfb 1505: *
1506: * Send a `Directory' request along with the 2 paths that follow it.
1507: */
1508:
1509: int
1.13 jfb 1510: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 1511: {
1.13 jfb 1512: char buf[MAXPATHLEN];
1.12 jfb 1513:
1.13 jfb 1514: if (dir->cf_ddat->cd_repo == NULL)
1515: strlcpy(buf, root->cr_dir, sizeof(buf));
1516: else
1517: snprintf(buf, sizeof(buf), "%s/%s", root->cr_dir,
1518: dir->cf_ddat->cd_repo);
1.12 jfb 1519:
1.13 jfb 1520: if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, dir->cf_path) < 0) ||
1521: (cvs_sendln(root, buf) < 0))
1.12 jfb 1522: return (-1);
1523:
1524: return (0);
1525: }
1526:
1527:
1528: /*
1.13 jfb 1529: * cvs_sendarg()
1.12 jfb 1530: *
1531: * Send the argument <arg> to the server. The argument <append> is used to
1532: * determine if the argument should be simply appended to the last argument
1533: * sent or if it should be created as a new argument (0).
1534: */
1535:
1536: int
1.13 jfb 1537: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12 jfb 1538: {
1.13 jfb 1539: return cvs_sendreq(root, ((append == 0) ?
1540: CVS_REQ_ARGUMENT : CVS_REQ_ARGUMENTX), arg);
1.12 jfb 1541: }
1542:
1543:
1544: /*
1.13 jfb 1545: * cvs_sendentry()
1.12 jfb 1546: *
1547: * Send an `Entry' request to the server along with the mandatory fields from
1548: * the CVS entry <ent> (which are the name and revision).
1549: */
1550:
1551: int
1.13 jfb 1552: cvs_sendentry(struct cvsroot *root, const struct cvs_ent *ent)
1.12 jfb 1553: {
1554: char ebuf[128], numbuf[64];
1555:
1556: snprintf(ebuf, sizeof(ebuf), "/%s/%s///", ent->ce_name,
1557: rcsnum_tostr(ent->ce_rev, numbuf, sizeof(numbuf)));
1558:
1.13 jfb 1559: return cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12 jfb 1560: }
1561:
1562:
1563: /*
1.13 jfb 1564: * cvs_initlog()
1.12 jfb 1565: *
1566: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1567: * set. In this case, the variable's value is used as a path to which the
1568: * appropriate suffix is added (".in" for server input and ".out" for server
1569: * output.
1570: * Returns 0 on success, or -1 on failure.
1571: */
1572:
1573: static int
1.13 jfb 1574: cvs_initlog(void)
1.12 jfb 1575: {
1576: char *env, fpath[MAXPATHLEN];
1577:
1.13 jfb 1578: /* avoid doing it more than once */
1579: if (cvs_server_logon)
1580: return (0);
1581:
1.12 jfb 1582: env = getenv("CVS_CLIENT_LOG");
1583: if (env == NULL)
1584: return (0);
1585:
1586: strlcpy(fpath, env, sizeof(fpath));
1587: strlcat(fpath, ".in", sizeof(fpath));
1588: cvs_server_inlog = fopen(fpath, "w");
1589: if (cvs_server_inlog == NULL) {
1590: cvs_log(LP_ERRNO, "failed to open server input log `%s'",
1591: fpath);
1592: return (-1);
1593: }
1594:
1595: strlcpy(fpath, env, sizeof(fpath));
1596: strlcat(fpath, ".out", sizeof(fpath));
1597: cvs_server_outlog = fopen(fpath, "w");
1598: if (cvs_server_outlog == NULL) {
1599: cvs_log(LP_ERRNO, "failed to open server output log `%s'",
1600: fpath);
1601: return (-1);
1602: }
1603:
1604: /* make the streams line-buffered */
1605: setvbuf(cvs_server_inlog, NULL, _IOLBF, 0);
1606: setvbuf(cvs_server_outlog, NULL, _IOLBF, 0);
1607:
1.13 jfb 1608: cvs_server_logon = 1;
1.12 jfb 1609:
1610: return (0);
1611: }