2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
7 * Poritons Copyright (c) 1992, Mark D. Baushke
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.
14 * Determine the path to the CVSROOT and set "Root" accordingly.
21 /* Printable names for things in the current_parsed_root->method enum variable.
22 Watch out if the enum is changed in cvs.h! */
24 const char method_names[][16] = {
25 "undefined", "local", "server (rsh)", "pserver",
26 "kserver", "gserver", "ext", "fork"
32 Name_Root (const char *dir, const char *update_dir)
36 const char *xupdate_dir;
38 size_t root_allocated = 0;
44 TRACE (TRACE_FLOW, "Name_Root (%s, %s)",
46 update_dir ? update_dir : "(null)");
48 if (update_dir && *update_dir)
49 xupdate_dir = update_dir;
55 cvsadm = Xasprintf ("%s/%s", dir, CVSADM);
56 tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
60 cvsadm = xstrdup (CVSADM);
61 tmp = xstrdup (CVSADM_ROOT);
65 * Do not bother looking for a readable file if there is no cvsadm
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)))
79 * The assumption here is that the CVS Root is always contained in the
80 * first line of the "Root" file.
82 fpin = xfopen (tmp, "r");
84 if ((len = getline (&root, &root_allocated, fpin)) < 0)
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");
98 *cp = '\0'; /* strip the newline */
101 * root now contains a candidate for CVSroot. It must be an
102 * absolute pathname or specify a remote server.
105 ret = parse_cvsroot (root);
108 error (0, 0, "in directory %s:", xupdate_dir);
110 "ignoring %s because it does not contain a valid root.",
116 #ifdef CLIENT_SUPPORT
119 !isdir (ret->directory))
121 error (0, 0, "in directory %s:", xupdate_dir);
123 "ignoring %s because it specifies a non-existent repository %s",
141 * Write the CVS/Root file so that the environment variable CVSROOT
142 * and/or the -d option to cvs will be validated or not necessary for
146 Create_Root (const char *dir, const char *rootdir)
154 /* record the current cvs root */
159 tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
161 tmp = xstrdup (CVSADM_ROOT);
163 fout = xfopen (tmp, "w+");
164 if (fprintf (fout, "%s\n", rootdir) < 0)
165 error (1, errno, "write to %s failed", tmp);
166 if (fclose (fout) == EOF)
167 error (1, errno, "cannot close %s", tmp);
176 /* Translate an absolute repository string for a primary server and return it.
179 * root_in The root to be translated.
182 * A translated string this function owns, or a pointer to the original
183 * string passed in if no translation was necessary.
185 * If the returned string is the translated one, it may be overwritten
186 * by the next call to this function.
189 primary_root_translate (const char *root_in)
193 static char *previous = NULL;
196 /* This can happen, for instance, during `cvs init'. */
197 if (!config) return root_in;
199 if (config->PrimaryServer
200 && !strncmp (root_in, config->PrimaryServer->directory,
201 strlen (config->PrimaryServer->directory))
202 && (ISSLASH (root_in[strlen (config->PrimaryServer->directory)])
203 || root_in[strlen (config->PrimaryServer->directory)] == '\0')
207 Xasnprintf (previous, &len,
208 "%s%s", current_parsed_root->directory,
209 root_in + strlen (config->PrimaryServer->directory));
210 if (previous && previous != translated)
212 return previous = translated;
216 /* There is no primary root configured or it didn't match. */
222 /* Translate a primary root in reverse for PATHNAMEs in responses.
225 * root_in The root to be translated.
228 * A translated string this function owns, or a pointer to the original
229 * string passed in if no translation was necessary.
231 * If the returned string is the translated one, it may be overwritten
232 * by the next call to this function.
235 primary_root_inverse_translate (const char *root_in)
239 static char *previous = NULL;
242 /* This can happen, for instance, during `cvs init'. */
243 if (!config) return root_in;
245 if (config->PrimaryServer
246 && !strncmp (root_in, current_parsed_root->directory,
247 strlen (current_parsed_root->directory))
248 && (ISSLASH (root_in[strlen (current_parsed_root->directory)])
249 || root_in[strlen (current_parsed_root->directory)] == '\0')
253 Xasnprintf (previous, &len,
254 "%s%s", config->PrimaryServer->directory,
255 root_in + strlen (current_parsed_root->directory));
256 if (previous && previous != translated)
258 return previous = translated;
262 /* There is no primary root configured or it didn't match. */
268 /* The root_allow_* stuff maintains a list of valid CVSROOT
269 directories. Then we can check against them when a remote user
270 hands us a CVSROOT directory. */
271 static List *root_allow;
276 if (n->data) free_config (n->data);
282 root_allow_add (const char *arg)
286 if (!root_allow) root_allow = getlist();
288 n->key = xstrdup (arg);
289 n->data = parse_config (arg);
290 n->delproc = delconfig;
291 addnode (root_allow, n);
295 root_allow_free (void)
297 dellist (&root_allow);
301 root_allow_ok (const char *arg)
305 /* Probably someone upgraded from CVS before 1.9.10 to 1.9.10
306 or later without reading the documentation about
307 --allow-root. Printing an error here doesn't disclose any
308 particularly useful information to an attacker because a
309 CVS server configured in this way won't let *anyone* in. */
311 /* Note that we are called from a context where we can spit
312 back "error" rather than waiting for the next request which
313 expects responses. */
315 error 0 Server configuration missing --allow-root in inetd.conf\n");
319 if (findnode (root_allow, arg))
326 /* Get a config we stored in response to root_allow.
329 * The config associated with ARG.
332 get_root_allow_config (const char *arg)
336 TRACE (TRACE_FUNCTION, "get_root_allow_config (%s)", arg);
339 n = findnode (root_allow, arg);
343 if (n) return n->data;
344 return parse_config (arg);
349 /* This global variable holds the global -d option. It is NULL if -d
350 was not used, which means that we must get the CVSroot information
351 from the CVSROOT environment variable or from a CVS/Root file. */
352 char *CVSroot_cmdline;
356 /* FIXME - Deglobalize this. */
357 cvsroot_t *current_parsed_root = NULL;
358 /* Used to save the original root being processed so that we can still find it
359 * in lists and the like after a `Redirect' response. Also set to mirror
360 * current_parsed_root in server mode so that code which runs on both the
361 * client and server but which wants to use original data on the client can
362 * just always reference the original_parsed_root.
364 const cvsroot_t *original_parsed_root;
367 /* allocate and initialize a cvsroot_t
369 * We must initialize the strings to NULL so we know later what we should
372 * Some of the other zeroes remain meaningful as, "never set, use default",
375 /* Functions which allocate memory are not pure. */
376 static cvsroot_t *new_cvsroot_t(void)
377 __attribute__( (__malloc__) );
383 /* gotta store it somewhere */
384 newroot = xmalloc(sizeof(cvsroot_t));
386 newroot->original = NULL;
387 newroot->method = null_method;
388 #ifdef CLIENT_SUPPORT
389 newroot->username = NULL;
390 newroot->password = NULL;
391 newroot->hostname = NULL;
392 newroot->cvs_rsh = NULL;
393 newroot->cvs_server = NULL;
395 newroot->directory = NULL;
396 newroot->proxy_hostname = NULL;
397 newroot->proxy_port = 0;
398 newroot->isremote = 0;
399 newroot->redirect = true; /* Advertise Redirect support */
400 #endif /* CLIENT_SUPPORT */
407 /* Dispose of a cvsroot_t and its component parts */
409 free_cvsroot_t (cvsroot_t *root)
411 if (root->original != NULL)
412 free (root->original);
413 if (root->directory != NULL)
414 free (root->directory);
415 #ifdef CLIENT_SUPPORT
416 if (root->username != NULL)
417 free (root->username);
418 if (root->password != NULL)
420 /* I like to be paranoid */
421 memset (root->password, 0, strlen (root->password));
422 free (root->password);
424 if (root->hostname != NULL)
425 free (root->hostname);
426 if (root->cvs_rsh != NULL)
427 free (root->cvs_rsh);
428 if (root->cvs_server != NULL)
429 free (root->cvs_server);
430 if (root->proxy_hostname != NULL)
431 free (root->proxy_hostname);
432 #endif /* CLIENT_SUPPORT */
439 * Parse a CVSROOT string to allocate and return a new cvsroot_t structure.
440 * Valid specifications are:
442 * :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path
443 * [:(ext|server):][[user]@]host[:]/path
448 * root_in C String containing the CVSROOT to be parsed.
451 * A pointer to a newly allocated cvsroot_t structure upon success and
452 * NULL upon failure. The caller should never dispose of this structure,
453 * as it is stored in a cache, but the caller may rely on it not to
457 * This would have been a lot easier to write in Perl.
459 * Would it make sense to reimplement the root and config file parsing
466 parse_cvsroot (const char *root_in)
468 cvsroot_t *newroot; /* the new root to be returned */
469 char *cvsroot_save; /* what we allocated so we can dispose
470 * it when finished */
471 char *cvsroot_copy, *p; /* temporary pointers for parsing */
472 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
473 char *q; /* temporary pointer for parsing */
474 char *firstslash; /* save where the path spec starts
476 * [[user][:password]@]host[:[port]]
478 int check_hostname, no_port, no_password, no_proxy;
479 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
480 static List *cache = NULL;
483 assert (root_in != NULL);
485 /* This message is TRACE_FLOW since this function is called repeatedly by
486 * the recursion routines.
488 TRACE (TRACE_FLOW, "parse_cvsroot (%s)", root_in);
490 if ((node = findnode (cache, root_in)))
495 /* allocate some space */
496 newroot = new_cvsroot_t();
498 /* save the original string */
499 newroot->original = xstrdup (root_in);
501 /* and another copy we can munge while parsing */
502 cvsroot_save = cvsroot_copy = xstrdup (root_in);
504 if (*cvsroot_copy == ':')
506 char *method = ++cvsroot_copy;
508 /* Access method specified, as in
509 * "cvs -d :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path",
510 * "cvs -d [:(ext|server):][[user]@]host[:]/path",
511 * "cvs -d :local:e:\path",
512 * "cvs -d :fork:/path".
513 * We need to get past that part of CVSroot before parsing the
517 if (! (p = strchr (method, ':')))
519 error (0, 0, "No closing `:' on method in CVSROOT.");
525 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
526 /* Look for method options, for instance, proxy, proxyport.
527 * Calling strtok again is saved until after parsing the method.
529 method = strtok (method, ";");
531 /* Could just exit now, but this keeps the error message in sync.
534 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
536 /* Now we have an access method -- see if it's valid. */
538 if (!strcasecmp (method, "local"))
539 newroot->method = local_method;
540 else if (!strcasecmp (method, "pserver"))
541 newroot->method = pserver_method;
542 else if (!strcasecmp (method, "kserver"))
543 newroot->method = kserver_method;
544 else if (!strcasecmp (method, "gserver"))
545 newroot->method = gserver_method;
546 else if (!strcasecmp (method, "server"))
547 newroot->method = server_method;
548 else if (!strcasecmp (method, "ext"))
549 newroot->method = ext_method;
550 else if (!strcasecmp (method, "fork"))
551 newroot->method = fork_method;
554 error (0, 0, "Unknown method (`%s') in CVSROOT.", method);
558 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
559 /* Parse the method options, for instance, proxy, proxyport */
560 while ((p = strtok (NULL, ";")))
562 char *q = strchr (p, '=');
565 error (0, 0, "Option (`%s') has no argument in CVSROOT.",
571 TRACE (TRACE_DATA, "CVSROOT option=`%s' value=`%s'", p, q);
572 if (!strcasecmp (p, "proxy"))
574 newroot->proxy_hostname = xstrdup (q);
576 else if (!strcasecmp (p, "proxyport"))
585 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
590 if ((newroot->proxy_port = atoi (q)) <= 0)
592 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
595 else if (!strcasecmp (p, "CVS_RSH"))
597 /* override CVS_RSH environment variable */
598 if (newroot->method == ext_method)
599 newroot->cvs_rsh = xstrdup (q);
601 else if (!strcasecmp (p, "CVS_SERVER"))
603 /* override CVS_SERVER environment variable */
604 if (newroot->method == ext_method
605 || newroot->method == fork_method)
606 newroot->cvs_server = xstrdup (q);
608 else if (!strcasecmp (p, "Redirect"))
609 readBool ("CVSROOT", "Redirect", q, &newroot->redirect);
612 error (0, 0, "Unknown option (`%s') in CVSROOT.", p);
616 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
620 /* If the method isn't specified, assume EXT_METHOD if the string looks
621 like a relative path and LOCAL_METHOD otherwise. */
623 newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
629 * There are a few sanity checks we can do now, only knowing the
630 * method of this root.
633 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
634 newroot->isremote = (newroot->method != local_method);
636 if (readonlyfs && newroot->isremote)
638 "Read-only repository feature unavailable with remote roots (cvsroot = %s)",
641 if ((newroot->method != local_method)
642 && (newroot->method != fork_method)
645 /* split the string into [[user][:password]@]host[:[port]] & /path
647 * this will allow some characters such as '@' & ':' to remain unquoted
648 * in the path portion of the spec
650 if ((p = strchr (cvsroot_copy, '/')) == NULL)
652 error (0, 0, "CVSROOT requires a path spec:");
654 ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
655 error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
658 firstslash = p; /* == NULL if '/' not in string */
661 /* Check to see if there is a username[:password] in the string. */
662 if ((p = strchr (cvsroot_copy, '@')) != NULL)
665 /* check for a password */
666 if ((q = strchr (cvsroot_copy, ':')) != NULL)
669 newroot->password = xstrdup (++q);
670 /* Don't check for *newroot->password == '\0' since
671 * a user could conceivably wish to specify a blank password
673 * (newroot->password == NULL means to use the
674 * password from .cvspass)
678 /* copy the username */
679 if (*cvsroot_copy != '\0')
680 /* a blank username is impossible, so leave it NULL in that
681 * case so we know to use the default username
683 newroot->username = xstrdup (cvsroot_copy);
688 /* now deal with host[:[port]] */
691 if ((p = strchr (cvsroot_copy, ':')) != NULL)
703 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
706 "Perhaps you entered a relative pathname?");
710 if ((newroot->port = atoi (p)) <= 0)
713 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
715 error (0, 0, "Perhaps you entered a relative pathname?");
722 if (*cvsroot_copy != '\0')
723 /* blank hostnames are invalid, but for now leave the field NULL
724 * and catch the error during the sanity checks later
726 newroot->hostname = xstrdup (cvsroot_copy);
728 /* restore the '/' */
729 cvsroot_copy = firstslash;
732 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
735 * Parse the path for all methods.
737 /* Here & local_cvsroot() should be the only places this needs to be
738 * called on a CVSROOT now. cvsroot->original is saved for error messages
739 * and, otherwise, we want no trailing slashes.
741 Sanitize_Repository_Name (cvsroot_copy);
742 newroot->directory = xstrdup (cvsroot_copy);
745 * Do various sanity checks.
748 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
749 if (newroot->username && ! newroot->hostname)
751 error (0, 0, "Missing hostname in CVSROOT.");
755 /* We won't have attempted to parse these without CLIENT_SUPPORT or
762 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
763 switch (newroot->method)
766 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
767 if (newroot->username || newroot->hostname)
769 error (0, 0, "Can't specify hostname and username in CVSROOT");
770 error (0, 0, "when using local access method.");
773 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
774 /* cvs.texinfo has always told people that CVSROOT must be an
775 absolute pathname. Furthermore, attempts to use a relative
776 pathname produced various errors (I couldn't get it to work),
777 so there would seem to be little risk in making this a fatal
779 if (!isabsolute (newroot->directory))
781 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
783 error (0, 0, "when using local access method.");
786 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
787 /* We don't need to check for these in :local: mode, really, since
788 * we shouldn't be able to hit the code above which parses them, but
789 * I'm leaving them here in lieu of assertions.
792 /* no_password already set */
793 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
795 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
797 /* We want :fork: to behave the same as other remote access
798 methods. Therefore, don't check to see that the repository
799 name is absolute -- let the server do it. */
800 if (newroot->username || newroot->hostname)
802 error (0, 0, "Can't specify hostname and username in CVSROOT");
803 error (0, 0, "when using fork access method.");
806 newroot->hostname = xstrdup("server"); /* for error messages */
807 if (!isabsolute (newroot->directory))
809 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
811 error (0, 0, "when using fork access method.");
815 /* no_password already set */
819 /* no_password already set */
824 /* no_password already set */
829 /* no_password already set */
837 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
839 error (1, 0, "Invalid method found in parse_cvsroot");
842 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
843 if (no_password && newroot->password)
845 error (0, 0, "CVSROOT password specification is only valid for");
846 error (0, 0, "pserver connection method.");
849 if (no_proxy && (newroot->proxy_hostname || newroot->proxy_port))
852 "CVSROOT proxy specification is only valid for gserver and");
853 error (0, 0, "pserver connection methods.");
857 if (!newroot->proxy_hostname && newroot->proxy_port)
859 error (0, 0, "Proxy port specified in CVSROOT without proxy host.");
863 if (check_hostname && !newroot->hostname)
865 error (0, 0, "Didn't specify hostname in CVSROOT.");
869 if (no_port && newroot->port)
872 "CVSROOT port specification is only valid for gserver, kserver,");
873 error (0, 0, "and pserver connection methods.");
876 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
878 if (*newroot->directory == '\0')
880 error (0, 0, "Missing directory in CVSROOT.");
884 /* Hooray! We finally parsed it! */
887 if (!cache) cache = getlist();
889 node->key = xstrdup (newroot->original);
890 node->data = newroot;
891 addnode (cache, node);
896 free_cvsroot_t (newroot);
902 #ifdef AUTH_CLIENT_SUPPORT
903 /* Use root->username, root->hostname, root->port, and root->directory
904 * to create a normalized CVSROOT fit for the .cvspass file
906 * username defaults to the result of getcaller()
907 * port defaults to the result of get_cvs_port_number()
909 * FIXME - we could cache the canonicalized version of a root inside the
910 * cvsroot_t, but we'd have to un'const the input here and stop expecting the
911 * caller to be responsible for our return value
914 * ROOT->method == pserver_method
917 normalize_cvsroot (const cvsroot_t *root)
919 char *cvsroot_canonical;
922 assert (root && root->hostname && root->directory);
924 /* use a lower case hostname since we know hostnames are case insensitive */
925 /* Some logic says we should be tacking our domain name on too if it isn't
926 * there already, but for now this works. Reverse->Forward lookups are
927 * almost certainly too much since that would make CVS immune to some of
928 * the DNS trickery that makes life easier for sysadmins when they want to
929 * move a repository or the like
931 p = hostname = xstrdup (root->hostname);
938 cvsroot_canonical = Xasprintf (":pserver:%s@%s:%d%s",
939 root->username ? root->username
941 hostname, get_cvs_port_number (root),
945 return cvsroot_canonical;
947 #endif /* AUTH_CLIENT_SUPPORT */
952 /* A walklist() function to walk the root_allow list looking for a PrimaryServer
953 * configuration with a directory matching the requested directory.
955 * If found, replace it.
957 static bool get_local_root_dir_done;
959 get_local_root_dir (Node *p, void *root_in)
961 struct config *c = p->data;
964 if (get_local_root_dir_done)
967 if (c->PrimaryServer && !strcmp (*r, c->PrimaryServer->directory))
970 *r = xstrdup (p->key);
971 get_local_root_dir_done = true;
975 #endif /* PROXY_SUPPORT */
979 /* allocate and return a cvsroot_t structure set up as if we're using the local
982 local_cvsroot (const char *dir)
984 cvsroot_t *newroot = new_cvsroot_t();
986 newroot->original = xstrdup(dir);
987 newroot->method = local_method;
988 newroot->directory = xstrdup(dir);
989 /* Here and parse_cvsroot() should be the only places this needs to be
990 * called on a CVSROOT now. cvsroot->original is saved for error messages
991 * and, otherwise, we want no trailing slashes.
993 Sanitize_Repository_Name (newroot->directory);
996 /* Translate the directory to a local one in the case that we are
997 * configured as a secondary. If root_allow has not been initialized,
1000 get_local_root_dir_done = false;
1001 walklist (root_allow, get_local_root_dir, &newroot->directory);
1002 #endif /* PROXY_SUPPORT */
1010 /* This is for testing the parsing function. Use
1012 gcc -I. -I.. -I../lib -DDEBUG root.c -o root
1018 char *program_name = "testing";
1019 char *cvs_cmd_name = "parse_cvsroot"; /* XXX is this used??? */
1021 /* Toy versions of various functions when debugging under unix. Yes,
1022 these make various bad assumptions, but they're pretty easy to
1023 debug when something goes wrong. */
1026 isabsolute( const char *dir )
1028 return (dir && (*dir == '/'));
1032 main( int argc, char *argv[] )
1034 program_name = argv[0];
1038 fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
1042 if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
1044 fprintf (stderr, "%s: Parsing failed.\n", program_name);
1047 printf ("CVSroot: %s\n", argv[1]);
1048 printf ("current_parsed_root->method: %s\n", method_names[current_parsed_root->method]);
1049 printf ("current_parsed_root->username: %s\n",
1050 current_parsed_root->username ? current_parsed_root->username : "NULL");
1051 printf ("current_parsed_root->hostname: %s\n",
1052 current_parsed_root->hostname ? current_parsed_root->hostname : "NULL");
1053 printf ("current_parsed_root->directory: %s\n", current_parsed_root->directory);