#include "cvs.h"
#include <assert.h>
#include "getline.h"
const char method_names[][16] = {
"undefined", "local", "server (rsh)", "pserver",
"kserver", "gserver", "ext", "fork"
};
#ifndef DEBUG
cvsroot_t *
Name_Root (const char *dir, const char *update_dir)
{
FILE *fpin;
cvsroot_t *ret;
const char *xupdate_dir;
char *root = NULL;
size_t root_allocated = 0;
char *tmp;
char *cvsadm;
char *cp;
int len;
TRACE (TRACE_FLOW, "Name_Root (%s, %s)",
dir ? dir : "(null)",
update_dir ? update_dir : "(null)");
if (update_dir && *update_dir)
xupdate_dir = update_dir;
else
xupdate_dir = ".";
if (dir != NULL)
{
cvsadm = Xasprintf ("%s/%s", dir, CVSADM);
tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
}
else
{
cvsadm = xstrdup (CVSADM);
tmp = xstrdup (CVSADM_ROOT);
}
if ((!isdir (cvsadm)) || (!isreadable (tmp)))
{
ret = NULL;
goto out;
}
fpin = xfopen (tmp, "r");
if ((len = getline (&root, &root_allocated, fpin)) < 0)
{
int saved_errno = errno;
error (0, 0, "in directory %s:", xupdate_dir);
error (0, saved_errno, "cannot read %s", CVSADM_ROOT);
error (0, 0, "please correct this problem");
ret = NULL;
goto out;
}
fclose (fpin);
cp = root + len - 1;
if (*cp == '\n')
*cp = '\0';
ret = parse_cvsroot (root);
if (ret == NULL)
{
error (0, 0, "in directory %s:", xupdate_dir);
error (0, 0,
"ignoring %s because it does not contain a valid root.",
CVSADM_ROOT);
goto out;
}
if (!ret->isremote && !isdir (ret->directory))
{
error (0, 0, "in directory %s:", xupdate_dir);
error (0, 0,
"ignoring %s because it specifies a non-existent repository %s",
CVSADM_ROOT, root);
ret = NULL;
goto out;
}
out:
free (cvsadm);
free (tmp);
if (root != NULL)
free (root);
return ret;
}
void
Create_Root (const char *dir, const char *rootdir)
{
FILE *fout;
char *tmp;
if (noexec)
return;
if (rootdir != NULL)
{
if (dir != NULL)
tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
else
tmp = xstrdup (CVSADM_ROOT);
fout = xfopen (tmp, "w+");
if (fprintf (fout, "%s\n", rootdir) < 0)
error (1, errno, "write to %s failed", tmp);
if (fclose (fout) == EOF)
error (1, errno, "cannot close %s", tmp);
free (tmp);
}
}
#endif
const char *
primary_root_translate (const char *root_in)
{
#ifdef PROXY_SUPPORT
char *translated;
static char *previous = NULL;
static size_t len;
if (!config) return root_in;
if (config->PrimaryServer
&& !strncmp (root_in, config->PrimaryServer->directory,
strlen (config->PrimaryServer->directory))
&& (ISSLASH (root_in[strlen (config->PrimaryServer->directory)])
|| root_in[strlen (config->PrimaryServer->directory)] == '\0')
)
{
translated =
Xasnprintf (previous, &len,
"%s%s", current_parsed_root->directory,
root_in + strlen (config->PrimaryServer->directory));
if (previous && previous != translated)
free (previous);
return previous = translated;
}
#endif
return root_in;
}
const char *
primary_root_inverse_translate (const char *root_in)
{
#ifdef PROXY_SUPPORT
char *translated;
static char *previous = NULL;
static size_t len;
if (!config) return root_in;
if (config->PrimaryServer
&& !strncmp (root_in, current_parsed_root->directory,
strlen (current_parsed_root->directory))
&& (ISSLASH (root_in[strlen (current_parsed_root->directory)])
|| root_in[strlen (current_parsed_root->directory)] == '\0')
)
{
translated =
Xasnprintf (previous, &len,
"%s%s", config->PrimaryServer->directory,
root_in + strlen (current_parsed_root->directory));
if (previous && previous != translated)
free (previous);
return previous = translated;
}
#endif
return root_in;
}
static List *root_allow;
static void
delconfig (Node *n)
{
if (n->data) free_config (n->data);
}
void
root_allow_add (const char *arg, const char *configPath)
{
Node *n;
if (!root_allow) root_allow = getlist();
n = getnode();
n->key = xstrdup (arg);
n->data = parse_config (arg, configPath);
n->delproc = delconfig;
addnode (root_allow, n);
}
void
root_allow_free (void)
{
dellist (&root_allow);
}
bool
root_allow_ok (const char *arg)
{
if (!root_allow)
{
printf ("\
error 0 Server configuration missing --allow-root in inetd.conf\n");
exit (EXIT_FAILURE);
}
if (findnode (root_allow, arg))
return true;
return false;
}
struct config *
get_root_allow_config (const char *arg, const char *configPath)
{
Node *n;
TRACE (TRACE_FUNCTION, "get_root_allow_config (%s)", arg);
if (root_allow)
n = findnode (root_allow, arg);
else
n = NULL;
if (n) return n->data;
return parse_config (arg, configPath);
}
char *CVSroot_cmdline;
cvsroot_t *current_parsed_root = NULL;
const cvsroot_t *original_parsed_root;
static cvsroot_t *new_cvsroot_t(void)
__attribute__( (__malloc__) );
static cvsroot_t *
new_cvsroot_t (void)
{
cvsroot_t *newroot;
newroot = xmalloc(sizeof(cvsroot_t));
newroot->original = NULL;
newroot->directory = NULL;
newroot->method = null_method;
newroot->isremote = false;
#ifdef CLIENT_SUPPORT
newroot->username = NULL;
newroot->password = NULL;
newroot->hostname = NULL;
newroot->cvs_rsh = NULL;
newroot->cvs_server = NULL;
newroot->port = 0;
newroot->proxy_hostname = NULL;
newroot->proxy_port = 0;
newroot->redirect = true;
#endif
return newroot;
}
static void
free_cvsroot_t (cvsroot_t *root)
{
assert (root);
if (root->original != NULL)
free (root->original);
if (root->directory != NULL)
free (root->directory);
#ifdef CLIENT_SUPPORT
if (root->username != NULL)
free (root->username);
if (root->password != NULL)
{
memset (root->password, 0, strlen (root->password));
free (root->password);
}
if (root->hostname != NULL)
free (root->hostname);
if (root->cvs_rsh != NULL)
free (root->cvs_rsh);
if (root->cvs_server != NULL)
free (root->cvs_server);
if (root->proxy_hostname != NULL)
free (root->proxy_hostname);
#endif
free (root);
}
cvsroot_t *
parse_cvsroot (const char *root_in)
{
cvsroot_t *newroot;
char *cvsroot_save;
char *cvsroot_copy, *p;
#if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
char *q;
char *firstslash;
int check_hostname, no_port, no_password, no_proxy;
#endif
static List *cache = NULL;
Node *node;
assert (root_in != NULL);
TRACE (TRACE_FLOW, "parse_cvsroot (%s)", root_in);
if ((node = findnode (cache, root_in)))
return node->data;
assert (root_in);
newroot = new_cvsroot_t();
newroot->original = xstrdup (root_in);
cvsroot_save = cvsroot_copy = xstrdup (root_in);
if (*cvsroot_copy == ':')
{
char *method = ++cvsroot_copy;
if (! (p = strchr (method, ':')))
{
error (0, 0, "No closing `:' on method in CVSROOT.");
goto error_exit;
}
*p = '\0';
cvsroot_copy = ++p;
#if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
method = strtok (method, ";");
if (!method)
method = "";
#endif
if (!strcasecmp (method, "local"))
newroot->method = local_method;
else if (!strcasecmp (method, "pserver"))
newroot->method = pserver_method;
else if (!strcasecmp (method, "kserver"))
newroot->method = kserver_method;
else if (!strcasecmp (method, "gserver"))
newroot->method = gserver_method;
else if (!strcasecmp (method, "server"))
newroot->method = server_method;
else if (!strcasecmp (method, "ext"))
newroot->method = ext_method;
else if (!strcasecmp (method, "fork"))
newroot->method = fork_method;
else
{
error (0, 0, "Unknown method (`%s') in CVSROOT.", method);
goto error_exit;
}
#if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
while ((p = strtok (NULL, ";")))
{
char *q = strchr (p, '=');
if (q == NULL)
{
error (0, 0, "Option (`%s') has no argument in CVSROOT.",
p);
goto error_exit;
}
*q++ = '\0';
TRACE (TRACE_DATA, "CVSROOT option=`%s' value=`%s'", p, q);
if (!strcasecmp (p, "proxy"))
{
newroot->proxy_hostname = xstrdup (q);
}
else if (!strcasecmp (p, "proxyport"))
{
char *r = q;
if (*r == '-') r++;
while (*r)
{
if (!isdigit(*r++))
{
error (0, 0,
"CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
q);
goto error_exit;
}
}
if ((newroot->proxy_port = atoi (q)) <= 0)
error (0, 0,
"CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
q);
}
else if (!strcasecmp (p, "CVS_RSH"))
{
if (newroot->method == ext_method)
newroot->cvs_rsh = xstrdup (q);
}
else if (!strcasecmp (p, "CVS_SERVER"))
{
if (newroot->method == ext_method
|| newroot->method == fork_method)
newroot->cvs_server = xstrdup (q);
}
else if (!strcasecmp (p, "Redirect"))
readBool ("CVSROOT", "Redirect", q, &newroot->redirect);
else
{
error (0, 0, "Unknown option (`%s') in CVSROOT.", p);
goto error_exit;
}
}
#endif
}
else
{
newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
? ext_method
: local_method);
}
newroot->isremote = (newroot->method != local_method);
#if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
if (readonlyfs && newroot->isremote)
error (1, 0,
"Read-only repository feature unavailable with remote roots (cvsroot = %s)",
cvsroot_copy);
if ((newroot->method != local_method)
&& (newroot->method != fork_method)
)
{
if ((p = strchr (cvsroot_copy, '/')) == NULL)
{
error (0, 0, "CVSROOT requires a path spec:");
error (0, 0,
":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
goto error_exit;
}
firstslash = p;
*p = '\0';
if ((p = strchr (cvsroot_copy, '@')) != NULL)
{
*p = '\0';
if ((q = strchr (cvsroot_copy, ':')) != NULL)
{
*q = '\0';
newroot->password = xstrdup (++q);
}
if (*cvsroot_copy != '\0')
newroot->username = xstrdup (cvsroot_copy);
cvsroot_copy = ++p;
}
if ((p = strchr (cvsroot_copy, ':')) != NULL)
{
*p++ = '\0';
if (strlen(p))
{
q = p;
if (*q == '-') q++;
while (*q)
{
if (!isdigit(*q++))
{
error (0, 0,
"CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
p);
error (0, 0,
"Perhaps you entered a relative pathname?");
goto error_exit;
}
}
if ((newroot->port = atoi (p)) <= 0)
{
error (0, 0,
"CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
p);
error (0, 0, "Perhaps you entered a relative pathname?");
goto error_exit;
}
}
}
if (*cvsroot_copy != '\0')
newroot->hostname = xstrdup (cvsroot_copy);
cvsroot_copy = firstslash;
*cvsroot_copy = '/';
}
#endif
Sanitize_Repository_Name (cvsroot_copy);
newroot->directory = xstrdup (cvsroot_copy);
#if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
if (newroot->username && ! newroot->hostname)
{
error (0, 0, "Missing hostname in CVSROOT.");
goto error_exit;
}
check_hostname = 0;
no_password = 1;
no_proxy = 1;
no_port = 0;
#endif
switch (newroot->method)
{
case local_method:
#if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
if (newroot->username || newroot->hostname)
{
error (0, 0, "Can't specify hostname and username in CVSROOT");
error (0, 0, "when using local access method.");
goto error_exit;
}
#endif
if (!ISABSOLUTE (newroot->directory))
{
error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
newroot->directory);
error (0, 0, "when using local access method.");
goto error_exit;
}
#if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
no_port = 1;
#endif
break;
#if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
case fork_method:
if (newroot->username || newroot->hostname)
{
error (0, 0, "Can't specify hostname and username in CVSROOT");
error (0, 0, "when using fork access method.");
goto error_exit;
}
newroot->hostname = xstrdup("server");
if (!ISABSOLUTE (newroot->directory))
{
error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
newroot->directory);
error (0, 0, "when using fork access method.");
goto error_exit;
}
no_port = 1;
break;
case kserver_method:
check_hostname = 1;
break;
case gserver_method:
check_hostname = 1;
no_proxy = 0;
break;
case server_method:
case ext_method:
no_port = 1;
check_hostname = 1;
break;
case pserver_method:
no_password = 0;
no_proxy = 0;
check_hostname = 1;
break;
#endif
default:
error (1, 0, "Invalid method found in parse_cvsroot");
}
#if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
if (no_password && newroot->password)
{
error (0, 0, "CVSROOT password specification is only valid for");
error (0, 0, "pserver connection method.");
goto error_exit;
}
if (no_proxy && (newroot->proxy_hostname || newroot->proxy_port))
{
error (0, 0,
"CVSROOT proxy specification is only valid for gserver and");
error (0, 0, "pserver connection methods.");
goto error_exit;
}
if (!newroot->proxy_hostname && newroot->proxy_port)
{
error (0, 0, "Proxy port specified in CVSROOT without proxy host.");
goto error_exit;
}
if (check_hostname && !newroot->hostname)
{
error (0, 0, "Didn't specify hostname in CVSROOT.");
goto error_exit;
}
if (no_port && newroot->port)
{
error (0, 0,
"CVSROOT port specification is only valid for gserver, kserver,");
error (0, 0, "and pserver connection methods.");
goto error_exit;
}
#endif
if (*newroot->directory == '\0')
{
error (0, 0, "Missing directory in CVSROOT.");
goto error_exit;
}
free (cvsroot_save);
if (!cache) cache = getlist();
node = getnode();
node->key = xstrdup (newroot->original);
node->data = newroot;
addnode (cache, node);
return newroot;
error_exit:
free (cvsroot_save);
free_cvsroot_t (newroot);
return NULL;
}
#ifdef AUTH_CLIENT_SUPPORT
char *
normalize_cvsroot (const cvsroot_t *root)
{
char *cvsroot_canonical;
char *p, *hostname;
assert (root && root->hostname && root->directory);
p = hostname = xstrdup (root->hostname);
while (*p)
{
*p = tolower (*p);
p++;
}
cvsroot_canonical = Xasprintf (":pserver:%s@%s:%d%s",
root->username ? root->username
: getcaller(),
hostname, get_cvs_port_number (root),
root->directory);
free (hostname);
return cvsroot_canonical;
}
#endif
#ifdef PROXY_SUPPORT
static bool get_local_root_dir_done;
static int
get_local_root_dir (Node *p, void *root_in)
{
struct config *c = p->data;
char **r = root_in;
if (get_local_root_dir_done)
return 0;
if (c->PrimaryServer && !strcmp (*r, c->PrimaryServer->directory))
{
free (*r);
*r = xstrdup (p->key);
get_local_root_dir_done = true;
}
return 0;
}
#endif
cvsroot_t *
local_cvsroot (const char *dir)
{
cvsroot_t *newroot = new_cvsroot_t();
newroot->original = xstrdup(dir);
newroot->method = local_method;
newroot->directory = xstrdup(dir);
Sanitize_Repository_Name (newroot->directory);
#ifdef PROXY_SUPPORT
get_local_root_dir_done = false;
walklist (root_allow, get_local_root_dir, &newroot->directory);
#endif
return newroot;
}
#ifdef DEBUG
#include <stdio.h>
char *program_name = "testing";
char *cvs_cmd_name = "parse_cvsroot";
void
main (int argc, char *argv[])
{
program_name = argv[0];
if (argc != 2)
{
fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
exit (2);
}
if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
{
fprintf (stderr, "%s: Parsing failed.\n", program_name);
exit (1);
}
printf ("CVSroot: %s\n", argv[1]);
printf ("current_parsed_root->method: %s\n",
method_names[current_parsed_root->method]);
printf ("current_parsed_root->username: %s\n",
current_parsed_root->username
? current_parsed_root->username : "NULL");
printf ("current_parsed_root->hostname: %s\n",
current_parsed_root->hostname
? current_parsed_root->hostname : "NULL");
printf ("current_parsed_root->directory: %s\n",
current_parsed_root->directory);
exit (0);
}
#endif