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.",
115 if (!ret->isremote && !isdir (ret->directory))
117 error (0, 0, "in directory %s:", xupdate_dir);
119 "ignoring %s because it specifies a non-existent repository %s",
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
142 Create_Root (const char *dir, const char *rootdir)
150 /* record the current cvs root */
155 tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
157 tmp = xstrdup (CVSADM_ROOT);
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);
172 /* Translate an absolute repository string for a primary server and return it.
175 * root_in The root to be translated.
178 * A translated string this function owns, or a pointer to the original
179 * string passed in if no translation was necessary.
181 * If the returned string is the translated one, it may be overwritten
182 * by the next call to this function.
185 primary_root_translate (const char *root_in)
189 static char *previous = NULL;
192 /* This can happen, for instance, during `cvs init'. */
193 if (!config) return root_in;
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')
203 Xasnprintf (previous, &len,
204 "%s%s", current_parsed_root->directory,
205 root_in + strlen (config->PrimaryServer->directory));
206 if (previous && previous != translated)
208 return previous = translated;
212 /* There is no primary root configured or it didn't match. */
218 /* Translate a primary root in reverse for PATHNAMEs in responses.
221 * root_in The root to be translated.
224 * A translated string this function owns, or a pointer to the original
225 * string passed in if no translation was necessary.
227 * If the returned string is the translated one, it may be overwritten
228 * by the next call to this function.
231 primary_root_inverse_translate (const char *root_in)
235 static char *previous = NULL;
238 /* This can happen, for instance, during `cvs init'. */
239 if (!config) return root_in;
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')
249 Xasnprintf (previous, &len,
250 "%s%s", config->PrimaryServer->directory,
251 root_in + strlen (current_parsed_root->directory));
252 if (previous && previous != translated)
254 return previous = translated;
258 /* There is no primary root configured or it didn't match. */
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;
272 if (n->data) free_config (n->data);
278 root_allow_add (const char *arg, const char *configPath)
282 if (!root_allow) root_allow = getlist();
284 n->key = xstrdup (arg);
285 n->data = parse_config (arg, configPath);
286 n->delproc = delconfig;
287 addnode (root_allow, n);
291 root_allow_free (void)
293 dellist (&root_allow);
297 root_allow_ok (const char *arg)
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. */
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. */
311 error 0 Server configuration missing --allow-root in inetd.conf\n");
315 if (findnode (root_allow, arg))
322 /* Get a config we stored in response to root_allow.
325 * The config associated with ARG.
328 get_root_allow_config (const char *arg, const char *configPath)
332 TRACE (TRACE_FUNCTION, "get_root_allow_config (%s)", arg);
335 n = findnode (root_allow, arg);
339 if (n) return n->data;
340 return parse_config (arg, configPath);
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;
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.
360 const cvsroot_t *original_parsed_root;
363 /* allocate and initialize a cvsroot_t
365 * We must initialize the strings to NULL so we know later what we should
368 * Some of the other zeroes remain meaningful as, "never set, use default",
371 /* Functions which allocate memory are not pure. */
372 static cvsroot_t *new_cvsroot_t(void)
373 __attribute__( (__malloc__) );
379 /* gotta store it somewhere */
380 newroot = xmalloc(sizeof(cvsroot_t));
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;
393 newroot->proxy_hostname = NULL;
394 newroot->proxy_port = 0;
395 newroot->redirect = true; /* Advertise Redirect support */
396 #endif /* CLIENT_SUPPORT */
403 /* Dispose of a cvsroot_t and its component parts.
406 * It is dangerous for most code to call this function since parse_cvsroot
407 * maintains a cache of parsed roots.
410 free_cvsroot_t (cvsroot_t *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)
422 /* I like to be paranoid */
423 memset (root->password, 0, strlen (root->password));
424 free (root->password);
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 */
441 * Parse a CVSROOT string to allocate and return a new cvsroot_t structure.
442 * Valid specifications are:
444 * :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path
445 * [:(ext|server):][[user]@]host[:]/path
450 * root_in C String containing the CVSROOT to be parsed.
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
459 * This would have been a lot easier to write in Perl.
461 * Would it make sense to reimplement the root and config file parsing
468 parse_cvsroot (const char *root_in)
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
478 * [[user][:password]@]host[:[port]]
480 int check_hostname, no_port, no_password, no_proxy;
481 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
482 static List *cache = NULL;
485 assert (root_in != NULL);
487 /* This message is TRACE_FLOW since this function is called repeatedly by
488 * the recursion routines.
490 TRACE (TRACE_FLOW, "parse_cvsroot (%s)", root_in);
492 if ((node = findnode (cache, root_in)))
497 /* allocate some space */
498 newroot = new_cvsroot_t();
500 /* save the original string */
501 newroot->original = xstrdup (root_in);
503 /* and another copy we can munge while parsing */
504 cvsroot_save = cvsroot_copy = xstrdup (root_in);
506 if (*cvsroot_copy == ':')
508 char *method = ++cvsroot_copy;
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
519 if (! (p = strchr (method, ':')))
521 error (0, 0, "No closing `:' on method in CVSROOT.");
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.
531 method = strtok (method, ";");
533 /* Could just exit now, but this keeps the error message in sync.
536 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
538 /* Now we have an access method -- see if it's valid. */
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;
556 error (0, 0, "Unknown method (`%s') in CVSROOT.", method);
560 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
561 /* Parse the method options, for instance, proxy, proxyport */
562 while ((p = strtok (NULL, ";")))
564 char *q = strchr (p, '=');
567 error (0, 0, "Option (`%s') has no argument in CVSROOT.",
573 TRACE (TRACE_DATA, "CVSROOT option=`%s' value=`%s'", p, q);
574 if (!strcasecmp (p, "proxy"))
576 newroot->proxy_hostname = xstrdup (q);
578 else if (!strcasecmp (p, "proxyport"))
587 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
592 if ((newroot->proxy_port = atoi (q)) <= 0)
594 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
597 else if (!strcasecmp (p, "CVS_RSH"))
599 /* override CVS_RSH environment variable */
600 if (newroot->method == ext_method)
601 newroot->cvs_rsh = xstrdup (q);
603 else if (!strcasecmp (p, "CVS_SERVER"))
605 /* override CVS_SERVER environment variable */
606 if (newroot->method == ext_method
607 || newroot->method == fork_method)
608 newroot->cvs_server = xstrdup (q);
610 else if (!strcasecmp (p, "Redirect"))
611 readBool ("CVSROOT", "Redirect", q, &newroot->redirect);
614 error (0, 0, "Unknown option (`%s') in CVSROOT.", p);
618 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
622 /* If the method isn't specified, assume EXT_METHOD if the string looks
623 like a relative path and LOCAL_METHOD otherwise. */
625 newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
631 * There are a few sanity checks we can do now, only knowing the
632 * method of this root.
635 newroot->isremote = (newroot->method != local_method);
637 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
638 if (readonlyfs && newroot->isremote) {
639 /* downgrade readonlyfs settings via environment */
642 "Read-only repository feature unavailable with remote roots (cvsroot = %s)",
647 if ((newroot->method != local_method)
648 && (newroot->method != fork_method)
651 /* split the string into [[user][:password]@]host[:[port]] & /path
653 * this will allow some characters such as '@' & ':' to remain unquoted
654 * in the path portion of the spec
656 if ((p = strchr (cvsroot_copy, '/')) == NULL)
658 error (0, 0, "CVSROOT requires a path spec:");
660 ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
661 error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
664 firstslash = p; /* == NULL if '/' not in string */
667 /* Check to see if there is a username[:password] in the string. */
668 if ((p = strchr (cvsroot_copy, '@')) != NULL)
671 /* check for a password */
672 if ((q = strchr (cvsroot_copy, ':')) != NULL)
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
679 * (newroot->password == NULL means to use the
680 * password from .cvspass)
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
689 newroot->username = xstrdup (cvsroot_copy);
694 /* now deal with host[:[port]] */
697 if ((p = strchr (cvsroot_copy, ':')) != NULL)
709 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
712 "Perhaps you entered a relative pathname?");
716 if ((newroot->port = atoi (p)) <= 0)
719 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
721 error (0, 0, "Perhaps you entered a relative pathname?");
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
732 newroot->hostname = xstrdup (cvsroot_copy);
734 /* restore the '/' */
735 cvsroot_copy = firstslash;
738 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
741 * Parse the path for all methods.
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.
747 Sanitize_Repository_Name (cvsroot_copy);
748 newroot->directory = xstrdup (cvsroot_copy);
751 * Do various sanity checks.
754 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
755 if (newroot->username && ! newroot->hostname)
757 error (0, 0, "Missing hostname in CVSROOT.");
761 /* We won't have attempted to parse these without CLIENT_SUPPORT or
768 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
769 switch (newroot->method)
772 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
773 if (newroot->username || newroot->hostname)
775 error (0, 0, "Can't specify hostname and username in CVSROOT");
776 error (0, 0, "when using local access method.");
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
785 if (!ISABSOLUTE (newroot->directory))
787 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
789 error (0, 0, "when using local access method.");
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.
798 /* no_password already set */
799 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
801 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
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)
808 error (0, 0, "Can't specify hostname and username in CVSROOT");
809 error (0, 0, "when using fork access method.");
812 newroot->hostname = xstrdup("server"); /* for error messages */
813 if (!ISABSOLUTE (newroot->directory))
815 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
817 error (0, 0, "when using fork access method.");
821 /* no_password already set */
825 /* no_password already set */
830 /* no_password already set */
835 /* no_password already set */
843 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
845 error (1, 0, "Invalid method found in parse_cvsroot");
848 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
849 if (no_password && newroot->password)
851 error (0, 0, "CVSROOT password specification is only valid for");
852 error (0, 0, "pserver connection method.");
855 if (no_proxy && (newroot->proxy_hostname || newroot->proxy_port))
858 "CVSROOT proxy specification is only valid for gserver and");
859 error (0, 0, "pserver connection methods.");
863 if (!newroot->proxy_hostname && newroot->proxy_port)
865 error (0, 0, "Proxy port specified in CVSROOT without proxy host.");
869 if (check_hostname && !newroot->hostname)
871 error (0, 0, "Didn't specify hostname in CVSROOT.");
875 if (no_port && newroot->port)
878 "CVSROOT port specification is only valid for gserver, kserver,");
879 error (0, 0, "and pserver connection methods.");
882 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
884 if (*newroot->directory == '\0')
886 error (0, 0, "Missing directory in CVSROOT.");
890 /* Hooray! We finally parsed it! */
893 if (!cache) cache = getlist();
895 node->key = xstrdup (newroot->original);
896 node->data = newroot;
897 addnode (cache, node);
902 free_cvsroot_t (newroot);
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
912 * username defaults to the result of getcaller()
913 * port defaults to the result of get_cvs_port_number()
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
920 * ROOT->method == pserver_method
923 normalize_cvsroot (const cvsroot_t *root)
925 char *cvsroot_canonical;
928 assert (root && root->hostname && root->directory);
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
937 p = hostname = xstrdup (root->hostname);
944 cvsroot_canonical = Xasprintf (":pserver:%s@%s:%d%s",
945 root->username ? root->username
947 hostname, get_cvs_port_number (root),
951 return cvsroot_canonical;
953 #endif /* AUTH_CLIENT_SUPPORT */
958 /* A walklist() function to walk the root_allow list looking for a PrimaryServer
959 * configuration with a directory matching the requested directory.
961 * If found, replace it.
963 static bool get_local_root_dir_done;
965 get_local_root_dir (Node *p, void *root_in)
967 struct config *c = p->data;
970 if (get_local_root_dir_done)
973 if (c->PrimaryServer && !strcmp (*r, c->PrimaryServer->directory))
976 *r = xstrdup (p->key);
977 get_local_root_dir_done = true;
981 #endif /* PROXY_SUPPORT */
985 /* allocate and return a cvsroot_t structure set up as if we're using the local
988 local_cvsroot (const char *dir)
990 cvsroot_t *newroot = new_cvsroot_t();
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.
999 Sanitize_Repository_Name (newroot->directory);
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,
1006 get_local_root_dir_done = false;
1007 walklist (root_allow, get_local_root_dir, &newroot->directory);
1008 #endif /* PROXY_SUPPORT */
1016 /* This is for testing the parsing function. Use
1018 gcc -I. -I.. -I../lib -DDEBUG root.c -o root
1024 char *program_name = "testing";
1025 char *cvs_cmd_name = "parse_cvsroot"; /* XXX is this used??? */
1028 main (int argc, char *argv[])
1030 program_name = argv[0];
1034 fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
1038 if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
1040 fprintf (stderr, "%s: Parsing failed.\n", program_name);
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);