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)
640 "Read-only repository feature unavailable with remote roots (cvsroot = %s)",
643 if ((newroot->method != local_method)
644 && (newroot->method != fork_method)
647 /* split the string into [[user][:password]@]host[:[port]] & /path
649 * this will allow some characters such as '@' & ':' to remain unquoted
650 * in the path portion of the spec
652 if ((p = strchr (cvsroot_copy, '/')) == NULL)
654 error (0, 0, "CVSROOT requires a path spec:");
656 ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
657 error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
660 firstslash = p; /* == NULL if '/' not in string */
663 /* Check to see if there is a username[:password] in the string. */
664 if ((p = strchr (cvsroot_copy, '@')) != NULL)
667 /* check for a password */
668 if ((q = strchr (cvsroot_copy, ':')) != NULL)
671 newroot->password = xstrdup (++q);
672 /* Don't check for *newroot->password == '\0' since
673 * a user could conceivably wish to specify a blank password
675 * (newroot->password == NULL means to use the
676 * password from .cvspass)
680 /* copy the username */
681 if (*cvsroot_copy != '\0')
682 /* a blank username is impossible, so leave it NULL in that
683 * case so we know to use the default username
685 newroot->username = xstrdup (cvsroot_copy);
690 /* now deal with host[:[port]] */
693 if ((p = strchr (cvsroot_copy, ':')) != NULL)
705 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
708 "Perhaps you entered a relative pathname?");
712 if ((newroot->port = atoi (p)) <= 0)
715 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
717 error (0, 0, "Perhaps you entered a relative pathname?");
724 if (*cvsroot_copy != '\0')
725 /* blank hostnames are invalid, but for now leave the field NULL
726 * and catch the error during the sanity checks later
728 newroot->hostname = xstrdup (cvsroot_copy);
730 /* restore the '/' */
731 cvsroot_copy = firstslash;
734 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
737 * Parse the path for all methods.
739 /* Here & local_cvsroot() should be the only places this needs to be
740 * called on a CVSROOT now. cvsroot->original is saved for error messages
741 * and, otherwise, we want no trailing slashes.
743 Sanitize_Repository_Name (cvsroot_copy);
744 newroot->directory = xstrdup (cvsroot_copy);
747 * Do various sanity checks.
750 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
751 if (newroot->username && ! newroot->hostname)
753 error (0, 0, "Missing hostname in CVSROOT.");
757 /* We won't have attempted to parse these without CLIENT_SUPPORT or
764 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
765 switch (newroot->method)
768 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
769 if (newroot->username || newroot->hostname)
771 error (0, 0, "Can't specify hostname and username in CVSROOT");
772 error (0, 0, "when using local access method.");
775 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
776 /* cvs.texinfo has always told people that CVSROOT must be an
777 absolute pathname. Furthermore, attempts to use a relative
778 pathname produced various errors (I couldn't get it to work),
779 so there would seem to be little risk in making this a fatal
781 if (!ISABSOLUTE (newroot->directory))
783 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
785 error (0, 0, "when using local access method.");
788 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
789 /* We don't need to check for these in :local: mode, really, since
790 * we shouldn't be able to hit the code above which parses them, but
791 * I'm leaving them here in lieu of assertions.
794 /* no_password already set */
795 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
797 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
799 /* We want :fork: to behave the same as other remote access
800 methods. Therefore, don't check to see that the repository
801 name is absolute -- let the server do it. */
802 if (newroot->username || newroot->hostname)
804 error (0, 0, "Can't specify hostname and username in CVSROOT");
805 error (0, 0, "when using fork access method.");
808 newroot->hostname = xstrdup("server"); /* for error messages */
809 if (!ISABSOLUTE (newroot->directory))
811 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
813 error (0, 0, "when using fork access method.");
817 /* no_password already set */
821 /* no_password already set */
826 /* no_password already set */
831 /* no_password already set */
839 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
841 error (1, 0, "Invalid method found in parse_cvsroot");
844 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
845 if (no_password && newroot->password)
847 error (0, 0, "CVSROOT password specification is only valid for");
848 error (0, 0, "pserver connection method.");
851 if (no_proxy && (newroot->proxy_hostname || newroot->proxy_port))
854 "CVSROOT proxy specification is only valid for gserver and");
855 error (0, 0, "pserver connection methods.");
859 if (!newroot->proxy_hostname && newroot->proxy_port)
861 error (0, 0, "Proxy port specified in CVSROOT without proxy host.");
865 if (check_hostname && !newroot->hostname)
867 error (0, 0, "Didn't specify hostname in CVSROOT.");
871 if (no_port && newroot->port)
874 "CVSROOT port specification is only valid for gserver, kserver,");
875 error (0, 0, "and pserver connection methods.");
878 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
880 if (*newroot->directory == '\0')
882 error (0, 0, "Missing directory in CVSROOT.");
886 /* Hooray! We finally parsed it! */
889 if (!cache) cache = getlist();
891 node->key = xstrdup (newroot->original);
892 node->data = newroot;
893 addnode (cache, node);
898 free_cvsroot_t (newroot);
904 #ifdef AUTH_CLIENT_SUPPORT
905 /* Use root->username, root->hostname, root->port, and root->directory
906 * to create a normalized CVSROOT fit for the .cvspass file
908 * username defaults to the result of getcaller()
909 * port defaults to the result of get_cvs_port_number()
911 * FIXME - we could cache the canonicalized version of a root inside the
912 * cvsroot_t, but we'd have to un'const the input here and stop expecting the
913 * caller to be responsible for our return value
916 * ROOT->method == pserver_method
919 normalize_cvsroot (const cvsroot_t *root)
921 char *cvsroot_canonical;
924 assert (root && root->hostname && root->directory);
926 /* use a lower case hostname since we know hostnames are case insensitive */
927 /* Some logic says we should be tacking our domain name on too if it isn't
928 * there already, but for now this works. Reverse->Forward lookups are
929 * almost certainly too much since that would make CVS immune to some of
930 * the DNS trickery that makes life easier for sysadmins when they want to
931 * move a repository or the like
933 p = hostname = xstrdup (root->hostname);
940 cvsroot_canonical = Xasprintf (":pserver:%s@%s:%d%s",
941 root->username ? root->username
943 hostname, get_cvs_port_number (root),
947 return cvsroot_canonical;
949 #endif /* AUTH_CLIENT_SUPPORT */
954 /* A walklist() function to walk the root_allow list looking for a PrimaryServer
955 * configuration with a directory matching the requested directory.
957 * If found, replace it.
959 static bool get_local_root_dir_done;
961 get_local_root_dir (Node *p, void *root_in)
963 struct config *c = p->data;
966 if (get_local_root_dir_done)
969 if (c->PrimaryServer && !strcmp (*r, c->PrimaryServer->directory))
972 *r = xstrdup (p->key);
973 get_local_root_dir_done = true;
977 #endif /* PROXY_SUPPORT */
981 /* allocate and return a cvsroot_t structure set up as if we're using the local
984 local_cvsroot (const char *dir)
986 cvsroot_t *newroot = new_cvsroot_t();
988 newroot->original = xstrdup(dir);
989 newroot->method = local_method;
990 newroot->directory = xstrdup(dir);
991 /* Here and parse_cvsroot() should be the only places this needs to be
992 * called on a CVSROOT now. cvsroot->original is saved for error messages
993 * and, otherwise, we want no trailing slashes.
995 Sanitize_Repository_Name (newroot->directory);
998 /* Translate the directory to a local one in the case that we are
999 * configured as a secondary. If root_allow has not been initialized,
1002 get_local_root_dir_done = false;
1003 walklist (root_allow, get_local_root_dir, &newroot->directory);
1004 #endif /* PROXY_SUPPORT */
1012 /* This is for testing the parsing function. Use
1014 gcc -I. -I.. -I../lib -DDEBUG root.c -o root
1020 char *program_name = "testing";
1021 char *cvs_cmd_name = "parse_cvsroot"; /* XXX is this used??? */
1024 main (int argc, char *argv[])
1026 program_name = argv[0];
1030 fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
1034 if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
1036 fprintf (stderr, "%s: Parsing failed.\n", program_name);
1039 printf ("CVSroot: %s\n", argv[1]);
1040 printf ("current_parsed_root->method: %s\n",
1041 method_names[current_parsed_root->method]);
1042 printf ("current_parsed_root->username: %s\n",
1043 current_parsed_root->username
1044 ? current_parsed_root->username : "NULL");
1045 printf ("current_parsed_root->hostname: %s\n",
1046 current_parsed_root->hostname
1047 ? current_parsed_root->hostname : "NULL");
1048 printf ("current_parsed_root->directory: %s\n",
1049 current_parsed_root->directory);