Annotation of src/usr.bin/cvs/req.c, Revision 1.28
1.28 ! xsa 1: /* $OpenBSD: req.c,v 1.27 2005/07/22 17:55:35 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.11 jfb 53: static int cvs_req_set (int, char *);
1.12 jfb 54: static int cvs_req_noop (int, char *);
1.11 jfb 55: static int cvs_req_root (int, char *);
56: static int cvs_req_validreq (int, char *);
57: static int cvs_req_validresp (int, char *);
1.12 jfb 58: static int cvs_req_expandmod (int, char *);
1.11 jfb 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 *);
1.13 jfb 65: static int cvs_req_entry (int, char *);
66: static int cvs_req_filestate (int, char *);
1.11 jfb 67:
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: */
301: s = cvs_req_currentdir + strlen(cvs_req_rootpath) + 1;
302: if (s >= (cvs_req_currentdir + dirlen)) {
303: cvs_log(LP_ERR, "you're bad, go away");
1.14 joris 304: return (-1);
305: }
306:
1.22 joris 307: if ((repo = strdup(s)) == NULL) {
308: cvs_log(LP_ERR, "failed to save repository path");
309: return (-1);
310: }
311:
312: /*
313: * Skip back "foo/bar" part, so we can feed the repo
314: * as a startpoint for cvs_create_dir().
315: */
316: if (!pwd) {
317: s = repo + strlen(repo) - strlen(line) - 1;
318: if (*s != '/') {
319: cvs_log(LP_ERR, "malformed directory");
320: free(repo);
321: return (-1);
322: }
323:
324: *s = '\0';
325: }
326:
327: /*
328: * Obtain the modulename, we only need to do this at
329: * the very first time we get a Directory request.
330: */
331: if (cvs_req_modulename == NULL) {
332: if ((p = strchr(repo, '/')) != NULL)
333: *p = '\0';
334:
335: if ((cvs_req_modulename = strdup(repo)) == NULL) {
336: cvs_log(LP_ERR, "failed to save modulename");
337: free(repo);
338: return (-1);
339: }
340:
341: if (p != NULL)
342: *p = '/';
343:
344: /*
345: * Now, create the admin files in the top-level
346: * directory for the temp repo.
347: */
348: if (cvs_mkadmin(cvs_server_tmpdir, cvs_rootstr, repo) < 0) {
349: cvs_log(LP_ERR, "failed to create admin files");
350: free(repo);
351: return (-1);
352: }
353: }
354:
355: /*
356: * create the directory plus the administrative files.
357: */
358: if (cvs_create_dir(line, 1, cvs_rootstr, repo) < 0) {
359: free(repo);
360: return (-1);
361: }
1.14 joris 362:
1.22 joris 363: /*
364: * cvs_create_dir() has already put us in the correct directory
365: * so now open it's Entry file for incoming files.
366: */
367: if (cvs_req_entf != NULL)
368: cvs_ent_close(cvs_req_entf);
369: cvs_req_entf = cvs_ent_open(".", O_RDWR);
370: if (cvs_req_entf == NULL) {
371: cvs_log(LP_ERR, "failed to open Entry file for %s", line);
372: free(repo);
373: return (-1);
374: }
375:
376: free(repo);
1.11 jfb 377: return (0);
378: }
379:
1.13 jfb 380: static int
381: cvs_req_entry(int reqid, char *line)
382: {
383: struct cvs_ent *ent;
384:
1.22 joris 385: /* parse received entry */
1.13 jfb 386: if ((ent = cvs_ent_parse(line)) == NULL)
387: return (-1);
388:
1.22 joris 389: /* add it to the entry file and done */
390: if (cvs_ent_add(cvs_req_entf, ent) < 0) {
391: cvs_log(LP_ERR, "failed to add '%s' to the Entry file",
392: ent->ce_name);
393: return (-1);
394: }
395:
396: /* XXX */
397: cvs_ent_write(cvs_req_entf);
1.15 jfb 398:
1.13 jfb 399: return (0);
400: }
401:
402: /*
403: * cvs_req_filestate()
404: *
405: * Handler for the `Modified', `Is-Modified', `Unchanged' and `Questionable'
406: * requests, which are all used to report the assumed state of a file from the
407: * client.
408: */
409: static int
410: cvs_req_filestate(int reqid, char *line)
411: {
1.22 joris 412: int ret;
1.13 jfb 413: mode_t fmode;
414: BUF *fdata;
1.22 joris 415: struct cvs_ent *ent;
1.13 jfb 416:
1.22 joris 417: ret = 0;
418: switch (reqid) {
419: case CVS_REQ_MODIFIED:
1.13 jfb 420: fdata = cvs_recvfile(NULL, &fmode);
421: if (fdata == NULL)
422: return (-1);
1.14 joris 423:
424: /* write the file */
1.22 joris 425: if (cvs_buf_write(fdata, line, fmode) < 0) {
426: cvs_log(LP_ERR, "failed to create file %s", line);
1.14 joris 427: cvs_buf_free(fdata);
428: return (-1);
429: }
430:
431: cvs_buf_free(fdata);
1.22 joris 432: break;
433: case CVS_REQ_ISMODIFIED:
434: break;
435: case CVS_REQ_UNCHANGED:
436: ent = cvs_ent_get(cvs_req_entf, line);
437: if (ent == NULL) {
438: cvs_log(LP_ERR,
439: "received Unchanged request for a non-existing file");
440: ret = -1;
441: } else {
442: ent->ce_status = CVS_ENT_UPTODATE;
443: }
444: break;
445: case CVS_REQ_QUESTIONABLE:
1.26 joris 446: cvs_printf("? %s\n", line);
1.22 joris 447: break;
448: default:
449: cvs_log(LP_ERR, "wrong request id type");
450: ret = -1;
451: break;
1.13 jfb 452: }
453:
1.22 joris 454: /* XXX */
455: cvs_req_entf->cef_flags &= ~CVS_ENTF_SYNC;
456: cvs_ent_write(cvs_req_entf);
457:
458: return (ret);
1.13 jfb 459: }
1.12 jfb 460:
461: /*
462: * cvs_req_expandmod()
463: *
464: */
465: static int
466: cvs_req_expandmod(int reqid, char *line)
467: {
468: int ret;
469:
470: ret = cvs_sendresp(CVS_RESP_OK, NULL);
471: if (ret < 0)
472: return (-1);
473: return (0);
474: }
475:
476:
1.11 jfb 477: /*
478: * cvs_req_useunchanged()
479: *
480: * Handler for the `UseUnchanged' requests. The protocol documentation
481: * specifies that this request must be supported by the server and must be
482: * sent by the client, though it gives no clue regarding its use.
483: */
484: static int
485: cvs_req_useunchanged(int reqid, char *line)
486: {
1.5 jfb 487: return (0);
488: }
489:
1.11 jfb 490:
1.5 jfb 491: /*
492: * cvs_req_case()
493: *
494: * Handler for the `Case' requests, which toggles case sensitivity ON or OFF
495: */
496: static int
497: cvs_req_case(int reqid, char *line)
498: {
499: cvs_nocase = 1;
1.2 jfb 500: return (0);
501: }
502:
503:
504: static int
1.11 jfb 505: cvs_req_set(int reqid, char *line)
506: {
507: char *cp, *lp;
508:
509: if ((lp = strdup(line)) == NULL) {
510: cvs_log(LP_ERRNO, "failed to copy Set argument");
511: return (-1);
512: }
513:
514: if ((cp = strchr(lp, '=')) == NULL) {
515: cvs_log(LP_ERR, "error in Set request "
516: "(no = in variable assignment)");
517: free(lp);
518: return (-1);
519: }
520: *(cp++) = '\0';
521:
522: if (cvs_var_set(lp, cp) < 0) {
523: free(lp);
524: return (-1);
525: }
526:
527: free(lp);
528:
529: return (0);
530: }
531:
532:
533: static int
1.2 jfb 534: cvs_req_argument(int reqid, char *line)
535: {
536: char *nap;
537:
538: if (cvs_req_nargs == CVS_PROTO_MAXARG) {
539: cvs_log(LP_ERR, "too many arguments");
540: return (-1);
541: }
542:
543: if (reqid == CVS_REQ_ARGUMENT) {
544: cvs_req_args[cvs_req_nargs] = strdup(line);
545: if (cvs_req_args[cvs_req_nargs] == NULL) {
546: cvs_log(LP_ERRNO, "failed to copy argument");
547: return (-1);
548: }
549: cvs_req_nargs++;
1.6 deraadt 550: } else if (reqid == CVS_REQ_ARGUMENTX) {
1.2 jfb 551: if (cvs_req_nargs == 0)
552: cvs_log(LP_WARN, "no argument to append to");
553: else {
554: asprintf(&nap, "%s%s", cvs_req_args[cvs_req_nargs - 1],
555: line);
556: if (nap == NULL) {
557: cvs_log(LP_ERRNO,
558: "failed to append to argument");
559: return (-1);
560: }
561: free(cvs_req_args[cvs_req_nargs - 1]);
562: cvs_req_args[cvs_req_nargs - 1] = nap;
563: }
1.3 jfb 564: }
565:
566: return (0);
567: }
568:
569:
570: static int
571: cvs_req_globalopt(int reqid, char *line)
572: {
573: if ((*line != '-') || (*(line + 2) != '\0')) {
574: cvs_log(LP_ERR,
575: "invalid `Global_option' request format");
576: return (-1);
577: }
578:
579: switch (*(line + 1)) {
580: case 'l':
581: cvs_nolog = 1;
582: break;
583: case 'n':
1.24 xsa 584: cvs_noexec = 1;
1.3 jfb 585: break;
586: case 'Q':
587: verbosity = 0;
588: break;
589: case 'q':
590: if (verbosity > 1)
591: verbosity = 1;
592: break;
593: case 'r':
594: cvs_readonly = 1;
595: break;
596: case 't':
597: cvs_trace = 1;
598: break;
599: default:
600: cvs_log(LP_ERR, "unknown global option `%s'", line);
601: return (-1);
1.2 jfb 602: }
1.8 jfb 603:
604: return (0);
605: }
606:
607:
608: /*
609: * cvs_req_gzipstream()
610: *
611: * Handler for the `Gzip-stream' request, which enables compression at the
612: * level given along with the request. After this request has been processed,
613: * all further connection data should be compressed.
614: */
615: static int
616: cvs_req_gzipstream(int reqid, char *line)
617: {
618: char *ep;
619: long val;
620:
621: val = strtol(line, &ep, 10);
622: if ((line[0] == '\0') || (*ep != '\0')) {
623: cvs_log(LP_ERR, "invalid Gzip-stream level `%s'", line);
624: return (-1);
625: } else if ((errno == ERANGE) && ((val < 0) || (val > 9))) {
626: cvs_log(LP_ERR, "Gzip-stream level %ld out of range", val);
627: return (-1);
628: }
629:
630: cvs_compress = (int)val;
1.1 jfb 631:
632: return (0);
633: }
634:
635:
1.11 jfb 636: /*
637: * cvs_req_command()
638: *
639: * Generic request handler for CVS command requests (i.e. diff, update, tag).
640: */
1.1 jfb 641: static int
1.11 jfb 642: cvs_req_command(int reqid, char *line)
1.1 jfb 643: {
1.16 jfb 644: int ret = 0;
1.15 jfb 645: struct cvs_cmd *cmdp;
1.11 jfb 646:
1.15 jfb 647: cmdp = cvs_findcmdbyreq(reqid);
648: if (cmdp == NULL) {
1.11 jfb 649: cvs_sendresp(CVS_RESP_ERROR, NULL);
1.15 jfb 650: return (-1);
1.11 jfb 651: }
1.15 jfb 652:
1.22 joris 653: /* close the Entry file if it's still open */
654: if (cvs_req_entf != NULL)
655: cvs_ent_close(cvs_req_entf);
656:
657: /* fill in the command name */
658: cvs_req_args[0] = cmdp->cmd_name;
659:
660: /* switch to the correct directory */
661: if (cmdp->cmd_op != CVS_OP_VERSION) {
1.28 ! xsa 662: if (cvs_chdir(cvs_server_tmpdir) == -1)
1.22 joris 663: return (-1);
664: }
665:
1.16 jfb 666: ret = cvs_startcmd(cmdp, cvs_req_nargs, cvs_req_args);
1.22 joris 667:
1.11 jfb 668: if (ret == 0)
669: ret = cvs_sendresp(CVS_RESP_OK, NULL);
670:
671: return (ret);
1.1 jfb 672: }