1 /* 2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc. 3 * 4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>, 5 * and others. 6 * 7 * Poritons Copyright (c) 1992, Mark D. Baushke 8 * 9 * You may distribute under the terms of the GNU General Public License as 10 * specified in the README file that comes with the CVS source distribution. 11 * 12 * Name of Root 13 * 14 * Determine the path to the CVSROOT and set "Root" accordingly. 15 */ 16 17 #include "cvs.h" 18 #include <assert.h> 19 #include "getline.h" 20 21 /* Printable names for things in the current_parsed_root->method enum variable. 22 Watch out if the enum is changed in cvs.h! */ 23 24 const char method_names[][16] = { 25 "undefined", "local", "server (rsh)", "pserver", 26 "kserver", "gserver", "ext", "fork" 27 }; 28 29 #ifndef DEBUG 30 31 cvsroot_t * 32 Name_Root (const char *dir, const char *update_dir) 33 { 34 FILE *fpin; 35 cvsroot_t *ret; 36 const char *xupdate_dir; 37 char *root = NULL; 38 size_t root_allocated = 0; 39 char *tmp; 40 char *cvsadm; 41 char *cp; 42 int len; 43 44 TRACE (TRACE_FLOW, "Name_Root (%s, %s)", 45 dir ? dir : "(null)", 46 update_dir ? update_dir : "(null)"); 47 48 if (update_dir && *update_dir) 49 xupdate_dir = update_dir; 50 else 51 xupdate_dir = "."; 52 53 if (dir != NULL) 54 { 55 cvsadm = Xasprintf ("%s/%s", dir, CVSADM); 56 tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT); 57 } 58 else 59 { 60 cvsadm = xstrdup (CVSADM); 61 tmp = xstrdup (CVSADM_ROOT); 62 } 63 64 /* 65 * Do not bother looking for a readable file if there is no cvsadm 66 * directory present. 67 * 68 * It is possible that not all repositories will have a CVS/Root 69 * file. This is ok, but the user will need to specify -d 70 * /path/name or have the environment variable CVSROOT set in 71 * order to continue. */ 72 if ((!isdir (cvsadm)) || (!isreadable (tmp))) 73 { 74 ret = NULL; 75 goto out; 76 } 77 78 /* 79 * The assumption here is that the CVS Root is always contained in the 80 * first line of the "Root" file. 81 */ 82 fpin = xfopen (tmp, "r"); 83 84 if ((len = getline (&root, &root_allocated, fpin)) < 0) 85 { 86 int saved_errno = errno; 87 /* FIXME: should be checking for end of file separately; errno 88 is not set in that case. */ 89 error (0, 0, "in directory %s:", xupdate_dir); 90 error (0, saved_errno, "cannot read %s", CVSADM_ROOT); 91 error (0, 0, "please correct this problem"); 92 ret = NULL; 93 goto out; 94 } 95 fclose (fpin); 96 cp = root + len - 1; 97 if (*cp == '\n') 98 *cp = '\0'; /* strip the newline */ 99 100 /* 101 * root now contains a candidate for CVSroot. It must be an 102 * absolute pathname or specify a remote server. 103 */ 104 105 ret = parse_cvsroot (root); 106 if (ret == NULL) 107 { 108 error (0, 0, "in directory %s:", xupdate_dir); 109 error (0, 0, 110 "ignoring %s because it does not contain a valid root.", 111 CVSADM_ROOT); 112 goto out; 113 } 114 115 if (!ret->isremote && !isdir (ret->directory)) 116 { 117 error (0, 0, "in directory %s:", xupdate_dir); 118 error (0, 0, 119 "ignoring %s because it specifies a non-existent repository %s", 120 CVSADM_ROOT, root); 121 ret = NULL; 122 goto out; 123 } 124 125 126 out: 127 free (cvsadm); 128 free (tmp); 129 if (root != NULL) 130 free (root); 131 return ret; 132 } 133 134 135 136 /* 137 * Write the CVS/Root file so that the environment variable CVSROOT 138 * and/or the -d option to cvs will be validated or not necessary for 139 * future work. 140 */ 141 void 142 Create_Root (const char *dir, const char *rootdir) 143 { 144 FILE *fout; 145 char *tmp; 146 147 if (noexec) 148 return; 149 150 /* record the current cvs root */ 151 152 if (rootdir != NULL) 153 { 154 if (dir != NULL) 155 tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT); 156 else 157 tmp = xstrdup (CVSADM_ROOT); 158 159 fout = xfopen (tmp, "w+"); 160 if (fprintf (fout, "%s\n", rootdir) < 0) 161 error (1, errno, "write to %s failed", tmp); 162 if (fclose (fout) == EOF) 163 error (1, errno, "cannot close %s", tmp); 164 free (tmp); 165 } 166 } 167 168 #endif /* ! DEBUG */ 169 170 171 172 /* Translate an absolute repository string for a primary server and return it. 173 * 174 * INPUTS 175 * root_in The root to be translated. 176 * 177 * RETURNS 178 * A translated string this function owns, or a pointer to the original 179 * string passed in if no translation was necessary. 180 * 181 * If the returned string is the translated one, it may be overwritten 182 * by the next call to this function. 183 */ 184 const char * 185 primary_root_translate (const char *root_in) 186 { 187 #ifdef PROXY_SUPPORT 188 char *translated; 189 static char *previous = NULL; 190 static size_t len; 191 192 /* This can happen, for instance, during `cvs init'. */ 193 if (!config) return root_in; 194 195 if (config->PrimaryServer 196 && !strncmp (root_in, config->PrimaryServer->directory, 197 strlen (config->PrimaryServer->directory)) 198 && (ISSLASH (root_in[strlen (config->PrimaryServer->directory)]) 199 || root_in[strlen (config->PrimaryServer->directory)] == '\0') 200 ) 201 { 202 translated = 203 Xasnprintf (previous, &len, 204 "%s%s", current_parsed_root->directory, 205 root_in + strlen (config->PrimaryServer->directory)); 206 if (previous && previous != translated) 207 free (previous); 208 return previous = translated; 209 } 210 #endif 211 212 /* There is no primary root configured or it didn't match. */ 213 return root_in; 214 } 215 216 217 218 /* Translate a primary root in reverse for PATHNAMEs in responses. 219 * 220 * INPUTS 221 * root_in The root to be translated. 222 * 223 * RETURNS 224 * A translated string this function owns, or a pointer to the original 225 * string passed in if no translation was necessary. 226 * 227 * If the returned string is the translated one, it may be overwritten 228 * by the next call to this function. 229 */ 230 const char * 231 primary_root_inverse_translate (const char *root_in) 232 { 233 #ifdef PROXY_SUPPORT 234 char *translated; 235 static char *previous = NULL; 236 static size_t len; 237 238 /* This can happen, for instance, during `cvs init'. */ 239 if (!config) return root_in; 240 241 if (config->PrimaryServer 242 && !strncmp (root_in, current_parsed_root->directory, 243 strlen (current_parsed_root->directory)) 244 && (ISSLASH (root_in[strlen (current_parsed_root->directory)]) 245 || root_in[strlen (current_parsed_root->directory)] == '\0') 246 ) 247 { 248 translated = 249 Xasnprintf (previous, &len, 250 "%s%s", config->PrimaryServer->directory, 251 root_in + strlen (current_parsed_root->directory)); 252 if (previous && previous != translated) 253 free (previous); 254 return previous = translated; 255 } 256 #endif 257 258 /* There is no primary root configured or it didn't match. */ 259 return root_in; 260 } 261 262 263 264 /* The root_allow_* stuff maintains a list of valid CVSROOT 265 directories. Then we can check against them when a remote user 266 hands us a CVSROOT directory. */ 267 static List *root_allow; 268 269 static void 270 delconfig (Node *n) 271 { 272 if (n->data) free_config (n->data); 273 } 274 275 276 277 void 278 root_allow_add (const char *arg, const char *configPath) 279 { 280 Node *n; 281 282 if (!root_allow) root_allow = getlist(); 283 n = getnode(); 284 n->key = xstrdup (arg); 285 n->data = parse_config (arg, configPath); 286 n->delproc = delconfig; 287 addnode (root_allow, n); 288 } 289 290 void 291 root_allow_free (void) 292 { 293 dellist (&root_allow); 294 } 295 296 bool 297 root_allow_ok (const char *arg) 298 { 299 if (!root_allow) 300 { 301 /* Probably someone upgraded from CVS before 1.9.10 to 1.9.10 302 or later without reading the documentation about 303 --allow-root. Printing an error here doesn't disclose any 304 particularly useful information to an attacker because a 305 CVS server configured in this way won't let *anyone* in. */ 306 307 /* Note that we are called from a context where we can spit 308 back "error" rather than waiting for the next request which 309 expects responses. */ 310 printf ("\ 311 error 0 Server configuration missing --allow-root in inetd.conf\n"); 312 exit (EXIT_FAILURE); 313 } 314 315 if (findnode (root_allow, arg)) 316 return true; 317 return false; 318 } 319 320 321 322 /* Get a config we stored in response to root_allow. 323 * 324 * RETURNS 325 * The config associated with ARG. 326 */ 327 struct config * 328 get_root_allow_config (const char *arg, const char *configPath) 329 { 330 Node *n; 331 332 TRACE (TRACE_FUNCTION, "get_root_allow_config (%s)", arg); 333 334 if (root_allow) 335 n = findnode (root_allow, arg); 336 else 337 n = NULL; 338 339 if (n) return n->data; 340 return parse_config (arg, configPath); 341 } 342 343 344 345 /* This global variable holds the global -d option. It is NULL if -d 346 was not used, which means that we must get the CVSroot information 347 from the CVSROOT environment variable or from a CVS/Root file. */ 348 char *CVSroot_cmdline; 349 350 351 352 /* FIXME - Deglobalize this. */ 353 cvsroot_t *current_parsed_root = NULL; 354 /* Used to save the original root being processed so that we can still find it 355 * in lists and the like after a `Redirect' response. Also set to mirror 356 * current_parsed_root in server mode so that code which runs on both the 357 * client and server but which wants to use original data on the client can 358 * just always reference the original_parsed_root. 359 */ 360 const cvsroot_t *original_parsed_root; 361 362 363 /* allocate and initialize a cvsroot_t 364 * 365 * We must initialize the strings to NULL so we know later what we should 366 * free 367 * 368 * Some of the other zeroes remain meaningful as, "never set, use default", 369 * or the like 370 */ 371 /* Functions which allocate memory are not pure. */ 372 static cvsroot_t *new_cvsroot_t(void) 373 __attribute__( (__malloc__) ); 374 static cvsroot_t * 375 new_cvsroot_t (void) 376 { 377 cvsroot_t *newroot; 378 379 /* gotta store it somewhere */ 380 newroot = xmalloc(sizeof(cvsroot_t)); 381 382 newroot->original = NULL; 383 newroot->directory = NULL; 384 newroot->method = null_method; 385 newroot->isremote = false; 386 #ifdef CLIENT_SUPPORT 387 newroot->username = NULL; 388 newroot->password = NULL; 389 newroot->hostname = NULL; 390 newroot->cvs_rsh = NULL; 391 newroot->cvs_server = NULL; 392 newroot->port = 0; 393 newroot->proxy_hostname = NULL; 394 newroot->proxy_port = 0; 395 newroot->redirect = true; /* Advertise Redirect support */ 396 #endif /* CLIENT_SUPPORT */ 397 398 return newroot; 399 } 400 401 402 403 /* Dispose of a cvsroot_t and its component parts. 404 * 405 * NOTE 406 * It is dangerous for most code to call this function since parse_cvsroot 407 * maintains a cache of parsed roots. 408 */ 409 static void 410 free_cvsroot_t (cvsroot_t *root) 411 { 412 assert (root); 413 if (root->original != NULL) 414 free (root->original); 415 if (root->directory != NULL) 416 free (root->directory); 417 #ifdef CLIENT_SUPPORT 418 if (root->username != NULL) 419 free (root->username); 420 if (root->password != NULL) 421 { 422 /* I like to be paranoid */ 423 memset (root->password, 0, strlen (root->password)); 424 free (root->password); 425 } 426 if (root->hostname != NULL) 427 free (root->hostname); 428 if (root->cvs_rsh != NULL) 429 free (root->cvs_rsh); 430 if (root->cvs_server != NULL) 431 free (root->cvs_server); 432 if (root->proxy_hostname != NULL) 433 free (root->proxy_hostname); 434 #endif /* CLIENT_SUPPORT */ 435 free (root); 436 } 437 438 439 440 /* 441 * Parse a CVSROOT string to allocate and return a new cvsroot_t structure. 442 * Valid specifications are: 443 * 444 * :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path 445 * [:(ext|server):][[user]@]host[:]/path 446 * [:local:[e:]]/path 447 * :fork:/path 448 * 449 * INPUTS 450 * root_in C String containing the CVSROOT to be parsed. 451 * 452 * RETURNS 453 * A pointer to a newly allocated cvsroot_t structure upon success and 454 * NULL upon failure. The caller should never dispose of this structure, 455 * as it is stored in a cache, but the caller may rely on it not to 456 * change. 457 * 458 * NOTES 459 * This would have been a lot easier to write in Perl. 460 * 461 * Would it make sense to reimplement the root and config file parsing 462 * gunk in Lex/Yacc? 463 * 464 * SEE ALSO 465 * free_cvsroot_t() 466 */ 467 cvsroot_t * 468 parse_cvsroot (const char *root_in) 469 { 470 cvsroot_t *newroot; /* the new root to be returned */ 471 char *cvsroot_save; /* what we allocated so we can dispose 472 * it when finished */ 473 char *cvsroot_copy, *p; /* temporary pointers for parsing */ 474 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) 475 char *q; /* temporary pointer for parsing */ 476 char *firstslash; /* save where the path spec starts 477 * while we parse 478 * [[user][:password]@]host[:[port]] 479 */ 480 int check_hostname, no_port, no_password, no_proxy; 481 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */ 482 static List *cache = NULL; 483 Node *node; 484 485 assert (root_in != NULL); 486 487 /* This message is TRACE_FLOW since this function is called repeatedly by 488 * the recursion routines. 489 */ 490 TRACE (TRACE_FLOW, "parse_cvsroot (%s)", root_in); 491 492 if ((node = findnode (cache, root_in))) 493 return node->data; 494 495 assert (root_in); 496 497 /* allocate some space */ 498 newroot = new_cvsroot_t(); 499 500 /* save the original string */ 501 newroot->original = xstrdup (root_in); 502 503 /* and another copy we can munge while parsing */ 504 cvsroot_save = cvsroot_copy = xstrdup (root_in); 505 506 if (*cvsroot_copy == ':') 507 { 508 char *method = ++cvsroot_copy; 509 510 /* Access method specified, as in 511 * "cvs -d :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path", 512 * "cvs -d [:(ext|server):][[user]@]host[:]/path", 513 * "cvs -d :local:e:\path", 514 * "cvs -d :fork:/path". 515 * We need to get past that part of CVSroot before parsing the 516 * rest of it. 517 */ 518 519 if (! (p = strchr (method, ':'))) 520 { 521 error (0, 0, "No closing `:' on method in CVSROOT."); 522 goto error_exit; 523 } 524 *p = '\0'; 525 cvsroot_copy = ++p; 526 527 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) 528 /* Look for method options, for instance, proxy, proxyport. 529 * Calling strtok again is saved until after parsing the method. 530 */ 531 method = strtok (method, ";"); 532 if (!method) 533 /* Could just exit now, but this keeps the error message in sync. 534 */ 535 method = ""; 536 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */ 537 538 /* Now we have an access method -- see if it's valid. */ 539 540 if (!strcasecmp (method, "local")) 541 newroot->method = local_method; 542 else if (!strcasecmp (method, "pserver")) 543 newroot->method = pserver_method; 544 else if (!strcasecmp (method, "kserver")) 545 newroot->method = kserver_method; 546 else if (!strcasecmp (method, "gserver")) 547 newroot->method = gserver_method; 548 else if (!strcasecmp (method, "server")) 549 newroot->method = server_method; 550 else if (!strcasecmp (method, "ext")) 551 newroot->method = ext_method; 552 else if (!strcasecmp (method, "fork")) 553 newroot->method = fork_method; 554 else 555 { 556 error (0, 0, "Unknown method (`%s') in CVSROOT.", method); 557 goto error_exit; 558 } 559 560 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) 561 /* Parse the method options, for instance, proxy, proxyport */ 562 while ((p = strtok (NULL, ";"))) 563 { 564 char *q = strchr (p, '='); 565 if (q == NULL) 566 { 567 error (0, 0, "Option (`%s') has no argument in CVSROOT.", 568 p); 569 goto error_exit; 570 } 571 572 *q++ = '\0'; 573 TRACE (TRACE_DATA, "CVSROOT option=`%s' value=`%s'", p, q); 574 if (!strcasecmp (p, "proxy")) 575 { 576 newroot->proxy_hostname = xstrdup (q); 577 } 578 else if (!strcasecmp (p, "proxyport")) 579 { 580 char *r = q; 581 if (*r == '-') r++; 582 while (*r) 583 { 584 if (!isdigit(*r++)) 585 { 586 error (0, 0, 587 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').", 588 q); 589 goto error_exit; 590 } 591 } 592 if ((newroot->proxy_port = atoi (q)) <= 0) 593 error (0, 0, 594 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').", 595 q); 596 } 597 else if (!strcasecmp (p, "CVS_RSH")) 598 { 599 /* override CVS_RSH environment variable */ 600 if (newroot->method == ext_method) 601 newroot->cvs_rsh = xstrdup (q); 602 } 603 else if (!strcasecmp (p, "CVS_SERVER")) 604 { 605 /* override CVS_SERVER environment variable */ 606 if (newroot->method == ext_method 607 || newroot->method == fork_method) 608 newroot->cvs_server = xstrdup (q); 609 } 610 else if (!strcasecmp (p, "Redirect")) 611 readBool ("CVSROOT", "Redirect", q, &newroot->redirect); 612 else 613 { 614 error (0, 0, "Unknown option (`%s') in CVSROOT.", p); 615 goto error_exit; 616 } 617 } 618 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */ 619 } 620 else 621 { 622 /* If the method isn't specified, assume EXT_METHOD if the string looks 623 like a relative path and LOCAL_METHOD otherwise. */ 624 625 newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/')) 626 ? ext_method 627 : local_method); 628 } 629 630 /* 631 * There are a few sanity checks we can do now, only knowing the 632 * method of this root. 633 */ 634 635 newroot->isremote = (newroot->method != local_method); 636 637 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) 638 if (readonlyfs && newroot->isremote) { 639 /* downgrade readonlyfs settings via environment */ 640 if (readonlyfs < 0) 641 error (1, 0, 642 "Read-only repository feature unavailable with remote roots (cvsroot = %s)", 643 cvsroot_copy); 644 readonlyfs = 0; 645 } 646 647 if ((newroot->method != local_method) 648 && (newroot->method != fork_method) 649 ) 650 { 651 /* split the string into [[user][:password]@]host[:[port]] & /path 652 * 653 * this will allow some characters such as '@' & ':' to remain unquoted 654 * in the path portion of the spec 655 */ 656 if ((p = strchr (cvsroot_copy, '/')) == NULL) 657 { 658 error (0, 0, "CVSROOT requires a path spec:"); 659 error (0, 0, 660 ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path"); 661 error (0, 0, "[:(ext|server):][[user]@]host[:]/path"); 662 goto error_exit; 663 } 664 firstslash = p; /* == NULL if '/' not in string */ 665 *p = '\0'; 666 667 /* Check to see if there is a username[:password] in the string. */ 668 if ((p = strchr (cvsroot_copy, '@')) != NULL) 669 { 670 *p = '\0'; 671 /* check for a password */ 672 if ((q = strchr (cvsroot_copy, ':')) != NULL) 673 { 674 *q = '\0'; 675 newroot->password = xstrdup (++q); 676 /* Don't check for *newroot->password == '\0' since 677 * a user could conceivably wish to specify a blank password 678 * 679 * (newroot->password == NULL means to use the 680 * password from .cvspass) 681 */ 682 } 683 684 /* copy the username */ 685 if (*cvsroot_copy != '\0') 686 /* a blank username is impossible, so leave it NULL in that 687 * case so we know to use the default username 688 */ 689 newroot->username = xstrdup (cvsroot_copy); 690 691 cvsroot_copy = ++p; 692 } 693 694 /* now deal with host[:[port]] */ 695 696 /* the port */ 697 if ((p = strchr (cvsroot_copy, ':')) != NULL) 698 { 699 *p++ = '\0'; 700 if (strlen(p)) 701 { 702 q = p; 703 if (*q == '-') q++; 704 while (*q) 705 { 706 if (!isdigit(*q++)) 707 { 708 error (0, 0, 709 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').", 710 p); 711 error (0, 0, 712 "Perhaps you entered a relative pathname?"); 713 goto error_exit; 714 } 715 } 716 if ((newroot->port = atoi (p)) <= 0) 717 { 718 error (0, 0, 719 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').", 720 p); 721 error (0, 0, "Perhaps you entered a relative pathname?"); 722 goto error_exit; 723 } 724 } 725 } 726 727 /* copy host */ 728 if (*cvsroot_copy != '\0') 729 /* blank hostnames are invalid, but for now leave the field NULL 730 * and catch the error during the sanity checks later 731 */ 732 newroot->hostname = xstrdup (cvsroot_copy); 733 734 /* restore the '/' */ 735 cvsroot_copy = firstslash; 736 *cvsroot_copy = '/'; 737 } 738 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */ 739 740 /* 741 * Parse the path for all methods. 742 */ 743 /* Here & local_cvsroot() should be the only places this needs to be 744 * called on a CVSROOT now. cvsroot->original is saved for error messages 745 * and, otherwise, we want no trailing slashes. 746 */ 747 Sanitize_Repository_Name (cvsroot_copy); 748 newroot->directory = xstrdup (cvsroot_copy); 749 750 /* 751 * Do various sanity checks. 752 */ 753 754 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) 755 if (newroot->username && ! newroot->hostname) 756 { 757 error (0, 0, "Missing hostname in CVSROOT."); 758 goto error_exit; 759 } 760 761 /* We won't have attempted to parse these without CLIENT_SUPPORT or 762 * SERVER_SUPPORT. 763 */ 764 check_hostname = 0; 765 no_password = 1; 766 no_proxy = 1; 767 no_port = 0; 768 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */ 769 switch (newroot->method) 770 { 771 case local_method: 772 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) 773 if (newroot->username || newroot->hostname) 774 { 775 error (0, 0, "Can't specify hostname and username in CVSROOT"); 776 error (0, 0, "when using local access method."); 777 goto error_exit; 778 } 779 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */ 780 /* cvs.texinfo has always told people that CVSROOT must be an 781 absolute pathname. Furthermore, attempts to use a relative 782 pathname produced various errors (I couldn't get it to work), 783 so there would seem to be little risk in making this a fatal 784 error. */ 785 if (!ISABSOLUTE (newroot->directory)) 786 { 787 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')", 788 newroot->directory); 789 error (0, 0, "when using local access method."); 790 goto error_exit; 791 } 792 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) 793 /* We don't need to check for these in :local: mode, really, since 794 * we shouldn't be able to hit the code above which parses them, but 795 * I'm leaving them here in lieu of assertions. 796 */ 797 no_port = 1; 798 /* no_password already set */ 799 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */ 800 break; 801 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) 802 case fork_method: 803 /* We want :fork: to behave the same as other remote access 804 methods. Therefore, don't check to see that the repository 805 name is absolute -- let the server do it. */ 806 if (newroot->username || newroot->hostname) 807 { 808 error (0, 0, "Can't specify hostname and username in CVSROOT"); 809 error (0, 0, "when using fork access method."); 810 goto error_exit; 811 } 812 newroot->hostname = xstrdup("server"); /* for error messages */ 813 if (!ISABSOLUTE (newroot->directory)) 814 { 815 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')", 816 newroot->directory); 817 error (0, 0, "when using fork access method."); 818 goto error_exit; 819 } 820 no_port = 1; 821 /* no_password already set */ 822 break; 823 case kserver_method: 824 check_hostname = 1; 825 /* no_password already set */ 826 break; 827 case gserver_method: 828 check_hostname = 1; 829 no_proxy = 0; 830 /* no_password already set */ 831 break; 832 case server_method: 833 case ext_method: 834 no_port = 1; 835 /* no_password already set */ 836 check_hostname = 1; 837 break; 838 case pserver_method: 839 no_password = 0; 840 no_proxy = 0; 841 check_hostname = 1; 842 break; 843 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */ 844 default: 845 error (1, 0, "Invalid method found in parse_cvsroot"); 846 } 847 848 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) 849 if (no_password && newroot->password) 850 { 851 error (0, 0, "CVSROOT password specification is only valid for"); 852 error (0, 0, "pserver connection method."); 853 goto error_exit; 854 } 855 if (no_proxy && (newroot->proxy_hostname || newroot->proxy_port)) 856 { 857 error (0, 0, 858 "CVSROOT proxy specification is only valid for gserver and"); 859 error (0, 0, "pserver connection methods."); 860 goto error_exit; 861 } 862 863 if (!newroot->proxy_hostname && newroot->proxy_port) 864 { 865 error (0, 0, "Proxy port specified in CVSROOT without proxy host."); 866 goto error_exit; 867 } 868 869 if (check_hostname && !newroot->hostname) 870 { 871 error (0, 0, "Didn't specify hostname in CVSROOT."); 872 goto error_exit; 873 } 874 875 if (no_port && newroot->port) 876 { 877 error (0, 0, 878 "CVSROOT port specification is only valid for gserver, kserver,"); 879 error (0, 0, "and pserver connection methods."); 880 goto error_exit; 881 } 882 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */ 883 884 if (*newroot->directory == '\0') 885 { 886 error (0, 0, "Missing directory in CVSROOT."); 887 goto error_exit; 888 } 889 890 /* Hooray! We finally parsed it! */ 891 free (cvsroot_save); 892 893 if (!cache) cache = getlist(); 894 node = getnode(); 895 node->key = xstrdup (newroot->original); 896 node->data = newroot; 897 addnode (cache, node); 898 return newroot; 899 900 error_exit: 901 free (cvsroot_save); 902 free_cvsroot_t (newroot); 903 return NULL; 904 } 905 906 907 908 #ifdef AUTH_CLIENT_SUPPORT 909 /* Use root->username, root->hostname, root->port, and root->directory 910 * to create a normalized CVSROOT fit for the .cvspass file 911 * 912 * username defaults to the result of getcaller() 913 * port defaults to the result of get_cvs_port_number() 914 * 915 * FIXME - we could cache the canonicalized version of a root inside the 916 * cvsroot_t, but we'd have to un'const the input here and stop expecting the 917 * caller to be responsible for our return value 918 * 919 * ASSUMPTIONS 920 * ROOT->method == pserver_method 921 */ 922 char * 923 normalize_cvsroot (const cvsroot_t *root) 924 { 925 char *cvsroot_canonical; 926 char *p, *hostname; 927 928 assert (root && root->hostname && root->directory); 929 930 /* use a lower case hostname since we know hostnames are case insensitive */ 931 /* Some logic says we should be tacking our domain name on too if it isn't 932 * there already, but for now this works. Reverse->Forward lookups are 933 * almost certainly too much since that would make CVS immune to some of 934 * the DNS trickery that makes life easier for sysadmins when they want to 935 * move a repository or the like 936 */ 937 p = hostname = xstrdup (root->hostname); 938 while (*p) 939 { 940 *p = tolower (*p); 941 p++; 942 } 943 944 cvsroot_canonical = Xasprintf (":pserver:%s@%s:%d%s", 945 root->username ? root->username 946 : getcaller(), 947 hostname, get_cvs_port_number (root), 948 root->directory); 949 950 free (hostname); 951 return cvsroot_canonical; 952 } 953 #endif /* AUTH_CLIENT_SUPPORT */ 954 955 956 957 #ifdef PROXY_SUPPORT 958 /* A walklist() function to walk the root_allow list looking for a PrimaryServer 959 * configuration with a directory matching the requested directory. 960 * 961 * If found, replace it. 962 */ 963 static bool get_local_root_dir_done; 964 static int 965 get_local_root_dir (Node *p, void *root_in) 966 { 967 struct config *c = p->data; 968 char **r = root_in; 969 970 if (get_local_root_dir_done) 971 return 0; 972 973 if (c->PrimaryServer && !strcmp (*r, c->PrimaryServer->directory)) 974 { 975 free (*r); 976 *r = xstrdup (p->key); 977 get_local_root_dir_done = true; 978 } 979 return 0; 980 } 981 #endif /* PROXY_SUPPORT */ 982 983 984 985 /* allocate and return a cvsroot_t structure set up as if we're using the local 986 * repository DIR. */ 987 cvsroot_t * 988 local_cvsroot (const char *dir) 989 { 990 cvsroot_t *newroot = new_cvsroot_t(); 991 992 newroot->original = xstrdup(dir); 993 newroot->method = local_method; 994 newroot->directory = xstrdup(dir); 995 /* Here and parse_cvsroot() should be the only places this needs to be 996 * called on a CVSROOT now. cvsroot->original is saved for error messages 997 * and, otherwise, we want no trailing slashes. 998 */ 999 Sanitize_Repository_Name (newroot->directory); 1000 1001 #ifdef PROXY_SUPPORT 1002 /* Translate the directory to a local one in the case that we are 1003 * configured as a secondary. If root_allow has not been initialized, 1004 * nothing happens. 1005 */ 1006 get_local_root_dir_done = false; 1007 walklist (root_allow, get_local_root_dir, &newroot->directory); 1008 #endif /* PROXY_SUPPORT */ 1009 1010 return newroot; 1011 } 1012 1013 1014 1015 #ifdef DEBUG 1016 /* This is for testing the parsing function. Use 1017 1018 gcc -I. -I.. -I../lib -DDEBUG root.c -o root 1019 1020 to compile. */ 1021 1022 #include <stdio.h> 1023 1024 char *program_name = "testing"; 1025 char *cvs_cmd_name = "parse_cvsroot"; /* XXX is this used??? */ 1026 1027 void 1028 main (int argc, char *argv[]) 1029 { 1030 program_name = argv[0]; 1031 1032 if (argc != 2) 1033 { 1034 fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name); 1035 exit (2); 1036 } 1037 1038 if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL) 1039 { 1040 fprintf (stderr, "%s: Parsing failed.\n", program_name); 1041 exit (1); 1042 } 1043 printf ("CVSroot: %s\n", argv[1]); 1044 printf ("current_parsed_root->method: %s\n", 1045 method_names[current_parsed_root->method]); 1046 printf ("current_parsed_root->username: %s\n", 1047 current_parsed_root->username 1048 ? current_parsed_root->username : "NULL"); 1049 printf ("current_parsed_root->hostname: %s\n", 1050 current_parsed_root->hostname 1051 ? current_parsed_root->hostname : "NULL"); 1052 printf ("current_parsed_root->directory: %s\n", 1053 current_parsed_root->directory); 1054 1055 exit (0); 1056 /* NOTREACHED */ 1057 } 1058 #endif 1059