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