cvs.c revision 1.101
1/*	$OpenBSD: cvs.c,v 1.101 2006/05/30 06:36:09 joris Exp $	*/
2/*
3 * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4 * Copyright (c) 2004 Jean-Francois Brousseau <jfb@openbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. The name of the author may not be used to endorse or promote products
14 *    derived from this software without specific prior written permission.
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
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include "includes.h"
29
30#include "cvs.h"
31#include "config.h"
32#include "log.h"
33#include "file.h"
34
35extern char *__progname;
36
37/* verbosity level: 0 = really quiet, 1 = quiet, 2 = verbose */
38int verbosity = 2;
39
40/* compression level used with zlib, 0 meaning no compression taking place */
41int	cvs_compress = 0;
42int	cvs_readrc = 1;		/* read .cvsrc on startup */
43int	cvs_trace = 0;
44int	cvs_nolog = 0;
45int	cvs_readonly = 0;
46int	cvs_nocase = 0;	/* set to 1 to disable filename case sensitivity */
47int	cvs_noexec = 0;	/* set to 1 to disable disk operations (-n option) */
48int	cvs_error = -1;	/* set to the correct error code on failure */
49int	cvs_cmdop;
50int	cvs_umask = CVS_UMASK_DEFAULT;
51
52char	*cvs_tagname = NULL;
53char	*cvs_defargs;		/* default global arguments from .cvsrc */
54char	*cvs_command;		/* name of the command we are running */
55char	*cvs_rootstr;
56char	*cvs_rsh = CVS_RSH_DEFAULT;
57char	*cvs_editor = CVS_EDITOR_DEFAULT;
58char	*cvs_homedir = NULL;
59char	*cvs_msg = NULL;
60char	*cvs_tmpdir = CVS_TMPDIR_DEFAULT;
61
62struct cvsroot *current_cvsroot = NULL;
63
64static TAILQ_HEAD(, cvs_var) cvs_variables;
65
66int		cvs_getopt(int, char **);
67void		usage(void);
68static void	cvs_read_rcfile(void);
69
70struct cvs_wklhead temp_files;
71
72void sighandler(int);
73volatile sig_atomic_t cvs_quit = 0;
74volatile sig_atomic_t sig_received = 0;
75
76void
77sighandler(int sig)
78{
79	sig_received = sig;
80
81	switch (sig) {
82	case SIGINT:
83	case SIGTERM:
84		cvs_quit = 1;
85		break;
86	default:
87		break;
88	}
89}
90
91void
92cvs_cleanup(void)
93{
94	cvs_log(LP_TRACE, "cvs_cleanup: removing locks");
95	cvs_worklist_run(&repo_locks, cvs_worklist_unlink);
96
97	cvs_log(LP_TRACE, "cvs_cleanup: removing temp files");
98	cvs_worklist_run(&temp_files, cvs_worklist_unlink);
99}
100
101void
102usage(void)
103{
104	fprintf(stderr,
105	    "Usage: %s [-flnQqrtvw] [-d root] [-e editor] [-s var=val] "
106	    "[-T tmpdir] [-z level] command [...]\n", __progname);
107}
108
109int
110main(int argc, char **argv)
111{
112	char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv;
113	int i, ret, cmd_argc;
114	struct cvs_cmd *cmdp;
115	struct passwd *pw;
116	struct stat st;
117	char fpath[MAXPATHLEN];
118
119	tzset();
120
121	TAILQ_INIT(&cvs_variables);
122	SLIST_INIT(&repo_locks);
123	SLIST_INIT(&temp_files);
124
125	/* check environment so command-line options override it */
126	if ((envstr = getenv("CVS_RSH")) != NULL)
127		cvs_rsh = envstr;
128
129	if (((envstr = getenv("CVSEDITOR")) != NULL) ||
130	    ((envstr = getenv("VISUAL")) != NULL) ||
131	    ((envstr = getenv("EDITOR")) != NULL))
132		cvs_editor = envstr;
133
134	if ((envstr = getenv("CVSREAD")) != NULL)
135		cvs_readonly = 1;
136
137	if ((cvs_homedir = getenv("HOME")) == NULL) {
138		if ((pw = getpwuid(getuid())) == NULL)
139			fatal("getpwuid failed");
140		cvs_homedir = pw->pw_dir;
141	}
142
143	if ((envstr = getenv("TMPDIR")) != NULL)
144		cvs_tmpdir = envstr;
145
146	ret = cvs_getopt(argc, argv);
147
148	argc -= ret;
149	argv += ret;
150	if (argc == 0) {
151		usage();
152		exit(1);
153	}
154
155	cvs_command = argv[0];
156
157	/*
158	 * check the tmp dir, either specified through
159	 * the environment variable TMPDIR, or via
160	 * the global option -T <dir>
161	 */
162	if (stat(cvs_tmpdir, &st) == -1)
163		fatal("stat failed on `%s': %s", cvs_tmpdir, strerror(errno));
164	else if (!S_ISDIR(st.st_mode))
165		fatal("`%s' is not valid temporary directory", cvs_tmpdir);
166
167	if (cvs_readrc == 1) {
168		cvs_read_rcfile();
169
170		if (cvs_defargs != NULL) {
171			if ((targv = cvs_makeargv(cvs_defargs, &i)) == NULL)
172				fatal("failed to load default arguments to %s",
173				    __progname);
174
175			cvs_getopt(i, targv);
176			cvs_freeargv(targv, i);
177			xfree(targv);
178		}
179	}
180
181	/* setup signal handlers */
182	signal(SIGTERM, sighandler);
183	signal(SIGINT, sighandler);
184	signal(SIGHUP, sighandler);
185	signal(SIGABRT, sighandler);
186	signal(SIGALRM, sighandler);
187	signal(SIGPIPE, sighandler);
188
189	cmdp = cvs_findcmd(cvs_command);
190	if (cmdp == NULL) {
191		fprintf(stderr, "Unknown command: `%s'\n\n", cvs_command);
192		fprintf(stderr, "CVS commands are:\n");
193		for (i = 0; cvs_cdt[i] != NULL; i++)
194			fprintf(stderr, "\t%-16s%s\n",
195			    cvs_cdt[i]->cmd_name, cvs_cdt[i]->cmd_descr);
196		exit(1);
197	}
198
199	cvs_cmdop = cmdp->cmd_op;
200
201	cmd_argc = 0;
202	memset(cmd_argv, 0, sizeof(cmd_argv));
203
204	cmd_argv[cmd_argc++] = argv[0];
205	if (cmdp->cmd_defargs != NULL) {
206		/* transform into a new argument vector */
207		ret = cvs_getargv(cmdp->cmd_defargs, cmd_argv + 1,
208		    CVS_CMD_MAXARG - 1);
209		if (ret < 0)
210			fatal("main: cvs_getargv failed");
211
212		cmd_argc += ret;
213	}
214
215	for (ret = 1; ret < argc; ret++)
216		cmd_argv[cmd_argc++] = argv[ret];
217
218	cvs_file_init();
219
220	if ((current_cvsroot = cvsroot_get(".")) == NULL) {
221		cvs_log(LP_ERR,
222		    "No CVSROOT specified! Please use the '-d' option");
223		fatal("or set the CVSROOT enviroment variable.");
224	}
225
226	if (current_cvsroot->cr_method != CVS_METHOD_LOCAL)
227		fatal("remote setups are not supported yet");
228
229	i = snprintf(fpath, sizeof(fpath), "%s/%s", current_cvsroot->cr_dir,
230	    CVS_PATH_ROOT);
231	if (stat(fpath, &st) == -1) {
232		if (errno == ENOENT)
233			fatal("'%s' does not seem to be a valid repository",
234			    current_cvsroot->cr_dir);
235		else
236			fatal("%s: %s", current_cvsroot->cr_dir,
237			    strerror(errno));
238	} else {
239		if (!S_ISDIR(st.st_mode))
240			fatal("'%s' is not a directory",
241			    current_cvsroot->cr_dir);
242	}
243
244	cvs_parse_configfile();
245
246	umask(cvs_umask);
247
248	cmdp->cmd(cmd_argc, cmd_argv);
249	cvs_cleanup();
250
251	return (0);
252}
253
254int
255cvs_getopt(int argc, char **argv)
256{
257	int ret;
258	char *ep;
259
260	while ((ret = getopt(argc, argv, "b:d:e:fHlnQqrs:T:tvwz:")) != -1) {
261		switch (ret) {
262		case 'b':
263			/*
264			 * We do not care about the bin directory for RCS files
265			 * as this program has no dependencies on RCS programs,
266			 * so it is only here for backwards compatibility.
267			 */
268			cvs_log(LP_NOTICE, "the -b argument is obsolete");
269			break;
270		case 'd':
271			cvs_rootstr = optarg;
272			break;
273		case 'e':
274			cvs_editor = optarg;
275			break;
276		case 'f':
277			cvs_readrc = 0;
278			break;
279		case 'l':
280			cvs_nolog = 1;
281			break;
282		case 'n':
283			cvs_noexec = 1;
284			break;
285		case 'Q':
286			verbosity = 0;
287			break;
288		case 'q':
289			/* don't override -Q */
290			if (verbosity > 1)
291				verbosity = 1;
292			break;
293		case 'r':
294			cvs_readonly = 1;
295			break;
296		case 's':
297			ep = strchr(optarg, '=');
298			if (ep == NULL) {
299				cvs_log(LP_ERR, "no = in variable assignment");
300				exit(1);
301			}
302			*(ep++) = '\0';
303			if (cvs_var_set(optarg, ep) < 0)
304				exit(1);
305			break;
306		case 'T':
307			cvs_tmpdir = optarg;
308			break;
309		case 't':
310			cvs_trace = 1;
311			break;
312		case 'v':
313			printf("%s\n", CVS_VERSION);
314			exit(0);
315			/* NOTREACHED */
316			break;
317		case 'w':
318			cvs_readonly = 0;
319			break;
320		case 'x':
321			/*
322			 * Kerberos encryption support, kept for compatibility
323			 */
324			break;
325		case 'z':
326			cvs_compress = (int)strtol(optarg, &ep, 10);
327			if (*ep != '\0')
328				fatal("error parsing compression level");
329			if (cvs_compress < 0 || cvs_compress > 9)
330				fatal("gzip compression level must be "
331				    "between 0 and 9");
332			break;
333		default:
334			usage();
335			exit(1);
336		}
337	}
338
339	ret = optind;
340	optind = 1;
341	optreset = 1;	/* for next call */
342
343	return (ret);
344}
345
346/*
347 * cvs_read_rcfile()
348 *
349 * Read the CVS `.cvsrc' file in the user's home directory.  If the file
350 * exists, it should contain a list of arguments that should always be given
351 * implicitly to the specified commands.
352 */
353static void
354cvs_read_rcfile(void)
355{
356	char rcpath[MAXPATHLEN], linebuf[128], *lp, *p;
357	int linenum = 0;
358	size_t len;
359	struct cvs_cmd *cmdp;
360	FILE *fp;
361
362	if (strlcpy(rcpath, cvs_homedir, sizeof(rcpath)) >= sizeof(rcpath) ||
363	    strlcat(rcpath, "/", sizeof(rcpath)) >= sizeof(rcpath) ||
364	    strlcat(rcpath, CVS_PATH_RC, sizeof(rcpath)) >= sizeof(rcpath)) {
365		errno = ENAMETOOLONG;
366		cvs_log(LP_ERR, "%s", rcpath);
367		return;
368	}
369
370	fp = fopen(rcpath, "r");
371	if (fp == NULL) {
372		if (errno != ENOENT)
373			cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath,
374			    strerror(errno));
375		return;
376	}
377
378	while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) {
379		linenum++;
380		if ((len = strlen(linebuf)) == 0)
381			continue;
382		if (linebuf[len - 1] != '\n') {
383			cvs_log(LP_ERR, "line too long in `%s:%d'", rcpath,
384				linenum);
385			break;
386		}
387		linebuf[--len] = '\0';
388
389		/* skip any whitespaces */
390		p = linebuf;
391		while (*p == ' ')
392			p++;
393
394		/* allow comments */
395		if (*p == '#')
396			continue;
397
398		lp = strchr(p, ' ');
399		if (lp == NULL)
400			continue;	/* ignore lines with no arguments */
401		*lp = '\0';
402		if (strcmp(p, "cvs") == 0) {
403			/*
404			 * Global default options.  In the case of cvs only,
405			 * we keep the 'cvs' string as first argument because
406			 * getopt() does not like starting at index 0 for
407			 * argument processing.
408			 */
409			*lp = ' ';
410			cvs_defargs = xstrdup(p);
411		} else {
412			lp++;
413			cmdp = cvs_findcmd(p);
414			if (cmdp == NULL) {
415				cvs_log(LP_NOTICE,
416				    "unknown command `%s' in `%s:%d'",
417				    p, rcpath, linenum);
418				continue;
419			}
420
421			cmdp->cmd_defargs = xstrdup(lp);
422		}
423	}
424
425	if (ferror(fp)) {
426		cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath);
427	}
428
429	(void)fclose(fp);
430}
431
432/*
433 * cvs_var_set()
434 *
435 * Set the value of the variable <var> to <val>.  If there is no such variable,
436 * a new entry is created, otherwise the old value is overwritten.
437 * Returns 0 on success, or -1 on failure.
438 */
439int
440cvs_var_set(const char *var, const char *val)
441{
442	char *valcp;
443	const char *cp;
444	struct cvs_var *vp;
445
446	if (var == NULL || *var == '\0') {
447		cvs_log(LP_ERR, "no variable name");
448		return (-1);
449	}
450
451	/* sanity check on the name */
452	for (cp = var; *cp != '\0'; cp++)
453		if (!isalnum(*cp) && (*cp != '_')) {
454			cvs_log(LP_ERR,
455			    "variable name `%s' contains invalid characters",
456			    var);
457			return (-1);
458		}
459
460	TAILQ_FOREACH(vp, &cvs_variables, cv_link)
461		if (strcmp(vp->cv_name, var) == 0)
462			break;
463
464	valcp = xstrdup(val);
465	if (vp == NULL) {
466		vp = xcalloc(1, sizeof(*vp));
467
468		vp->cv_name = xstrdup(var);
469		TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link);
470
471	} else	/* free the previous value */
472		xfree(vp->cv_val);
473
474	vp->cv_val = valcp;
475
476	return (0);
477}
478
479/*
480 * cvs_var_set()
481 *
482 * Remove any entry for the variable <var>.
483 * Returns 0 on success, or -1 on failure.
484 */
485int
486cvs_var_unset(const char *var)
487{
488	struct cvs_var *vp;
489
490	TAILQ_FOREACH(vp, &cvs_variables, cv_link)
491		if (strcmp(vp->cv_name, var) == 0) {
492			TAILQ_REMOVE(&cvs_variables, vp, cv_link);
493			xfree(vp->cv_name);
494			xfree(vp->cv_val);
495			xfree(vp);
496			return (0);
497		}
498
499	return (-1);
500}
501
502/*
503 * cvs_var_get()
504 *
505 * Get the value associated with the variable <var>.  Returns a pointer to the
506 * value string on success, or NULL if the variable does not exist.
507 */
508
509const char *
510cvs_var_get(const char *var)
511{
512	struct cvs_var *vp;
513
514	TAILQ_FOREACH(vp, &cvs_variables, cv_link)
515		if (strcmp(vp->cv_name, var) == 0)
516			return (vp->cv_val);
517
518	return (NULL);
519}
520