Annotation of src/usr.bin/cvs/req.c, Revision 1.30
1.30 ! joris 1: /* $OpenBSD: req.c,v 1.29 2005/07/25 12:13:08 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:
28: #include <sys/types.h>
29: #include <sys/stat.h>
30:
1.20 xsa 31: #include <errno.h>
1.1 jfb 32: #include <fcntl.h>
1.22 joris 33: #include <libgen.h>
1.1 jfb 34: #include <stdio.h>
35: #include <stdlib.h>
1.20 xsa 36: #include <string.h>
1.1 jfb 37: #include <unistd.h>
38:
39: #include "buf.h"
40: #include "cvs.h"
41: #include "log.h"
42: #include "proto.h"
43:
44:
1.22 joris 45: extern char *cvs_rootstr;
1.1 jfb 46: extern int cvs_compress;
47: extern char *cvs_rsh;
48: extern int cvs_trace;
49: extern int cvs_nolog;
50: extern int cvs_readonly;
51:
52:
1.29 xsa 53: static int cvs_req_set(int, char *);
54: static int cvs_req_noop(int, char *);
55: static int cvs_req_root(int, char *);
56: static int cvs_req_validreq(int, char *);
57: static int cvs_req_validresp(int, char *);
58: static int cvs_req_expandmod(int, char *);
59: static int cvs_req_directory(int, char *);
60: static int cvs_req_useunchanged(int, char *);
61: static int cvs_req_case(int, char *);
62: static int cvs_req_argument(int, char *);
63: static int cvs_req_globalopt(int, char *);
64: static int cvs_req_gzipstream(int, char *);
65: static int cvs_req_entry(int, char *);
66: static int cvs_req_filestate(int, char *);
1.11 jfb 67:
1.29 xsa 68: static int cvs_req_command(int, char *);
1.1 jfb 69:
70:
71: struct cvs_reqhdlr {
72: int (*hdlr)(int, char *);
73: } cvs_req_swtab[CVS_REQ_MAX + 1] = {
1.11 jfb 74: { NULL },
75: { cvs_req_root },
76: { cvs_req_validreq },
77: { cvs_req_validresp },
78: { cvs_req_directory },
79: { NULL },
80: { NULL },
81: { NULL },
1.13 jfb 82: { cvs_req_entry },
1.11 jfb 83: { NULL },
84: { NULL }, /* 10 */
1.13 jfb 85: { cvs_req_filestate },
86: { cvs_req_filestate },
87: { cvs_req_filestate },
1.11 jfb 88: { cvs_req_useunchanged },
89: { NULL },
90: { NULL },
1.13 jfb 91: { cvs_req_filestate },
1.11 jfb 92: { cvs_req_case },
93: { NULL },
94: { cvs_req_argument }, /* 20 */
95: { cvs_req_argument },
96: { cvs_req_globalopt },
97: { cvs_req_gzipstream },
98: { NULL },
99: { NULL },
100: { NULL },
101: { NULL },
102: { NULL },
103: { NULL },
104: { NULL }, /* 30 */
105: { NULL },
106: { NULL },
107: { NULL },
108: { cvs_req_set },
1.12 jfb 109: { cvs_req_expandmod },
1.11 jfb 110: { cvs_req_command },
111: { NULL },
112: { NULL },
113: { NULL },
114: { NULL }, /* 40 */
115: { NULL },
116: { NULL },
117: { NULL },
118: { NULL },
1.17 joris 119: { cvs_req_command },
1.11 jfb 120: { cvs_req_command },
121: { NULL },
122: { cvs_req_command },
1.22 joris 123: { cvs_req_command },
1.27 xsa 124: { cvs_req_command }, /* 50 */
125: { cvs_req_command },
126: { cvs_req_command },
127: { cvs_req_command },
128: { cvs_req_command },
129: { cvs_req_command },
1.11 jfb 130: { cvs_req_command },
131: { cvs_req_command },
132: { cvs_req_command },
1.25 xsa 133: { cvs_req_command },
1.11 jfb 134: { cvs_req_command }, /* 60 */
135: { NULL },
136: { cvs_req_command },
137: { cvs_req_command },
1.12 jfb 138: { cvs_req_noop },
1.27 xsa 139: { cvs_req_command },
140: { cvs_req_command },
141: { cvs_req_command },
142: { cvs_req_command },
1.11 jfb 143: { cvs_req_command },
1.1 jfb 144: };
145:
146:
147:
148: /*
1.2 jfb 149: * Argument array built by `Argument' and `Argumentx' requests.
150: */
1.22 joris 151: static char *cvs_req_args[CVS_PROTO_MAXARG];
152:
153: /* start at 1, because 0 will be the command name */
154: static int cvs_req_nargs = 1;
1.2 jfb 155:
1.22 joris 156: static char *cvs_req_modulename;
1.11 jfb 157: static char *cvs_req_rootpath;
1.14 joris 158: static char *cvs_req_currentdir;
159: extern char cvs_server_tmpdir[MAXPATHLEN];
1.22 joris 160: static CVSENTRIES *cvs_req_entf;
1.2 jfb 161:
162: /*
1.1 jfb 163: * cvs_req_handle()
164: *
165: * Generic request handler dispatcher. The handler expects the first line
166: * of the command as single argument.
167: * Returns the return value of the command on success, or -1 on failure.
168: */
169: int
170: cvs_req_handle(char *line)
171: {
172: char *cp, *cmd;
173: struct cvs_req *req;
174:
175: cmd = line;
176:
177: cp = strchr(cmd, ' ');
178: if (cp != NULL)
179: *(cp++) = '\0';
180:
181: req = cvs_req_getbyname(cmd);
182: if (req == NULL)
183: return (-1);
184: else if (cvs_req_swtab[req->req_id].hdlr == NULL) {
1.10 jfb 185: cvs_log(LP_ERR, "handler for `%s' not implemented", cmd);
1.1 jfb 186: return (-1);
187: }
188:
189: return (*cvs_req_swtab[req->req_id].hdlr)(req->req_id, cp);
190: }
191:
1.12 jfb 192: /*
193: * cvs_req_noop()
194: */
195: static int
196: cvs_req_noop(int reqid, char *line)
197: {
198: int ret;
199:
200: ret = cvs_sendresp(CVS_RESP_OK, NULL);
201: if (ret < 0)
202: return (-1);
203: return (0);
204: }
205:
1.1 jfb 206:
207: static int
208: cvs_req_root(int reqid, char *line)
209: {
1.11 jfb 210: if (cvs_req_rootpath != NULL) {
211: cvs_log(LP_ERR, "duplicate Root request received");
1.13 jfb 212: cvs_printf("Protocol error: Duplicate Root request");
1.11 jfb 213: return (-1);
214: }
1.9 jfb 215:
1.11 jfb 216: cvs_req_rootpath = strdup(line);
217: if (cvs_req_rootpath == NULL) {
218: cvs_log(LP_ERRNO, "failed to copy Root path");
1.9 jfb 219: return (-1);
220: }
221:
1.22 joris 222: cvs_rootstr = cvs_req_rootpath;
223:
1.4 jfb 224: return (0);
225: }
226:
227:
228: static int
229: cvs_req_validreq(int reqid, char *line)
230: {
231: char *vreq;
232:
233: vreq = cvs_req_getvalid();
234: if (vreq == NULL)
235: return (-1);
236:
1.11 jfb 237: if ((cvs_sendresp(CVS_RESP_VALIDREQ, vreq) < 0) ||
238: (cvs_sendresp(CVS_RESP_OK, NULL) < 0))
239: return (-1);
1.4 jfb 240:
241: return (0);
242: }
243:
244: static int
245: cvs_req_validresp(int reqid, char *line)
246: {
247: char *sp, *ep;
248: struct cvs_resp *resp;
1.1 jfb 249:
1.4 jfb 250: sp = line;
251: do {
252: ep = strchr(sp, ' ');
253: if (ep != NULL)
254: *(ep++) = '\0';
255:
256: resp = cvs_resp_getbyname(sp);
257: if (resp != NULL)
258: ;
259:
260: if (ep != NULL)
261: sp = ep + 1;
262: } while (ep != NULL);
1.1 jfb 263:
264: return (0);
265: }
266:
267: static int
268: cvs_req_directory(int reqid, char *line)
269: {
1.22 joris 270: int pwd;
271: size_t dirlen;
1.11 jfb 272: char rdir[MAXPATHLEN];
1.22 joris 273: char *repo, *s, *p;
274:
275: pwd = (!strcmp(line, "."));
1.1 jfb 276:
1.11 jfb 277: if (cvs_getln(NULL, rdir, sizeof(rdir)) < 0)
278: return (-1);
279:
1.14 joris 280: if (cvs_req_currentdir != NULL)
281: free(cvs_req_currentdir);
282:
283: cvs_req_currentdir = strdup(rdir);
284: if (cvs_req_currentdir == NULL) {
1.19 xsa 285: cvs_log(LP_ERR, "failed to duplicate directory");
1.14 joris 286: return (-1);
287: }
288:
1.22 joris 289: dirlen = strlen(cvs_req_currentdir);
1.14 joris 290:
1.22 joris 291: /*
292: * Lets make sure we always start at the correct
293: * directory.
294: */
1.28 xsa 295: if (cvs_chdir(cvs_server_tmpdir) == -1)
1.14 joris 296: return (-1);
297:
1.22 joris 298: /*
299: * Set repository path.
300: */
1.30 ! joris 301: if (strlen(cvs_req_rootpath) < dirlen) {
! 302: s = cvs_req_currentdir + strlen(cvs_req_rootpath) + 1;
! 303: if (s >= (cvs_req_currentdir + dirlen)) {
! 304: cvs_log(LP_ERR, "you're bad, go away");
! 305: return (-1);
! 306: }
! 307: } else
! 308: s = cvs_req_currentdir;
1.14 joris 309:
1.22 joris 310: if ((repo = strdup(s)) == NULL) {
311: cvs_log(LP_ERR, "failed to save repository path");
312: return (-1);
313: }
314:
315: /*
316: * Skip back "foo/bar" part, so we can feed the repo
317: * as a startpoint for cvs_create_dir().
318: */
319: if (!pwd) {
320: s = repo + strlen(repo) - strlen(line) - 1;
321: if (*s != '/') {
322: cvs_log(LP_ERR, "malformed directory");
323: free(repo);
324: return (-1);
325: }
326:
327: *s = '\0';
328: }
329:
330: /*
331: * Obtain the modulename, we only need to do this at
332: * the very first time we get a Directory request.
333: */
334: if (cvs_req_modulename == NULL) {
335: if ((p = strchr(repo, '/')) != NULL)
336: *p = '\0';
337:
338: if ((cvs_req_modulename = strdup(repo)) == NULL) {
339: cvs_log(LP_ERR, "failed to save modulename");
340: free(repo);
341: return (-1);
342: }
343:
344: if (p != NULL)
345: *p = '/';
346:
347: /*
348: * Now, create the admin files in the top-level
349: * directory for the temp repo.
350: */
351: if (cvs_mkadmin(cvs_server_tmpdir, cvs_rootstr, repo) < 0) {
352: cvs_log(LP_ERR, "failed to create admin files");
353: free(repo);
354: return (-1);
355: }
356: }
357:
358: /*
359: * create the directory plus the administrative files.
360: */
361: if (cvs_create_dir(line, 1, cvs_rootstr, repo) < 0) {
362: free(repo);
363: return (-1);
364: }
1.14 joris 365:
1.22 joris 366: /*
367: * cvs_create_dir() has already put us in the correct directory
368: * so now open it's Entry file for incoming files.
369: */
370: if (cvs_req_entf != NULL)
371: cvs_ent_close(cvs_req_entf);
372: cvs_req_entf = cvs_ent_open(".", O_RDWR);
373: if (cvs_req_entf == NULL) {
374: cvs_log(LP_ERR, "failed to open Entry file for %s", line);
375: free(repo);
376: return (-1);
377: }
378:
379: free(repo);
1.11 jfb 380: return (0);
381: }
382:
1.13 jfb 383: static int
384: cvs_req_entry(int reqid, char *line)
385: {
386: struct cvs_ent *ent;
387:
1.22 joris 388: /* parse received entry */
1.13 jfb 389: if ((ent = cvs_ent_parse(line)) == NULL)
390: return (-1);
391:
1.22 joris 392: /* add it to the entry file and done */
393: if (cvs_ent_add(cvs_req_entf, ent) < 0) {
394: cvs_log(LP_ERR, "failed to add '%s' to the Entry file",
395: ent->ce_name);
396: return (-1);
397: }
398:
399: /* XXX */
400: cvs_ent_write(cvs_req_entf);
1.15 jfb 401:
1.13 jfb 402: return (0);
403: }
404:
405: /*
406: * cvs_req_filestate()
407: *
408: * Handler for the `Modified', `Is-Modified', `Unchanged' and `Questionable'
409: * requests, which are all used to report the assumed state of a file from the
410: * client.
411: */
412: static int
413: cvs_req_filestate(int reqid, char *line)
414: {
1.22 joris 415: int ret;
1.13 jfb 416: mode_t fmode;
417: BUF *fdata;
1.22 joris 418: struct cvs_ent *ent;
1.13 jfb 419:
1.22 joris 420: ret = 0;
421: switch (reqid) {
422: case CVS_REQ_MODIFIED:
1.13 jfb 423: fdata = cvs_recvfile(NULL, &fmode);
424: if (fdata == NULL)
425: return (-1);
1.14 joris 426:
427: /* write the file */
1.22 joris 428: if (cvs_buf_write(fdata, line, fmode) < 0) {
429: cvs_log(LP_ERR, "failed to create file %s", line);
1.14 joris 430: cvs_buf_free(fdata);
431: return (-1);
432: }
433:
434: cvs_buf_free(fdata);
1.22 joris 435: break;
436: case CVS_REQ_ISMODIFIED:
437: break;
438: case CVS_REQ_UNCHANGED:
439: ent = cvs_ent_get(cvs_req_entf, line);
440: if (ent == NULL) {
441: cvs_log(LP_ERR,
442: "received Unchanged request for a non-existing file");
443: ret = -1;
444: } else {
445: ent->ce_status = CVS_ENT_UPTODATE;
446: }
447: break;
448: case CVS_REQ_QUESTIONABLE:
1.26 joris 449: cvs_printf("? %s\n", line);
1.22 joris 450: break;
451: default:
452: cvs_log(LP_ERR, "wrong request id type");
453: ret = -1;
454: break;
1.13 jfb 455: }
456:
1.22 joris 457: /* XXX */
458: cvs_req_entf->cef_flags &= ~CVS_ENTF_SYNC;
459: cvs_ent_write(cvs_req_entf);
460:
461: return (ret);
1.13 jfb 462: }
1.12 jfb 463:
464: /*
465: * cvs_req_expandmod()
466: *
467: */
468: static int
469: cvs_req_expandmod(int reqid, char *line)
470: {
471: int ret;
472:
473: ret = cvs_sendresp(CVS_RESP_OK, NULL);
474: if (ret < 0)
475: return (-1);
476: return (0);
477: }
478:
479:
1.11 jfb 480: /*
481: * cvs_req_useunchanged()
482: *
483: * Handler for the `UseUnchanged' requests. The protocol documentation
484: * specifies that this request must be supported by the server and must be
485: * sent by the client, though it gives no clue regarding its use.
486: */
487: static int
488: cvs_req_useunchanged(int reqid, char *line)
489: {
1.5 jfb 490: return (0);
491: }
492:
1.11 jfb 493:
1.5 jfb 494: /*
495: * cvs_req_case()
496: *
497: * Handler for the `Case' requests, which toggles case sensitivity ON or OFF
498: */
499: static int
500: cvs_req_case(int reqid, char *line)
501: {
502: cvs_nocase = 1;
1.2 jfb 503: return (0);
504: }
505:
506:
507: static int
1.11 jfb 508: cvs_req_set(int reqid, char *line)
509: {
510: char *cp, *lp;
511:
512: if ((lp = strdup(line)) == NULL) {
513: cvs_log(LP_ERRNO, "failed to copy Set argument");
514: return (-1);
515: }
516:
517: if ((cp = strchr(lp, '=')) == NULL) {
518: cvs_log(LP_ERR, "error in Set request "
519: "(no = in variable assignment)");
520: free(lp);
521: return (-1);
522: }
523: *(cp++) = '\0';
524:
525: if (cvs_var_set(lp, cp) < 0) {
526: free(lp);
527: return (-1);
528: }
529:
530: free(lp);
531:
532: return (0);
533: }
534:
535:
536: static int
1.2 jfb 537: cvs_req_argument(int reqid, char *line)
538: {
539: char *nap;
540:
541: if (cvs_req_nargs == CVS_PROTO_MAXARG) {
542: cvs_log(LP_ERR, "too many arguments");
543: return (-1);
544: }
545:
546: if (reqid == CVS_REQ_ARGUMENT) {
547: cvs_req_args[cvs_req_nargs] = strdup(line);
548: if (cvs_req_args[cvs_req_nargs] == NULL) {
549: cvs_log(LP_ERRNO, "failed to copy argument");
550: return (-1);
551: }
552: cvs_req_nargs++;
1.6 deraadt 553: } else if (reqid == CVS_REQ_ARGUMENTX) {
1.2 jfb 554: if (cvs_req_nargs == 0)
555: cvs_log(LP_WARN, "no argument to append to");
556: else {
557: asprintf(&nap, "%s%s", cvs_req_args[cvs_req_nargs - 1],
558: line);
559: if (nap == NULL) {
560: cvs_log(LP_ERRNO,
561: "failed to append to argument");
562: return (-1);
563: }
564: free(cvs_req_args[cvs_req_nargs - 1]);
565: cvs_req_args[cvs_req_nargs - 1] = nap;
566: }
1.3 jfb 567: }
568:
569: return (0);
570: }
571:
572:
573: static int
574: cvs_req_globalopt(int reqid, char *line)
575: {
576: if ((*line != '-') || (*(line + 2) != '\0')) {
577: cvs_log(LP_ERR,
578: "invalid `Global_option' request format");
579: return (-1);
580: }
581:
582: switch (*(line + 1)) {
583: case 'l':
584: cvs_nolog = 1;
585: break;
586: case 'n':
1.24 xsa 587: cvs_noexec = 1;
1.3 jfb 588: break;
589: case 'Q':
590: verbosity = 0;
591: break;
592: case 'q':
593: if (verbosity > 1)
594: verbosity = 1;
595: break;
596: case 'r':
597: cvs_readonly = 1;
598: break;
599: case 't':
600: cvs_trace = 1;
601: break;
602: default:
603: cvs_log(LP_ERR, "unknown global option `%s'", line);
604: return (-1);
1.2 jfb 605: }
1.8 jfb 606:
607: return (0);
608: }
609:
610:
611: /*
612: * cvs_req_gzipstream()
613: *
614: * Handler for the `Gzip-stream' request, which enables compression at the
615: * level given along with the request. After this request has been processed,
616: * all further connection data should be compressed.
617: */
618: static int
619: cvs_req_gzipstream(int reqid, char *line)
620: {
621: char *ep;
622: long val;
623:
624: val = strtol(line, &ep, 10);
625: if ((line[0] == '\0') || (*ep != '\0')) {
626: cvs_log(LP_ERR, "invalid Gzip-stream level `%s'", line);
627: return (-1);
628: } else if ((errno == ERANGE) && ((val < 0) || (val > 9))) {
629: cvs_log(LP_ERR, "Gzip-stream level %ld out of range", val);
630: return (-1);
631: }
632:
633: cvs_compress = (int)val;
1.1 jfb 634:
635: return (0);
636: }
637:
638:
1.11 jfb 639: /*
640: * cvs_req_command()
641: *
642: * Generic request handler for CVS command requests (i.e. diff, update, tag).
643: */
1.1 jfb 644: static int
1.11 jfb 645: cvs_req_command(int reqid, char *line)
1.1 jfb 646: {
1.16 jfb 647: int ret = 0;
1.15 jfb 648: struct cvs_cmd *cmdp;
1.11 jfb 649:
1.15 jfb 650: cmdp = cvs_findcmdbyreq(reqid);
651: if (cmdp == NULL) {
1.11 jfb 652: cvs_sendresp(CVS_RESP_ERROR, NULL);
1.15 jfb 653: return (-1);
1.11 jfb 654: }
1.15 jfb 655:
1.22 joris 656: /* close the Entry file if it's still open */
657: if (cvs_req_entf != NULL)
658: cvs_ent_close(cvs_req_entf);
659:
660: /* fill in the command name */
661: cvs_req_args[0] = cmdp->cmd_name;
662:
663: /* switch to the correct directory */
664: if (cmdp->cmd_op != CVS_OP_VERSION) {
1.28 xsa 665: if (cvs_chdir(cvs_server_tmpdir) == -1)
1.22 joris 666: return (-1);
667: }
668:
1.16 jfb 669: ret = cvs_startcmd(cmdp, cvs_req_nargs, cvs_req_args);
1.22 joris 670:
1.11 jfb 671: if (ret == 0)
672: ret = cvs_sendresp(CVS_RESP_OK, NULL);
673:
674: return (ret);
1.1 jfb 675: }