186d7f5d3SJohn Marino /*
286d7f5d3SJohn Marino * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
386d7f5d3SJohn Marino *
486d7f5d3SJohn Marino * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
586d7f5d3SJohn Marino * and others.
686d7f5d3SJohn Marino *
786d7f5d3SJohn Marino * Poritons Copyright (c) 1992, Mark D. Baushke
886d7f5d3SJohn Marino *
986d7f5d3SJohn Marino * You may distribute under the terms of the GNU General Public License as
1086d7f5d3SJohn Marino * specified in the README file that comes with the CVS source distribution.
1186d7f5d3SJohn Marino *
1286d7f5d3SJohn Marino * Name of Root
1386d7f5d3SJohn Marino *
1486d7f5d3SJohn Marino * Determine the path to the CVSROOT and set "Root" accordingly.
1586d7f5d3SJohn Marino */
1686d7f5d3SJohn Marino
1786d7f5d3SJohn Marino #include "cvs.h"
1886d7f5d3SJohn Marino #include <assert.h>
1986d7f5d3SJohn Marino #include "getline.h"
2086d7f5d3SJohn Marino
2186d7f5d3SJohn Marino /* Printable names for things in the current_parsed_root->method enum variable.
2286d7f5d3SJohn Marino Watch out if the enum is changed in cvs.h! */
2386d7f5d3SJohn Marino
2486d7f5d3SJohn Marino const char method_names[][16] = {
2586d7f5d3SJohn Marino "undefined", "local", "server (rsh)", "pserver",
2686d7f5d3SJohn Marino "kserver", "gserver", "ext", "fork"
2786d7f5d3SJohn Marino };
2886d7f5d3SJohn Marino
2986d7f5d3SJohn Marino #ifndef DEBUG
3086d7f5d3SJohn Marino
3186d7f5d3SJohn Marino cvsroot_t *
Name_Root(const char * dir,const char * update_dir)3286d7f5d3SJohn Marino Name_Root (const char *dir, const char *update_dir)
3386d7f5d3SJohn Marino {
3486d7f5d3SJohn Marino FILE *fpin;
3586d7f5d3SJohn Marino cvsroot_t *ret;
3686d7f5d3SJohn Marino const char *xupdate_dir;
3786d7f5d3SJohn Marino char *root = NULL;
3886d7f5d3SJohn Marino size_t root_allocated = 0;
3986d7f5d3SJohn Marino char *tmp;
4086d7f5d3SJohn Marino char *cvsadm;
4186d7f5d3SJohn Marino char *cp;
4286d7f5d3SJohn Marino int len;
4386d7f5d3SJohn Marino
4486d7f5d3SJohn Marino TRACE (TRACE_FLOW, "Name_Root (%s, %s)",
4586d7f5d3SJohn Marino dir ? dir : "(null)",
4686d7f5d3SJohn Marino update_dir ? update_dir : "(null)");
4786d7f5d3SJohn Marino
4886d7f5d3SJohn Marino if (update_dir && *update_dir)
4986d7f5d3SJohn Marino xupdate_dir = update_dir;
5086d7f5d3SJohn Marino else
5186d7f5d3SJohn Marino xupdate_dir = ".";
5286d7f5d3SJohn Marino
5386d7f5d3SJohn Marino if (dir != NULL)
5486d7f5d3SJohn Marino {
5586d7f5d3SJohn Marino cvsadm = Xasprintf ("%s/%s", dir, CVSADM);
5686d7f5d3SJohn Marino tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
5786d7f5d3SJohn Marino }
5886d7f5d3SJohn Marino else
5986d7f5d3SJohn Marino {
6086d7f5d3SJohn Marino cvsadm = xstrdup (CVSADM);
6186d7f5d3SJohn Marino tmp = xstrdup (CVSADM_ROOT);
6286d7f5d3SJohn Marino }
6386d7f5d3SJohn Marino
6486d7f5d3SJohn Marino /*
6586d7f5d3SJohn Marino * Do not bother looking for a readable file if there is no cvsadm
6686d7f5d3SJohn Marino * directory present.
6786d7f5d3SJohn Marino *
6886d7f5d3SJohn Marino * It is possible that not all repositories will have a CVS/Root
6986d7f5d3SJohn Marino * file. This is ok, but the user will need to specify -d
7086d7f5d3SJohn Marino * /path/name or have the environment variable CVSROOT set in
7186d7f5d3SJohn Marino * order to continue. */
7286d7f5d3SJohn Marino if ((!isdir (cvsadm)) || (!isreadable (tmp)))
7386d7f5d3SJohn Marino {
7486d7f5d3SJohn Marino ret = NULL;
7586d7f5d3SJohn Marino goto out;
7686d7f5d3SJohn Marino }
7786d7f5d3SJohn Marino
7886d7f5d3SJohn Marino /*
7986d7f5d3SJohn Marino * The assumption here is that the CVS Root is always contained in the
8086d7f5d3SJohn Marino * first line of the "Root" file.
8186d7f5d3SJohn Marino */
8286d7f5d3SJohn Marino fpin = xfopen (tmp, "r");
8386d7f5d3SJohn Marino
8486d7f5d3SJohn Marino if ((len = getline (&root, &root_allocated, fpin)) < 0)
8586d7f5d3SJohn Marino {
8686d7f5d3SJohn Marino int saved_errno = errno;
8786d7f5d3SJohn Marino /* FIXME: should be checking for end of file separately; errno
8886d7f5d3SJohn Marino is not set in that case. */
8986d7f5d3SJohn Marino error (0, 0, "in directory %s:", xupdate_dir);
9086d7f5d3SJohn Marino error (0, saved_errno, "cannot read %s", CVSADM_ROOT);
9186d7f5d3SJohn Marino error (0, 0, "please correct this problem");
9286d7f5d3SJohn Marino ret = NULL;
9386d7f5d3SJohn Marino goto out;
9486d7f5d3SJohn Marino }
9586d7f5d3SJohn Marino fclose (fpin);
9686d7f5d3SJohn Marino cp = root + len - 1;
9786d7f5d3SJohn Marino if (*cp == '\n')
9886d7f5d3SJohn Marino *cp = '\0'; /* strip the newline */
9986d7f5d3SJohn Marino
10086d7f5d3SJohn Marino /*
10186d7f5d3SJohn Marino * root now contains a candidate for CVSroot. It must be an
10286d7f5d3SJohn Marino * absolute pathname or specify a remote server.
10386d7f5d3SJohn Marino */
10486d7f5d3SJohn Marino
10586d7f5d3SJohn Marino ret = parse_cvsroot (root);
10686d7f5d3SJohn Marino if (ret == NULL)
10786d7f5d3SJohn Marino {
10886d7f5d3SJohn Marino error (0, 0, "in directory %s:", xupdate_dir);
10986d7f5d3SJohn Marino error (0, 0,
11086d7f5d3SJohn Marino "ignoring %s because it does not contain a valid root.",
11186d7f5d3SJohn Marino CVSADM_ROOT);
11286d7f5d3SJohn Marino goto out;
11386d7f5d3SJohn Marino }
11486d7f5d3SJohn Marino
11586d7f5d3SJohn Marino if (!ret->isremote && !isdir (ret->directory))
11686d7f5d3SJohn Marino {
11786d7f5d3SJohn Marino error (0, 0, "in directory %s:", xupdate_dir);
11886d7f5d3SJohn Marino error (0, 0,
11986d7f5d3SJohn Marino "ignoring %s because it specifies a non-existent repository %s",
12086d7f5d3SJohn Marino CVSADM_ROOT, root);
12186d7f5d3SJohn Marino ret = NULL;
12286d7f5d3SJohn Marino goto out;
12386d7f5d3SJohn Marino }
12486d7f5d3SJohn Marino
12586d7f5d3SJohn Marino
12686d7f5d3SJohn Marino out:
12786d7f5d3SJohn Marino free (cvsadm);
12886d7f5d3SJohn Marino free (tmp);
12986d7f5d3SJohn Marino if (root != NULL)
13086d7f5d3SJohn Marino free (root);
13186d7f5d3SJohn Marino return ret;
13286d7f5d3SJohn Marino }
13386d7f5d3SJohn Marino
13486d7f5d3SJohn Marino
13586d7f5d3SJohn Marino
13686d7f5d3SJohn Marino /*
13786d7f5d3SJohn Marino * Write the CVS/Root file so that the environment variable CVSROOT
13886d7f5d3SJohn Marino * and/or the -d option to cvs will be validated or not necessary for
13986d7f5d3SJohn Marino * future work.
14086d7f5d3SJohn Marino */
14186d7f5d3SJohn Marino void
Create_Root(const char * dir,const char * rootdir)14286d7f5d3SJohn Marino Create_Root (const char *dir, const char *rootdir)
14386d7f5d3SJohn Marino {
14486d7f5d3SJohn Marino FILE *fout;
14586d7f5d3SJohn Marino char *tmp;
14686d7f5d3SJohn Marino
14786d7f5d3SJohn Marino if (noexec)
14886d7f5d3SJohn Marino return;
14986d7f5d3SJohn Marino
15086d7f5d3SJohn Marino /* record the current cvs root */
15186d7f5d3SJohn Marino
15286d7f5d3SJohn Marino if (rootdir != NULL)
15386d7f5d3SJohn Marino {
15486d7f5d3SJohn Marino if (dir != NULL)
15586d7f5d3SJohn Marino tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
15686d7f5d3SJohn Marino else
15786d7f5d3SJohn Marino tmp = xstrdup (CVSADM_ROOT);
15886d7f5d3SJohn Marino
15986d7f5d3SJohn Marino fout = xfopen (tmp, "w+");
16086d7f5d3SJohn Marino if (fprintf (fout, "%s\n", rootdir) < 0)
16186d7f5d3SJohn Marino error (1, errno, "write to %s failed", tmp);
16286d7f5d3SJohn Marino if (fclose (fout) == EOF)
16386d7f5d3SJohn Marino error (1, errno, "cannot close %s", tmp);
16486d7f5d3SJohn Marino free (tmp);
16586d7f5d3SJohn Marino }
16686d7f5d3SJohn Marino }
16786d7f5d3SJohn Marino
16886d7f5d3SJohn Marino #endif /* ! DEBUG */
16986d7f5d3SJohn Marino
17086d7f5d3SJohn Marino
17186d7f5d3SJohn Marino
17286d7f5d3SJohn Marino /* Translate an absolute repository string for a primary server and return it.
17386d7f5d3SJohn Marino *
17486d7f5d3SJohn Marino * INPUTS
17586d7f5d3SJohn Marino * root_in The root to be translated.
17686d7f5d3SJohn Marino *
17786d7f5d3SJohn Marino * RETURNS
17886d7f5d3SJohn Marino * A translated string this function owns, or a pointer to the original
17986d7f5d3SJohn Marino * string passed in if no translation was necessary.
18086d7f5d3SJohn Marino *
18186d7f5d3SJohn Marino * If the returned string is the translated one, it may be overwritten
18286d7f5d3SJohn Marino * by the next call to this function.
18386d7f5d3SJohn Marino */
18486d7f5d3SJohn Marino const char *
primary_root_translate(const char * root_in)18586d7f5d3SJohn Marino primary_root_translate (const char *root_in)
18686d7f5d3SJohn Marino {
18786d7f5d3SJohn Marino #ifdef PROXY_SUPPORT
18886d7f5d3SJohn Marino char *translated;
18986d7f5d3SJohn Marino static char *previous = NULL;
19086d7f5d3SJohn Marino static size_t len;
19186d7f5d3SJohn Marino
19286d7f5d3SJohn Marino /* This can happen, for instance, during `cvs init'. */
19386d7f5d3SJohn Marino if (!config) return root_in;
19486d7f5d3SJohn Marino
19586d7f5d3SJohn Marino if (config->PrimaryServer
19686d7f5d3SJohn Marino && !strncmp (root_in, config->PrimaryServer->directory,
19786d7f5d3SJohn Marino strlen (config->PrimaryServer->directory))
19886d7f5d3SJohn Marino && (ISSLASH (root_in[strlen (config->PrimaryServer->directory)])
19986d7f5d3SJohn Marino || root_in[strlen (config->PrimaryServer->directory)] == '\0')
20086d7f5d3SJohn Marino )
20186d7f5d3SJohn Marino {
20286d7f5d3SJohn Marino translated =
20386d7f5d3SJohn Marino Xasnprintf (previous, &len,
20486d7f5d3SJohn Marino "%s%s", current_parsed_root->directory,
20586d7f5d3SJohn Marino root_in + strlen (config->PrimaryServer->directory));
20686d7f5d3SJohn Marino if (previous && previous != translated)
20786d7f5d3SJohn Marino free (previous);
20886d7f5d3SJohn Marino return previous = translated;
20986d7f5d3SJohn Marino }
21086d7f5d3SJohn Marino #endif
21186d7f5d3SJohn Marino
21286d7f5d3SJohn Marino /* There is no primary root configured or it didn't match. */
21386d7f5d3SJohn Marino return root_in;
21486d7f5d3SJohn Marino }
21586d7f5d3SJohn Marino
21686d7f5d3SJohn Marino
21786d7f5d3SJohn Marino
21886d7f5d3SJohn Marino /* Translate a primary root in reverse for PATHNAMEs in responses.
21986d7f5d3SJohn Marino *
22086d7f5d3SJohn Marino * INPUTS
22186d7f5d3SJohn Marino * root_in The root to be translated.
22286d7f5d3SJohn Marino *
22386d7f5d3SJohn Marino * RETURNS
22486d7f5d3SJohn Marino * A translated string this function owns, or a pointer to the original
22586d7f5d3SJohn Marino * string passed in if no translation was necessary.
22686d7f5d3SJohn Marino *
22786d7f5d3SJohn Marino * If the returned string is the translated one, it may be overwritten
22886d7f5d3SJohn Marino * by the next call to this function.
22986d7f5d3SJohn Marino */
23086d7f5d3SJohn Marino const char *
primary_root_inverse_translate(const char * root_in)23186d7f5d3SJohn Marino primary_root_inverse_translate (const char *root_in)
23286d7f5d3SJohn Marino {
23386d7f5d3SJohn Marino #ifdef PROXY_SUPPORT
23486d7f5d3SJohn Marino char *translated;
23586d7f5d3SJohn Marino static char *previous = NULL;
23686d7f5d3SJohn Marino static size_t len;
23786d7f5d3SJohn Marino
23886d7f5d3SJohn Marino /* This can happen, for instance, during `cvs init'. */
23986d7f5d3SJohn Marino if (!config) return root_in;
24086d7f5d3SJohn Marino
24186d7f5d3SJohn Marino if (config->PrimaryServer
24286d7f5d3SJohn Marino && !strncmp (root_in, current_parsed_root->directory,
24386d7f5d3SJohn Marino strlen (current_parsed_root->directory))
24486d7f5d3SJohn Marino && (ISSLASH (root_in[strlen (current_parsed_root->directory)])
24586d7f5d3SJohn Marino || root_in[strlen (current_parsed_root->directory)] == '\0')
24686d7f5d3SJohn Marino )
24786d7f5d3SJohn Marino {
24886d7f5d3SJohn Marino translated =
24986d7f5d3SJohn Marino Xasnprintf (previous, &len,
25086d7f5d3SJohn Marino "%s%s", config->PrimaryServer->directory,
25186d7f5d3SJohn Marino root_in + strlen (current_parsed_root->directory));
25286d7f5d3SJohn Marino if (previous && previous != translated)
25386d7f5d3SJohn Marino free (previous);
25486d7f5d3SJohn Marino return previous = translated;
25586d7f5d3SJohn Marino }
25686d7f5d3SJohn Marino #endif
25786d7f5d3SJohn Marino
25886d7f5d3SJohn Marino /* There is no primary root configured or it didn't match. */
25986d7f5d3SJohn Marino return root_in;
26086d7f5d3SJohn Marino }
26186d7f5d3SJohn Marino
26286d7f5d3SJohn Marino
26386d7f5d3SJohn Marino
26486d7f5d3SJohn Marino /* The root_allow_* stuff maintains a list of valid CVSROOT
26586d7f5d3SJohn Marino directories. Then we can check against them when a remote user
26686d7f5d3SJohn Marino hands us a CVSROOT directory. */
26786d7f5d3SJohn Marino static List *root_allow;
26886d7f5d3SJohn Marino
26986d7f5d3SJohn Marino static void
delconfig(Node * n)27086d7f5d3SJohn Marino delconfig (Node *n)
27186d7f5d3SJohn Marino {
27286d7f5d3SJohn Marino if (n->data) free_config (n->data);
27386d7f5d3SJohn Marino }
27486d7f5d3SJohn Marino
27586d7f5d3SJohn Marino
27686d7f5d3SJohn Marino
27786d7f5d3SJohn Marino void
root_allow_add(const char * arg,const char * configPath)27886d7f5d3SJohn Marino root_allow_add (const char *arg, const char *configPath)
27986d7f5d3SJohn Marino {
28086d7f5d3SJohn Marino Node *n;
28186d7f5d3SJohn Marino
28286d7f5d3SJohn Marino if (!root_allow) root_allow = getlist();
28386d7f5d3SJohn Marino n = getnode();
28486d7f5d3SJohn Marino n->key = xstrdup (arg);
28586d7f5d3SJohn Marino n->data = parse_config (arg, configPath);
28686d7f5d3SJohn Marino n->delproc = delconfig;
28786d7f5d3SJohn Marino addnode (root_allow, n);
28886d7f5d3SJohn Marino }
28986d7f5d3SJohn Marino
29086d7f5d3SJohn Marino void
root_allow_free(void)29186d7f5d3SJohn Marino root_allow_free (void)
29286d7f5d3SJohn Marino {
29386d7f5d3SJohn Marino dellist (&root_allow);
29486d7f5d3SJohn Marino }
29586d7f5d3SJohn Marino
29686d7f5d3SJohn Marino bool
root_allow_ok(const char * arg)29786d7f5d3SJohn Marino root_allow_ok (const char *arg)
29886d7f5d3SJohn Marino {
29986d7f5d3SJohn Marino if (!root_allow)
30086d7f5d3SJohn Marino {
30186d7f5d3SJohn Marino /* Probably someone upgraded from CVS before 1.9.10 to 1.9.10
30286d7f5d3SJohn Marino or later without reading the documentation about
30386d7f5d3SJohn Marino --allow-root. Printing an error here doesn't disclose any
30486d7f5d3SJohn Marino particularly useful information to an attacker because a
30586d7f5d3SJohn Marino CVS server configured in this way won't let *anyone* in. */
30686d7f5d3SJohn Marino
30786d7f5d3SJohn Marino /* Note that we are called from a context where we can spit
30886d7f5d3SJohn Marino back "error" rather than waiting for the next request which
30986d7f5d3SJohn Marino expects responses. */
31086d7f5d3SJohn Marino printf ("\
31186d7f5d3SJohn Marino error 0 Server configuration missing --allow-root in inetd.conf\n");
31286d7f5d3SJohn Marino exit (EXIT_FAILURE);
31386d7f5d3SJohn Marino }
31486d7f5d3SJohn Marino
31586d7f5d3SJohn Marino if (findnode (root_allow, arg))
31686d7f5d3SJohn Marino return true;
31786d7f5d3SJohn Marino return false;
31886d7f5d3SJohn Marino }
31986d7f5d3SJohn Marino
32086d7f5d3SJohn Marino
32186d7f5d3SJohn Marino
32286d7f5d3SJohn Marino /* Get a config we stored in response to root_allow.
32386d7f5d3SJohn Marino *
32486d7f5d3SJohn Marino * RETURNS
32586d7f5d3SJohn Marino * The config associated with ARG.
32686d7f5d3SJohn Marino */
32786d7f5d3SJohn Marino struct config *
get_root_allow_config(const char * arg,const char * configPath)32886d7f5d3SJohn Marino get_root_allow_config (const char *arg, const char *configPath)
32986d7f5d3SJohn Marino {
33086d7f5d3SJohn Marino Node *n;
33186d7f5d3SJohn Marino
33286d7f5d3SJohn Marino TRACE (TRACE_FUNCTION, "get_root_allow_config (%s)", arg);
33386d7f5d3SJohn Marino
33486d7f5d3SJohn Marino if (root_allow)
33586d7f5d3SJohn Marino n = findnode (root_allow, arg);
33686d7f5d3SJohn Marino else
33786d7f5d3SJohn Marino n = NULL;
33886d7f5d3SJohn Marino
33986d7f5d3SJohn Marino if (n) return n->data;
34086d7f5d3SJohn Marino return parse_config (arg, configPath);
34186d7f5d3SJohn Marino }
34286d7f5d3SJohn Marino
34386d7f5d3SJohn Marino
34486d7f5d3SJohn Marino
34586d7f5d3SJohn Marino /* This global variable holds the global -d option. It is NULL if -d
34686d7f5d3SJohn Marino was not used, which means that we must get the CVSroot information
34786d7f5d3SJohn Marino from the CVSROOT environment variable or from a CVS/Root file. */
34886d7f5d3SJohn Marino char *CVSroot_cmdline;
34986d7f5d3SJohn Marino
35086d7f5d3SJohn Marino
35186d7f5d3SJohn Marino
35286d7f5d3SJohn Marino /* FIXME - Deglobalize this. */
35386d7f5d3SJohn Marino cvsroot_t *current_parsed_root = NULL;
35486d7f5d3SJohn Marino /* Used to save the original root being processed so that we can still find it
35586d7f5d3SJohn Marino * in lists and the like after a `Redirect' response. Also set to mirror
35686d7f5d3SJohn Marino * current_parsed_root in server mode so that code which runs on both the
35786d7f5d3SJohn Marino * client and server but which wants to use original data on the client can
35886d7f5d3SJohn Marino * just always reference the original_parsed_root.
35986d7f5d3SJohn Marino */
36086d7f5d3SJohn Marino const cvsroot_t *original_parsed_root;
36186d7f5d3SJohn Marino
36286d7f5d3SJohn Marino
36386d7f5d3SJohn Marino /* allocate and initialize a cvsroot_t
36486d7f5d3SJohn Marino *
36586d7f5d3SJohn Marino * We must initialize the strings to NULL so we know later what we should
36686d7f5d3SJohn Marino * free
36786d7f5d3SJohn Marino *
36886d7f5d3SJohn Marino * Some of the other zeroes remain meaningful as, "never set, use default",
36986d7f5d3SJohn Marino * or the like
37086d7f5d3SJohn Marino */
37186d7f5d3SJohn Marino /* Functions which allocate memory are not pure. */
37286d7f5d3SJohn Marino static cvsroot_t *new_cvsroot_t(void)
37386d7f5d3SJohn Marino __attribute__( (__malloc__) );
37486d7f5d3SJohn Marino static cvsroot_t *
new_cvsroot_t(void)37586d7f5d3SJohn Marino new_cvsroot_t (void)
37686d7f5d3SJohn Marino {
37786d7f5d3SJohn Marino cvsroot_t *newroot;
37886d7f5d3SJohn Marino
37986d7f5d3SJohn Marino /* gotta store it somewhere */
38086d7f5d3SJohn Marino newroot = xmalloc(sizeof(cvsroot_t));
38186d7f5d3SJohn Marino
38286d7f5d3SJohn Marino newroot->original = NULL;
38386d7f5d3SJohn Marino newroot->directory = NULL;
38486d7f5d3SJohn Marino newroot->method = null_method;
38586d7f5d3SJohn Marino newroot->isremote = false;
38686d7f5d3SJohn Marino #ifdef CLIENT_SUPPORT
38786d7f5d3SJohn Marino newroot->username = NULL;
38886d7f5d3SJohn Marino newroot->password = NULL;
38986d7f5d3SJohn Marino newroot->hostname = NULL;
39086d7f5d3SJohn Marino newroot->cvs_rsh = NULL;
39186d7f5d3SJohn Marino newroot->cvs_server = NULL;
39286d7f5d3SJohn Marino newroot->port = 0;
39386d7f5d3SJohn Marino newroot->proxy_hostname = NULL;
39486d7f5d3SJohn Marino newroot->proxy_port = 0;
39586d7f5d3SJohn Marino newroot->redirect = true; /* Advertise Redirect support */
39686d7f5d3SJohn Marino #endif /* CLIENT_SUPPORT */
39786d7f5d3SJohn Marino
39886d7f5d3SJohn Marino return newroot;
39986d7f5d3SJohn Marino }
40086d7f5d3SJohn Marino
40186d7f5d3SJohn Marino
40286d7f5d3SJohn Marino
40386d7f5d3SJohn Marino /* Dispose of a cvsroot_t and its component parts.
40486d7f5d3SJohn Marino *
40586d7f5d3SJohn Marino * NOTE
40686d7f5d3SJohn Marino * It is dangerous for most code to call this function since parse_cvsroot
40786d7f5d3SJohn Marino * maintains a cache of parsed roots.
40886d7f5d3SJohn Marino */
40986d7f5d3SJohn Marino static void
free_cvsroot_t(cvsroot_t * root)41086d7f5d3SJohn Marino free_cvsroot_t (cvsroot_t *root)
41186d7f5d3SJohn Marino {
41286d7f5d3SJohn Marino assert (root);
41386d7f5d3SJohn Marino if (root->original != NULL)
41486d7f5d3SJohn Marino free (root->original);
41586d7f5d3SJohn Marino if (root->directory != NULL)
41686d7f5d3SJohn Marino free (root->directory);
41786d7f5d3SJohn Marino #ifdef CLIENT_SUPPORT
41886d7f5d3SJohn Marino if (root->username != NULL)
41986d7f5d3SJohn Marino free (root->username);
42086d7f5d3SJohn Marino if (root->password != NULL)
42186d7f5d3SJohn Marino {
42286d7f5d3SJohn Marino /* I like to be paranoid */
42386d7f5d3SJohn Marino memset (root->password, 0, strlen (root->password));
42486d7f5d3SJohn Marino free (root->password);
42586d7f5d3SJohn Marino }
42686d7f5d3SJohn Marino if (root->hostname != NULL)
42786d7f5d3SJohn Marino free (root->hostname);
42886d7f5d3SJohn Marino if (root->cvs_rsh != NULL)
42986d7f5d3SJohn Marino free (root->cvs_rsh);
43086d7f5d3SJohn Marino if (root->cvs_server != NULL)
43186d7f5d3SJohn Marino free (root->cvs_server);
43286d7f5d3SJohn Marino if (root->proxy_hostname != NULL)
43386d7f5d3SJohn Marino free (root->proxy_hostname);
43486d7f5d3SJohn Marino #endif /* CLIENT_SUPPORT */
43586d7f5d3SJohn Marino free (root);
43686d7f5d3SJohn Marino }
43786d7f5d3SJohn Marino
43886d7f5d3SJohn Marino
43986d7f5d3SJohn Marino
44086d7f5d3SJohn Marino /*
44186d7f5d3SJohn Marino * Parse a CVSROOT string to allocate and return a new cvsroot_t structure.
44286d7f5d3SJohn Marino * Valid specifications are:
44386d7f5d3SJohn Marino *
44486d7f5d3SJohn Marino * :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path
44586d7f5d3SJohn Marino * [:(ext|server):][[user]@]host[:]/path
44686d7f5d3SJohn Marino * [:local:[e:]]/path
44786d7f5d3SJohn Marino * :fork:/path
44886d7f5d3SJohn Marino *
44986d7f5d3SJohn Marino * INPUTS
45086d7f5d3SJohn Marino * root_in C String containing the CVSROOT to be parsed.
45186d7f5d3SJohn Marino *
45286d7f5d3SJohn Marino * RETURNS
45386d7f5d3SJohn Marino * A pointer to a newly allocated cvsroot_t structure upon success and
45486d7f5d3SJohn Marino * NULL upon failure. The caller should never dispose of this structure,
45586d7f5d3SJohn Marino * as it is stored in a cache, but the caller may rely on it not to
45686d7f5d3SJohn Marino * change.
45786d7f5d3SJohn Marino *
45886d7f5d3SJohn Marino * NOTES
45986d7f5d3SJohn Marino * This would have been a lot easier to write in Perl.
46086d7f5d3SJohn Marino *
46186d7f5d3SJohn Marino * Would it make sense to reimplement the root and config file parsing
46286d7f5d3SJohn Marino * gunk in Lex/Yacc?
46386d7f5d3SJohn Marino *
46486d7f5d3SJohn Marino * SEE ALSO
46586d7f5d3SJohn Marino * free_cvsroot_t()
46686d7f5d3SJohn Marino */
46786d7f5d3SJohn Marino cvsroot_t *
parse_cvsroot(const char * root_in)46886d7f5d3SJohn Marino parse_cvsroot (const char *root_in)
46986d7f5d3SJohn Marino {
47086d7f5d3SJohn Marino cvsroot_t *newroot; /* the new root to be returned */
47186d7f5d3SJohn Marino char *cvsroot_save; /* what we allocated so we can dispose
47286d7f5d3SJohn Marino * it when finished */
47386d7f5d3SJohn Marino char *cvsroot_copy, *p; /* temporary pointers for parsing */
47486d7f5d3SJohn Marino #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
47586d7f5d3SJohn Marino char *q; /* temporary pointer for parsing */
47686d7f5d3SJohn Marino char *firstslash; /* save where the path spec starts
47786d7f5d3SJohn Marino * while we parse
47886d7f5d3SJohn Marino * [[user][:password]@]host[:[port]]
47986d7f5d3SJohn Marino */
48086d7f5d3SJohn Marino int check_hostname, no_port, no_password, no_proxy;
48186d7f5d3SJohn Marino #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
48286d7f5d3SJohn Marino static List *cache = NULL;
48386d7f5d3SJohn Marino Node *node;
48486d7f5d3SJohn Marino
48586d7f5d3SJohn Marino assert (root_in != NULL);
48686d7f5d3SJohn Marino
48786d7f5d3SJohn Marino /* This message is TRACE_FLOW since this function is called repeatedly by
48886d7f5d3SJohn Marino * the recursion routines.
48986d7f5d3SJohn Marino */
49086d7f5d3SJohn Marino TRACE (TRACE_FLOW, "parse_cvsroot (%s)", root_in);
49186d7f5d3SJohn Marino
49286d7f5d3SJohn Marino if ((node = findnode (cache, root_in)))
49386d7f5d3SJohn Marino return node->data;
49486d7f5d3SJohn Marino
49586d7f5d3SJohn Marino assert (root_in);
49686d7f5d3SJohn Marino
49786d7f5d3SJohn Marino /* allocate some space */
49886d7f5d3SJohn Marino newroot = new_cvsroot_t();
49986d7f5d3SJohn Marino
50086d7f5d3SJohn Marino /* save the original string */
50186d7f5d3SJohn Marino newroot->original = xstrdup (root_in);
50286d7f5d3SJohn Marino
50386d7f5d3SJohn Marino /* and another copy we can munge while parsing */
50486d7f5d3SJohn Marino cvsroot_save = cvsroot_copy = xstrdup (root_in);
50586d7f5d3SJohn Marino
50686d7f5d3SJohn Marino if (*cvsroot_copy == ':')
50786d7f5d3SJohn Marino {
50886d7f5d3SJohn Marino char *method = ++cvsroot_copy;
50986d7f5d3SJohn Marino
51086d7f5d3SJohn Marino /* Access method specified, as in
51186d7f5d3SJohn Marino * "cvs -d :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path",
51286d7f5d3SJohn Marino * "cvs -d [:(ext|server):][[user]@]host[:]/path",
51386d7f5d3SJohn Marino * "cvs -d :local:e:\path",
51486d7f5d3SJohn Marino * "cvs -d :fork:/path".
51586d7f5d3SJohn Marino * We need to get past that part of CVSroot before parsing the
51686d7f5d3SJohn Marino * rest of it.
51786d7f5d3SJohn Marino */
51886d7f5d3SJohn Marino
51986d7f5d3SJohn Marino if (! (p = strchr (method, ':')))
52086d7f5d3SJohn Marino {
52186d7f5d3SJohn Marino error (0, 0, "No closing `:' on method in CVSROOT.");
52286d7f5d3SJohn Marino goto error_exit;
52386d7f5d3SJohn Marino }
52486d7f5d3SJohn Marino *p = '\0';
52586d7f5d3SJohn Marino cvsroot_copy = ++p;
52686d7f5d3SJohn Marino
52786d7f5d3SJohn Marino #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
52886d7f5d3SJohn Marino /* Look for method options, for instance, proxy, proxyport.
52986d7f5d3SJohn Marino * Calling strtok again is saved until after parsing the method.
53086d7f5d3SJohn Marino */
53186d7f5d3SJohn Marino method = strtok (method, ";");
53286d7f5d3SJohn Marino if (!method)
53386d7f5d3SJohn Marino /* Could just exit now, but this keeps the error message in sync.
53486d7f5d3SJohn Marino */
53586d7f5d3SJohn Marino method = "";
53686d7f5d3SJohn Marino #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
53786d7f5d3SJohn Marino
53886d7f5d3SJohn Marino /* Now we have an access method -- see if it's valid. */
53986d7f5d3SJohn Marino
54086d7f5d3SJohn Marino if (!strcasecmp (method, "local"))
54186d7f5d3SJohn Marino newroot->method = local_method;
54286d7f5d3SJohn Marino else if (!strcasecmp (method, "pserver"))
54386d7f5d3SJohn Marino newroot->method = pserver_method;
54486d7f5d3SJohn Marino else if (!strcasecmp (method, "kserver"))
54586d7f5d3SJohn Marino newroot->method = kserver_method;
54686d7f5d3SJohn Marino else if (!strcasecmp (method, "gserver"))
54786d7f5d3SJohn Marino newroot->method = gserver_method;
54886d7f5d3SJohn Marino else if (!strcasecmp (method, "server"))
54986d7f5d3SJohn Marino newroot->method = server_method;
55086d7f5d3SJohn Marino else if (!strcasecmp (method, "ext"))
55186d7f5d3SJohn Marino newroot->method = ext_method;
55286d7f5d3SJohn Marino else if (!strcasecmp (method, "fork"))
55386d7f5d3SJohn Marino newroot->method = fork_method;
55486d7f5d3SJohn Marino else
55586d7f5d3SJohn Marino {
55686d7f5d3SJohn Marino error (0, 0, "Unknown method (`%s') in CVSROOT.", method);
55786d7f5d3SJohn Marino goto error_exit;
55886d7f5d3SJohn Marino }
55986d7f5d3SJohn Marino
56086d7f5d3SJohn Marino #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
56186d7f5d3SJohn Marino /* Parse the method options, for instance, proxy, proxyport */
56286d7f5d3SJohn Marino while ((p = strtok (NULL, ";")))
56386d7f5d3SJohn Marino {
56486d7f5d3SJohn Marino char *q = strchr (p, '=');
56586d7f5d3SJohn Marino if (q == NULL)
56686d7f5d3SJohn Marino {
56786d7f5d3SJohn Marino error (0, 0, "Option (`%s') has no argument in CVSROOT.",
56886d7f5d3SJohn Marino p);
56986d7f5d3SJohn Marino goto error_exit;
57086d7f5d3SJohn Marino }
57186d7f5d3SJohn Marino
57286d7f5d3SJohn Marino *q++ = '\0';
57386d7f5d3SJohn Marino TRACE (TRACE_DATA, "CVSROOT option=`%s' value=`%s'", p, q);
57486d7f5d3SJohn Marino if (!strcasecmp (p, "proxy"))
57586d7f5d3SJohn Marino {
57686d7f5d3SJohn Marino newroot->proxy_hostname = xstrdup (q);
57786d7f5d3SJohn Marino }
57886d7f5d3SJohn Marino else if (!strcasecmp (p, "proxyport"))
57986d7f5d3SJohn Marino {
58086d7f5d3SJohn Marino char *r = q;
58186d7f5d3SJohn Marino if (*r == '-') r++;
58286d7f5d3SJohn Marino while (*r)
58386d7f5d3SJohn Marino {
58486d7f5d3SJohn Marino if (!isdigit(*r++))
58586d7f5d3SJohn Marino {
58686d7f5d3SJohn Marino error (0, 0,
58786d7f5d3SJohn Marino "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
58886d7f5d3SJohn Marino q);
58986d7f5d3SJohn Marino goto error_exit;
59086d7f5d3SJohn Marino }
59186d7f5d3SJohn Marino }
59286d7f5d3SJohn Marino if ((newroot->proxy_port = atoi (q)) <= 0)
59386d7f5d3SJohn Marino error (0, 0,
59486d7f5d3SJohn Marino "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
59586d7f5d3SJohn Marino q);
59686d7f5d3SJohn Marino }
59786d7f5d3SJohn Marino else if (!strcasecmp (p, "CVS_RSH"))
59886d7f5d3SJohn Marino {
59986d7f5d3SJohn Marino /* override CVS_RSH environment variable */
60086d7f5d3SJohn Marino if (newroot->method == ext_method)
60186d7f5d3SJohn Marino newroot->cvs_rsh = xstrdup (q);
60286d7f5d3SJohn Marino }
60386d7f5d3SJohn Marino else if (!strcasecmp (p, "CVS_SERVER"))
60486d7f5d3SJohn Marino {
60586d7f5d3SJohn Marino /* override CVS_SERVER environment variable */
60686d7f5d3SJohn Marino if (newroot->method == ext_method
60786d7f5d3SJohn Marino || newroot->method == fork_method)
60886d7f5d3SJohn Marino newroot->cvs_server = xstrdup (q);
60986d7f5d3SJohn Marino }
61086d7f5d3SJohn Marino else if (!strcasecmp (p, "Redirect"))
61186d7f5d3SJohn Marino readBool ("CVSROOT", "Redirect", q, &newroot->redirect);
61286d7f5d3SJohn Marino else
61386d7f5d3SJohn Marino {
61486d7f5d3SJohn Marino error (0, 0, "Unknown option (`%s') in CVSROOT.", p);
61586d7f5d3SJohn Marino goto error_exit;
61686d7f5d3SJohn Marino }
61786d7f5d3SJohn Marino }
61886d7f5d3SJohn Marino #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
61986d7f5d3SJohn Marino }
62086d7f5d3SJohn Marino else
62186d7f5d3SJohn Marino {
62286d7f5d3SJohn Marino /* If the method isn't specified, assume EXT_METHOD if the string looks
62386d7f5d3SJohn Marino like a relative path and LOCAL_METHOD otherwise. */
62486d7f5d3SJohn Marino
62586d7f5d3SJohn Marino newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
62686d7f5d3SJohn Marino ? ext_method
62786d7f5d3SJohn Marino : local_method);
62886d7f5d3SJohn Marino }
62986d7f5d3SJohn Marino
63086d7f5d3SJohn Marino /*
63186d7f5d3SJohn Marino * There are a few sanity checks we can do now, only knowing the
63286d7f5d3SJohn Marino * method of this root.
63386d7f5d3SJohn Marino */
63486d7f5d3SJohn Marino
63586d7f5d3SJohn Marino newroot->isremote = (newroot->method != local_method);
63686d7f5d3SJohn Marino
63786d7f5d3SJohn Marino #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
63886d7f5d3SJohn Marino if (readonlyfs && newroot->isremote) {
63986d7f5d3SJohn Marino /* downgrade readonlyfs settings via environment */
64086d7f5d3SJohn Marino if (readonlyfs < 0)
64186d7f5d3SJohn Marino error (1, 0,
64286d7f5d3SJohn Marino "Read-only repository feature unavailable with remote roots (cvsroot = %s)",
64386d7f5d3SJohn Marino cvsroot_copy);
64486d7f5d3SJohn Marino readonlyfs = 0;
64586d7f5d3SJohn Marino }
64686d7f5d3SJohn Marino
64786d7f5d3SJohn Marino if ((newroot->method != local_method)
64886d7f5d3SJohn Marino && (newroot->method != fork_method)
64986d7f5d3SJohn Marino )
65086d7f5d3SJohn Marino {
65186d7f5d3SJohn Marino /* split the string into [[user][:password]@]host[:[port]] & /path
65286d7f5d3SJohn Marino *
65386d7f5d3SJohn Marino * this will allow some characters such as '@' & ':' to remain unquoted
65486d7f5d3SJohn Marino * in the path portion of the spec
65586d7f5d3SJohn Marino */
65686d7f5d3SJohn Marino if ((p = strchr (cvsroot_copy, '/')) == NULL)
65786d7f5d3SJohn Marino {
65886d7f5d3SJohn Marino error (0, 0, "CVSROOT requires a path spec:");
65986d7f5d3SJohn Marino error (0, 0,
66086d7f5d3SJohn Marino ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
66186d7f5d3SJohn Marino error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
66286d7f5d3SJohn Marino goto error_exit;
66386d7f5d3SJohn Marino }
66486d7f5d3SJohn Marino firstslash = p; /* == NULL if '/' not in string */
66586d7f5d3SJohn Marino *p = '\0';
66686d7f5d3SJohn Marino
66786d7f5d3SJohn Marino /* Check to see if there is a username[:password] in the string. */
66886d7f5d3SJohn Marino if ((p = strchr (cvsroot_copy, '@')) != NULL)
66986d7f5d3SJohn Marino {
67086d7f5d3SJohn Marino *p = '\0';
67186d7f5d3SJohn Marino /* check for a password */
67286d7f5d3SJohn Marino if ((q = strchr (cvsroot_copy, ':')) != NULL)
67386d7f5d3SJohn Marino {
67486d7f5d3SJohn Marino *q = '\0';
67586d7f5d3SJohn Marino newroot->password = xstrdup (++q);
67686d7f5d3SJohn Marino /* Don't check for *newroot->password == '\0' since
67786d7f5d3SJohn Marino * a user could conceivably wish to specify a blank password
67886d7f5d3SJohn Marino *
67986d7f5d3SJohn Marino * (newroot->password == NULL means to use the
68086d7f5d3SJohn Marino * password from .cvspass)
68186d7f5d3SJohn Marino */
68286d7f5d3SJohn Marino }
68386d7f5d3SJohn Marino
68486d7f5d3SJohn Marino /* copy the username */
68586d7f5d3SJohn Marino if (*cvsroot_copy != '\0')
68686d7f5d3SJohn Marino /* a blank username is impossible, so leave it NULL in that
68786d7f5d3SJohn Marino * case so we know to use the default username
68886d7f5d3SJohn Marino */
68986d7f5d3SJohn Marino newroot->username = xstrdup (cvsroot_copy);
69086d7f5d3SJohn Marino
69186d7f5d3SJohn Marino cvsroot_copy = ++p;
69286d7f5d3SJohn Marino }
69386d7f5d3SJohn Marino
69486d7f5d3SJohn Marino /* now deal with host[:[port]] */
69586d7f5d3SJohn Marino
69686d7f5d3SJohn Marino /* the port */
69786d7f5d3SJohn Marino if ((p = strchr (cvsroot_copy, ':')) != NULL)
69886d7f5d3SJohn Marino {
69986d7f5d3SJohn Marino *p++ = '\0';
70086d7f5d3SJohn Marino if (strlen(p))
70186d7f5d3SJohn Marino {
70286d7f5d3SJohn Marino q = p;
70386d7f5d3SJohn Marino if (*q == '-') q++;
70486d7f5d3SJohn Marino while (*q)
70586d7f5d3SJohn Marino {
70686d7f5d3SJohn Marino if (!isdigit(*q++))
70786d7f5d3SJohn Marino {
70886d7f5d3SJohn Marino error (0, 0,
70986d7f5d3SJohn Marino "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
71086d7f5d3SJohn Marino p);
71186d7f5d3SJohn Marino error (0, 0,
71286d7f5d3SJohn Marino "Perhaps you entered a relative pathname?");
71386d7f5d3SJohn Marino goto error_exit;
71486d7f5d3SJohn Marino }
71586d7f5d3SJohn Marino }
71686d7f5d3SJohn Marino if ((newroot->port = atoi (p)) <= 0)
71786d7f5d3SJohn Marino {
71886d7f5d3SJohn Marino error (0, 0,
71986d7f5d3SJohn Marino "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
72086d7f5d3SJohn Marino p);
72186d7f5d3SJohn Marino error (0, 0, "Perhaps you entered a relative pathname?");
72286d7f5d3SJohn Marino goto error_exit;
72386d7f5d3SJohn Marino }
72486d7f5d3SJohn Marino }
72586d7f5d3SJohn Marino }
72686d7f5d3SJohn Marino
72786d7f5d3SJohn Marino /* copy host */
72886d7f5d3SJohn Marino if (*cvsroot_copy != '\0')
72986d7f5d3SJohn Marino /* blank hostnames are invalid, but for now leave the field NULL
73086d7f5d3SJohn Marino * and catch the error during the sanity checks later
73186d7f5d3SJohn Marino */
73286d7f5d3SJohn Marino newroot->hostname = xstrdup (cvsroot_copy);
73386d7f5d3SJohn Marino
73486d7f5d3SJohn Marino /* restore the '/' */
73586d7f5d3SJohn Marino cvsroot_copy = firstslash;
73686d7f5d3SJohn Marino *cvsroot_copy = '/';
73786d7f5d3SJohn Marino }
73886d7f5d3SJohn Marino #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
73986d7f5d3SJohn Marino
74086d7f5d3SJohn Marino /*
74186d7f5d3SJohn Marino * Parse the path for all methods.
74286d7f5d3SJohn Marino */
74386d7f5d3SJohn Marino /* Here & local_cvsroot() should be the only places this needs to be
74486d7f5d3SJohn Marino * called on a CVSROOT now. cvsroot->original is saved for error messages
74586d7f5d3SJohn Marino * and, otherwise, we want no trailing slashes.
74686d7f5d3SJohn Marino */
74786d7f5d3SJohn Marino Sanitize_Repository_Name (cvsroot_copy);
74886d7f5d3SJohn Marino newroot->directory = xstrdup (cvsroot_copy);
74986d7f5d3SJohn Marino
75086d7f5d3SJohn Marino /*
75186d7f5d3SJohn Marino * Do various sanity checks.
75286d7f5d3SJohn Marino */
75386d7f5d3SJohn Marino
75486d7f5d3SJohn Marino #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
75586d7f5d3SJohn Marino if (newroot->username && ! newroot->hostname)
75686d7f5d3SJohn Marino {
75786d7f5d3SJohn Marino error (0, 0, "Missing hostname in CVSROOT.");
75886d7f5d3SJohn Marino goto error_exit;
75986d7f5d3SJohn Marino }
76086d7f5d3SJohn Marino
76186d7f5d3SJohn Marino /* We won't have attempted to parse these without CLIENT_SUPPORT or
76286d7f5d3SJohn Marino * SERVER_SUPPORT.
76386d7f5d3SJohn Marino */
76486d7f5d3SJohn Marino check_hostname = 0;
76586d7f5d3SJohn Marino no_password = 1;
76686d7f5d3SJohn Marino no_proxy = 1;
76786d7f5d3SJohn Marino no_port = 0;
76886d7f5d3SJohn Marino #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
76986d7f5d3SJohn Marino switch (newroot->method)
77086d7f5d3SJohn Marino {
77186d7f5d3SJohn Marino case local_method:
77286d7f5d3SJohn Marino #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
77386d7f5d3SJohn Marino if (newroot->username || newroot->hostname)
77486d7f5d3SJohn Marino {
77586d7f5d3SJohn Marino error (0, 0, "Can't specify hostname and username in CVSROOT");
77686d7f5d3SJohn Marino error (0, 0, "when using local access method.");
77786d7f5d3SJohn Marino goto error_exit;
77886d7f5d3SJohn Marino }
77986d7f5d3SJohn Marino #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
78086d7f5d3SJohn Marino /* cvs.texinfo has always told people that CVSROOT must be an
78186d7f5d3SJohn Marino absolute pathname. Furthermore, attempts to use a relative
78286d7f5d3SJohn Marino pathname produced various errors (I couldn't get it to work),
78386d7f5d3SJohn Marino so there would seem to be little risk in making this a fatal
78486d7f5d3SJohn Marino error. */
78586d7f5d3SJohn Marino if (!ISABSOLUTE (newroot->directory))
78686d7f5d3SJohn Marino {
78786d7f5d3SJohn Marino error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
78886d7f5d3SJohn Marino newroot->directory);
78986d7f5d3SJohn Marino error (0, 0, "when using local access method.");
79086d7f5d3SJohn Marino goto error_exit;
79186d7f5d3SJohn Marino }
79286d7f5d3SJohn Marino #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
79386d7f5d3SJohn Marino /* We don't need to check for these in :local: mode, really, since
79486d7f5d3SJohn Marino * we shouldn't be able to hit the code above which parses them, but
79586d7f5d3SJohn Marino * I'm leaving them here in lieu of assertions.
79686d7f5d3SJohn Marino */
79786d7f5d3SJohn Marino no_port = 1;
79886d7f5d3SJohn Marino /* no_password already set */
79986d7f5d3SJohn Marino #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
80086d7f5d3SJohn Marino break;
80186d7f5d3SJohn Marino #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
80286d7f5d3SJohn Marino case fork_method:
80386d7f5d3SJohn Marino /* We want :fork: to behave the same as other remote access
80486d7f5d3SJohn Marino methods. Therefore, don't check to see that the repository
80586d7f5d3SJohn Marino name is absolute -- let the server do it. */
80686d7f5d3SJohn Marino if (newroot->username || newroot->hostname)
80786d7f5d3SJohn Marino {
80886d7f5d3SJohn Marino error (0, 0, "Can't specify hostname and username in CVSROOT");
80986d7f5d3SJohn Marino error (0, 0, "when using fork access method.");
81086d7f5d3SJohn Marino goto error_exit;
81186d7f5d3SJohn Marino }
81286d7f5d3SJohn Marino newroot->hostname = xstrdup("server"); /* for error messages */
81386d7f5d3SJohn Marino if (!ISABSOLUTE (newroot->directory))
81486d7f5d3SJohn Marino {
81586d7f5d3SJohn Marino error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
81686d7f5d3SJohn Marino newroot->directory);
81786d7f5d3SJohn Marino error (0, 0, "when using fork access method.");
81886d7f5d3SJohn Marino goto error_exit;
81986d7f5d3SJohn Marino }
82086d7f5d3SJohn Marino no_port = 1;
82186d7f5d3SJohn Marino /* no_password already set */
82286d7f5d3SJohn Marino break;
82386d7f5d3SJohn Marino case kserver_method:
82486d7f5d3SJohn Marino check_hostname = 1;
82586d7f5d3SJohn Marino /* no_password already set */
82686d7f5d3SJohn Marino break;
82786d7f5d3SJohn Marino case gserver_method:
82886d7f5d3SJohn Marino check_hostname = 1;
82986d7f5d3SJohn Marino no_proxy = 0;
83086d7f5d3SJohn Marino /* no_password already set */
83186d7f5d3SJohn Marino break;
83286d7f5d3SJohn Marino case server_method:
83386d7f5d3SJohn Marino case ext_method:
83486d7f5d3SJohn Marino no_port = 1;
83586d7f5d3SJohn Marino /* no_password already set */
83686d7f5d3SJohn Marino check_hostname = 1;
83786d7f5d3SJohn Marino break;
83886d7f5d3SJohn Marino case pserver_method:
83986d7f5d3SJohn Marino no_password = 0;
84086d7f5d3SJohn Marino no_proxy = 0;
84186d7f5d3SJohn Marino check_hostname = 1;
84286d7f5d3SJohn Marino break;
84386d7f5d3SJohn Marino #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
84486d7f5d3SJohn Marino default:
84586d7f5d3SJohn Marino error (1, 0, "Invalid method found in parse_cvsroot");
84686d7f5d3SJohn Marino }
84786d7f5d3SJohn Marino
84886d7f5d3SJohn Marino #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
84986d7f5d3SJohn Marino if (no_password && newroot->password)
85086d7f5d3SJohn Marino {
85186d7f5d3SJohn Marino error (0, 0, "CVSROOT password specification is only valid for");
85286d7f5d3SJohn Marino error (0, 0, "pserver connection method.");
85386d7f5d3SJohn Marino goto error_exit;
85486d7f5d3SJohn Marino }
85586d7f5d3SJohn Marino if (no_proxy && (newroot->proxy_hostname || newroot->proxy_port))
85686d7f5d3SJohn Marino {
85786d7f5d3SJohn Marino error (0, 0,
85886d7f5d3SJohn Marino "CVSROOT proxy specification is only valid for gserver and");
85986d7f5d3SJohn Marino error (0, 0, "pserver connection methods.");
86086d7f5d3SJohn Marino goto error_exit;
86186d7f5d3SJohn Marino }
86286d7f5d3SJohn Marino
86386d7f5d3SJohn Marino if (!newroot->proxy_hostname && newroot->proxy_port)
86486d7f5d3SJohn Marino {
86586d7f5d3SJohn Marino error (0, 0, "Proxy port specified in CVSROOT without proxy host.");
86686d7f5d3SJohn Marino goto error_exit;
86786d7f5d3SJohn Marino }
86886d7f5d3SJohn Marino
86986d7f5d3SJohn Marino if (check_hostname && !newroot->hostname)
87086d7f5d3SJohn Marino {
87186d7f5d3SJohn Marino error (0, 0, "Didn't specify hostname in CVSROOT.");
87286d7f5d3SJohn Marino goto error_exit;
87386d7f5d3SJohn Marino }
87486d7f5d3SJohn Marino
87586d7f5d3SJohn Marino if (no_port && newroot->port)
87686d7f5d3SJohn Marino {
87786d7f5d3SJohn Marino error (0, 0,
87886d7f5d3SJohn Marino "CVSROOT port specification is only valid for gserver, kserver,");
87986d7f5d3SJohn Marino error (0, 0, "and pserver connection methods.");
88086d7f5d3SJohn Marino goto error_exit;
88186d7f5d3SJohn Marino }
88286d7f5d3SJohn Marino #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
88386d7f5d3SJohn Marino
88486d7f5d3SJohn Marino if (*newroot->directory == '\0')
88586d7f5d3SJohn Marino {
88686d7f5d3SJohn Marino error (0, 0, "Missing directory in CVSROOT.");
88786d7f5d3SJohn Marino goto error_exit;
88886d7f5d3SJohn Marino }
88986d7f5d3SJohn Marino
89086d7f5d3SJohn Marino /* Hooray! We finally parsed it! */
89186d7f5d3SJohn Marino free (cvsroot_save);
89286d7f5d3SJohn Marino
89386d7f5d3SJohn Marino if (!cache) cache = getlist();
89486d7f5d3SJohn Marino node = getnode();
89586d7f5d3SJohn Marino node->key = xstrdup (newroot->original);
89686d7f5d3SJohn Marino node->data = newroot;
89786d7f5d3SJohn Marino addnode (cache, node);
89886d7f5d3SJohn Marino return newroot;
89986d7f5d3SJohn Marino
90086d7f5d3SJohn Marino error_exit:
90186d7f5d3SJohn Marino free (cvsroot_save);
90286d7f5d3SJohn Marino free_cvsroot_t (newroot);
90386d7f5d3SJohn Marino return NULL;
90486d7f5d3SJohn Marino }
90586d7f5d3SJohn Marino
90686d7f5d3SJohn Marino
90786d7f5d3SJohn Marino
90886d7f5d3SJohn Marino #ifdef AUTH_CLIENT_SUPPORT
90986d7f5d3SJohn Marino /* Use root->username, root->hostname, root->port, and root->directory
91086d7f5d3SJohn Marino * to create a normalized CVSROOT fit for the .cvspass file
91186d7f5d3SJohn Marino *
91286d7f5d3SJohn Marino * username defaults to the result of getcaller()
91386d7f5d3SJohn Marino * port defaults to the result of get_cvs_port_number()
91486d7f5d3SJohn Marino *
91586d7f5d3SJohn Marino * FIXME - we could cache the canonicalized version of a root inside the
91686d7f5d3SJohn Marino * cvsroot_t, but we'd have to un'const the input here and stop expecting the
91786d7f5d3SJohn Marino * caller to be responsible for our return value
91886d7f5d3SJohn Marino *
91986d7f5d3SJohn Marino * ASSUMPTIONS
92086d7f5d3SJohn Marino * ROOT->method == pserver_method
92186d7f5d3SJohn Marino */
92286d7f5d3SJohn Marino char *
normalize_cvsroot(const cvsroot_t * root)92386d7f5d3SJohn Marino normalize_cvsroot (const cvsroot_t *root)
92486d7f5d3SJohn Marino {
92586d7f5d3SJohn Marino char *cvsroot_canonical;
92686d7f5d3SJohn Marino char *p, *hostname;
92786d7f5d3SJohn Marino
92886d7f5d3SJohn Marino assert (root && root->hostname && root->directory);
92986d7f5d3SJohn Marino
93086d7f5d3SJohn Marino /* use a lower case hostname since we know hostnames are case insensitive */
93186d7f5d3SJohn Marino /* Some logic says we should be tacking our domain name on too if it isn't
93286d7f5d3SJohn Marino * there already, but for now this works. Reverse->Forward lookups are
93386d7f5d3SJohn Marino * almost certainly too much since that would make CVS immune to some of
93486d7f5d3SJohn Marino * the DNS trickery that makes life easier for sysadmins when they want to
93586d7f5d3SJohn Marino * move a repository or the like
93686d7f5d3SJohn Marino */
93786d7f5d3SJohn Marino p = hostname = xstrdup (root->hostname);
93886d7f5d3SJohn Marino while (*p)
93986d7f5d3SJohn Marino {
94086d7f5d3SJohn Marino *p = tolower (*p);
94186d7f5d3SJohn Marino p++;
94286d7f5d3SJohn Marino }
94386d7f5d3SJohn Marino
94486d7f5d3SJohn Marino cvsroot_canonical = Xasprintf (":pserver:%s@%s:%d%s",
94586d7f5d3SJohn Marino root->username ? root->username
94686d7f5d3SJohn Marino : getcaller(),
94786d7f5d3SJohn Marino hostname, get_cvs_port_number (root),
94886d7f5d3SJohn Marino root->directory);
94986d7f5d3SJohn Marino
95086d7f5d3SJohn Marino free (hostname);
95186d7f5d3SJohn Marino return cvsroot_canonical;
95286d7f5d3SJohn Marino }
95386d7f5d3SJohn Marino #endif /* AUTH_CLIENT_SUPPORT */
95486d7f5d3SJohn Marino
95586d7f5d3SJohn Marino
95686d7f5d3SJohn Marino
95786d7f5d3SJohn Marino #ifdef PROXY_SUPPORT
95886d7f5d3SJohn Marino /* A walklist() function to walk the root_allow list looking for a PrimaryServer
95986d7f5d3SJohn Marino * configuration with a directory matching the requested directory.
96086d7f5d3SJohn Marino *
96186d7f5d3SJohn Marino * If found, replace it.
96286d7f5d3SJohn Marino */
96386d7f5d3SJohn Marino static bool get_local_root_dir_done;
96486d7f5d3SJohn Marino static int
get_local_root_dir(Node * p,void * root_in)96586d7f5d3SJohn Marino get_local_root_dir (Node *p, void *root_in)
96686d7f5d3SJohn Marino {
96786d7f5d3SJohn Marino struct config *c = p->data;
96886d7f5d3SJohn Marino char **r = root_in;
96986d7f5d3SJohn Marino
97086d7f5d3SJohn Marino if (get_local_root_dir_done)
97186d7f5d3SJohn Marino return 0;
97286d7f5d3SJohn Marino
97386d7f5d3SJohn Marino if (c->PrimaryServer && !strcmp (*r, c->PrimaryServer->directory))
97486d7f5d3SJohn Marino {
97586d7f5d3SJohn Marino free (*r);
97686d7f5d3SJohn Marino *r = xstrdup (p->key);
97786d7f5d3SJohn Marino get_local_root_dir_done = true;
97886d7f5d3SJohn Marino }
97986d7f5d3SJohn Marino return 0;
98086d7f5d3SJohn Marino }
98186d7f5d3SJohn Marino #endif /* PROXY_SUPPORT */
98286d7f5d3SJohn Marino
98386d7f5d3SJohn Marino
98486d7f5d3SJohn Marino
98586d7f5d3SJohn Marino /* allocate and return a cvsroot_t structure set up as if we're using the local
98686d7f5d3SJohn Marino * repository DIR. */
98786d7f5d3SJohn Marino cvsroot_t *
local_cvsroot(const char * dir)98886d7f5d3SJohn Marino local_cvsroot (const char *dir)
98986d7f5d3SJohn Marino {
99086d7f5d3SJohn Marino cvsroot_t *newroot = new_cvsroot_t();
99186d7f5d3SJohn Marino
99286d7f5d3SJohn Marino newroot->original = xstrdup(dir);
99386d7f5d3SJohn Marino newroot->method = local_method;
99486d7f5d3SJohn Marino newroot->directory = xstrdup(dir);
99586d7f5d3SJohn Marino /* Here and parse_cvsroot() should be the only places this needs to be
99686d7f5d3SJohn Marino * called on a CVSROOT now. cvsroot->original is saved for error messages
99786d7f5d3SJohn Marino * and, otherwise, we want no trailing slashes.
99886d7f5d3SJohn Marino */
99986d7f5d3SJohn Marino Sanitize_Repository_Name (newroot->directory);
100086d7f5d3SJohn Marino
100186d7f5d3SJohn Marino #ifdef PROXY_SUPPORT
100286d7f5d3SJohn Marino /* Translate the directory to a local one in the case that we are
100386d7f5d3SJohn Marino * configured as a secondary. If root_allow has not been initialized,
100486d7f5d3SJohn Marino * nothing happens.
100586d7f5d3SJohn Marino */
100686d7f5d3SJohn Marino get_local_root_dir_done = false;
100786d7f5d3SJohn Marino walklist (root_allow, get_local_root_dir, &newroot->directory);
100886d7f5d3SJohn Marino #endif /* PROXY_SUPPORT */
100986d7f5d3SJohn Marino
101086d7f5d3SJohn Marino return newroot;
101186d7f5d3SJohn Marino }
101286d7f5d3SJohn Marino
101386d7f5d3SJohn Marino
101486d7f5d3SJohn Marino
101586d7f5d3SJohn Marino #ifdef DEBUG
101686d7f5d3SJohn Marino /* This is for testing the parsing function. Use
101786d7f5d3SJohn Marino
101886d7f5d3SJohn Marino gcc -I. -I.. -I../lib -DDEBUG root.c -o root
101986d7f5d3SJohn Marino
102086d7f5d3SJohn Marino to compile. */
102186d7f5d3SJohn Marino
102286d7f5d3SJohn Marino #include <stdio.h>
102386d7f5d3SJohn Marino
102486d7f5d3SJohn Marino char *program_name = "testing";
102586d7f5d3SJohn Marino char *cvs_cmd_name = "parse_cvsroot"; /* XXX is this used??? */
102686d7f5d3SJohn Marino
102786d7f5d3SJohn Marino void
main(int argc,char * argv[])102886d7f5d3SJohn Marino main (int argc, char *argv[])
102986d7f5d3SJohn Marino {
103086d7f5d3SJohn Marino program_name = argv[0];
103186d7f5d3SJohn Marino
103286d7f5d3SJohn Marino if (argc != 2)
103386d7f5d3SJohn Marino {
103486d7f5d3SJohn Marino fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
103586d7f5d3SJohn Marino exit (2);
103686d7f5d3SJohn Marino }
103786d7f5d3SJohn Marino
103886d7f5d3SJohn Marino if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
103986d7f5d3SJohn Marino {
104086d7f5d3SJohn Marino fprintf (stderr, "%s: Parsing failed.\n", program_name);
104186d7f5d3SJohn Marino exit (1);
104286d7f5d3SJohn Marino }
104386d7f5d3SJohn Marino printf ("CVSroot: %s\n", argv[1]);
104486d7f5d3SJohn Marino printf ("current_parsed_root->method: %s\n",
104586d7f5d3SJohn Marino method_names[current_parsed_root->method]);
104686d7f5d3SJohn Marino printf ("current_parsed_root->username: %s\n",
104786d7f5d3SJohn Marino current_parsed_root->username
104886d7f5d3SJohn Marino ? current_parsed_root->username : "NULL");
104986d7f5d3SJohn Marino printf ("current_parsed_root->hostname: %s\n",
105086d7f5d3SJohn Marino current_parsed_root->hostname
105186d7f5d3SJohn Marino ? current_parsed_root->hostname : "NULL");
105286d7f5d3SJohn Marino printf ("current_parsed_root->directory: %s\n",
105386d7f5d3SJohn Marino current_parsed_root->directory);
105486d7f5d3SJohn Marino
105586d7f5d3SJohn Marino exit (0);
105686d7f5d3SJohn Marino /* NOTREACHED */
105786d7f5d3SJohn Marino }
105886d7f5d3SJohn Marino #endif
1059