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