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