Annotation of src/usr.bin/cvs/cvs.c, Revision 1.128
1.128 ! ray 1: /* $OpenBSD: cvs.c,v 1.127 2007/05/27 04:12:32 ray Exp $ */
1.1 jfb 2: /*
1.113 joris 3: * Copyright (c) 2006, 2007 Joris Vink <joris@openbsd.org>
1.1 jfb 4: * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
1.18 tedu 5: * All rights reserved.
1.1 jfb 6: *
1.18 tedu 7: * Redistribution and use in source and binary forms, with or without
8: * modification, are permitted provided that the following conditions
9: * are met:
1.1 jfb 10: *
1.18 tedu 11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
1.1 jfb 13: * 2. The name of the author may not be used to endorse or promote products
1.18 tedu 14: * derived from this software without specific prior written permission.
1.1 jfb 15: *
16: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
17: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
1.18 tedu 25: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1.1 jfb 26: */
27:
1.117 otto 28: #include <sys/stat.h>
29:
30: #include <ctype.h>
31: #include <errno.h>
32: #include <pwd.h>
33: #include <stdlib.h>
34: #include <string.h>
1.120 xsa 35: #include <time.h>
1.117 otto 36: #include <unistd.h>
1.1 jfb 37:
38: #include "cvs.h"
1.106 joris 39: #include "remote.h"
1.1 jfb 40:
41: extern char *__progname;
42:
43: /* verbosity level: 0 = really quiet, 1 = quiet, 2 = verbose */
1.105 reyk 44: int verbosity = 1;
1.1 jfb 45:
46: /* compression level used with zlib, 0 meaning no compression taking place */
1.98 joris 47: int cvs_compress = 0;
48: int cvs_readrc = 1; /* read .cvsrc on startup */
49: int cvs_trace = 0;
50: int cvs_nolog = 0;
51: int cvs_readonly = 0;
1.111 xsa 52: int cvs_readonlyfs = 0;
1.98 joris 53: int cvs_nocase = 0; /* set to 1 to disable filename case sensitivity */
54: int cvs_noexec = 0; /* set to 1 to disable disk operations (-n option) */
55: int cvs_error = -1; /* set to the correct error code on failure */
56: int cvs_cmdop;
1.99 joris 57: int cvs_umask = CVS_UMASK_DEFAULT;
1.106 joris 58: int cvs_server_active = 0;
1.98 joris 59:
1.99 joris 60: char *cvs_tagname = NULL;
1.98 joris 61: char *cvs_defargs; /* default global arguments from .cvsrc */
62: char *cvs_command; /* name of the command we are running */
63: char *cvs_rootstr;
64: char *cvs_rsh = CVS_RSH_DEFAULT;
65: char *cvs_editor = CVS_EDITOR_DEFAULT;
66: char *cvs_homedir = NULL;
67: char *cvs_msg = NULL;
68: char *cvs_tmpdir = CVS_TMPDIR_DEFAULT;
1.13 krapht 69:
1.98 joris 70: struct cvsroot *current_cvsroot = NULL;
1.27 jfb 71:
1.98 joris 72: int cvs_getopt(int, char **);
1.121 xsa 73: __dead void usage(void);
1.75 xsa 74: static void cvs_read_rcfile(void);
1.1 jfb 75:
1.98 joris 76: struct cvs_wklhead temp_files;
77:
78: void sighandler(int);
79: volatile sig_atomic_t cvs_quit = 0;
80: volatile sig_atomic_t sig_received = 0;
81:
82: void
83: sighandler(int sig)
84: {
85: sig_received = sig;
86:
87: switch (sig) {
88: case SIGINT:
89: case SIGTERM:
1.107 joris 90: case SIGPIPE:
1.98 joris 91: cvs_quit = 1;
92: break;
93: default:
94: break;
95: }
96: }
97:
98: void
99: cvs_cleanup(void)
100: {
101: cvs_log(LP_TRACE, "cvs_cleanup: removing locks");
102: cvs_worklist_run(&repo_locks, cvs_worklist_unlink);
103:
104: cvs_log(LP_TRACE, "cvs_cleanup: removing temp files");
105: cvs_worklist_run(&temp_files, cvs_worklist_unlink);
1.106 joris 106:
1.124 ray 107: if (cvs_server_path != NULL) {
1.106 joris 108: if (cvs_rmdir(cvs_server_path) == -1)
109: cvs_log(LP_ERR,
110: "warning: failed to remove server directory: %s",
111: cvs_server_path);
112: xfree(cvs_server_path);
1.124 ray 113: cvs_server_path = NULL;
1.106 joris 114: }
1.98 joris 115: }
116:
1.121 xsa 117: __dead void
1.1 jfb 118: usage(void)
119: {
1.121 xsa 120: (void)fprintf(stderr,
1.127 ray 121: "usage: %s [-flnQqRrtVvw] [-d root] [-e editor] [-s var=val]\n"
1.119 sobrado 122: " [-T tmpdir] [-z level] command [...]\n", __progname);
1.121 xsa 123: exit(1);
1.1 jfb 124: }
125:
126: int
127: main(int argc, char **argv)
128: {
1.17 jfb 129: char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv;
130: int i, ret, cmd_argc;
1.1 jfb 131: struct cvs_cmd *cmdp;
1.79 xsa 132: struct passwd *pw;
1.80 xsa 133: struct stat st;
1.100 joris 134: char fpath[MAXPATHLEN];
1.106 joris 135: char *root, *rootp;
1.87 joris 136:
137: tzset();
1.1 jfb 138:
1.27 jfb 139: TAILQ_INIT(&cvs_variables);
1.98 joris 140: SLIST_INIT(&repo_locks);
141: SLIST_INIT(&temp_files);
1.1 jfb 142:
143: /* check environment so command-line options override it */
144: if ((envstr = getenv("CVS_RSH")) != NULL)
145: cvs_rsh = envstr;
146:
147: if (((envstr = getenv("CVSEDITOR")) != NULL) ||
148: ((envstr = getenv("VISUAL")) != NULL) ||
149: ((envstr = getenv("EDITOR")) != NULL))
150: cvs_editor = envstr;
1.76 xsa 151:
152: if ((envstr = getenv("CVSREAD")) != NULL)
153: cvs_readonly = 1;
1.1 jfb 154:
1.111 xsa 155: if ((envstr = getenv("CVSREADONLYFS")) != NULL) {
156: cvs_readonlyfs = 1;
157: cvs_nolog = 1;
158: }
159:
1.79 xsa 160: if ((cvs_homedir = getenv("HOME")) == NULL) {
1.89 xsa 161: if ((pw = getpwuid(getuid())) == NULL)
162: fatal("getpwuid failed");
1.79 xsa 163: cvs_homedir = pw->pw_dir;
1.85 reyk 164: }
1.79 xsa 165:
1.80 xsa 166: if ((envstr = getenv("TMPDIR")) != NULL)
167: cvs_tmpdir = envstr;
168:
1.17 jfb 169: ret = cvs_getopt(argc, argv);
170:
171: argc -= ret;
172: argv += ret;
1.121 xsa 173: if (argc == 0)
1.17 jfb 174: usage();
1.80 xsa 175:
1.17 jfb 176: cvs_command = argv[0];
177:
1.80 xsa 178: /*
179: * check the tmp dir, either specified through
180: * the environment variable TMPDIR, or via
181: * the global option -T <dir>
182: */
1.94 xsa 183: if (stat(cvs_tmpdir, &st) == -1)
184: fatal("stat failed on `%s': %s", cvs_tmpdir, strerror(errno));
185: else if (!S_ISDIR(st.st_mode))
186: fatal("`%s' is not valid temporary directory", cvs_tmpdir);
1.80 xsa 187:
1.74 xsa 188: if (cvs_readrc == 1) {
1.17 jfb 189: cvs_read_rcfile();
190:
191: if (cvs_defargs != NULL) {
1.94 xsa 192: if ((targv = cvs_makeargv(cvs_defargs, &i)) == NULL)
193: fatal("failed to load default arguments to %s",
1.17 jfb 194: __progname);
195:
196: cvs_getopt(i, targv);
197: cvs_freeargv(targv, i);
1.88 joris 198: xfree(targv);
1.17 jfb 199: }
200: }
201:
202: /* setup signal handlers */
1.98 joris 203: signal(SIGTERM, sighandler);
204: signal(SIGINT, sighandler);
205: signal(SIGHUP, sighandler);
206: signal(SIGABRT, sighandler);
207: signal(SIGALRM, sighandler);
208: signal(SIGPIPE, sighandler);
1.17 jfb 209:
210: cmdp = cvs_findcmd(cvs_command);
211: if (cmdp == NULL) {
212: fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
213: fprintf(stderr, "CVS commands are:\n");
1.67 jfb 214: for (i = 0; cvs_cdt[i] != NULL; i++)
1.17 jfb 215: fprintf(stderr, "\t%-16s%s\n",
1.67 jfb 216: cvs_cdt[i]->cmd_name, cvs_cdt[i]->cmd_descr);
1.98 joris 217: exit(1);
1.17 jfb 218: }
219:
220: cvs_cmdop = cmdp->cmd_op;
221:
222: cmd_argc = 0;
223: memset(cmd_argv, 0, sizeof(cmd_argv));
224:
225: cmd_argv[cmd_argc++] = argv[0];
226: if (cmdp->cmd_defargs != NULL) {
227: /* transform into a new argument vector */
228: ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
229: CVS_CMD_MAXARG - 1);
1.94 xsa 230: if (ret < 0)
231: fatal("main: cvs_getargv failed");
232:
1.17 jfb 233: cmd_argc += ret;
234: }
1.98 joris 235:
1.128 ! ray 236: if (argc + cmd_argc >= CVS_CMD_MAXARG)
! 237: fatal("main: too many arguments for `%s'", cmd_argv[0]);
1.17 jfb 238: for (ret = 1; ret < argc; ret++)
239: cmd_argv[cmd_argc++] = argv[ret];
240:
1.98 joris 241: cvs_file_init();
242:
1.106 joris 243: if (cvs_cmdop == CVS_OP_SERVER) {
1.125 ray 244: if (cmd_argc > 1)
245: fatal("server does not take any extra arguments");
246:
1.106 joris 247: setvbuf(stdin, NULL, _IOLBF, 0);
248: setvbuf(stdout, NULL, _IOLBF, 0);
249:
250: cvs_server_active = 1;
251: root = cvs_remote_input();
252: if ((rootp = strchr(root, ' ')) == NULL)
253: fatal("bad Root request");
254: cvs_rootstr = xstrdup(rootp + 1);
255: xfree(root);
256: }
257:
1.98 joris 258: if ((current_cvsroot = cvsroot_get(".")) == NULL) {
1.71 xsa 259: cvs_log(LP_ERR,
1.98 joris 260: "No CVSROOT specified! Please use the '-d' option");
1.102 david 261: fatal("or set the CVSROOT environment variable.");
1.17 jfb 262: }
263:
1.106 joris 264: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
265: cmdp->cmd(cmd_argc, cmd_argv);
266: cvs_cleanup();
267: return (0);
268: }
1.100 joris 269:
1.116 xsa 270: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
271: current_cvsroot->cr_dir, CVS_PATH_ROOT);
1.110 xsa 272:
1.103 xsa 273: if (stat(fpath, &st) == -1 && cvs_cmdop != CVS_OP_INIT) {
1.100 joris 274: if (errno == ENOENT)
1.104 joris 275: fatal("repository '%s' does not exist",
1.100 joris 276: current_cvsroot->cr_dir);
277: else
278: fatal("%s: %s", current_cvsroot->cr_dir,
279: strerror(errno));
280: } else {
281: if (!S_ISDIR(st.st_mode))
282: fatal("'%s' is not a directory",
283: current_cvsroot->cr_dir);
284: }
1.99 joris 285:
1.103 xsa 286: if (cvs_cmdop != CVS_OP_INIT)
287: cvs_parse_configfile();
1.99 joris 288:
289: umask(cvs_umask);
1.98 joris 290:
291: cmdp->cmd(cmd_argc, cmd_argv);
292: cvs_cleanup();
1.17 jfb 293:
1.98 joris 294: return (0);
1.17 jfb 295: }
296:
297: int
298: cvs_getopt(int argc, char **argv)
299: {
300: int ret;
301: char *ep;
1.115 joris 302: const char *errstr;
1.17 jfb 303:
1.122 xsa 304: while ((ret = getopt(argc, argv, "b:d:e:flnQqRrs:T:tVvwxz:")) != -1) {
1.1 jfb 305: switch (ret) {
1.11 jfb 306: case 'b':
307: /*
308: * We do not care about the bin directory for RCS files
309: * as this program has no dependencies on RCS programs,
310: * so it is only here for backwards compatibility.
311: */
312: cvs_log(LP_NOTICE, "the -b argument is obsolete");
313: break;
1.1 jfb 314: case 'd':
315: cvs_rootstr = optarg;
316: break;
317: case 'e':
318: cvs_editor = optarg;
319: break;
320: case 'f':
1.17 jfb 321: cvs_readrc = 0;
1.1 jfb 322: break;
323: case 'l':
324: cvs_nolog = 1;
325: break;
326: case 'n':
1.65 xsa 327: cvs_noexec = 1;
1.112 xsa 328: cvs_nolog = 1;
1.1 jfb 329: break;
330: case 'Q':
331: verbosity = 0;
332: break;
333: case 'q':
1.105 reyk 334: /*
335: * Be quiet. This is the default in OpenCVS.
336: */
1.111 xsa 337: break;
338: case 'R':
339: cvs_readonlyfs = 1;
340: cvs_nolog = 1;
1.1 jfb 341: break;
342: case 'r':
343: cvs_readonly = 1;
344: break;
1.27 jfb 345: case 's':
346: ep = strchr(optarg, '=');
347: if (ep == NULL) {
348: cvs_log(LP_ERR, "no = in variable assignment");
1.98 joris 349: exit(1);
1.27 jfb 350: }
351: *(ep++) = '\0';
352: if (cvs_var_set(optarg, ep) < 0)
1.98 joris 353: exit(1);
1.80 xsa 354: break;
355: case 'T':
356: cvs_tmpdir = optarg;
1.27 jfb 357: break;
1.1 jfb 358: case 't':
359: cvs_trace = 1;
1.105 reyk 360: break;
361: case 'V':
362: /* don't override -Q */
363: if (verbosity)
364: verbosity = 2;
1.1 jfb 365: break;
366: case 'v':
367: printf("%s\n", CVS_VERSION);
368: exit(0);
369: /* NOTREACHED */
1.83 xsa 370: break;
371: case 'w':
372: cvs_readonly = 0;
1.11 jfb 373: break;
374: case 'x':
375: /*
376: * Kerberos encryption support, kept for compatibility
377: */
1.1 jfb 378: break;
379: case 'z':
1.115 joris 380: cvs_compress = strtonum(optarg, 0, 9, &errstr);
381: if (errstr != NULL)
382: fatal("cvs_compress: %s", errstr);
1.1 jfb 383: break;
384: default:
385: usage();
1.121 xsa 386: /* NOTREACHED */
1.1 jfb 387: }
388: }
389:
1.17 jfb 390: ret = optind;
1.1 jfb 391: optind = 1;
1.17 jfb 392: optreset = 1; /* for next call */
1.12 jfb 393:
1.1 jfb 394: return (ret);
395: }
396:
397: /*
1.17 jfb 398: * cvs_read_rcfile()
1.1 jfb 399: *
400: * Read the CVS `.cvsrc' file in the user's home directory. If the file
401: * exists, it should contain a list of arguments that should always be given
402: * implicitly to the specified commands.
403: */
1.42 joris 404: static void
1.17 jfb 405: cvs_read_rcfile(void)
1.1 jfb 406: {
1.79 xsa 407: char rcpath[MAXPATHLEN], linebuf[128], *lp, *p;
1.116 xsa 408: int i, linenum;
1.17 jfb 409: size_t len;
1.1 jfb 410: struct cvs_cmd *cmdp;
411: FILE *fp;
412:
1.116 xsa 413: linenum = 0;
414:
415: i = snprintf(rcpath, MAXPATHLEN, "%s/%s", cvs_homedir, CVS_PATH_RC);
416: if (i < 0 || i >= MAXPATHLEN) {
1.108 xsa 417: cvs_log(LP_ERRNO, "%s", rcpath);
1.54 xsa 418: return;
419: }
1.1 jfb 420:
421: fp = fopen(rcpath, "r");
422: if (fp == NULL) {
423: if (errno != ENOENT)
424: cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
425: strerror(errno));
426: return;
427: }
428:
1.84 xsa 429: while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
1.23 xsa 430: linenum++;
1.17 jfb 431: if ((len = strlen(linebuf)) == 0)
432: continue;
433: if (linebuf[len - 1] != '\n') {
1.98 joris 434: cvs_log(LP_ERR, "line too long in `%s:%d'", rcpath,
1.23 xsa 435: linenum);
1.17 jfb 436: break;
437: }
438: linebuf[--len] = '\0';
439:
1.70 joris 440: /* skip any whitespaces */
441: p = linebuf;
442: while (*p == ' ')
1.95 deraadt 443: p++;
1.70 joris 444:
445: /* allow comments */
446: if (*p == '#')
447: continue;
448:
449: lp = strchr(p, ' ');
1.1 jfb 450: if (lp == NULL)
1.17 jfb 451: continue; /* ignore lines with no arguments */
452: *lp = '\0';
1.70 joris 453: if (strcmp(p, "cvs") == 0) {
1.17 jfb 454: /*
455: * Global default options. In the case of cvs only,
456: * we keep the 'cvs' string as first argument because
457: * getopt() does not like starting at index 0 for
458: * argument processing.
459: */
460: *lp = ' ';
1.88 joris 461: cvs_defargs = xstrdup(p);
1.15 deraadt 462: } else {
1.17 jfb 463: lp++;
1.70 joris 464: cmdp = cvs_findcmd(p);
1.1 jfb 465: if (cmdp == NULL) {
466: cvs_log(LP_NOTICE,
1.25 xsa 467: "unknown command `%s' in `%s:%d'",
1.70 joris 468: p, rcpath, linenum);
1.1 jfb 469: continue;
470: }
1.17 jfb 471:
1.88 joris 472: cmdp->cmd_defargs = xstrdup(lp);
1.1 jfb 473: }
474: }
1.98 joris 475:
1.1 jfb 476: if (ferror(fp)) {
1.23 xsa 477: cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1 jfb 478: }
479:
480: (void)fclose(fp);
1.27 jfb 481: }
482:
483: /*
484: * cvs_var_set()
485: *
486: * Set the value of the variable <var> to <val>. If there is no such variable,
487: * a new entry is created, otherwise the old value is overwritten.
488: * Returns 0 on success, or -1 on failure.
489: */
490: int
491: cvs_var_set(const char *var, const char *val)
492: {
493: char *valcp;
494: const char *cp;
495: struct cvs_var *vp;
496:
1.97 deraadt 497: if (var == NULL || *var == '\0') {
1.27 jfb 498: cvs_log(LP_ERR, "no variable name");
499: return (-1);
500: }
501:
502: /* sanity check on the name */
503: for (cp = var; *cp != '\0'; cp++)
504: if (!isalnum(*cp) && (*cp != '_')) {
505: cvs_log(LP_ERR,
506: "variable name `%s' contains invalid characters",
507: var);
508: return (-1);
509: }
510:
511: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
512: if (strcmp(vp->cv_name, var) == 0)
513: break;
514:
1.88 joris 515: valcp = xstrdup(val);
1.27 jfb 516: if (vp == NULL) {
1.96 ray 517: vp = xcalloc(1, sizeof(*vp));
1.27 jfb 518:
1.88 joris 519: vp->cv_name = xstrdup(var);
1.27 jfb 520: TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
521:
522: } else /* free the previous value */
1.88 joris 523: xfree(vp->cv_val);
1.27 jfb 524:
525: vp->cv_val = valcp;
526:
527: return (0);
528: }
529:
530: /*
1.118 otto 531: * cvs_var_unset()
1.27 jfb 532: *
533: * Remove any entry for the variable <var>.
534: * Returns 0 on success, or -1 on failure.
535: */
536: int
537: cvs_var_unset(const char *var)
538: {
539: struct cvs_var *vp;
540:
541: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
542: if (strcmp(vp->cv_name, var) == 0) {
543: TAILQ_REMOVE(&cvs_variables, vp, cv_link);
1.88 joris 544: xfree(vp->cv_name);
545: xfree(vp->cv_val);
546: xfree(vp);
1.27 jfb 547: return (0);
548: }
549:
550: return (-1);
551: }
552:
553: /*
554: * cvs_var_get()
555: *
556: * Get the value associated with the variable <var>. Returns a pointer to the
557: * value string on success, or NULL if the variable does not exist.
558: */
559:
1.75 xsa 560: const char *
1.27 jfb 561: cvs_var_get(const char *var)
562: {
563: struct cvs_var *vp;
564:
565: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
566: if (strcmp(vp->cv_name, var) == 0)
567: return (vp->cv_val);
568:
569: return (NULL);
1.1 jfb 570: }