Annotation of src/usr.bin/cvs/cvs.c, Revision 1.144
1.144 ! tobias 1: /* $OpenBSD: cvs.c,v 1.143 2008/02/26 21:23:00 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.144 ! tobias 244: cvs_umask = umask(0);
! 245: umask(cvs_umask);
! 246:
1.98 joris 247: if ((current_cvsroot = cvsroot_get(".")) == NULL) {
1.71 xsa 248: cvs_log(LP_ERR,
1.98 joris 249: "No CVSROOT specified! Please use the '-d' option");
1.102 david 250: fatal("or set the CVSROOT environment variable.");
1.17 jfb 251: }
252:
1.106 joris 253: if (current_cvsroot->cr_method != CVS_METHOD_LOCAL) {
254: cmdp->cmd(cmd_argc, cmd_argv);
255: cvs_cleanup();
256: return (0);
257: }
1.100 joris 258:
1.116 xsa 259: (void)xsnprintf(fpath, sizeof(fpath), "%s/%s",
260: current_cvsroot->cr_dir, CVS_PATH_ROOT);
1.110 xsa 261:
1.103 xsa 262: if (stat(fpath, &st) == -1 && cvs_cmdop != CVS_OP_INIT) {
1.100 joris 263: if (errno == ENOENT)
1.104 joris 264: fatal("repository '%s' does not exist",
1.100 joris 265: current_cvsroot->cr_dir);
266: else
267: fatal("%s: %s", current_cvsroot->cr_dir,
268: strerror(errno));
269: } else {
270: if (!S_ISDIR(st.st_mode))
271: fatal("'%s' is not a directory",
272: current_cvsroot->cr_dir);
273: }
1.99 joris 274:
1.142 joris 275: if (cvs_cmdop != CVS_OP_INIT) {
1.103 xsa 276: cvs_parse_configfile();
1.142 joris 277: cvs_parse_modules();
278: }
1.98 joris 279:
280: cmdp->cmd(cmd_argc, cmd_argv);
281: cvs_cleanup();
1.17 jfb 282:
1.98 joris 283: return (0);
1.17 jfb 284: }
285:
286: int
287: cvs_getopt(int argc, char **argv)
288: {
289: int ret;
290: char *ep;
1.115 joris 291: const char *errstr;
1.17 jfb 292:
1.122 xsa 293: while ((ret = getopt(argc, argv, "b:d:e:flnQqRrs:T:tVvwxz:")) != -1) {
1.1 jfb 294: switch (ret) {
1.11 jfb 295: case 'b':
296: /*
297: * We do not care about the bin directory for RCS files
298: * as this program has no dependencies on RCS programs,
299: * so it is only here for backwards compatibility.
300: */
301: cvs_log(LP_NOTICE, "the -b argument is obsolete");
302: break;
1.1 jfb 303: case 'd':
304: cvs_rootstr = optarg;
305: break;
306: case 'e':
307: cvs_editor = optarg;
308: break;
309: case 'f':
1.17 jfb 310: cvs_readrc = 0;
1.1 jfb 311: break;
312: case 'l':
313: cvs_nolog = 1;
314: break;
315: case 'n':
1.65 xsa 316: cvs_noexec = 1;
1.112 xsa 317: cvs_nolog = 1;
1.1 jfb 318: break;
319: case 'Q':
320: verbosity = 0;
321: break;
322: case 'q':
1.105 reyk 323: /*
324: * Be quiet. This is the default in OpenCVS.
325: */
1.111 xsa 326: break;
327: case 'R':
328: cvs_readonlyfs = 1;
329: cvs_nolog = 1;
1.1 jfb 330: break;
331: case 'r':
332: cvs_readonly = 1;
333: break;
1.27 jfb 334: case 's':
335: ep = strchr(optarg, '=');
336: if (ep == NULL) {
337: cvs_log(LP_ERR, "no = in variable assignment");
1.98 joris 338: exit(1);
1.27 jfb 339: }
340: *(ep++) = '\0';
341: if (cvs_var_set(optarg, ep) < 0)
1.98 joris 342: exit(1);
1.80 xsa 343: break;
344: case 'T':
345: cvs_tmpdir = optarg;
1.27 jfb 346: break;
1.1 jfb 347: case 't':
348: cvs_trace = 1;
1.105 reyk 349: break;
350: case 'V':
351: /* don't override -Q */
352: if (verbosity)
353: verbosity = 2;
1.1 jfb 354: break;
355: case 'v':
356: printf("%s\n", CVS_VERSION);
357: exit(0);
358: /* NOTREACHED */
1.83 xsa 359: case 'w':
360: cvs_readonly = 0;
1.11 jfb 361: break;
362: case 'x':
363: /*
364: * Kerberos encryption support, kept for compatibility
365: */
1.1 jfb 366: break;
367: case 'z':
1.115 joris 368: cvs_compress = strtonum(optarg, 0, 9, &errstr);
369: if (errstr != NULL)
370: fatal("cvs_compress: %s", errstr);
1.1 jfb 371: break;
372: default:
373: usage();
1.121 xsa 374: /* NOTREACHED */
1.1 jfb 375: }
376: }
377:
1.17 jfb 378: ret = optind;
1.1 jfb 379: optind = 1;
1.17 jfb 380: optreset = 1; /* for next call */
1.12 jfb 381:
1.1 jfb 382: return (ret);
383: }
384:
385: /*
1.17 jfb 386: * cvs_read_rcfile()
1.1 jfb 387: *
388: * Read the CVS `.cvsrc' file in the user's home directory. If the file
389: * exists, it should contain a list of arguments that should always be given
390: * implicitly to the specified commands.
391: */
1.42 joris 392: static void
1.17 jfb 393: cvs_read_rcfile(void)
1.1 jfb 394: {
1.133 tobias 395: char rcpath[MAXPATHLEN], *buf, *lbuf, *lp, *p;
1.137 tobias 396: int cmd_parsed, cvs_parsed, i, linenum;
1.135 tobias 397: size_t len, pos;
1.140 tobias 398: struct cvs_cmd *tcmdp;
1.1 jfb 399: FILE *fp;
400:
1.116 xsa 401: linenum = 0;
402:
403: i = snprintf(rcpath, MAXPATHLEN, "%s/%s", cvs_homedir, CVS_PATH_RC);
404: if (i < 0 || i >= MAXPATHLEN) {
1.108 xsa 405: cvs_log(LP_ERRNO, "%s", rcpath);
1.54 xsa 406: return;
407: }
1.1 jfb 408:
409: fp = fopen(rcpath, "r");
410: if (fp == NULL) {
411: if (errno != ENOENT)
412: cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
413: strerror(errno));
414: return;
415: }
1.137 tobias 416:
417: cmd_parsed = cvs_parsed = 0;
1.133 tobias 418: lbuf = NULL;
419: while ((buf = fgetln(fp, &len)) != NULL) {
420: if (buf[len - 1] == '\n') {
421: buf[len - 1] = '\0';
422: } else {
423: lbuf = xmalloc(len + 1);
424: memcpy(lbuf, buf, len);
425: lbuf[len] = '\0';
426: buf = lbuf;
427: }
428:
1.23 xsa 429: linenum++;
1.17 jfb 430:
1.70 joris 431: /* skip any whitespaces */
1.133 tobias 432: p = buf;
1.70 joris 433: while (*p == ' ')
1.95 deraadt 434: p++;
1.70 joris 435:
1.134 tobias 436: /*
437: * Allow comments.
438: * GNU cvs stops parsing a line if it encounters a \t
439: * in front of a command, stick at this behaviour for
440: * compatibility.
441: */
442: if (*p == '#' || *p == '\t')
1.70 joris 443: continue;
444:
1.135 tobias 445: pos = strcspn(p, " \t");
446: if (pos == strlen(p)) {
1.138 tobias 447: lp = NULL;
1.135 tobias 448: } else {
449: lp = p + pos;
450: *lp = '\0';
451: }
452:
1.137 tobias 453: if (strcmp(p, "cvs") == 0 && !cvs_parsed) {
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: */
1.138 tobias 460: if (lp != NULL) {
461: *lp = ' ';
462: cvs_defargs = xstrdup(p);
463: }
1.137 tobias 464: cvs_parsed = 1;
1.15 deraadt 465: } else {
1.137 tobias 466: tcmdp = cvs_findcmd(p);
467: if (tcmdp == NULL && verbosity == 2)
468: cvs_log(LP_NOTICE,
469: "unknown command `%s' in `%s:%d'",
470: p, rcpath, linenum);
471:
472: if (tcmdp != cmdp || cmd_parsed)
1.1 jfb 473: continue;
1.17 jfb 474:
1.138 tobias 475: if (lp != NULL) {
476: lp++;
477: cmdp->cmd_defargs = xstrdup(lp);
478: }
1.137 tobias 479: cmd_parsed = 1;
1.1 jfb 480: }
481: }
1.133 tobias 482: if (lbuf != NULL)
483: xfree(lbuf);
1.98 joris 484:
1.1 jfb 485: if (ferror(fp)) {
1.23 xsa 486: cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
1.1 jfb 487: }
488:
489: (void)fclose(fp);
1.27 jfb 490: }
491:
492: /*
493: * cvs_var_set()
494: *
495: * Set the value of the variable <var> to <val>. If there is no such variable,
496: * a new entry is created, otherwise the old value is overwritten.
497: * Returns 0 on success, or -1 on failure.
498: */
499: int
500: cvs_var_set(const char *var, const char *val)
501: {
502: const char *cp;
503: struct cvs_var *vp;
504:
1.97 deraadt 505: if (var == NULL || *var == '\0') {
1.27 jfb 506: cvs_log(LP_ERR, "no variable name");
507: return (-1);
508: }
509:
510: /* sanity check on the name */
511: for (cp = var; *cp != '\0'; cp++)
512: if (!isalnum(*cp) && (*cp != '_')) {
513: cvs_log(LP_ERR,
514: "variable name `%s' contains invalid characters",
515: var);
516: return (-1);
517: }
518:
519: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
520: if (strcmp(vp->cv_name, var) == 0)
521: break;
522:
523: if (vp == NULL) {
1.96 ray 524: vp = xcalloc(1, sizeof(*vp));
1.27 jfb 525:
1.88 joris 526: vp->cv_name = xstrdup(var);
1.27 jfb 527: TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
528:
529: } else /* free the previous value */
1.88 joris 530: xfree(vp->cv_val);
1.27 jfb 531:
1.141 tobias 532: vp->cv_val = xstrdup(val);
1.27 jfb 533:
534: return (0);
535: }
536:
537: /*
1.118 otto 538: * cvs_var_unset()
1.27 jfb 539: *
540: * Remove any entry for the variable <var>.
541: * Returns 0 on success, or -1 on failure.
542: */
543: int
544: cvs_var_unset(const char *var)
545: {
546: struct cvs_var *vp;
547:
548: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
549: if (strcmp(vp->cv_name, var) == 0) {
550: TAILQ_REMOVE(&cvs_variables, vp, cv_link);
1.88 joris 551: xfree(vp->cv_name);
552: xfree(vp->cv_val);
553: xfree(vp);
1.27 jfb 554: return (0);
555: }
556:
557: return (-1);
558: }
559:
560: /*
561: * cvs_var_get()
562: *
563: * Get the value associated with the variable <var>. Returns a pointer to the
564: * value string on success, or NULL if the variable does not exist.
565: */
566:
1.75 xsa 567: const char *
1.27 jfb 568: cvs_var_get(const char *var)
569: {
570: struct cvs_var *vp;
571:
572: TAILQ_FOREACH(vp, &cvs_variables, cv_link)
573: if (strcmp(vp->cv_name, var) == 0)
574: return (vp->cv_val);
575:
576: return (NULL);
1.1 jfb 577: }