Annotation of src/usr.bin/cvs/proto.c, Revision 1.15
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 },
1.15 ! jfb 179: { CVS_RESP_MERGED, "Merged", cvs_resp_updated },
1.1 jfb 180: { CVS_RESP_REMOVED, "Removed", cvs_resp_removed },
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: {
1.15 ! jfb 822: char entbuf[128];
1.1 jfb 823: CVSENTRIES *entfile;
824:
825: /* get the remote path */
1.13 jfb 826: cvs_getln(root, entbuf, sizeof(entbuf));
1.1 jfb 827:
828: /* get the new Entries line */
1.13 jfb 829: if (cvs_getln(root, entbuf, sizeof(entbuf)) < 0)
1.1 jfb 830: return (-1);
831:
1.15 ! jfb 832: entfile = cvs_ent_open(line, O_WRONLY);
1.1 jfb 833: if (entfile == NULL)
834: return (-1);
1.3 jfb 835: cvs_ent_addln(entfile, entbuf);
1.1 jfb 836: cvs_ent_close(entfile);
837:
838: return (0);
839: }
840:
841:
842: /*
843: * cvs_resp_cksum()
844: *
845: * Handler for the `Checksum' response. We store the checksum received for
846: * the next file in a dynamically-allocated buffer pointed to by <cvs_fcksum>.
847: * Upon next file reception, the handler checks to see if there is a stored
848: * checksum.
849: * The file handler must make sure that the checksums match and free the
850: * checksum buffer once it's done to indicate there is no further checksum.
851: */
852:
853: static int
1.13 jfb 854: cvs_resp_cksum(struct cvsroot *root, int type, char *line)
1.1 jfb 855: {
856: if (cvs_fcksum != NULL) {
857: cvs_log(LP_WARN, "unused checksum");
858: free(cvs_fcksum);
859: }
860:
861: cvs_fcksum = strdup(line);
862: if (cvs_fcksum == NULL) {
863: cvs_log(LP_ERRNO, "failed to copy checksum string");
864: return (-1);
865: }
866:
1.4 jfb 867: return (0);
868: }
869:
870:
871: /*
872: * cvs_resp_modtime()
873: *
874: * Handler for the `Mod-time' file update modifying response. The timestamp
875: * given is used to set the last modification time on the next file that
876: * will be received.
877: */
878:
879: static int
1.13 jfb 880: cvs_resp_modtime(struct cvsroot *root, int type, char *line)
1.4 jfb 881: {
1.8 jfb 882: int i;
883: long off;
884: char sign, mon[8], gmt[8], hr[4], min[4], *ep;
885: struct tm cvs_tm;
886:
887: memset(&cvs_tm, 0, sizeof(cvs_tm));
1.9 jfb 888: sscanf(line, "%d %3s %d %2d:%2d:%2d %5s", &cvs_tm.tm_mday, mon,
1.8 jfb 889: &cvs_tm.tm_year, &cvs_tm.tm_hour, &cvs_tm.tm_min,
890: &cvs_tm.tm_sec, gmt);
891: cvs_tm.tm_year -= 1900;
1.9 jfb 892: cvs_tm.tm_isdst = -1;
1.8 jfb 893:
894: if (*gmt == '-') {
1.9 jfb 895: sscanf(gmt, "%c%2s%2s", &sign, hr, min);
1.8 jfb 896: cvs_tm.tm_gmtoff = strtol(hr, &ep, 10);
897: if ((cvs_tm.tm_gmtoff == LONG_MIN) ||
898: (cvs_tm.tm_gmtoff == LONG_MAX) ||
899: (*ep != '\0')) {
900: cvs_log(LP_ERR,
901: "parse error in GMT hours specification `%s'", hr);
902: cvs_tm.tm_gmtoff = 0;
903: }
904: else {
905: /* get seconds */
906: cvs_tm.tm_gmtoff *= 3600;
1.7 jfb 907:
1.8 jfb 908: /* add the minutes */
909: off = strtol(min, &ep, 10);
910: if ((cvs_tm.tm_gmtoff == LONG_MIN) ||
911: (cvs_tm.tm_gmtoff == LONG_MAX) ||
912: (*ep != '\0')) {
913: cvs_log(LP_ERR,
914: "parse error in GMT minutes "
915: "specification `%s'", min);
916: }
917: else
918: cvs_tm.tm_gmtoff += off * 60;
919: }
920: }
921: if (sign == '-')
922: cvs_tm.tm_gmtoff = -cvs_tm.tm_gmtoff;
1.7 jfb 923:
1.8 jfb 924: for (i = 0; i < (int)(sizeof(cvs_months)/sizeof(cvs_months[0])); i++) {
925: if (strcmp(cvs_months[i], mon) == 0) {
926: cvs_tm.tm_mon = i;
927: break;
928: }
1.7 jfb 929: }
930:
1.8 jfb 931: cvs_modtime = mktime(&cvs_tm);
1.1 jfb 932: return (0);
933: }
934:
935:
936: /*
937: * cvs_resp_updated()
938: *
1.6 jfb 939: * Handler for the `Updated' and `Created' responses.
1.1 jfb 940: */
941:
942: static int
1.13 jfb 943: cvs_resp_updated(struct cvsroot *root, int type, char *line)
1.1 jfb 944: {
1.7 jfb 945: size_t len;
1.14 jfb 946: mode_t fmode;
1.7 jfb 947: char tbuf[32], path[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
1.14 jfb 948: BUF *fbuf;
1.7 jfb 949: CVSENTRIES *ef;
1.6 jfb 950: struct cvs_ent *ep;
1.1 jfb 951:
1.13 jfb 952: ep = NULL;
953:
1.15 ! jfb 954: len = strlen(tbuf);
! 955: if ((len > 0) && (tbuf[len - 1] == '\n'))
! 956: tbuf[--len] = '\0';
! 957:
! 958: /* read the remote path of the file */
! 959: cvs_getln(root, path, sizeof(path));
! 960:
! 961: /* read the new entry */
! 962: cvs_getln(root, path, sizeof(path));
! 963: ep = cvs_ent_parse(path);
! 964: if (ep == NULL)
! 965: return (-1);
! 966: snprintf(path, sizeof(path), "%s/%s", line, ep->ce_name);
1.6 jfb 967:
1.14 jfb 968:
1.15 ! jfb 969: if (type == CVS_RESP_CREATED) {
1.7 jfb 970: /* set the timestamp as the last one received from Mod-time */
1.8 jfb 971: ep->ce_timestamp = ctime_r(&cvs_modtime, tbuf);
1.7 jfb 972:
973: ef = cvs_ent_open(line, O_WRONLY);
974: if (ef == NULL)
975: return (-1);
976:
977: cvs_ent_add(ef, ep);
978: cvs_ent_close(ef);
1.1 jfb 979: }
980: else if (type == CVS_RESP_UPDEXIST) {
981: }
982: else if (type == CVS_RESP_UPDATED) {
983: }
984:
1.14 jfb 985: fbuf = cvs_recvfile(root, &fmode);
986: if (fbuf == NULL)
1.1 jfb 987: return (-1);
1.14 jfb 988:
989: cvs_buf_write(fbuf, path, fmode);
1.1 jfb 990:
991: /* now see if there is a checksum */
1.6 jfb 992: if (cvs_fcksum != NULL) {
1.14 jfb 993: if (cvs_cksum(path, cksum_buf, sizeof(cksum_buf)) < 0) {
1.6 jfb 994: }
995:
996: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
997: cvs_log(LP_ERR, "checksum error on received file");
998: (void)unlink(line);
999: }
1.1 jfb 1000:
1.6 jfb 1001: free(cvs_fcksum);
1002: cvs_fcksum = NULL;
1.1 jfb 1003: }
1004:
1005: return (0);
1006: }
1007:
1008:
1009: /*
1010: * cvs_resp_removed()
1011: *
1012: * Handler for the `Updated' response.
1013: */
1014:
1015: static int
1.13 jfb 1016: cvs_resp_removed(struct cvsroot *root, int type, char *line)
1.1 jfb 1017: {
1018: return (0);
1019: }
1020:
1021:
1022: /*
1023: * cvs_resp_mode()
1024: *
1025: * Handler for the `Mode' response.
1026: */
1027:
1028: static int
1.13 jfb 1029: cvs_resp_mode(struct cvsroot *root, int type, char *line)
1.1 jfb 1030: {
1031: if (cvs_strtomode(line, &cvs_lastmode) < 0) {
1032: return (-1);
1033: }
1.5 jfb 1034: return (0);
1035: }
1036:
1037:
1038: /*
1039: * cvs_resp_modxpand()
1040: *
1041: * Handler for the `Module-expansion' response.
1042: */
1043:
1044: static int
1.13 jfb 1045: cvs_resp_modxpand(struct cvsroot *root, int type, char *line)
1.5 jfb 1046: {
1.1 jfb 1047: return (0);
1048: }
1049:
1.14 jfb 1050: /*
1051: * cvs_resp_rcsdiff()
1052: *
1053: * Handler for the `Rcs-diff' response.
1054: */
1055:
1056: static int
1057: cvs_resp_rcsdiff(struct cvsroot *root, int type, char *line)
1058: {
1059: char file[MAXPATHLEN], buf[MAXPATHLEN], cksum_buf[CVS_CKSUM_LEN];
1060: char *fname, *orig, *patch;
1061: mode_t fmode;
1062: BUF *res, *fcont, *patchbuf;
1063: CVSENTRIES *entf;
1064: struct cvs_ent *ent;
1065:
1066: /* get remote path and build local path of file to be patched */
1067: cvs_getln(root, buf, sizeof(buf));
1068: fname = strrchr(buf, '/');
1069: if (fname == NULL)
1070: fname = buf;
1071: snprintf(file, sizeof(file), "%s%s", line, fname);
1072:
1073: /* get updated entry fields */
1074: cvs_getln(root, buf, sizeof(buf));
1075: ent = cvs_ent_parse(buf);
1076: if (ent == NULL) {
1077: return (-1);
1078: }
1079:
1080: patchbuf = cvs_recvfile(root, &fmode);
1081: fcont = cvs_buf_load(file, BUF_AUTOEXT);
1082: if (fcont == NULL)
1083: return (-1);
1084:
1085: cvs_buf_putc(patchbuf, '\0');
1086: cvs_buf_putc(fcont, '\0');
1087: orig = cvs_buf_release(fcont);
1088: patch = cvs_buf_release(patchbuf);
1089:
1090: res = rcs_patch(orig, patch);
1091: if (res == NULL)
1092: return (-1);
1093:
1094: cvs_buf_write(res, file, fmode);
1095:
1096: /* now see if there is a checksum */
1097: if (cvs_fcksum != NULL) {
1098: if (cvs_cksum(file, cksum_buf, sizeof(cksum_buf)) < 0) {
1099: }
1100:
1101: if (strcmp(cksum_buf, cvs_fcksum) != 0) {
1102: cvs_log(LP_ERR, "checksum error on received file");
1103: (void)unlink(file);
1104: }
1105:
1106: free(cvs_fcksum);
1107: cvs_fcksum = NULL;
1108: }
1109:
1110: /* update revision in entries */
1111: entf = cvs_ent_open(line, O_WRONLY);
1112: if (entf == NULL)
1113: return (-1);
1114:
1115: cvs_ent_close(entf);
1116:
1117: return (0);
1118: }
1119:
1.1 jfb 1120:
1121: /*
1122: * cvs_sendfile()
1123: *
1124: * Send the mode and size of a file followed by the file's contents.
1125: * Returns 0 on success, or -1 on failure.
1126: */
1127:
1128: int
1.13 jfb 1129: cvs_sendfile(struct cvsroot *root, const char *path)
1.1 jfb 1130: {
1131: int fd;
1132: ssize_t ret;
1133: char buf[4096];
1134: struct stat st;
1135:
1136: if (stat(path, &st) == -1) {
1137: cvs_log(LP_ERRNO, "failed to stat `%s'", path);
1138: return (-1);
1139: }
1140:
1141: fd = open(path, O_RDONLY, 0);
1142: if (fd == -1) {
1143: return (-1);
1144: }
1145:
1146: if (cvs_modetostr(st.st_mode, buf, sizeof(buf)) < 0)
1147: return (-1);
1148:
1.13 jfb 1149: cvs_sendln(root, buf);
1.1 jfb 1150: snprintf(buf, sizeof(buf), "%lld\n", st.st_size);
1.13 jfb 1151: cvs_sendln(root, buf);
1.1 jfb 1152:
1153: while ((ret = read(fd, buf, sizeof(buf))) != 0) {
1154: if (ret == -1) {
1155: cvs_log(LP_ERRNO, "failed to read file `%s'", path);
1156: return (-1);
1157: }
1158:
1.13 jfb 1159: cvs_sendraw(root, buf, (size_t)ret);
1.1 jfb 1160:
1161: }
1162:
1163: (void)close(fd);
1164:
1165: return (0);
1166: }
1167:
1168:
1169: /*
1170: * cvs_recvfile()
1171: *
1172: * Receive the mode and size of a file followed the file's contents and
1173: * create or update the file whose path is <path> with the received
1174: * information.
1175: */
1176:
1.14 jfb 1177: BUF*
1178: cvs_recvfile(struct cvsroot *root, mode_t *mode)
1.1 jfb 1179: {
1180: size_t len;
1181: ssize_t ret;
1182: off_t fsz, cnt;
1183: char buf[4096], *ep;
1.14 jfb 1184: BUF *fbuf;
1185:
1186: fbuf = cvs_buf_alloc(sizeof(buf), BUF_AUTOEXT);
1187: if (fbuf == NULL)
1188: return (NULL);
1.1 jfb 1189:
1.13 jfb 1190: if ((cvs_getln(root, buf, sizeof(buf)) < 0) ||
1.14 jfb 1191: (cvs_strtomode(buf, mode) < 0)) {
1192: return (NULL);
1.1 jfb 1193: }
1194:
1.13 jfb 1195: cvs_getln(root, buf, sizeof(buf));
1.1 jfb 1196:
1197: fsz = (off_t)strtol(buf, &ep, 10);
1198: if (*ep != '\0') {
1199: cvs_log(LP_ERR, "parse error in file size transmission");
1.14 jfb 1200: return (NULL);
1.1 jfb 1201: }
1202:
1203: cnt = 0;
1204: do {
1205: len = MIN(sizeof(buf), (size_t)(fsz - cnt));
1.6 jfb 1206: if (len == 0)
1207: break;
1.13 jfb 1208: ret = cvs_recvraw(root, buf, len);
1.1 jfb 1209: if (ret == -1) {
1.14 jfb 1210: cvs_buf_free(fbuf);
1211: return (NULL);
1.1 jfb 1212: }
1213:
1.14 jfb 1214: if (cvs_buf_append(fbuf, buf, (size_t)ret) == -1) {
1215: cvs_log(LP_ERR,
1216: "failed to append received file data");
1217: cvs_buf_free(fbuf);
1218: return (NULL);
1.1 jfb 1219: }
1220:
1221: cnt += (off_t)ret;
1222: } while (cnt < fsz);
1223:
1.14 jfb 1224: return (fbuf);
1.1 jfb 1225: }
1.12 jfb 1226:
1227:
1228: /*
1229: * cvs_sendreq()
1230: *
1231: * Send a request to the server of type <rid>, with optional arguments
1232: * contained in <arg>, which should not be terminated by a newline.
1233: * Returns 0 on success, or -1 on failure.
1234: */
1235:
1236: int
1.13 jfb 1237: cvs_sendreq(struct cvsroot *root, u_int rid, const char *arg)
1.12 jfb 1238: {
1239: int ret;
1.13 jfb 1240: struct cvs_req *req;
1.12 jfb 1241:
1242: if (root->cr_srvin == NULL) {
1243: cvs_log(LP_ERR, "cannot send request: Not connected");
1244: return (-1);
1245: }
1246:
1.13 jfb 1247: req = cvs_req_getbyid(rid);
1248: if (req == NULL) {
1.12 jfb 1249: cvs_log(LP_ERR, "unsupported request type %u", rid);
1250: return (-1);
1251: }
1252:
1.13 jfb 1253: snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s%s%s\n",
1254: req->req_str, (arg == NULL) ? "" : " ", (arg == NULL) ? "" : arg);
1.12 jfb 1255:
1256: if (cvs_server_inlog != NULL)
1.13 jfb 1257: fputs(cvs_proto_buf, cvs_server_inlog);
1.12 jfb 1258:
1.13 jfb 1259: ret = fputs(cvs_proto_buf, root->cr_srvin);
1.12 jfb 1260: if (ret == EOF) {
1261: cvs_log(LP_ERRNO, "failed to send request to server");
1262: return (-1);
1263: }
1264:
1.13 jfb 1265: if (req->req_flags & CVS_REQF_RESP)
1.12 jfb 1266: ret = cvs_getresp(root);
1267:
1.13 jfb 1268: return (ret);
1.12 jfb 1269: }
1270:
1271:
1272: /*
1273: * cvs_getresp()
1274: *
1275: * Get a response from the server. This call will actually read and handle
1276: * responses from the server until one of the response handlers returns
1277: * non-zero (either an error occured or the end of the response was reached).
1278: * Returns the number of handled commands on success, or -1 on failure.
1279: */
1280:
1281: int
1.13 jfb 1282: cvs_getresp(struct cvsroot *root)
1.12 jfb 1283: {
1.13 jfb 1284: int nbcmd, ret;
1285: size_t len;
1.12 jfb 1286:
1287: nbcmd = 0;
1288:
1289: do {
1290: /* wait for incoming data */
1.13 jfb 1291: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 1292: root->cr_srvout) == NULL) {
1293: if (feof(root->cr_srvout))
1294: return (0);
1295: cvs_log(LP_ERRNO,
1296: "failed to read response from server");
1297: return (-1);
1298: }
1299:
1300: if (cvs_server_outlog != NULL)
1.13 jfb 1301: fputs(cvs_proto_buf, cvs_server_outlog);
1.12 jfb 1302:
1.13 jfb 1303: if ((len = strlen(cvs_proto_buf)) != 0) {
1304: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 1305: /* truncated line */
1306: }
1307: else
1.13 jfb 1308: cvs_proto_buf[--len] = '\0';
1.12 jfb 1309: }
1310:
1.13 jfb 1311: ret = cvs_resp_handle(root, cvs_proto_buf);
1.12 jfb 1312: nbcmd++;
1313: } while (ret == 0);
1314:
1315: if (ret > 0)
1316: ret = nbcmd;
1317: return (ret);
1318: }
1319:
1320:
1321: /*
1.13 jfb 1322: * cvs_getln()
1323: *
1324: * Get a line from the server's output and store it in <lbuf>. The terminating
1325: * newline character is stripped from the result.
1326: */
1327:
1328: int
1329: cvs_getln(struct cvsroot *root, char *lbuf, size_t len)
1330: {
1331: size_t rlen;
1332:
1333: if (fgets(lbuf, len, root->cr_srvout) == NULL) {
1334: if (ferror(root->cr_srvout)) {
1335: cvs_log(LP_ERRNO, "failed to read line from server");
1336: return (-1);
1337: }
1338:
1339: if (feof(root->cr_srvout))
1340: *lbuf = '\0';
1341: }
1342:
1343: if (cvs_server_outlog != NULL)
1344: fputs(lbuf, cvs_server_outlog);
1345:
1346: rlen = strlen(lbuf);
1347: if ((rlen > 0) && (lbuf[rlen - 1] == '\n'))
1348: lbuf[--rlen] = '\0';
1349:
1350: return (0);
1351: }
1352:
1353: #ifdef notyet
1354: /*
1.12 jfb 1355: * cvs_sendresp()
1356: *
1357: * Send a response to the client of type <rid>, with optional arguments
1358: * contained in <arg>, which should not be terminated by a newline.
1359: * Returns 0 on success, or -1 on failure.
1360: */
1361:
1362: int
1363: cvs_sendresp(u_int rid, const char *arg)
1364: {
1365: int ret;
1366: size_t len;
1367: const char *resp;
1368:
1369: resp = cvs_resp_getbyid(rid);
1.13 jfb 1370: if (resp == NULL) {
1.12 jfb 1371: cvs_log(LP_ERR, "unsupported response type %u", rid);
1372: return (-1);
1373: }
1374:
1.13 jfb 1375: snprintf(cvs_proto_buf, sizeof(cvs_proto_buf), "%s %s\n", resp,
1.12 jfb 1376: (arg == NULL) ? "" : arg);
1377:
1378: ret = fputs(resp, stdout);
1379: if (ret == EOF) {
1380: cvs_log(LP_ERRNO, "failed to send response to client");
1381: }
1382: else {
1383: if (arg != NULL)
1384: ret = fprintf(stdout, " %s", arg);
1385: putc('\n', stdout);
1386: }
1387: return (0);
1388: }
1389:
1390:
1391: /*
1392: * cvs_getreq()
1393: *
1394: * Get a request from the client.
1395: */
1396:
1397: int
1398: cvs_getreq(void)
1399: {
1400: int nbcmd;
1401:
1402: nbcmd = 0;
1403:
1404: do {
1405: /* wait for incoming data */
1.13 jfb 1406: if (fgets(cvs_proto_buf, sizeof(cvs_proto_buf),
1.12 jfb 1407: stdin) == NULL) {
1408: if (feof(stdin))
1409: return (0);
1410: cvs_log(LP_ERRNO,
1411: "failed to read request from client");
1412: return (-1);
1413: }
1414:
1.13 jfb 1415: if ((len = strlen(cvs_proto_buf)) != 0) {
1416: if (cvs_proto_buf[len - 1] != '\n') {
1.12 jfb 1417: /* truncated line */
1418: }
1419: else
1.13 jfb 1420: cvs_proto_buf[--len] = '\0';
1.12 jfb 1421: }
1422:
1.13 jfb 1423: ret = cvs_resp_handle(cvs_proto_buf);
1.12 jfb 1424: } while (ret == 0);
1425: }
1426: #endif
1427:
1428:
1429: /*
1430: * cvs_sendln()
1431: *
1432: * Send a single line <line> string to the server. The line is sent as is,
1433: * without any modifications.
1434: * Returns 0 on success, or -1 on failure.
1435: */
1436:
1437: int
1438: cvs_sendln(struct cvsroot *root, const char *line)
1439: {
1440: int nl;
1441: size_t len;
1442:
1443: nl = 0;
1444: len = strlen(line);
1445:
1446: if ((len > 0) && (line[len - 1] != '\n'))
1447: nl = 1;
1448:
1449: if (cvs_server_inlog != NULL) {
1450: fputs(line, cvs_server_inlog);
1451: if (nl)
1452: fputc('\n', cvs_server_inlog);
1453: }
1454: fputs(line, root->cr_srvin);
1455: if (nl)
1456: fputc('\n', root->cr_srvin);
1457:
1458: return (0);
1459: }
1460:
1461:
1462: /*
1.13 jfb 1463: * cvs_sendraw()
1.12 jfb 1464: *
1465: * Send the first <len> bytes from the buffer <src> to the server.
1466: */
1467:
1468: int
1.13 jfb 1469: cvs_sendraw(struct cvsroot *root, const void *src, size_t len)
1.12 jfb 1470: {
1471: if (cvs_server_inlog != NULL)
1472: fwrite(src, sizeof(char), len, cvs_server_inlog);
1.13 jfb 1473: if (fwrite(src, sizeof(char), len, root->cr_srvin) < len) {
1.12 jfb 1474: return (-1);
1475: }
1476:
1477: return (0);
1478: }
1479:
1480:
1481: /*
1.13 jfb 1482: * cvs_recvraw()
1.12 jfb 1483: *
1484: * Receive the first <len> bytes from the buffer <src> to the server.
1485: */
1486:
1487: ssize_t
1.13 jfb 1488: cvs_recvraw(struct cvsroot *root, void *dst, size_t len)
1.12 jfb 1489: {
1490: size_t ret;
1491:
1.13 jfb 1492: ret = fread(dst, sizeof(char), len, root->cr_srvout);
1.12 jfb 1493: if (ret == 0)
1494: return (-1);
1495: if (cvs_server_outlog != NULL)
1496: fwrite(dst, sizeof(char), len, cvs_server_outlog);
1497: return (ssize_t)ret;
1498: }
1499:
1500:
1501: /*
1.13 jfb 1502: * cvs_senddir()
1.12 jfb 1503: *
1504: * Send a `Directory' request along with the 2 paths that follow it.
1505: */
1506:
1507: int
1.13 jfb 1508: cvs_senddir(struct cvsroot *root, CVSFILE *dir)
1.12 jfb 1509: {
1.13 jfb 1510: char buf[MAXPATHLEN];
1.12 jfb 1511:
1.13 jfb 1512: if (dir->cf_ddat->cd_repo == NULL)
1513: strlcpy(buf, root->cr_dir, sizeof(buf));
1514: else
1515: snprintf(buf, sizeof(buf), "%s/%s", root->cr_dir,
1516: dir->cf_ddat->cd_repo);
1.12 jfb 1517:
1.13 jfb 1518: if ((cvs_sendreq(root, CVS_REQ_DIRECTORY, dir->cf_path) < 0) ||
1519: (cvs_sendln(root, buf) < 0))
1.12 jfb 1520: return (-1);
1521:
1522: return (0);
1523: }
1524:
1525:
1526: /*
1.13 jfb 1527: * cvs_sendarg()
1.12 jfb 1528: *
1529: * Send the argument <arg> to the server. The argument <append> is used to
1530: * determine if the argument should be simply appended to the last argument
1531: * sent or if it should be created as a new argument (0).
1532: */
1533:
1534: int
1.13 jfb 1535: cvs_sendarg(struct cvsroot *root, const char *arg, int append)
1.12 jfb 1536: {
1.13 jfb 1537: return cvs_sendreq(root, ((append == 0) ?
1538: CVS_REQ_ARGUMENT : CVS_REQ_ARGUMENTX), arg);
1.12 jfb 1539: }
1540:
1541:
1542: /*
1.13 jfb 1543: * cvs_sendentry()
1.12 jfb 1544: *
1545: * Send an `Entry' request to the server along with the mandatory fields from
1546: * the CVS entry <ent> (which are the name and revision).
1547: */
1548:
1549: int
1.13 jfb 1550: cvs_sendentry(struct cvsroot *root, const struct cvs_ent *ent)
1.12 jfb 1551: {
1552: char ebuf[128], numbuf[64];
1553:
1554: snprintf(ebuf, sizeof(ebuf), "/%s/%s///", ent->ce_name,
1555: rcsnum_tostr(ent->ce_rev, numbuf, sizeof(numbuf)));
1556:
1.13 jfb 1557: return cvs_sendreq(root, CVS_REQ_ENTRY, ebuf);
1.12 jfb 1558: }
1559:
1560:
1561: /*
1.13 jfb 1562: * cvs_initlog()
1.12 jfb 1563: *
1564: * Initialize protocol logging if the CVS_CLIENT_LOG environment variable is
1565: * set. In this case, the variable's value is used as a path to which the
1566: * appropriate suffix is added (".in" for server input and ".out" for server
1567: * output.
1568: * Returns 0 on success, or -1 on failure.
1569: */
1570:
1571: static int
1.13 jfb 1572: cvs_initlog(void)
1.12 jfb 1573: {
1574: char *env, fpath[MAXPATHLEN];
1575:
1.13 jfb 1576: /* avoid doing it more than once */
1577: if (cvs_server_logon)
1578: return (0);
1579:
1.12 jfb 1580: env = getenv("CVS_CLIENT_LOG");
1581: if (env == NULL)
1582: return (0);
1583:
1584: strlcpy(fpath, env, sizeof(fpath));
1585: strlcat(fpath, ".in", sizeof(fpath));
1586: cvs_server_inlog = fopen(fpath, "w");
1587: if (cvs_server_inlog == NULL) {
1588: cvs_log(LP_ERRNO, "failed to open server input log `%s'",
1589: fpath);
1590: return (-1);
1591: }
1592:
1593: strlcpy(fpath, env, sizeof(fpath));
1594: strlcat(fpath, ".out", sizeof(fpath));
1595: cvs_server_outlog = fopen(fpath, "w");
1596: if (cvs_server_outlog == NULL) {
1597: cvs_log(LP_ERRNO, "failed to open server output log `%s'",
1598: fpath);
1599: return (-1);
1600: }
1601:
1602: /* make the streams line-buffered */
1603: setvbuf(cvs_server_inlog, NULL, _IOLBF, 0);
1604: setvbuf(cvs_server_outlog, NULL, _IOLBF, 0);
1605:
1.13 jfb 1606: cvs_server_logon = 1;
1.12 jfb 1607:
1608: return (0);
1609: }