cvs.c revision 1.99
1/* $OpenBSD: cvs.c,v 1.99 2006/05/27 18:04:46 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_repo_base = NULL; 61char *cvs_tmpdir = CVS_TMPDIR_DEFAULT; 62 63struct cvsroot *current_cvsroot = NULL; 64 65static TAILQ_HEAD(, cvs_var) cvs_variables; 66 67int cvs_getopt(int, char **); 68void usage(void); 69static void cvs_read_rcfile(void); 70 71struct cvs_wklhead temp_files; 72 73void sighandler(int); 74volatile sig_atomic_t cvs_quit = 0; 75volatile sig_atomic_t sig_received = 0; 76 77void 78sighandler(int sig) 79{ 80 sig_received = sig; 81 82 switch (sig) { 83 case SIGINT: 84 case SIGTERM: 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 102void 103usage(void) 104{ 105 fprintf(stderr, 106 "Usage: %s [-flnQqrtvw] [-d root] [-e editor] [-s var=val] " 107 "[-T tmpdir] [-z level] command [...]\n", __progname); 108} 109 110int 111main(int argc, char **argv) 112{ 113 char *envstr, *cmd_argv[CVS_CMD_MAXARG], **targv; 114 int i, ret, cmd_argc; 115 struct cvs_cmd *cmdp; 116 struct passwd *pw; 117 struct stat st; 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 cvs_parse_configfile(); 230 231 umask(cvs_umask); 232 233 cmdp->cmd(cmd_argc, cmd_argv); 234 cvs_cleanup(); 235 236 return (0); 237} 238 239int 240cvs_getopt(int argc, char **argv) 241{ 242 int ret; 243 char *ep; 244 245 while ((ret = getopt(argc, argv, "b:d:e:fHlnQqrs:T:tvwz:")) != -1) { 246 switch (ret) { 247 case 'b': 248 /* 249 * We do not care about the bin directory for RCS files 250 * as this program has no dependencies on RCS programs, 251 * so it is only here for backwards compatibility. 252 */ 253 cvs_log(LP_NOTICE, "the -b argument is obsolete"); 254 break; 255 case 'd': 256 cvs_rootstr = optarg; 257 break; 258 case 'e': 259 cvs_editor = optarg; 260 break; 261 case 'f': 262 cvs_readrc = 0; 263 break; 264 case 'l': 265 cvs_nolog = 1; 266 break; 267 case 'n': 268 cvs_noexec = 1; 269 break; 270 case 'Q': 271 verbosity = 0; 272 break; 273 case 'q': 274 /* don't override -Q */ 275 if (verbosity > 1) 276 verbosity = 1; 277 break; 278 case 'r': 279 cvs_readonly = 1; 280 break; 281 case 's': 282 ep = strchr(optarg, '='); 283 if (ep == NULL) { 284 cvs_log(LP_ERR, "no = in variable assignment"); 285 exit(1); 286 } 287 *(ep++) = '\0'; 288 if (cvs_var_set(optarg, ep) < 0) 289 exit(1); 290 break; 291 case 'T': 292 cvs_tmpdir = optarg; 293 break; 294 case 't': 295 cvs_trace = 1; 296 break; 297 case 'v': 298 printf("%s\n", CVS_VERSION); 299 exit(0); 300 /* NOTREACHED */ 301 break; 302 case 'w': 303 cvs_readonly = 0; 304 break; 305 case 'x': 306 /* 307 * Kerberos encryption support, kept for compatibility 308 */ 309 break; 310 case 'z': 311 cvs_compress = (int)strtol(optarg, &ep, 10); 312 if (*ep != '\0') 313 fatal("error parsing compression level"); 314 if (cvs_compress < 0 || cvs_compress > 9) 315 fatal("gzip compression level must be " 316 "between 0 and 9"); 317 break; 318 default: 319 usage(); 320 exit(1); 321 } 322 } 323 324 ret = optind; 325 optind = 1; 326 optreset = 1; /* for next call */ 327 328 return (ret); 329} 330 331/* 332 * cvs_read_rcfile() 333 * 334 * Read the CVS `.cvsrc' file in the user's home directory. If the file 335 * exists, it should contain a list of arguments that should always be given 336 * implicitly to the specified commands. 337 */ 338static void 339cvs_read_rcfile(void) 340{ 341 char rcpath[MAXPATHLEN], linebuf[128], *lp, *p; 342 int linenum = 0; 343 size_t len; 344 struct cvs_cmd *cmdp; 345 FILE *fp; 346 347 if (strlcpy(rcpath, cvs_homedir, sizeof(rcpath)) >= sizeof(rcpath) || 348 strlcat(rcpath, "/", sizeof(rcpath)) >= sizeof(rcpath) || 349 strlcat(rcpath, CVS_PATH_RC, sizeof(rcpath)) >= sizeof(rcpath)) { 350 errno = ENAMETOOLONG; 351 cvs_log(LP_ERR, "%s", rcpath); 352 return; 353 } 354 355 fp = fopen(rcpath, "r"); 356 if (fp == NULL) { 357 if (errno != ENOENT) 358 cvs_log(LP_NOTICE, "failed to open `%s': %s", rcpath, 359 strerror(errno)); 360 return; 361 } 362 363 while (fgets(linebuf, (int)sizeof(linebuf), fp) != NULL) { 364 linenum++; 365 if ((len = strlen(linebuf)) == 0) 366 continue; 367 if (linebuf[len - 1] != '\n') { 368 cvs_log(LP_ERR, "line too long in `%s:%d'", rcpath, 369 linenum); 370 break; 371 } 372 linebuf[--len] = '\0'; 373 374 /* skip any whitespaces */ 375 p = linebuf; 376 while (*p == ' ') 377 p++; 378 379 /* allow comments */ 380 if (*p == '#') 381 continue; 382 383 lp = strchr(p, ' '); 384 if (lp == NULL) 385 continue; /* ignore lines with no arguments */ 386 *lp = '\0'; 387 if (strcmp(p, "cvs") == 0) { 388 /* 389 * Global default options. In the case of cvs only, 390 * we keep the 'cvs' string as first argument because 391 * getopt() does not like starting at index 0 for 392 * argument processing. 393 */ 394 *lp = ' '; 395 cvs_defargs = xstrdup(p); 396 } else { 397 lp++; 398 cmdp = cvs_findcmd(p); 399 if (cmdp == NULL) { 400 cvs_log(LP_NOTICE, 401 "unknown command `%s' in `%s:%d'", 402 p, rcpath, linenum); 403 continue; 404 } 405 406 cmdp->cmd_defargs = xstrdup(lp); 407 } 408 } 409 410 if (ferror(fp)) { 411 cvs_log(LP_NOTICE, "failed to read line from `%s'", rcpath); 412 } 413 414 (void)fclose(fp); 415} 416 417/* 418 * cvs_var_set() 419 * 420 * Set the value of the variable <var> to <val>. If there is no such variable, 421 * a new entry is created, otherwise the old value is overwritten. 422 * Returns 0 on success, or -1 on failure. 423 */ 424int 425cvs_var_set(const char *var, const char *val) 426{ 427 char *valcp; 428 const char *cp; 429 struct cvs_var *vp; 430 431 if (var == NULL || *var == '\0') { 432 cvs_log(LP_ERR, "no variable name"); 433 return (-1); 434 } 435 436 /* sanity check on the name */ 437 for (cp = var; *cp != '\0'; cp++) 438 if (!isalnum(*cp) && (*cp != '_')) { 439 cvs_log(LP_ERR, 440 "variable name `%s' contains invalid characters", 441 var); 442 return (-1); 443 } 444 445 TAILQ_FOREACH(vp, &cvs_variables, cv_link) 446 if (strcmp(vp->cv_name, var) == 0) 447 break; 448 449 valcp = xstrdup(val); 450 if (vp == NULL) { 451 vp = xcalloc(1, sizeof(*vp)); 452 453 vp->cv_name = xstrdup(var); 454 TAILQ_INSERT_TAIL(&cvs_variables, vp, cv_link); 455 456 } else /* free the previous value */ 457 xfree(vp->cv_val); 458 459 vp->cv_val = valcp; 460 461 return (0); 462} 463 464/* 465 * cvs_var_set() 466 * 467 * Remove any entry for the variable <var>. 468 * Returns 0 on success, or -1 on failure. 469 */ 470int 471cvs_var_unset(const char *var) 472{ 473 struct cvs_var *vp; 474 475 TAILQ_FOREACH(vp, &cvs_variables, cv_link) 476 if (strcmp(vp->cv_name, var) == 0) { 477 TAILQ_REMOVE(&cvs_variables, vp, cv_link); 478 xfree(vp->cv_name); 479 xfree(vp->cv_val); 480 xfree(vp); 481 return (0); 482 } 483 484 return (-1); 485} 486 487/* 488 * cvs_var_get() 489 * 490 * Get the value associated with the variable <var>. Returns a pointer to the 491 * value string on success, or NULL if the variable does not exist. 492 */ 493 494const char * 495cvs_var_get(const char *var) 496{ 497 struct cvs_var *vp; 498 499 TAILQ_FOREACH(vp, &cvs_variables, cv_link) 500 if (strcmp(vp->cv_name, var) == 0) 501 return (vp->cv_val); 502 503 return (NULL); 504} 505