Annotation of src/usr.bin/cvs/req.c, Revision 1.31
1.31 ! xsa 1: /* $OpenBSD: req.c,v 1.30 2005/07/29 10:38:55 joris Exp $ */
1.1 jfb 2: /*
3: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
4: * All rights reserved.
5: *
6: * Redistribution and use in source and binary forms, with or without
7: * modification, are permitted provided that the following conditions
8: * are met:
9: *
10: * 1. Redistributions of source code must retain the above copyright
11: * notice, this list of conditions and the following disclaimer.
12: * 2. The name of the author may not be used to endorse or promote products
13: * derived from this software without specific prior written permission.
14: *
15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25: */
26:
27:
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: */
1.31 ! xsa 351: if (cvs_mkadmin(cvs_server_tmpdir, cvs_rootstr, repo,
! 352: NULL, NULL, 0) < 0) {
1.22 joris 353: cvs_log(LP_ERR, "failed to create admin files");
354: free(repo);
355: return (-1);
356: }
357: }
358:
359: /*
360: * create the directory plus the administrative files.
361: */
362: if (cvs_create_dir(line, 1, cvs_rootstr, repo) < 0) {
363: free(repo);
364: return (-1);
365: }
1.14 joris 366:
1.22 joris 367: /*
368: * cvs_create_dir() has already put us in the correct directory
369: * so now open it's Entry file for incoming files.
370: */
371: if (cvs_req_entf != NULL)
372: cvs_ent_close(cvs_req_entf);
373: cvs_req_entf = cvs_ent_open(".", O_RDWR);
374: if (cvs_req_entf == NULL) {
375: cvs_log(LP_ERR, "failed to open Entry file for %s", line);
376: free(repo);
377: return (-1);
378: }
379:
380: free(repo);
1.11 jfb 381: return (0);
382: }
383:
1.13 jfb 384: static int
385: cvs_req_entry(int reqid, char *line)
386: {
387: struct cvs_ent *ent;
388:
1.22 joris 389: /* parse received entry */
1.13 jfb 390: if ((ent = cvs_ent_parse(line)) == NULL)
391: return (-1);
392:
1.22 joris 393: /* add it to the entry file and done */
394: if (cvs_ent_add(cvs_req_entf, ent) < 0) {
395: cvs_log(LP_ERR, "failed to add '%s' to the Entry file",
396: ent->ce_name);
397: return (-1);
398: }
399:
400: /* XXX */
401: cvs_ent_write(cvs_req_entf);
1.15 jfb 402:
1.13 jfb 403: return (0);
404: }
405:
406: /*
407: * cvs_req_filestate()
408: *
409: * Handler for the `Modified', `Is-Modified', `Unchanged' and `Questionable'
410: * requests, which are all used to report the assumed state of a file from the
411: * client.
412: */
413: static int
414: cvs_req_filestate(int reqid, char *line)
415: {
1.22 joris 416: int ret;
1.13 jfb 417: mode_t fmode;
418: BUF *fdata;
1.22 joris 419: struct cvs_ent *ent;
1.13 jfb 420:
1.22 joris 421: ret = 0;
422: switch (reqid) {
423: case CVS_REQ_MODIFIED:
1.13 jfb 424: fdata = cvs_recvfile(NULL, &fmode);
425: if (fdata == NULL)
426: return (-1);
1.14 joris 427:
428: /* write the file */
1.22 joris 429: if (cvs_buf_write(fdata, line, fmode) < 0) {
430: cvs_log(LP_ERR, "failed to create file %s", line);
1.14 joris 431: cvs_buf_free(fdata);
432: return (-1);
433: }
434:
435: cvs_buf_free(fdata);
1.22 joris 436: break;
437: case CVS_REQ_ISMODIFIED:
438: break;
439: case CVS_REQ_UNCHANGED:
440: ent = cvs_ent_get(cvs_req_entf, line);
441: if (ent == NULL) {
442: cvs_log(LP_ERR,
443: "received Unchanged request for a non-existing file");
444: ret = -1;
445: } else {
446: ent->ce_status = CVS_ENT_UPTODATE;
447: }
448: break;
449: case CVS_REQ_QUESTIONABLE:
1.26 joris 450: cvs_printf("? %s\n", line);
1.22 joris 451: break;
452: default:
453: cvs_log(LP_ERR, "wrong request id type");
454: ret = -1;
455: break;
1.13 jfb 456: }
457:
1.22 joris 458: /* XXX */
459: cvs_req_entf->cef_flags &= ~CVS_ENTF_SYNC;
460: cvs_ent_write(cvs_req_entf);
461:
462: return (ret);
1.13 jfb 463: }
1.12 jfb 464:
465: /*
466: * cvs_req_expandmod()
467: *
468: */
469: static int
470: cvs_req_expandmod(int reqid, char *line)
471: {
472: int ret;
473:
474: ret = cvs_sendresp(CVS_RESP_OK, NULL);
475: if (ret < 0)
476: return (-1);
477: return (0);
478: }
479:
480:
1.11 jfb 481: /*
482: * cvs_req_useunchanged()
483: *
484: * Handler for the `UseUnchanged' requests. The protocol documentation
485: * specifies that this request must be supported by the server and must be
486: * sent by the client, though it gives no clue regarding its use.
487: */
488: static int
489: cvs_req_useunchanged(int reqid, char *line)
490: {
1.5 jfb 491: return (0);
492: }
493:
1.11 jfb 494:
1.5 jfb 495: /*
496: * cvs_req_case()
497: *
498: * Handler for the `Case' requests, which toggles case sensitivity ON or OFF
499: */
500: static int
501: cvs_req_case(int reqid, char *line)
502: {
503: cvs_nocase = 1;
1.2 jfb 504: return (0);
505: }
506:
507:
508: static int
1.11 jfb 509: cvs_req_set(int reqid, char *line)
510: {
511: char *cp, *lp;
512:
513: if ((lp = strdup(line)) == NULL) {
514: cvs_log(LP_ERRNO, "failed to copy Set argument");
515: return (-1);
516: }
517:
518: if ((cp = strchr(lp, '=')) == NULL) {
519: cvs_log(LP_ERR, "error in Set request "
520: "(no = in variable assignment)");
521: free(lp);
522: return (-1);
523: }
524: *(cp++) = '\0';
525:
526: if (cvs_var_set(lp, cp) < 0) {
527: free(lp);
528: return (-1);
529: }
530:
531: free(lp);
532:
533: return (0);
534: }
535:
536:
537: static int
1.2 jfb 538: cvs_req_argument(int reqid, char *line)
539: {
540: char *nap;
541:
542: if (cvs_req_nargs == CVS_PROTO_MAXARG) {
543: cvs_log(LP_ERR, "too many arguments");
544: return (-1);
545: }
546:
547: if (reqid == CVS_REQ_ARGUMENT) {
548: cvs_req_args[cvs_req_nargs] = strdup(line);
549: if (cvs_req_args[cvs_req_nargs] == NULL) {
550: cvs_log(LP_ERRNO, "failed to copy argument");
551: return (-1);
552: }
553: cvs_req_nargs++;
1.6 deraadt 554: } else if (reqid == CVS_REQ_ARGUMENTX) {
1.2 jfb 555: if (cvs_req_nargs == 0)
556: cvs_log(LP_WARN, "no argument to append to");
557: else {
558: asprintf(&nap, "%s%s", cvs_req_args[cvs_req_nargs - 1],
559: line);
560: if (nap == NULL) {
561: cvs_log(LP_ERRNO,
562: "failed to append to argument");
563: return (-1);
564: }
565: free(cvs_req_args[cvs_req_nargs - 1]);
566: cvs_req_args[cvs_req_nargs - 1] = nap;
567: }
1.3 jfb 568: }
569:
570: return (0);
571: }
572:
573:
574: static int
575: cvs_req_globalopt(int reqid, char *line)
576: {
577: if ((*line != '-') || (*(line + 2) != '\0')) {
578: cvs_log(LP_ERR,
579: "invalid `Global_option' request format");
580: return (-1);
581: }
582:
583: switch (*(line + 1)) {
584: case 'l':
585: cvs_nolog = 1;
586: break;
587: case 'n':
1.24 xsa 588: cvs_noexec = 1;
1.3 jfb 589: break;
590: case 'Q':
591: verbosity = 0;
592: break;
593: case 'q':
594: if (verbosity > 1)
595: verbosity = 1;
596: break;
597: case 'r':
598: cvs_readonly = 1;
599: break;
600: case 't':
601: cvs_trace = 1;
602: break;
603: default:
604: cvs_log(LP_ERR, "unknown global option `%s'", line);
605: return (-1);
1.2 jfb 606: }
1.8 jfb 607:
608: return (0);
609: }
610:
611:
612: /*
613: * cvs_req_gzipstream()
614: *
615: * Handler for the `Gzip-stream' request, which enables compression at the
616: * level given along with the request. After this request has been processed,
617: * all further connection data should be compressed.
618: */
619: static int
620: cvs_req_gzipstream(int reqid, char *line)
621: {
622: char *ep;
623: long val;
624:
625: val = strtol(line, &ep, 10);
626: if ((line[0] == '\0') || (*ep != '\0')) {
627: cvs_log(LP_ERR, "invalid Gzip-stream level `%s'", line);
628: return (-1);
629: } else if ((errno == ERANGE) && ((val < 0) || (val > 9))) {
630: cvs_log(LP_ERR, "Gzip-stream level %ld out of range", val);
631: return (-1);
632: }
633:
634: cvs_compress = (int)val;
1.1 jfb 635:
636: return (0);
637: }
638:
639:
1.11 jfb 640: /*
641: * cvs_req_command()
642: *
643: * Generic request handler for CVS command requests (i.e. diff, update, tag).
644: */
1.1 jfb 645: static int
1.11 jfb 646: cvs_req_command(int reqid, char *line)
1.1 jfb 647: {
1.16 jfb 648: int ret = 0;
1.15 jfb 649: struct cvs_cmd *cmdp;
1.11 jfb 650:
1.15 jfb 651: cmdp = cvs_findcmdbyreq(reqid);
652: if (cmdp == NULL) {
1.11 jfb 653: cvs_sendresp(CVS_RESP_ERROR, NULL);
1.15 jfb 654: return (-1);
1.11 jfb 655: }
1.15 jfb 656:
1.22 joris 657: /* close the Entry file if it's still open */
658: if (cvs_req_entf != NULL)
659: cvs_ent_close(cvs_req_entf);
660:
661: /* fill in the command name */
662: cvs_req_args[0] = cmdp->cmd_name;
663:
664: /* switch to the correct directory */
665: if (cmdp->cmd_op != CVS_OP_VERSION) {
1.28 xsa 666: if (cvs_chdir(cvs_server_tmpdir) == -1)
1.22 joris 667: return (-1);
668: }
669:
1.16 jfb 670: ret = cvs_startcmd(cmdp, cvs_req_nargs, cvs_req_args);
1.22 joris 671:
1.11 jfb 672: if (ret == 0)
673: ret = cvs_sendresp(CVS_RESP_OK, NULL);
674:
675: return (ret);
1.1 jfb 676: }