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", "extssh", "fork"
32 Name_Root (dir, update_dir)
34 const char *update_dir;
38 const char *xupdate_dir;
40 size_t root_allocated = 0;
46 if (update_dir && *update_dir)
47 xupdate_dir = update_dir;
53 cvsadm = xmalloc (strlen (dir) + sizeof (CVSADM) + 10);
54 (void) sprintf (cvsadm, "%s/%s", dir, CVSADM);
55 tmp = xmalloc (strlen (dir) + sizeof (CVSADM_ROOT) + 10);
56 (void) sprintf (tmp, "%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 = open_file (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",
121 free_cvsroot_t (ret);
138 * Write the CVS/Root file so that the environment variable CVSROOT
139 * and/or the -d option to cvs will be validated or not necessary for
143 Create_Root (dir, rootdir)
153 /* record the current cvs root */
159 tmp = xmalloc (strlen (dir) + sizeof (CVSADM_ROOT) + 10);
160 (void) sprintf (tmp, "%s/%s", dir, CVSADM_ROOT);
163 tmp = xstrdup (CVSADM_ROOT);
165 fout = open_file (tmp, "w+");
166 if (fprintf (fout, "%s\n", rootdir) < 0)
167 error (1, errno, "write to %s failed", tmp);
168 if (fclose (fout) == EOF)
169 error (1, errno, "cannot close %s", tmp);
177 /* The root_allow_* stuff maintains a list of legal CVSROOT
178 directories. Then we can check against them when a remote user
179 hands us a CVSROOT directory. */
181 static int root_allow_count;
182 static char **root_allow_vector;
183 static int root_allow_size;
191 if (root_allow_size <= root_allow_count)
193 if (root_allow_size == 0)
197 (char **) xmalloc (root_allow_size * sizeof (char *));
201 root_allow_size *= 2;
203 (char **) xrealloc (root_allow_vector,
204 root_allow_size * sizeof (char *));
207 if (root_allow_vector == NULL)
210 /* Strictly speaking, we're not supposed to output anything
211 now. But we're about to exit(), give it a try. */
212 printf ("E Fatal server error, aborting.\n\
213 error ENOMEM Virtual memory exhausted.\n");
218 p = xmalloc (strlen (arg) + 1);
222 root_allow_vector[root_allow_count++] = p;
228 if (root_allow_vector != NULL)
229 free_names (&root_allow_count, root_allow_vector);
239 if (root_allow_count == 0)
241 /* Probably someone upgraded from CVS before 1.9.10 to 1.9.10
242 or later without reading the documentation about
243 --allow-root. Printing an error here doesn't disclose any
244 particularly useful information to an attacker because a
245 CVS server configured in this way won't let *anyone* in. */
247 /* Note that we are called from a context where we can spit
248 back "error" rather than waiting for the next request which
249 expects responses. */
251 error 0 Server configuration missing --allow-root in inetd.conf\n");
255 for (i = 0; i < root_allow_count; ++i)
256 if (strcmp (root_allow_vector[i], arg) == 0)
263 /* This global variable holds the global -d option. It is NULL if -d
264 was not used, which means that we must get the CVSroot information
265 from the CVSROOT environment variable or from a CVS/Root file. */
266 char *CVSroot_cmdline;
270 /* FIXME - Deglobalize this. */
271 cvsroot_t *current_parsed_root = NULL;
275 /* allocate and initialize a cvsroot_t
277 * We must initialize the strings to NULL so we know later what we should
280 * Some of the other zeroes remain meaningful as, "never set, use default",
288 /* gotta store it somewhere */
289 newroot = xmalloc(sizeof(cvsroot_t));
291 newroot->original = NULL;
292 newroot->method = null_method;
293 newroot->isremote = 0;
294 #ifdef CLIENT_SUPPORT
295 newroot->username = NULL;
296 newroot->password = NULL;
297 newroot->hostname = NULL;
299 newroot->directory = NULL;
300 newroot->proxy_hostname = NULL;
301 newroot->proxy_port = 0;
302 #endif /* CLIENT_SUPPORT */
309 /* Dispose of a cvsroot_t and its component parts */
311 free_cvsroot_t (root)
314 if (root->original != NULL)
315 free (root->original);
316 if (root->directory != NULL)
317 free (root->directory);
318 #ifdef CLIENT_SUPPORT
319 if (root->username != NULL)
320 free (root->username);
321 if (root->password != NULL)
323 /* I like to be paranoid */
324 memset (root->password, 0, strlen (root->password));
325 free (root->password);
327 if (root->hostname != NULL)
328 free (root->hostname);
329 if (root->proxy_hostname != NULL)
330 free (root->proxy_hostname);
331 #endif /* CLIENT_SUPPORT */
338 * Parse a CVSROOT string to allocate and return a new cvsroot_t structure.
339 * Valid specifications are:
341 * :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path
342 * [:(ext|server):][[user]@]host[:]/path
347 * root_in C String containing the CVSROOT to be parsed.
350 * A pointer to a newly allocated cvsroot_t structure upon success and
351 * NULL upon failure. The caller is responsible for disposing of
352 * new structures with a call to free_cvsroot_t().
355 * This would have been a lot easier to write in Perl.
361 parse_cvsroot (root_in)
364 cvsroot_t *newroot; /* the new root to be returned */
365 char *cvsroot_save; /* what we allocated so we can dispose
366 * it when finished */
367 char *firstslash; /* save where the path spec starts
369 * [[user][:password]@]host[:[port]]
371 char *cvsroot_copy, *p, *q; /* temporary pointers for parsing */
372 #ifdef CLIENT_SUPPORT
373 int check_hostname, no_port, no_password;
374 #endif /* CLIENT_SUPPORT */
378 /* allocate some space */
379 newroot = new_cvsroot_t();
381 /* save the original string */
382 newroot->original = xstrdup (root_in);
384 /* and another copy we can munge while parsing */
385 cvsroot_save = cvsroot_copy = xstrdup (root_in);
387 if (*cvsroot_copy == ':')
389 char *method = ++cvsroot_copy;
391 /* Access method specified, as in
392 * "cvs -d :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path",
393 * "cvs -d [:(ext|server):][[user]@]host[:]/path",
394 * "cvs -d :local:e:\path",
395 * "cvs -d :fork:/path".
396 * We need to get past that part of CVSroot before parsing the
400 if (! (p = strchr (method, ':')))
402 error (0, 0, "No closing `:' on method in CVSROOT.");
408 #ifdef CLIENT_SUPPORT
409 /* Look for method options, for instance, proxy, proxyport.
410 * We don't handle these, but we like to try and warn the user that
411 * they are being ignored.
413 if ((p = strchr (method, ';')) != NULL)
419 "WARNING: Ignoring method options found in CVSROOT: `%s'.",
422 "Use CVS version 1.12.7 or later to handle method options.");
425 #endif /* CLIENT_SUPPORT */
427 /* Now we have an access method -- see if it's valid. */
429 if (strcmp (method, "local") == 0)
430 newroot->method = local_method;
431 else if (strcmp (method, "pserver") == 0)
432 newroot->method = pserver_method;
433 else if (strcmp (method, "kserver") == 0)
434 newroot->method = kserver_method;
435 else if (strcmp (method, "gserver") == 0)
436 newroot->method = gserver_method;
437 else if (strcmp (method, "server") == 0)
438 newroot->method = server_method;
439 else if (strcmp (method, "ext") == 0)
440 newroot->method = ext_method;
441 else if (strcmp (method, "fork") == 0)
442 newroot->method = fork_method;
445 error (0, 0, "Unknown method (`%s') in CVSROOT.", method);
451 /* If the method isn't specified, assume EXT_METHOD if the string looks
452 like a relative path and LOCAL_METHOD otherwise. */
454 newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
459 newroot->isremote = (newroot->method != local_method);
461 if ((newroot->method != local_method)
462 && (newroot->method != fork_method))
464 /* split the string into [[user][:password]@]host[:[port]] & /path
466 * this will allow some characters such as '@' & ':' to remain unquoted
467 * in the path portion of the spec
469 if ((p = strchr (cvsroot_copy, '/')) == NULL)
471 error (0, 0, "CVSROOT requires a path spec:");
473 ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
474 error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
477 firstslash = p; /* == NULL if '/' not in string */
480 /* Don't parse username, password, hostname, or port without client
483 #ifdef CLIENT_SUPPORT
484 /* Check to see if there is a username[:password] in the string. */
485 if ((p = strchr (cvsroot_copy, '@')) != NULL)
488 /* check for a password */
489 if ((q = strchr (cvsroot_copy, ':')) != NULL)
492 newroot->password = xstrdup (++q);
493 /* Don't check for *newroot->password == '\0' since
494 * a user could conceivably wish to specify a blank password
496 * (newroot->password == NULL means to use the
497 * password from .cvspass)
501 /* copy the username */
502 if (*cvsroot_copy != '\0')
503 /* a blank username is impossible, so leave it NULL in that
504 * case so we know to use the default username
506 newroot->username = xstrdup (cvsroot_copy);
511 /* now deal with host[:[port]] */
514 if ((p = strchr (cvsroot_copy, ':')) != NULL)
526 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
529 "Perhaps you entered a relative pathname?");
533 if ((newroot->port = atoi (p)) <= 0)
536 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
538 error (0, 0, "Perhaps you entered a relative pathname?");
545 if (*cvsroot_copy != '\0')
546 /* blank hostnames are invalid, but for now leave the field NULL
547 * and catch the error during the sanity checks later
549 newroot->hostname = xstrdup (cvsroot_copy);
551 /* restore the '/' */
552 cvsroot_copy = firstslash;
554 #endif /* CLIENT_SUPPORT */
558 * Parse the path for all methods.
560 /* Here & local_cvsroot() should be the only places this needs to be
561 * called on a CVSROOT now. cvsroot->original is saved for error messages
562 * and, otherwise, we want no trailing slashes.
564 Sanitize_Repository_Name( cvsroot_copy );
565 newroot->directory = xstrdup(cvsroot_copy);
568 * Do various sanity checks.
571 #if ! defined (CLIENT_SUPPORT) && ! defined (DEBUG)
572 if (newroot->method != local_method)
574 error (0, 0, "CVSROOT is set for a remote access method but your");
575 error (0, 0, "CVS executable doesn't support it.");
580 #if ! defined (SERVER_SUPPORT) && ! defined (DEBUG)
581 if (newroot->method == fork_method)
583 error (0, 0, "CVSROOT is set to use the :fork: access method but your");
584 error (0, 0, "CVS executable doesn't support it.");
589 #ifdef CLIENT_SUPPORT
590 if (newroot->username && ! newroot->hostname)
592 error (0, 0, "Missing hostname in CVSROOT.");
599 #endif /* CLIENT_SUPPORT */
600 switch (newroot->method)
603 #ifdef CLIENT_SUPPORT
604 if (newroot->username || newroot->hostname)
606 error (0, 0, "Can't specify hostname and username in CVSROOT");
607 error (0, 0, "when using local access method.");
611 /* no_password already set */
612 #endif /* CLIENT_SUPPORT */
613 /* cvs.texinfo has always told people that CVSROOT must be an
614 absolute pathname. Furthermore, attempts to use a relative
615 pathname produced various errors (I couldn't get it to work),
616 so there would seem to be little risk in making this a fatal
618 if (!isabsolute (newroot->directory))
620 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
622 error (0, 0, "when using local access method.");
626 #ifdef CLIENT_SUPPORT
628 /* We want :fork: to behave the same as other remote access
629 methods. Therefore, don't check to see that the repository
630 name is absolute -- let the server do it. */
631 if (newroot->username || newroot->hostname)
633 error (0, 0, "Can't specify hostname and username in CVSROOT");
634 error (0, 0, "when using fork access method.");
637 newroot->hostname = xstrdup("server"); /* for error messages */
638 if (!isabsolute (newroot->directory))
640 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
642 error (0, 0, "when using fork access method.");
646 /* no_password already set */
649 # ifndef HAVE_KERBEROS
650 error (0, 0, "CVSROOT is set for a kerberos access method but your");
651 error (0, 0, "CVS executable doesn't support it.");
655 /* no_password already set */
660 error (0, 0, "CVSROOT is set for a GSSAPI access method but your");
661 error (0, 0, "CVS executable doesn't support it.");
665 /* no_password already set */
671 /* no_password already set */
678 #endif /* CLIENT_SUPPORT */
680 error (1, 0, "Invalid method found in parse_cvsroot");
683 #ifdef CLIENT_SUPPORT
684 if (no_password && newroot->password)
686 error (0, 0, "CVSROOT password specification is only valid for");
687 error (0, 0, "pserver connection method.");
691 if (check_hostname && !newroot->hostname)
693 error (0, 0, "Didn't specify hostname in CVSROOT.");
697 if (no_port && newroot->port)
699 error (0, 0, "CVSROOT port specification is only valid for gserver, kserver,");
700 error (0, 0, "and pserver connection methods.");
703 #endif /* CLIENT_SUPPORT */
705 if (*newroot->directory == '\0')
707 error (0, 0, "Missing directory in CVSROOT.");
711 /* Hooray! We finally parsed it! */
717 free_cvsroot_t (newroot);
723 #ifdef AUTH_CLIENT_SUPPORT
724 /* Use root->username, root->hostname, root->port, and root->directory
725 * to create a normalized CVSROOT fit for the .cvspass file
727 * username defaults to the result of getcaller()
728 * port defaults to the result of get_cvs_port_number()
730 * FIXME - we could cache the canonicalized version of a root inside the
731 * cvsroot_t, but we'd have to un'const the input here and stop expecting the
732 * caller to be responsible for our return value
735 normalize_cvsroot (root)
736 const cvsroot_t *root;
738 char *cvsroot_canonical;
739 char *p, *hostname, *username;
742 assert (root && root->hostname && root->directory);
744 /* get the appropriate port string */
745 sprintf (port_s, "%d", get_cvs_port_number (root));
747 /* use a lower case hostname since we know hostnames are case insensitive */
748 /* Some logic says we should be tacking our domain name on too if it isn't
749 * there already, but for now this works. Reverse->Forward lookups are
750 * almost certainly too much since that would make CVS immune to some of
751 * the DNS trickery that makes life easier for sysadmins when they want to
752 * move a repository or the like
754 p = hostname = xstrdup(root->hostname);
761 /* get the username string */
762 username = root->username ? root->username : getcaller();
763 cvsroot_canonical = xmalloc ( strlen(username)
764 + strlen(hostname) + strlen(port_s)
765 + strlen(root->directory) + 12);
766 sprintf (cvsroot_canonical, ":pserver:%s@%s:%s%s",
767 username, hostname, port_s, root->directory);
770 return cvsroot_canonical;
772 #endif /* AUTH_CLIENT_SUPPORT */
776 /* allocate and return a cvsroot_t structure set up as if we're using the local
782 cvsroot_t *newroot = new_cvsroot_t();
784 newroot->original = xstrdup(dir);
785 newroot->method = local_method;
786 newroot->directory = xstrdup(dir);
787 /* Here and parse_cvsroot() should be the only places this needs to be
788 * called on a CVSROOT now. cvsroot->original is saved for error messages
789 * and, otherwise, we want no trailing slashes.
791 Sanitize_Repository_Name( newroot->directory );
798 /* This is for testing the parsing function. Use
800 gcc -I. -I.. -I../lib -DDEBUG root.c -o root
806 char *program_name = "testing";
807 char *cvs_cmd_name = "parse_cvsroot"; /* XXX is this used??? */
809 /* Toy versions of various functions when debugging under unix. Yes,
810 these make various bad assumptions, but they're pretty easy to
811 debug when something goes wrong. */
814 error_exit PROTO ((void))
823 return (dir && (*dir == '/'));
831 program_name = argv[0];
835 fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
839 if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
841 fprintf (stderr, "%s: Parsing failed.\n", program_name);
844 printf ("CVSroot: %s\n", argv[1]);
845 printf ("current_parsed_root->method: %s\n", method_names[current_parsed_root->method]);
846 printf ("current_parsed_root->username: %s\n",
847 current_parsed_root->username ? current_parsed_root->username : "NULL");
848 printf ("current_parsed_root->hostname: %s\n",
849 current_parsed_root->hostname ? current_parsed_root->hostname : "NULL");
850 printf ("current_parsed_root->directory: %s\n", current_parsed_root->directory);