Annotation of src/usr.bin/cvs/cvs.c, Revision 1.143
1.143 ! joris 1: /* $OpenBSD: cvs.c,v 1.142 2008/02/02 19:32:28 joris 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_rootstr;
63: char *cvs_rsh = CVS_RSH_DEFAULT;
64: char *cvs_editor = CVS_EDITOR_DEFAULT;
65: char *cvs_homedir = NULL;
66: char *cvs_msg = NULL;
67: char *cvs_tmpdir = CVS_TMPDIR_DEFAULT;
1.13 krapht 68:
1.98 joris 69: struct cvsroot *current_cvsroot = NULL;
1.140 tobias 70: struct cvs_cmd *cmdp; /* struct of command we are running */
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.129 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.79 xsa 131: struct passwd *pw;
1.80 xsa 132: struct stat st;
1.100 joris 133: char fpath[MAXPATHLEN];
1.87 joris 134:
135: tzset();
1.1 jfb 136:
1.27 jfb 137: TAILQ_INIT(&cvs_variables);
1.98 joris 138: SLIST_INIT(&repo_locks);
139: SLIST_INIT(&temp_files);
1.1 jfb 140:
141: /* check environment so command-line options override it */
142: if ((envstr = getenv("CVS_RSH")) != NULL)
143: cvs_rsh = envstr;
144:
145: if (((envstr = getenv("CVSEDITOR")) != NULL) ||
146: ((envstr = getenv("VISUAL")) != NULL) ||
147: ((envstr = getenv("EDITOR")) != NULL))
148: cvs_editor = envstr;
1.76 xsa 149:
150: if ((envstr = getenv("CVSREAD")) != NULL)
151: cvs_readonly = 1;
1.1 jfb 152:
1.111 xsa 153: if ((envstr = getenv("CVSREADONLYFS")) != NULL) {
154: cvs_readonlyfs = 1;
155: cvs_nolog = 1;
156: }
157:
1.79 xsa 158: if ((cvs_homedir = getenv("HOME")) == NULL) {
1.131 tobias 159: if ((pw = getpwuid(getuid())) != NULL)
160: cvs_homedir = pw->pw_dir;
1.85 reyk 161: }
1.79 xsa 162:
1.80 xsa 163: if ((envstr = getenv("TMPDIR")) != NULL)
164: cvs_tmpdir = envstr;
165:
1.17 jfb 166: ret = cvs_getopt(argc, argv);
167:
168: argc -= ret;
169: argv += ret;
1.121 xsa 170: if (argc == 0)
1.17 jfb 171: usage();
1.80 xsa 172:
173: /*
174: * check the tmp dir, either specified through
175: * the environment variable TMPDIR, or via
176: * the global option -T <dir>
177: */
1.94 xsa 178: if (stat(cvs_tmpdir, &st) == -1)
179: fatal("stat failed on `%s': %s", cvs_tmpdir, strerror(errno));
180: else if (!S_ISDIR(st.st_mode))
181: fatal("`%s' is not valid temporary directory", cvs_tmpdir);
1.80 xsa 182:
1.140 tobias 183: cmdp = cvs_findcmd(argv[0]);
1.132 tobias 184: if (cmdp == NULL) {
1.140 tobias 185: fprintf(stderr, "Unknown command: `%s'\n\n", argv[0]);
1.132 tobias 186: fprintf(stderr, "CVS commands are:\n");
187: for (i = 0; cvs_cdt[i] != NULL; i++)
188: fprintf(stderr, "\t%-16s%s\n",
189: cvs_cdt[i]->cmd_name, cvs_cdt[i]->cmd_descr);
190: exit(1);
191: }
192:
1.131 tobias 193: if (cvs_readrc == 1 && cvs_homedir != NULL) {
1.17 jfb 194: cvs_read_rcfile();
195:
196: if (cvs_defargs != NULL) {
1.94 xsa 197: if ((targv = cvs_makeargv(cvs_defargs, &i)) == NULL)
198: fatal("failed to load default arguments to %s",
1.17 jfb 199: __progname);
200:
201: cvs_getopt(i, targv);
202: cvs_freeargv(targv, i);
1.88 joris 203: xfree(targv);
1.17 jfb 204: }
205: }
206:
207: /* setup signal handlers */
1.98 joris 208: signal(SIGTERM, sighandler);
209: signal(SIGINT, sighandler);
210: signal(SIGHUP, sighandler);
211: signal(SIGABRT, sighandler);
212: signal(SIGALRM, sighandler);
213: signal(SIGPIPE, sighandler);
1.17 jfb 214:
215: cvs_cmdop = cmdp->cmd_op;
216:
217: cmd_argc = 0;
218: memset(cmd_argv, 0, sizeof(cmd_argv));
219:
220: cmd_argv[cmd_argc++] = argv[0];
221: if (cmdp->cmd_defargs != NULL) {
222: /* transform into a new argument vector */
223: ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
224: CVS_CMD_MAXARG - 1);
1.94 xsa 225: if (ret < 0)
226: fatal("main: cvs_getargv failed");
227:
1.17 jfb 228: cmd_argc += ret;
229: }
1.98 joris 230:
1.128 ray 231: if (argc + cmd_argc >= CVS_CMD_MAXARG)
232: fatal("main: too many arguments for `%s'", cmd_argv[0]);
1.17 jfb 233: for (ret = 1; ret < argc; ret++)
234: cmd_argv[cmd_argc++] = argv[ret];
235:
1.98 joris 236: cvs_file_init();
237:
1.106 joris 238: if (cvs_cmdop == CVS_OP_SERVER) {
1.130 tobias 239: cmdp->cmd(cmd_argc, cmd_argv);
240: cvs_cleanup();
241: return (0);
1.106 joris 242: }
243:
1.98 joris 244: if ((current_cvsroot = cvsroot_get(".")) == NULL) {
1.71 xsa 245: cvs_log(LP_ERR,
1.98 joris 246: "No CVSROOT specified! Please use the '-d' option");
1.102 david 247: fatal("or set the CVSROOT environment variable.");
1.17 jfb 248: }
249:
1.106 joris 250: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
251: cmdp->cmd(cmd_argc, cmd_argv);
252: cvs_cleanup();
253: return (0);
254: }
1.100 joris 255:
1.116 xsa 256: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
257: current_cvsroot->cr_dir, CVS_PATH_ROOT);
1.110 xsa 258:
1.103 xsa 259: if (stat(fpath, &st) == -1 && cvs_cmdop != CVS_OP_INIT) {
1.100 joris 260: if (errno == ENOENT)
1.104 joris 261: fatal("repository '%s' does not exist",
1.100 joris 262: current_cvsroot->cr_dir);
263: else
264: fatal("%s: %s", current_cvsroot->cr_dir,
265: strerror(errno));
266: } else {
267: if (!S_ISDIR(st.st_mode))
268: fatal("'%s' is not a directory",
269: current_cvsroot->cr_dir);
270: }
1.99 joris 271:
1.142 joris 272: if (cvs_cmdop != CVS_OP_INIT) {
1.103 xsa 273: cvs_parse_configfile();
1.142 joris 274: cvs_parse_modules();
275: }
1.99 joris 276:
277: umask(cvs_umask);
1.98 joris 278:
279: cmdp->cmd(cmd_argc, cmd_argv);
280: cvs_cleanup();
1.17 jfb 281:
1.98 joris 282: return (0);
1.17 jfb 283: }
284:
285: int
286: cvs_getopt(int argc, char **argv)
287: {
288: int ret;
289: char *ep;
1.115 joris 290: const char *errstr;
1.17 jfb 291:
1.122 xsa 292: while ((ret = getopt(argc, argv, "b:d:e:flnQqRrs:T:tVvwxz:")) != -1) {
1.1 jfb 293: switch (ret) {
1.11 jfb 294: case 'b':
295: /*
296: * We do not care about the bin directory for RCS files
297: * as this program has no dependencies on RCS programs,
298: * so it is only here for backwards compatibility.
299: */
300: cvs_log(LP_NOTICE, "the -b argument is obsolete");
301: break;
1.1 jfb 302: case 'd':
303: cvs_rootstr = optarg;
304: break;
305: case 'e':
306: cvs_editor = optarg;
307: break;
308: case 'f':
1.17 jfb 309: cvs_readrc = 0;
1.1 jfb 310: break;
311: case 'l':
312: cvs_nolog = 1;
313: break;
314: case 'n':
1.65 xsa 315: cvs_noexec = 1;
1.112 xsa 316: cvs_nolog = 1;
1.1 jfb 317: break;
318: case 'Q':
319: verbosity = 0;
320: break;
321: case 'q':
1.105 reyk 322: /*
323: * Be quiet. This is the default in OpenCVS.
324: */
1.111 xsa 325: break;
326: case 'R':
327: cvs_readonlyfs = 1;
328: cvs_nolog = 1;
1.1 jfb 329: break;
330: case 'r':
331: cvs_readonly = 1;
332: break;
1.27 jfb 333: case 's':
334: ep = strchr(optarg, '=');
335: if (ep == NULL) {
336: cvs_log(LP_ERR, "no = in variable assignment");
1.98 joris 337: exit(1);
1.27 jfb 338: }
339: *(ep++) = '\0';
340: if (cvs_var_set(optarg, ep) < 0)
1.98 joris 341: exit(1);
1.80 xsa 342: break;
343: case 'T':
344: cvs_tmpdir = optarg;
1.27 jfb 345: break;
1.1 jfb 346: case 't':
347: cvs_trace = 1;
1.105 reyk 348: break;
349: case 'V':
350: /* don't override -Q */
351: if (verbosity)
352: verbosity = 2;
1.1 jfb 353: break;
354: case 'v':
355: printf("%s\n", CVS_VERSION);
356: exit(0);
357: /* NOTREACHED */
1.83 xsa 358: case 'w':
359: cvs_readonly = 0;
1.11 jfb 360: break;
361: case 'x':
362: /*
363: * Kerberos encryption support, kept for compatibility
364: */
1.1 jfb 365: break;
366: case 'z':
1.115 joris 367: cvs_compress = strtonum(optarg, 0, 9, &errstr);
368: if (errstr != NULL)
369: fatal("cvs_compress: %s", errstr);
1.1 jfb 370: break;
371: default:
372: usage();
1.121 xsa 373: /* NOTREACHED */
1.1 jfb 374: }
375: }
376:
1.17 jfb 377: ret = optind;
1.1 jfb 378: optind = 1;
1.17 jfb 379: optreset = 1; /* for next call */
1.12 jfb 380:
1.1 jfb 381: return (ret);
382: }
383:
384: /*
1.17 jfb 385: * cvs_read_rcfile()
1.1 jfb 386: *
387: * Read the CVS `.cvsrc' file in the user's home directory. If the file
388: * exists, it should contain a list of arguments that should always be given
389: * implicitly to the specified commands.
390: */
1.42 joris 391: static void
1.17 jfb 392: cvs_read_rcfile(void)
1.1 jfb 393: {
1.133 tobias 394: char rcpath[MAXPATHLEN], *buf, *lbuf, *lp, *p;
1.137 tobias 395: int cmd_parsed, cvs_parsed, i, linenum;
1.135 tobias 396: size_t len, pos;
1.140 tobias 397: struct cvs_cmd *tcmdp;
1.1 jfb 398: FILE *fp;
399:
1.116 xsa 400: linenum = 0;
401:
402: i = snprintf(rcpath, MAXPATHLEN, "%s/%s", cvs_homedir, CVS_PATH_RC);
403: if (i < 0 || i >= MAXPATHLEN) {
1.108 xsa 404: cvs_log(LP_ERRNO, "%s", rcpath);
1.54 xsa 405: return;
406: }
1.1 jfb 407:
408: fp = fopen(rcpath, "r");
409: if (fp == NULL) {
410: if (errno != ENOENT)
411: cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
412: strerror(errno));
413: return;
414: }
1.137 tobias 415:
416: cmd_parsed = cvs_parsed = 0;
1.133 tobias 417: lbuf = NULL;
418: while ((buf = fgetln(fp, &len)) != NULL) {
419: if (buf[len - 1] == '\n') {
420: buf[len - 1] = '\0';
421: } else {
422: lbuf = xmalloc(len + 1);
423: memcpy(lbuf, buf, len);
424: lbuf[len] = '\0';
425: buf = lbuf;
426: }
427:
1.23 xsa 428: linenum++;
1.17 jfb 429:
1.70 joris 430: /* skip any whitespaces */
1.133 tobias 431: p = buf;
1.70 joris 432: while (*p == ' ')
1.95 deraadt 433: p++;
1.70 joris 434:
1.134 tobias 435: /*
436: * Allow comments.
437: * GNU cvs stops parsing a line if it encounters a \t
438: * in front of a command, stick at this behaviour for
439: * compatibility.
440: */
441: if (*p == '#' || *p == '\t')
1.70 joris 442: continue;
443:
1.135 tobias 444: pos = strcspn(p, " \t");
445: if (pos == strlen(p)) {
1.138 tobias 446: lp = NULL;
1.135 tobias 447: } else {
448: lp = p + pos;
449: *lp = '\0';
450: }
451:
1.137 tobias 452: if (strcmp(p, "cvs") == 0 && !cvs_parsed) {
1.17 jfb 453: /*
454: * Global default options. In the case of cvs only,
455: * we keep the 'cvs' string as first argument because
456: * getopt() does not like starting at index 0 for
457: * argument processing.
458: */
1.138 tobias 459: if (lp != NULL) {
460: *lp = ' ';
461: cvs_defargs = xstrdup(p);
462: }
1.137 tobias 463: cvs_parsed = 1;
1.15 deraadt 464: } else {
1.137 tobias 465: tcmdp = cvs_findcmd(p);
466: if (tcmdp == NULL && verbosity == 2)
467: cvs_log(LP_NOTICE,
468: "unknown command `%s' in `%s:%d'",
469: p, rcpath, linenum);
470:
471: if (tcmdp != cmdp || cmd_parsed)
1.1 jfb 472: continue;
1.17 jfb 473:
1.138 tobias 474: if (lp != NULL) {
475: lp++;
476: cmdp->cmd_defargs = xstrdup(lp);
477: }
1.137 tobias 478: cmd_parsed = 1;
1.1 jfb 479: }
480: }
1.133 tobias 481: if (lbuf != NULL)
482: xfree(lbuf);
1.98 joris 483:
1.1 jfb 484: if (ferror(fp)) {
1.23 xsa 485: cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1 jfb 486: }
487:
488: (void)fclose(fp);
1.27 jfb 489: }
490:
491: /*
492: * cvs_var_set()
493: *
494: * Set the value of the variable <var> to <val>. If there is no such variable,
495: * a new entry is created, otherwise the old value is overwritten.
496: * Returns 0 on success, or -1 on failure.
497: */
498: int
499: cvs_var_set(const char *var, const char *val)
500: {
501: const char *cp;
502: struct cvs_var *vp;
503:
1.97 deraadt 504: if (var == NULL || *var == '\0') {
1.27 jfb 505: cvs_log(LP_ERR, "no variable name");
506: return (-1);
507: }
508:
509: /* sanity check on the name */
510: for (cp = var; *cp != '\0'; cp++)
511: if (!isalnum(*cp) && (*cp != '_')) {
512: cvs_log(LP_ERR,
513: "variable name `%s' contains invalid characters",
514: var);
515: return (-1);
516: }
517:
518: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
519: if (strcmp(vp->cv_name, var) == 0)
520: break;
521:
522: if (vp == NULL) {
1.96 ray 523: vp = xcalloc(1, sizeof(*vp));
1.27 jfb 524:
1.88 joris 525: vp->cv_name = xstrdup(var);
1.27 jfb 526: TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
527:
528: } else /* free the previous value */
1.88 joris 529: xfree(vp->cv_val);
1.27 jfb 530:
1.141 tobias 531: vp->cv_val = xstrdup(val);
1.27 jfb 532:
533: return (0);
534: }
535:
536: /*
1.118 otto 537: * cvs_var_unset()
1.27 jfb 538: *
539: * Remove any entry for the variable <var>.
540: * Returns 0 on success, or -1 on failure.
541: */
542: int
543: cvs_var_unset(const char *var)
544: {
545: struct cvs_var *vp;
546:
547: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
548: if (strcmp(vp->cv_name, var) == 0) {
549: TAILQ_REMOVE(&cvs_variables, vp, cv_link);
1.88 joris 550: xfree(vp->cv_name);
551: xfree(vp->cv_val);
552: xfree(vp);
1.27 jfb 553: return (0);
554: }
555:
556: return (-1);
557: }
558:
559: /*
560: * cvs_var_get()
561: *
562: * Get the value associated with the variable <var>. Returns a pointer to the
563: * value string on success, or NULL if the variable does not exist.
564: */
565:
1.75 xsa 566: const char *
1.27 jfb 567: cvs_var_get(const char *var)
568: {
569: struct cvs_var *vp;
570:
571: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
572: if (strcmp(vp->cv_name, var) == 0)
573: return (vp->cv_val);
574:
575: return (NULL);
1.1 jfb 576: }