#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 (dir, update_dir)
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;
if (update_dir && *update_dir)
xupdate_dir = update_dir;
else
xupdate_dir = ".";
if (dir != NULL)
{
cvsadm = xmalloc (strlen (dir) + sizeof (CVSADM) + 10);
(void) sprintf (cvsadm, "%s/%s", dir, CVSADM);
tmp = xmalloc (strlen (dir) + sizeof (CVSADM_ROOT) + 10);
(void) sprintf (tmp, "%s/%s", dir, CVSADM_ROOT);
}
else
{
cvsadm = xstrdup (CVSADM);
tmp = xstrdup (CVSADM_ROOT);
}
if ((!isdir (cvsadm)) || (!isreadable (tmp)))
{
ret = NULL;
goto out;
}
fpin = open_file (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 (
#ifdef CLIENT_SUPPORT
!ret->isremote &&
#endif
!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);
free_cvsroot_t (ret);
ret = NULL;
goto out;
}
out:
free (cvsadm);
free (tmp);
if (root != NULL)
free (root);
return ret;
}
void
Create_Root (dir, rootdir)
const char *dir;
const char *rootdir;
{
FILE *fout;
char *tmp;
if (noexec)
return;
if (rootdir != NULL)
{
if (dir != NULL)
{
tmp = xmalloc (strlen (dir) + sizeof (CVSADM_ROOT) + 10);
(void) sprintf (tmp, "%s/%s", dir, CVSADM_ROOT);
}
else
tmp = xstrdup (CVSADM_ROOT);
fout = open_file (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
static int root_allow_count;
static char **root_allow_vector;
static int root_allow_size;
void
root_allow_add (arg)
char *arg;
{
char *p;
if (root_allow_size <= root_allow_count)
{
if (root_allow_size == 0)
{
root_allow_size = 1;
root_allow_vector =
(char **) xmalloc (root_allow_size * sizeof (char *));
}
else
{
root_allow_size *= 2;
root_allow_vector =
(char **) xrealloc (root_allow_vector,
root_allow_size * sizeof (char *));
}
if (root_allow_vector == NULL)
{
no_memory:
printf ("E Fatal server error, aborting.\n\
error ENOMEM Virtual memory exhausted.\n");
error_exit ();
}
}
p = xmalloc (strlen (arg) + 1);
if (p == NULL)
goto no_memory;
strcpy (p, arg);
root_allow_vector[root_allow_count++] = p;
}
void
root_allow_free ()
{
if (root_allow_vector != NULL)
free_names (&root_allow_count, root_allow_vector);
root_allow_size = 0;
}
int
root_allow_ok (arg)
char *arg;
{
int i;
if (root_allow_count == 0)
{
printf ("\
error 0 Server configuration missing --allow-root in inetd.conf\n");
error_exit ();
}
for (i = 0; i < root_allow_count; ++i)
if (strcmp (root_allow_vector[i], arg) == 0)
return 1;
return 0;
}
char *CVSroot_cmdline;
cvsroot_t *current_parsed_root = NULL;
static cvsroot_t *
new_cvsroot_t ()
{
cvsroot_t *newroot;
newroot = xmalloc(sizeof(cvsroot_t));
newroot->original = NULL;
newroot->method = null_method;
#ifdef CLIENT_SUPPORT
newroot->username = NULL;
newroot->password = NULL;
newroot->hostname = NULL;
newroot->port = 0;
newroot->directory = NULL;
newroot->proxy_hostname = NULL;
newroot->proxy_port = 0;
newroot->isremote = 0;
#endif
return newroot;
}
void
free_cvsroot_t (root)
cvsroot_t *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->proxy_hostname != NULL)
free (root->proxy_hostname);
#endif
free (root);
}
cvsroot_t *
parse_cvsroot (root_in)
const char *root_in;
{
cvsroot_t *newroot;
char *cvsroot_save;
char *firstslash;
char *cvsroot_copy, *p, *q;
#ifdef CLIENT_SUPPORT
int check_hostname, no_port, no_password;
#endif
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;
#ifdef CLIENT_SUPPORT
if (p = strchr (method, ';'))
{
*p++ = '\0';
if (!really_quiet)
{
error (0, 0,
"WARNING: Ignoring method options found in CVSROOT: `%s'.",
p);
error (0, 0,
"Use CVS version 1.12.7 or later to handle method options.");
}
}
#endif
if (strcmp (method, "local") == 0)
newroot->method = local_method;
else if (strcmp (method, "pserver") == 0)
newroot->method = pserver_method;
else if (strcmp (method, "kserver") == 0)
newroot->method = kserver_method;
else if (strcmp (method, "gserver") == 0)
newroot->method = gserver_method;
else if (strcmp (method, "server") == 0)
newroot->method = server_method;
else if (strcmp (method, "ext") == 0)
newroot->method = ext_method;
else if (strcmp (method, "fork") == 0)
newroot->method = fork_method;
else
{
error (0, 0, "Unknown method (`%s') in CVSROOT.", method);
goto error_exit;
}
}
else
{
newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
? ext_method
: local_method);
}
#ifdef CLIENT_SUPPORT
newroot->isremote = (newroot->method != local_method);
#endif
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';
#ifdef CLIENT_SUPPORT
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 (DEBUG)
if (newroot->method != local_method)
{
error (0, 0, "CVSROOT is set for a remote access method but your");
error (0, 0, "CVS executable doesn't support it.");
goto error_exit;
}
#endif
#if ! defined (SERVER_SUPPORT) && ! defined (DEBUG)
if (newroot->method == fork_method)
{
error (0, 0, "CVSROOT is set to use the :fork: access method but your");
error (0, 0, "CVS executable doesn't support it.");
goto error_exit;
}
#endif
#ifdef CLIENT_SUPPORT
if (newroot->username && ! newroot->hostname)
{
error (0, 0, "Missing hostname in CVSROOT.");
goto error_exit;
}
check_hostname = 0;
no_password = 1;
no_port = 0;
#endif
switch (newroot->method)
{
case local_method:
#ifdef CLIENT_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;
}
no_port = 1;
#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;
}
break;
#ifdef CLIENT_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:
# ifndef HAVE_KERBEROS
error (0, 0, "CVSROOT is set for a kerberos access method but your");
error (0, 0, "CVS executable doesn't support it.");
goto error_exit;
# else
check_hostname = 1;
break;
# endif
case gserver_method:
# ifndef HAVE_GSSAPI
error (0, 0, "CVSROOT is set for a GSSAPI access method but your");
error (0, 0, "CVS executable doesn't support it.");
goto error_exit;
# else
check_hostname = 1;
break;
# endif
case server_method:
case ext_method:
no_port = 1;
check_hostname = 1;
break;
case pserver_method:
no_password = 0;
check_hostname = 1;
break;
#endif
default:
error (1, 0, "Invalid method found in parse_cvsroot");
}
#ifdef CLIENT_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 (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);
return newroot;
error_exit:
free (cvsroot_save);
free_cvsroot_t (newroot);
return NULL;
}
#ifdef AUTH_CLIENT_SUPPORT
char *
normalize_cvsroot (root)
const cvsroot_t *root;
{
char *cvsroot_canonical;
char *p, *hostname, *username;
char port_s[64];
assert (root && root->hostname && root->directory);
sprintf (port_s, "%d", get_cvs_port_number (root));
p = hostname = xstrdup(root->hostname);
while (*p)
{
*p = tolower(*p);
p++;
}
username = root->username ? root->username : getcaller();
cvsroot_canonical = xmalloc ( strlen(username)
+ strlen(hostname) + strlen(port_s)
+ strlen(root->directory) + 12);
sprintf (cvsroot_canonical, ":pserver:%s@%s:%s%s",
username, hostname, port_s, root->directory);
free (hostname);
return cvsroot_canonical;
}
#endif
cvsroot_t *
local_cvsroot (dir)
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 );
return newroot;
}
#ifdef DEBUG
#include <stdio.h>
char *program_name = "testing";
char *cvs_cmd_name = "parse_cvsroot";
void
error_exit PROTO ((void))
{
exit (1);
}
int
isabsolute (dir)
const char *dir;
{
return (dir && (*dir == '/'));
}
void
main (argc, argv)
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