xref: /openbsd-src/gnu/usr.bin/cvs/src/root.c (revision f9bbbf4534171e957bb7652e706dd78782fbffa2)
11e72d8d2Sderaadt /*
21e72d8d2Sderaadt  * Copyright (c) 1992, Mark D. Baushke
31e72d8d2Sderaadt  *
41e72d8d2Sderaadt  * You may distribute under the terms of the GNU General Public License as
52286d8edStholo  * specified in the README file that comes with the CVS source distribution.
61e72d8d2Sderaadt  *
71e72d8d2Sderaadt  * Name of Root
81e72d8d2Sderaadt  *
91e72d8d2Sderaadt  * Determine the path to the CVSROOT and set "Root" accordingly.
101e72d8d2Sderaadt  */
111e72d8d2Sderaadt 
121e72d8d2Sderaadt #include "cvs.h"
13780d15dfStholo #include "getline.h"
141e72d8d2Sderaadt 
1579822b2aStholo /* Printable names for things in the current_parsed_root->method enum variable.
1650bf276cStholo    Watch out if the enum is changed in cvs.h! */
1750bf276cStholo 
1850bf276cStholo char *method_names[] = {
1979822b2aStholo     "undefined", "local", "server (ssh)", "pserver", "kserver", "gserver", "ext", "fork"
2050bf276cStholo };
2150bf276cStholo 
2250bf276cStholo #ifndef DEBUG
2350bf276cStholo 
241e72d8d2Sderaadt char *
Name_Root(dir,update_dir)251e72d8d2Sderaadt Name_Root (dir, update_dir)
261e72d8d2Sderaadt     char *dir;
271e72d8d2Sderaadt     char *update_dir;
281e72d8d2Sderaadt {
291e72d8d2Sderaadt     FILE *fpin;
301e72d8d2Sderaadt     char *ret, *xupdate_dir;
31780d15dfStholo     char *root = NULL;
32780d15dfStholo     size_t root_allocated = 0;
33780d15dfStholo     char *tmp;
34780d15dfStholo     char *cvsadm;
351e72d8d2Sderaadt     char *cp;
361e72d8d2Sderaadt 
371e72d8d2Sderaadt     if (update_dir && *update_dir)
381e72d8d2Sderaadt 	xupdate_dir = update_dir;
391e72d8d2Sderaadt     else
401e72d8d2Sderaadt 	xupdate_dir = ".";
411e72d8d2Sderaadt 
421e72d8d2Sderaadt     if (dir != NULL)
431e72d8d2Sderaadt     {
44780d15dfStholo 	cvsadm = xmalloc (strlen (dir) + sizeof (CVSADM) + 10);
451e72d8d2Sderaadt 	(void) sprintf (cvsadm, "%s/%s", dir, CVSADM);
46780d15dfStholo 	tmp = xmalloc (strlen (dir) + sizeof (CVSADM_ROOT) + 10);
471e72d8d2Sderaadt 	(void) sprintf (tmp, "%s/%s", dir, CVSADM_ROOT);
481e72d8d2Sderaadt     }
491e72d8d2Sderaadt     else
501e72d8d2Sderaadt     {
51780d15dfStholo 	cvsadm = xstrdup (CVSADM);
52780d15dfStholo 	tmp = xstrdup (CVSADM_ROOT);
531e72d8d2Sderaadt     }
541e72d8d2Sderaadt 
551e72d8d2Sderaadt     /*
561e72d8d2Sderaadt      * Do not bother looking for a readable file if there is no cvsadm
571e72d8d2Sderaadt      * directory present.
581e72d8d2Sderaadt      *
5913571821Stholo      * It is possible that not all repositories will have a CVS/Root
601e72d8d2Sderaadt      * file. This is ok, but the user will need to specify -d
611e72d8d2Sderaadt      * /path/name or have the environment variable CVSROOT set in
6250bf276cStholo      * order to continue.  */
631e72d8d2Sderaadt     if ((!isdir (cvsadm)) || (!isreadable (tmp)))
64780d15dfStholo     {
65780d15dfStholo 	ret = NULL;
66780d15dfStholo 	goto out;
67780d15dfStholo     }
681e72d8d2Sderaadt 
691e72d8d2Sderaadt     /*
701e72d8d2Sderaadt      * The assumption here is that the CVS Root is always contained in the
711e72d8d2Sderaadt      * first line of the "Root" file.
721e72d8d2Sderaadt      */
731e72d8d2Sderaadt     fpin = open_file (tmp, "r");
741e72d8d2Sderaadt 
75*f9bbbf45Sfgsch     if (get_line (&root, &root_allocated, fpin) < 0)
761e72d8d2Sderaadt     {
77780d15dfStholo 	/* FIXME: should be checking for end of file separately; errno
78780d15dfStholo 	   is not set in that case.  */
791e72d8d2Sderaadt 	error (0, 0, "in directory %s:", xupdate_dir);
801e72d8d2Sderaadt 	error (0, errno, "cannot read %s", CVSADM_ROOT);
811e72d8d2Sderaadt 	error (0, 0, "please correct this problem");
82780d15dfStholo 	ret = NULL;
83780d15dfStholo 	goto out;
841e72d8d2Sderaadt     }
851e72d8d2Sderaadt     (void) fclose (fpin);
861e72d8d2Sderaadt     if ((cp = strrchr (root, '\n')) != NULL)
871e72d8d2Sderaadt 	*cp = '\0';			/* strip the newline */
881e72d8d2Sderaadt 
891e72d8d2Sderaadt     /*
901e72d8d2Sderaadt      * root now contains a candidate for CVSroot. It must be an
91461cc63eStholo      * absolute pathname or specify a remote server.
921e72d8d2Sderaadt      */
931e72d8d2Sderaadt 
94461cc63eStholo     if (
951e72d8d2Sderaadt #ifdef CLIENT_SUPPORT
96461cc63eStholo 	(strchr (root, ':') == NULL) &&
97461cc63eStholo #endif
98461cc63eStholo     	! isabsolute (root))
991e72d8d2Sderaadt     {
1001e72d8d2Sderaadt 	error (0, 0, "in directory %s:", xupdate_dir);
1011e72d8d2Sderaadt 	error (0, 0,
1021e72d8d2Sderaadt 	       "ignoring %s because it does not contain an absolute pathname.",
1031e72d8d2Sderaadt 	       CVSADM_ROOT);
104780d15dfStholo 	ret = NULL;
105780d15dfStholo 	goto out;
1061e72d8d2Sderaadt     }
1071e72d8d2Sderaadt 
1081e72d8d2Sderaadt #ifdef CLIENT_SUPPORT
1091e72d8d2Sderaadt     if ((strchr (root, ':') == NULL) && !isdir (root))
11013571821Stholo #else /* ! CLIENT_SUPPORT */
1111e72d8d2Sderaadt     if (!isdir (root))
11213571821Stholo #endif /* CLIENT_SUPPORT */
1131e72d8d2Sderaadt     {
1141e72d8d2Sderaadt 	error (0, 0, "in directory %s:", xupdate_dir);
1151e72d8d2Sderaadt 	error (0, 0,
1161e72d8d2Sderaadt 	       "ignoring %s because it specifies a non-existent repository %s",
1171e72d8d2Sderaadt 	       CVSADM_ROOT, root);
118780d15dfStholo 	ret = NULL;
119780d15dfStholo 	goto out;
1201e72d8d2Sderaadt     }
1211e72d8d2Sderaadt 
1221e72d8d2Sderaadt     /* allocate space to return and fill it in */
123461cc63eStholo     strip_trailing_slashes (root);
1241e72d8d2Sderaadt     ret = xstrdup (root);
125780d15dfStholo  out:
126780d15dfStholo     free (cvsadm);
127780d15dfStholo     free (tmp);
128780d15dfStholo     if (root != NULL)
129780d15dfStholo 	free (root);
1301e72d8d2Sderaadt     return (ret);
1311e72d8d2Sderaadt }
1321e72d8d2Sderaadt 
1331e72d8d2Sderaadt /*
1341e72d8d2Sderaadt  * Write the CVS/Root file so that the environment variable CVSROOT
1351e72d8d2Sderaadt  * and/or the -d option to cvs will be validated or not necessary for
1361e72d8d2Sderaadt  * future work.
1371e72d8d2Sderaadt  */
1381e72d8d2Sderaadt void
Create_Root(dir,rootdir)1391e72d8d2Sderaadt Create_Root (dir, rootdir)
1401e72d8d2Sderaadt     char *dir;
1411e72d8d2Sderaadt     char *rootdir;
1421e72d8d2Sderaadt {
1431e72d8d2Sderaadt     FILE *fout;
144780d15dfStholo     char *tmp;
1451e72d8d2Sderaadt 
14613571821Stholo     if (noexec)
14713571821Stholo 	return;
14813571821Stholo 
1491e72d8d2Sderaadt     /* record the current cvs root */
1501e72d8d2Sderaadt 
1511e72d8d2Sderaadt     if (rootdir != NULL)
1521e72d8d2Sderaadt     {
1531e72d8d2Sderaadt         if (dir != NULL)
154780d15dfStholo 	{
155780d15dfStholo 	    tmp = xmalloc (strlen (dir) + sizeof (CVSADM_ROOT) + 10);
1561e72d8d2Sderaadt 	    (void) sprintf (tmp, "%s/%s", dir, CVSADM_ROOT);
157780d15dfStholo 	}
1581e72d8d2Sderaadt         else
159780d15dfStholo 	    tmp = xstrdup (CVSADM_ROOT);
160780d15dfStholo 
1611e72d8d2Sderaadt         fout = open_file (tmp, "w+");
1621e72d8d2Sderaadt         if (fprintf (fout, "%s\n", rootdir) < 0)
1631e72d8d2Sderaadt 	    error (1, errno, "write to %s failed", tmp);
1641e72d8d2Sderaadt         if (fclose (fout) == EOF)
1651e72d8d2Sderaadt 	    error (1, errno, "cannot close %s", tmp);
166780d15dfStholo 	free (tmp);
1671e72d8d2Sderaadt     }
1681e72d8d2Sderaadt }
16950bf276cStholo 
17050bf276cStholo #endif /* ! DEBUG */
17150bf276cStholo 
17250bf276cStholo 
1732770ece5Stholo /* The root_allow_* stuff maintains a list of legal CVSROOT
1742770ece5Stholo    directories.  Then we can check against them when a remote user
1752770ece5Stholo    hands us a CVSROOT directory.  */
1762770ece5Stholo 
177bde78045Stholo static int root_allow_count;
1782770ece5Stholo static char **root_allow_vector;
179bde78045Stholo static int root_allow_size;
1802770ece5Stholo 
1812770ece5Stholo void
root_allow_add(arg)1822770ece5Stholo root_allow_add (arg)
1832770ece5Stholo     char *arg;
1842770ece5Stholo {
1852770ece5Stholo     char *p;
1862770ece5Stholo 
1872770ece5Stholo     if (root_allow_size <= root_allow_count)
1882770ece5Stholo     {
1892770ece5Stholo 	if (root_allow_size == 0)
1902770ece5Stholo 	{
1912770ece5Stholo 	    root_allow_size = 1;
1922770ece5Stholo 	    root_allow_vector =
1932770ece5Stholo 		(char **) malloc (root_allow_size * sizeof (char *));
1942770ece5Stholo 	}
1952770ece5Stholo 	else
1962770ece5Stholo 	{
1972770ece5Stholo 	    root_allow_size *= 2;
1982770ece5Stholo 	    root_allow_vector =
1992770ece5Stholo 		(char **) realloc (root_allow_vector,
2002770ece5Stholo 				   root_allow_size * sizeof (char *));
2012770ece5Stholo 	}
2022770ece5Stholo 
2032770ece5Stholo 	if (root_allow_vector == NULL)
2042770ece5Stholo 	{
2052770ece5Stholo 	no_memory:
2062770ece5Stholo 	    /* Strictly speaking, we're not supposed to output anything
2072770ece5Stholo 	       now.  But we're about to exit(), give it a try.  */
2082770ece5Stholo 	    printf ("E Fatal server error, aborting.\n\
2092770ece5Stholo error ENOMEM Virtual memory exhausted.\n");
2102770ece5Stholo 
2112770ece5Stholo 	    /* I'm doing this manually rather than via error_exit ()
2122770ece5Stholo 	       because I'm not sure whether we want to call server_cleanup.
2132770ece5Stholo 	       Needs more investigation....  */
2142770ece5Stholo 
2152770ece5Stholo #ifdef SYSTEM_CLEANUP
2162770ece5Stholo 	    /* Hook for OS-specific behavior, for example socket
2172770ece5Stholo 	       subsystems on NT and OS2 or dealing with windows
2182770ece5Stholo 	       and arguments on Mac.  */
2192770ece5Stholo 	    SYSTEM_CLEANUP ();
2202770ece5Stholo #endif
2212770ece5Stholo 
2222770ece5Stholo 	    exit (EXIT_FAILURE);
2232770ece5Stholo 	}
2242770ece5Stholo     }
2252770ece5Stholo     p = malloc (strlen (arg) + 1);
2262770ece5Stholo     if (p == NULL)
2272770ece5Stholo 	goto no_memory;
2282770ece5Stholo     strcpy (p, arg);
2292770ece5Stholo     root_allow_vector[root_allow_count++] = p;
2302770ece5Stholo }
2312770ece5Stholo 
2322770ece5Stholo void
root_allow_free()2332770ece5Stholo root_allow_free ()
2342770ece5Stholo {
2352770ece5Stholo     if (root_allow_vector != NULL)
236bde78045Stholo 	free_names (&root_allow_count, root_allow_vector);
2372770ece5Stholo     root_allow_size = 0;
2382770ece5Stholo }
2392770ece5Stholo 
2402770ece5Stholo int
root_allow_ok(arg)2412770ece5Stholo root_allow_ok (arg)
2422770ece5Stholo     char *arg;
2432770ece5Stholo {
244bde78045Stholo     int i;
2452286d8edStholo 
2462286d8edStholo     if (root_allow_count == 0)
2472286d8edStholo     {
2482286d8edStholo 	/* Probably someone upgraded from CVS before 1.9.10 to 1.9.10
2492286d8edStholo 	   or later without reading the documentation about
2502286d8edStholo 	   --allow-root.  Printing an error here doesn't disclose any
2512286d8edStholo 	   particularly useful information to an attacker because a
2522286d8edStholo 	   CVS server configured in this way won't let *anyone* in.  */
2532286d8edStholo 
2542286d8edStholo 	/* Note that we are called from a context where we can spit
2552286d8edStholo 	   back "error" rather than waiting for the next request which
2562286d8edStholo 	   expects responses.  */
2572286d8edStholo 	printf ("\
2582286d8edStholo error 0 Server configuration missing --allow-root in inetd.conf\n");
2592286d8edStholo 	error_exit ();
2602286d8edStholo     }
2612286d8edStholo 
2622770ece5Stholo     for (i = 0; i < root_allow_count; ++i)
2632770ece5Stholo 	if (strcmp (root_allow_vector[i], arg) == 0)
2642770ece5Stholo 	    return 1;
2652770ece5Stholo     return 0;
2662770ece5Stholo }
2672770ece5Stholo 
26879822b2aStholo 
26979822b2aStholo 
270c71bc7e2Stholo /* This global variable holds the global -d option.  It is NULL if -d
271c71bc7e2Stholo    was not used, which means that we must get the CVSroot information
272c71bc7e2Stholo    from the CVSROOT environment variable or from a CVS/Root file.  */
273c71bc7e2Stholo 
274c71bc7e2Stholo char *CVSroot_cmdline;
2752770ece5Stholo 
27679822b2aStholo 
27779822b2aStholo 
27850bf276cStholo /* Parse a CVSROOT variable into its constituent parts -- method,
27950bf276cStholo  * username, hostname, directory.  The prototypical CVSROOT variable
28050bf276cStholo  * looks like:
28150bf276cStholo  *
28250bf276cStholo  * :method:user@host:path
28350bf276cStholo  *
28450bf276cStholo  * Some methods may omit fields; local, for example, doesn't need user
28550bf276cStholo  * and host.
28650bf276cStholo  *
28779822b2aStholo  * Returns pointer to new cvsroot_t on success, NULL on failure. */
28850bf276cStholo 
28979822b2aStholo cvsroot_t *current_parsed_root = NULL;
29050bf276cStholo 
29179822b2aStholo 
29279822b2aStholo 
29379822b2aStholo /* allocate and initialize a cvsroot_t
29479822b2aStholo  *
29579822b2aStholo  * We must initialize the strings to NULL so we know later what we should
29679822b2aStholo  * free
29779822b2aStholo  *
29879822b2aStholo  * Some of the other zeroes remain meaningful as, "never set, use default",
29979822b2aStholo  * or the like
30079822b2aStholo  */
30179822b2aStholo static cvsroot_t *
new_cvsroot_t()30279822b2aStholo new_cvsroot_t ()
30350bf276cStholo {
30479822b2aStholo     cvsroot_t *newroot;
30550bf276cStholo 
30679822b2aStholo     /* gotta store it somewhere */
30779822b2aStholo     newroot = xmalloc(sizeof(cvsroot_t));
30879822b2aStholo 
30979822b2aStholo     newroot->original = NULL;
31079822b2aStholo     newroot->method = null_method;
31179822b2aStholo     newroot->username = NULL;
31279822b2aStholo     newroot->password = NULL;
31379822b2aStholo     newroot->hostname = NULL;
31479822b2aStholo     newroot->port = 0;
31579822b2aStholo     newroot->directory = NULL;
31679822b2aStholo #ifdef CLIENT_SUPPORT
31779822b2aStholo     newroot->isremote = 0;
31879822b2aStholo #endif /* CLIENT_SUPPORT */
31979822b2aStholo 
32079822b2aStholo     return newroot;
32150bf276cStholo }
32250bf276cStholo 
32350bf276cStholo 
32479822b2aStholo 
32579822b2aStholo /* Dispose of a cvsroot_t and its component parts */
32679822b2aStholo void
free_cvsroot_t(root)32779822b2aStholo free_cvsroot_t (root)
32879822b2aStholo     cvsroot_t *root;
32979822b2aStholo {
33079822b2aStholo     if (root->original != NULL)
33179822b2aStholo 	free (root->original);
33279822b2aStholo     if (root->username != NULL)
33379822b2aStholo 	free (root->username);
33479822b2aStholo     if (root->password != NULL)
33579822b2aStholo     {
33679822b2aStholo 	/* I like to be paranoid */
33779822b2aStholo 	memset (root->password, 0, strlen (root->password));
33879822b2aStholo 	free (root->password);
33979822b2aStholo     }
34079822b2aStholo     if (root->hostname != NULL)
34179822b2aStholo 	free (root->hostname);
34279822b2aStholo     if (root->directory != NULL)
34379822b2aStholo 	free (root->directory);
34479822b2aStholo     free (root);
34579822b2aStholo }
34679822b2aStholo 
34779822b2aStholo 
34879822b2aStholo 
34979822b2aStholo /*
35079822b2aStholo  * parse a CVSROOT string to allocate and return a new cvsroot_t structure
35179822b2aStholo  */
35279822b2aStholo cvsroot_t *
parse_cvsroot(root_in)35379822b2aStholo parse_cvsroot (root_in)
35479822b2aStholo     char *root_in;
35579822b2aStholo {
35679822b2aStholo     cvsroot_t *newroot;			/* the new root to be returned */
35779822b2aStholo     char *cvsroot_save;			/* what we allocated so we can dispose
35879822b2aStholo 					 * it when finished */
35979822b2aStholo     char *firstslash;			/* save where the path spec starts
36079822b2aStholo 					 * while we parse
36179822b2aStholo 					 * [[user][:password]@]host[:[port]]
36279822b2aStholo 					 */
36379822b2aStholo     char *cvsroot_copy, *p, *q;		/* temporary pointers for parsing */
36479822b2aStholo     char *new_hostname;
36579822b2aStholo     int check_hostname, no_port, no_password;
36679822b2aStholo 
36779822b2aStholo     /* allocate some space */
36879822b2aStholo     newroot = new_cvsroot_t();
36979822b2aStholo 
37079822b2aStholo     /* save the original string */
37179822b2aStholo     newroot->original = xstrdup (root_in);
37279822b2aStholo 
37379822b2aStholo     /* and another copy we can munge while parsing */
37479822b2aStholo     cvsroot_save = cvsroot_copy = xstrdup (root_in);
375bde78045Stholo 
376bde78045Stholo     if (*cvsroot_copy == ':')
37750bf276cStholo     {
37850bf276cStholo 	char *method = ++cvsroot_copy;
37950bf276cStholo 
38050bf276cStholo 	/* Access method specified, as in
38179822b2aStholo 	 * "cvs -d :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path",
38279822b2aStholo 	 * "cvs -d [:(ext|server):][[user]@]host[:]/path",
383c71bc7e2Stholo 	 * "cvs -d :local:e:\path",
384c71bc7e2Stholo 	 * "cvs -d :fork:/path".
38550bf276cStholo 	 * We need to get past that part of CVSroot before parsing the
38650bf276cStholo 	 * rest of it.
38750bf276cStholo 	 */
38850bf276cStholo 
38950bf276cStholo 	if (! (p = strchr (method, ':')))
39050bf276cStholo 	{
39179822b2aStholo 	    error (0, 0, "bad CVSroot: %s", root_in);
392bde78045Stholo 	    free (cvsroot_save);
39379822b2aStholo 	    goto error_exit;
39450bf276cStholo 	}
39550bf276cStholo 	*p = '\0';
39650bf276cStholo 	cvsroot_copy = ++p;
39750bf276cStholo 
39850bf276cStholo 	/* Now we have an access method -- see if it's valid. */
39950bf276cStholo 
40050bf276cStholo 	if (strcmp (method, "local") == 0)
40179822b2aStholo 	    newroot->method = local_method;
40250bf276cStholo 	else if (strcmp (method, "pserver") == 0)
40379822b2aStholo 	    newroot->method = pserver_method;
40450bf276cStholo 	else if (strcmp (method, "kserver") == 0)
40579822b2aStholo 	    newroot->method = kserver_method;
4062286d8edStholo 	else if (strcmp (method, "gserver") == 0)
40779822b2aStholo 	    newroot->method = gserver_method;
40850bf276cStholo 	else if (strcmp (method, "server") == 0)
40979822b2aStholo 	    newroot->method = server_method;
41050bf276cStholo 	else if (strcmp (method, "ext") == 0)
41179822b2aStholo 	    newroot->method = ext_method;
412c71bc7e2Stholo 	else if (strcmp (method, "fork") == 0)
41379822b2aStholo 	    newroot->method = fork_method;
41450bf276cStholo 	else
41550bf276cStholo 	{
41679822b2aStholo 	    error (0, 0, "unknown method in CVSroot: %s", root_in);
417bde78045Stholo 	    free (cvsroot_save);
41879822b2aStholo 	    goto error_exit;
41950bf276cStholo 	}
42050bf276cStholo     }
42150bf276cStholo     else
42250bf276cStholo     {
42350bf276cStholo 	/* If the method isn't specified, assume
42450bf276cStholo 	   SERVER_METHOD/EXT_METHOD if the string contains a colon or
42550bf276cStholo 	   LOCAL_METHOD otherwise.  */
42650bf276cStholo 
42779822b2aStholo 	newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
42879822b2aStholo /*#ifdef RSH_NOT_TRANSPARENT
42950bf276cStholo 			  ? server_method
43079822b2aStholo #else*/
43150bf276cStholo 			  ? ext_method
43279822b2aStholo /*#endif*/
43350bf276cStholo 			  : local_method);
43450bf276cStholo     }
43550bf276cStholo 
43679822b2aStholo #ifdef CLIENT_SUPPORT
43779822b2aStholo     newroot->isremote = (newroot->method != local_method);
43879822b2aStholo #endif /* CLIENT_SUPPORT */
43950bf276cStholo 
44050bf276cStholo 
44179822b2aStholo     if ((newroot->method != local_method)
44279822b2aStholo 	&& (newroot->method != fork_method))
44350bf276cStholo     {
44479822b2aStholo 	/* split the string into [[user][:password]@]host[:[port]] & /path
44579822b2aStholo 	 *
44679822b2aStholo 	 * this will allow some characters such as '@' & ':' to remain unquoted
44779822b2aStholo 	 * in the path portion of the spec
44879822b2aStholo 	 */
44979822b2aStholo 	if ((p = strchr (cvsroot_copy, '/')) == NULL)
45079822b2aStholo 	{
45179822b2aStholo 	    error (0, 0, "CVSROOT (\"%s\")", root_in);
45279822b2aStholo 	    error (0, 0, "requires a path spec");
45379822b2aStholo 	    error (0, 0, ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
45479822b2aStholo 	    error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
45579822b2aStholo 	    free (cvsroot_save);
45679822b2aStholo 	    goto error_exit;
45779822b2aStholo 	}
45879822b2aStholo 	firstslash = p;		/* == NULL if '/' not in string */
45979822b2aStholo 	*p = '\0';
46050bf276cStholo 
46179822b2aStholo 	/* Check to see if there is a username[:password] in the string. */
462bde78045Stholo 	if ((p = strchr (cvsroot_copy, '@')) != NULL)
46350bf276cStholo 	{
46450bf276cStholo 	    *p = '\0';
46579822b2aStholo 	    /* check for a password */
46679822b2aStholo 	    if ((q = strchr (cvsroot_copy, ':')) != NULL)
46779822b2aStholo 	    {
46879822b2aStholo 		*q = '\0';
46979822b2aStholo 		newroot->password = xstrdup (++q);
47079822b2aStholo 		/* Don't check for *newroot->password == '\0' since
47179822b2aStholo 		 * a user could conceivably wish to specify a blank password
47279822b2aStholo 		 * (newroot->password == NULL means to use the
47379822b2aStholo 		 * password from .cvspass)
47479822b2aStholo 		 */
47550bf276cStholo 	    }
47650bf276cStholo 
47779822b2aStholo 	    /* copy the username */
47879822b2aStholo 	    if (*cvsroot_copy != '\0')
47979822b2aStholo 		/* a blank username is impossible, so leave it NULL in that
48079822b2aStholo 		 * case so we know to use the default username
48179822b2aStholo 		 */
48279822b2aStholo 		newroot->username = xstrdup (cvsroot_copy);
48379822b2aStholo 
48479822b2aStholo 	    cvsroot_copy = ++p;
48579822b2aStholo 	}
48679822b2aStholo 
48779822b2aStholo 	new_hostname = NULL;
488b7b9f807Sderaadt 	if (*cvsroot_copy == '[')
489b7b9f807Sderaadt 	{
490b7b9f807Sderaadt 		p = strchr(cvsroot_copy, ']');
491b7b9f807Sderaadt 		if (p != NULL)
492b7b9f807Sderaadt 		{
493b7b9f807Sderaadt 			*p = '\0';
49479822b2aStholo 			new_hostname = xstrdup (cvsroot_copy+1);
495b7b9f807Sderaadt 			*p++ = ']';
496b7b9f807Sderaadt 			if (*p == ':')
49779822b2aStholo 				cvsroot_copy = p;
498b7b9f807Sderaadt 		}
499b7b9f807Sderaadt 	}
50079822b2aStholo 
50179822b2aStholo 	/* now deal with host[:[port]] */
50279822b2aStholo 
50379822b2aStholo 	/* the port */
50479822b2aStholo 	if ((p = strchr (cvsroot_copy, ':')) != NULL)
50550bf276cStholo 	{
50679822b2aStholo 	    *p++ = '\0';
50779822b2aStholo 	    if (strlen(p))
50879822b2aStholo 	    {
50979822b2aStholo 		q = p;
51079822b2aStholo 		if (*q == '-') q++;
51179822b2aStholo 		while (*q)
51279822b2aStholo 		{
51379822b2aStholo 		    if (!isdigit(*q++))
51479822b2aStholo 		    {
51579822b2aStholo 			error(0, 0, "CVSROOT (\"%s\")", root_in);
51679822b2aStholo 			error(0, 0, "may only specify a positive, non-zero, integer port (not \"%s\").",
51779822b2aStholo 				p);
51879822b2aStholo 			error(0, 0, "perhaps you entered a relative pathname?");
51979822b2aStholo 			free (cvsroot_save);
52079822b2aStholo 			if (new_hostname != NULL)
52179822b2aStholo 			    free (new_hostname);
52279822b2aStholo 			goto error_exit;
52379822b2aStholo 		    }
52479822b2aStholo 		}
52579822b2aStholo 		if ((newroot->port = atoi (p)) <= 0)
52679822b2aStholo 		{
52779822b2aStholo 		    error (0, 0, "CVSROOT (\"%s\")", root_in);
52879822b2aStholo 		    error(0, 0, "may only specify a positive, non-zero, integer port (not \"%s\").",
52979822b2aStholo 			    p);
53079822b2aStholo 		    error(0, 0, "perhaps you entered a relative pathname?");
53179822b2aStholo 		    if (new_hostname != NULL)
53279822b2aStholo 			free (new_hostname);
53379822b2aStholo 		    free (cvsroot_save);
53479822b2aStholo 		    goto error_exit;
53579822b2aStholo 		}
53650bf276cStholo 	    }
53750bf276cStholo 	}
53850bf276cStholo 
53979822b2aStholo 	/* copy host */
54079822b2aStholo 	if (new_hostname != NULL)
54179822b2aStholo 	    newroot->hostname = new_hostname;
54279822b2aStholo 	else if (*cvsroot_copy != '\0')
54379822b2aStholo 	    /* blank hostnames are invalid, but for now leave the field NULL
54479822b2aStholo 	     * and catch the error during the sanity checks later
54579822b2aStholo 	     */
54679822b2aStholo 	    newroot->hostname = xstrdup (cvsroot_copy);
54779822b2aStholo 
54879822b2aStholo 	/* restore the '/' */
54979822b2aStholo 	cvsroot_copy = firstslash;
55079822b2aStholo 	*cvsroot_copy = '/';
55179822b2aStholo     }
55279822b2aStholo 
55379822b2aStholo     /* parse the path for all methods */
55479822b2aStholo     newroot->directory = xstrdup(cvsroot_copy);
555bde78045Stholo     free (cvsroot_save);
55650bf276cStholo 
55779822b2aStholo     /*
55879822b2aStholo      * Do various sanity checks.
55979822b2aStholo      */
56079822b2aStholo 
56150bf276cStholo #if ! defined (CLIENT_SUPPORT) && ! defined (DEBUG)
56279822b2aStholo     if (newroot->method != local_method)
56350bf276cStholo     {
56479822b2aStholo 	error (0, 0, "CVSROOT \"%s\"", root_in);
56579822b2aStholo 	error (0, 0, "is set for a remote access method but your");
56679822b2aStholo 	error (0, 0, "CVS executable doesn't support it");
56779822b2aStholo 	goto error_exit;
56850bf276cStholo     }
56950bf276cStholo #endif
57050bf276cStholo 
57179822b2aStholo #if ! defined (SERVER_SUPPORT) && ! defined (DEBUG)
57279822b2aStholo     if (newroot->method == fork_method)
57350bf276cStholo     {
57479822b2aStholo 	error (0, 0, "CVSROOT \"%s\"", root_in);
57579822b2aStholo 	error (0, 0, "is set to use the :fork: access method but your");
57679822b2aStholo 	error (0, 0, "CVS executable doesn't support it");
57779822b2aStholo 	goto error_exit;
57879822b2aStholo      }
57979822b2aStholo #endif
58079822b2aStholo 
58179822b2aStholo     if (newroot->username && ! newroot->hostname)
58279822b2aStholo     {
58379822b2aStholo 	error (0, 0, "missing hostname in CVSROOT: \"%s\"", root_in);
58479822b2aStholo 	goto error_exit;
58550bf276cStholo     }
58650bf276cStholo 
5872286d8edStholo     check_hostname = 0;
58879822b2aStholo     no_password = 0;
58979822b2aStholo     no_port = 0;
59079822b2aStholo     switch (newroot->method)
59150bf276cStholo     {
59250bf276cStholo     case local_method:
59379822b2aStholo 	if (newroot->username || newroot->hostname)
59450bf276cStholo 	{
59550bf276cStholo 	    error (0, 0, "can't specify hostname and username in CVSROOT");
59679822b2aStholo 	    error (0, 0, "(\"%s\")", root_in);
597bde78045Stholo 	    error (0, 0, "when using local access method");
59879822b2aStholo 	    goto error_exit;
59950bf276cStholo 	}
600780d15dfStholo 	/* cvs.texinfo has always told people that CVSROOT must be an
601780d15dfStholo 	   absolute pathname.  Furthermore, attempts to use a relative
602780d15dfStholo 	   pathname produced various errors (I couldn't get it to work),
603780d15dfStholo 	   so there would seem to be little risk in making this a fatal
604780d15dfStholo 	   error.  */
60579822b2aStholo 	if (!isabsolute (newroot->directory))
60679822b2aStholo 	{
60779822b2aStholo 	    error (0, 0, "CVSROOT \"%s\" must be an absolute pathname",
60879822b2aStholo 		   newroot->directory);
60979822b2aStholo 	    goto error_exit;
61079822b2aStholo 	}
61179822b2aStholo 	no_port = 1;
61279822b2aStholo 	no_password = 1;
61350bf276cStholo 	break;
614bde78045Stholo     case fork_method:
615bde78045Stholo 	/* We want :fork: to behave the same as other remote access
616bde78045Stholo            methods.  Therefore, don't check to see that the repository
617bde78045Stholo            name is absolute -- let the server do it.  */
61879822b2aStholo 	if (newroot->username || newroot->hostname)
619bde78045Stholo 	{
620bde78045Stholo 	    error (0, 0, "can't specify hostname and username in CVSROOT");
62179822b2aStholo 	    error (0, 0, "(\"%s\")", root_in);
622bde78045Stholo 	    error (0, 0, "when using fork access method");
62379822b2aStholo 	    goto error_exit;
624bde78045Stholo 	}
62579822b2aStholo 	if (!isabsolute (newroot->directory))
62679822b2aStholo 	{
62779822b2aStholo 	    error (0, 0, "CVSROOT \"%s\" must be an absolute pathname",
62879822b2aStholo 		   newroot->directory);
62979822b2aStholo 	    goto error_exit;
63079822b2aStholo 	}
63179822b2aStholo 	no_port = 1;
63279822b2aStholo 	no_password = 1;
633bde78045Stholo 	break;
63450bf276cStholo     case kserver_method:
63550bf276cStholo #ifndef HAVE_KERBEROS
63679822b2aStholo 	error (0, 0, "CVSROOT \"%s\"", root_in);
63779822b2aStholo        	error (0, 0, "is set for a kerberos access method but your");
63879822b2aStholo 	error (0, 0, "CVS executable doesn't support it");
63979822b2aStholo 	goto error_exit;
640c71bc7e2Stholo #else
6412286d8edStholo 	check_hostname = 1;
6422286d8edStholo 	break;
643c71bc7e2Stholo #endif
6442286d8edStholo     case gserver_method:
6452286d8edStholo #ifndef HAVE_GSSAPI
64679822b2aStholo 	error (0, 0, "CVSROOT \"%s\"", root_in);
64779822b2aStholo 	error (0, 0, "is set for a GSSAPI access method but your");
64879822b2aStholo 	error (0, 0, "CVS executable doesn't support it");
64979822b2aStholo 	goto error_exit;
650c71bc7e2Stholo #else
6512286d8edStholo 	check_hostname = 1;
6522286d8edStholo 	break;
653c71bc7e2Stholo #endif
65450bf276cStholo     case server_method:
65550bf276cStholo     case ext_method:
65679822b2aStholo 	no_port = 1;
65779822b2aStholo 	no_password = 1;
65879822b2aStholo 	check_hostname = 1;
65979822b2aStholo 	break;
66050bf276cStholo     case pserver_method:
6612286d8edStholo 	check_hostname = 1;
6622286d8edStholo 	break;
6632286d8edStholo     }
6642286d8edStholo 
66579822b2aStholo     if (no_password && newroot->password)
6662286d8edStholo     {
66779822b2aStholo 	error (0, 0, "CVSROOT password specification is only valid for");
66879822b2aStholo 	error (0, 0, "pserver connection method.");
66979822b2aStholo 	goto error_exit;
67050bf276cStholo     }
67150bf276cStholo 
67279822b2aStholo     if (check_hostname && !newroot->hostname)
67350bf276cStholo     {
67479822b2aStholo 	error (0, 0, "didn't specify hostname in CVSROOT: %s", root_in);
67579822b2aStholo 	goto error_exit;
67679822b2aStholo     }
67779822b2aStholo 
67879822b2aStholo     if (no_port && newroot->port)
67979822b2aStholo 	{
68079822b2aStholo 	    error (0, 0, "CVSROOT port specification is only valid for gserver, kserver,");
68179822b2aStholo 	    error (0, 0, "and pserver connection methods.");
68279822b2aStholo 	    goto error_exit;
68379822b2aStholo 	}
68479822b2aStholo 
68579822b2aStholo     if (*newroot->directory == '\0')
68679822b2aStholo     {
68779822b2aStholo 	error (0, 0, "missing directory in CVSROOT: %s", root_in);
68879822b2aStholo 	goto error_exit;
68950bf276cStholo     }
69050bf276cStholo 
69150bf276cStholo     /* Hooray!  We finally parsed it! */
69279822b2aStholo     return newroot;
69379822b2aStholo 
69479822b2aStholo error_exit:
69579822b2aStholo     free_cvsroot_t (newroot);
69679822b2aStholo     return NULL;
69750bf276cStholo }
69850bf276cStholo 
69950bf276cStholo 
70050bf276cStholo 
70179822b2aStholo #ifdef AUTH_CLIENT_SUPPORT
70279822b2aStholo /* Use root->username, root->hostname, root->port, and root->directory
70379822b2aStholo  * to create a normalized CVSROOT fit for the .cvspass file
70479822b2aStholo  *
70579822b2aStholo  * username defaults to the result of getcaller()
70679822b2aStholo  * port defaults to the result of get_cvs_port_number()
70779822b2aStholo  *
70879822b2aStholo  * FIXME - we could cache the canonicalized version of a root inside the
70979822b2aStholo  * cvsroot_t, but we'd have to un'const the input here and stop expecting the
71079822b2aStholo  * caller to be responsible for our return value
71179822b2aStholo  */
71279822b2aStholo char *
normalize_cvsroot(root)71379822b2aStholo normalize_cvsroot (root)
71479822b2aStholo     const cvsroot_t *root;
71579822b2aStholo {
71679822b2aStholo     char *cvsroot_canonical;
71779822b2aStholo     char *p, *hostname, *username;
71879822b2aStholo     char port_s[64];
71979822b2aStholo 
72079822b2aStholo     /* get the appropriate port string */
72179822b2aStholo     sprintf (port_s, "%d", get_cvs_port_number (root));
72279822b2aStholo 
72379822b2aStholo     /* use a lower case hostname since we know hostnames are case insensitive */
72479822b2aStholo     /* Some logic says we should be tacking our domain name on too if it isn't
72579822b2aStholo      * there already, but for now this works.  Reverse->Forward lookups are
72679822b2aStholo      * almost certainly too much since that would make CVS immune to some of
72779822b2aStholo      * the DNS trickery that makes life easier for sysadmins when they want to
72879822b2aStholo      * move a repository or the like
72979822b2aStholo      */
73079822b2aStholo     p = hostname = xstrdup(root->hostname);
73179822b2aStholo     while (*p)
73279822b2aStholo     {
73379822b2aStholo 	*p = tolower(*p);
73479822b2aStholo 	p++;
73579822b2aStholo     }
73679822b2aStholo 
73779822b2aStholo     /* get the username string */
73879822b2aStholo     username = root->username ? root->username : getcaller();
73979822b2aStholo     cvsroot_canonical = xmalloc ( strlen(username)
74079822b2aStholo 				+ strlen(hostname) + strlen(port_s)
74179822b2aStholo 				+ strlen(root->directory) + 12);
74279822b2aStholo     sprintf (cvsroot_canonical, ":pserver:%s@%s:%s%s",
74379822b2aStholo 	    username, hostname, port_s, root->directory);
74479822b2aStholo 
74579822b2aStholo     free (hostname);
74679822b2aStholo     return cvsroot_canonical;
74779822b2aStholo }
74879822b2aStholo #endif /* AUTH_CLIENT_SUPPORT */
74979822b2aStholo 
75079822b2aStholo 
75179822b2aStholo 
75279822b2aStholo /* allocate and return a cvsroot_t structure set up as if we're using the local
75379822b2aStholo  * repository DIR.  */
75479822b2aStholo cvsroot_t *
local_cvsroot(dir)75579822b2aStholo local_cvsroot (dir)
75650bf276cStholo     char *dir;
75750bf276cStholo {
75879822b2aStholo     cvsroot_t *newroot = new_cvsroot_t();
75979822b2aStholo 
76079822b2aStholo     newroot->original = xstrdup(dir);
76179822b2aStholo     newroot->method = local_method;
76279822b2aStholo     newroot->directory = xstrdup(dir);
76379822b2aStholo 
76479822b2aStholo     return newroot;
76550bf276cStholo }
76650bf276cStholo 
76750bf276cStholo 
76879822b2aStholo 
76950bf276cStholo #ifdef DEBUG
770c71bc7e2Stholo /* This is for testing the parsing function.  Use
771c71bc7e2Stholo 
772c71bc7e2Stholo      gcc -I. -I.. -I../lib -DDEBUG root.c -o root
773c71bc7e2Stholo 
774c71bc7e2Stholo    to compile.  */
77550bf276cStholo 
77650bf276cStholo #include <stdio.h>
77750bf276cStholo 
77850bf276cStholo char *program_name = "testing";
77950bf276cStholo char *command_name = "parse_cvsroot";		/* XXX is this used??? */
78050bf276cStholo 
781c71bc7e2Stholo /* Toy versions of various functions when debugging under unix.  Yes,
782c71bc7e2Stholo    these make various bad assumptions, but they're pretty easy to
783c71bc7e2Stholo    debug when something goes wrong.  */
784c71bc7e2Stholo 
785c71bc7e2Stholo void
error_exit(void)786c71bc7e2Stholo error_exit PROTO ((void))
787c71bc7e2Stholo {
788c71bc7e2Stholo     exit (1);
789c71bc7e2Stholo }
790c71bc7e2Stholo 
791c71bc7e2Stholo int
isabsolute(dir)792c71bc7e2Stholo isabsolute (dir)
793c71bc7e2Stholo     const char *dir;
794c71bc7e2Stholo {
795c71bc7e2Stholo     return (dir && (*dir == '/'));
796c71bc7e2Stholo }
797c71bc7e2Stholo 
79850bf276cStholo void
main(argc,argv)79950bf276cStholo main (argc, argv)
80050bf276cStholo     int argc;
80150bf276cStholo     char *argv[];
80250bf276cStholo {
80350bf276cStholo     program_name = argv[0];
80450bf276cStholo 
80550bf276cStholo     if (argc != 2)
80650bf276cStholo     {
80750bf276cStholo 	fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
80850bf276cStholo 	exit (2);
80950bf276cStholo     }
81050bf276cStholo 
81179822b2aStholo     if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
81250bf276cStholo     {
813c71bc7e2Stholo 	fprintf (stderr, "%s: Parsing failed.\n", program_name);
81450bf276cStholo 	exit (1);
81550bf276cStholo     }
81650bf276cStholo     printf ("CVSroot: %s\n", argv[1]);
81779822b2aStholo     printf ("current_parsed_root->method: %s\n", method_names[current_parsed_root->method]);
81879822b2aStholo     printf ("current_parsed_root->username: %s\n",
81979822b2aStholo 	    current_parsed_root->username ? current_parsed_root->username : "NULL");
82079822b2aStholo     printf ("current_parsed_root->hostname: %s\n",
82179822b2aStholo 	    current_parsed_root->hostname ? current_parsed_root->hostname : "NULL");
82279822b2aStholo     printf ("current_parsed_root->directory: %s\n", current_parsed_root->directory);
82350bf276cStholo 
82450bf276cStholo    exit (0);
82550bf276cStholo    /* NOTREACHED */
82650bf276cStholo }
82750bf276cStholo #endif
828