Actually hook powernow.4 into the build.
[dragonfly.git] / contrib / cvs-1.12 / src / root.c
blob856dbb3d961324a0a357f48163518da08e901d24
1 /*
2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
5 * and others.
7 * Poritons Copyright (c) 1992, Mark D. Baushke
9 * You may distribute under the terms of the GNU General Public License as
10 * specified in the README file that comes with the CVS source distribution.
12 * Name of Root
14 * Determine the path to the CVSROOT and set "Root" accordingly.
17 #include "cvs.h"
18 #include <assert.h>
19 #include "getline.h"
21 /* Printable names for things in the current_parsed_root->method enum variable.
22 Watch out if the enum is changed in cvs.h! */
24 const char method_names[][16] = {
25 "undefined", "local", "server (rsh)", "pserver",
26 "kserver", "gserver", "ext", "fork"
29 #ifndef DEBUG
31 cvsroot_t *
32 Name_Root (const char *dir, const char *update_dir)
34 FILE *fpin;
35 cvsroot_t *ret;
36 const char *xupdate_dir;
37 char *root = NULL;
38 size_t root_allocated = 0;
39 char *tmp;
40 char *cvsadm;
41 char *cp;
42 int len;
44 TRACE (TRACE_FLOW, "Name_Root (%s, %s)",
45 dir ? dir : "(null)",
46 update_dir ? update_dir : "(null)");
48 if (update_dir && *update_dir)
49 xupdate_dir = update_dir;
50 else
51 xupdate_dir = ".";
53 if (dir != NULL)
55 cvsadm = Xasprintf ("%s/%s", dir, CVSADM);
56 tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
58 else
60 cvsadm = xstrdup (CVSADM);
61 tmp = xstrdup (CVSADM_ROOT);
65 * Do not bother looking for a readable file if there is no cvsadm
66 * directory present.
68 * It is possible that not all repositories will have a CVS/Root
69 * file. This is ok, but the user will need to specify -d
70 * /path/name or have the environment variable CVSROOT set in
71 * order to continue. */
72 if ((!isdir (cvsadm)) || (!isreadable (tmp)))
74 ret = NULL;
75 goto out;
79 * The assumption here is that the CVS Root is always contained in the
80 * first line of the "Root" file.
82 fpin = xfopen (tmp, "r");
84 if ((len = getline (&root, &root_allocated, fpin)) < 0)
86 int saved_errno = errno;
87 /* FIXME: should be checking for end of file separately; errno
88 is not set in that case. */
89 error (0, 0, "in directory %s:", xupdate_dir);
90 error (0, saved_errno, "cannot read %s", CVSADM_ROOT);
91 error (0, 0, "please correct this problem");
92 ret = NULL;
93 goto out;
95 fclose (fpin);
96 cp = root + len - 1;
97 if (*cp == '\n')
98 *cp = '\0'; /* strip the newline */
101 * root now contains a candidate for CVSroot. It must be an
102 * absolute pathname or specify a remote server.
105 ret = parse_cvsroot (root);
106 if (ret == NULL)
108 error (0, 0, "in directory %s:", xupdate_dir);
109 error (0, 0,
110 "ignoring %s because it does not contain a valid root.",
111 CVSADM_ROOT);
112 goto out;
115 if (!ret->isremote && !isdir (ret->directory))
117 error (0, 0, "in directory %s:", xupdate_dir);
118 error (0, 0,
119 "ignoring %s because it specifies a non-existent repository %s",
120 CVSADM_ROOT, root);
121 ret = NULL;
122 goto out;
126 out:
127 free (cvsadm);
128 free (tmp);
129 if (root != NULL)
130 free (root);
131 return ret;
137 * Write the CVS/Root file so that the environment variable CVSROOT
138 * and/or the -d option to cvs will be validated or not necessary for
139 * future work.
141 void
142 Create_Root (const char *dir, const char *rootdir)
144 FILE *fout;
145 char *tmp;
147 if (noexec)
148 return;
150 /* record the current cvs root */
152 if (rootdir != NULL)
154 if (dir != NULL)
155 tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
156 else
157 tmp = xstrdup (CVSADM_ROOT);
159 fout = xfopen (tmp, "w+");
160 if (fprintf (fout, "%s\n", rootdir) < 0)
161 error (1, errno, "write to %s failed", tmp);
162 if (fclose (fout) == EOF)
163 error (1, errno, "cannot close %s", tmp);
164 free (tmp);
168 #endif /* ! DEBUG */
172 /* Translate an absolute repository string for a primary server and return it.
174 * INPUTS
175 * root_in The root to be translated.
177 * RETURNS
178 * A translated string this function owns, or a pointer to the original
179 * string passed in if no translation was necessary.
181 * If the returned string is the translated one, it may be overwritten
182 * by the next call to this function.
184 const char *
185 primary_root_translate (const char *root_in)
187 #ifdef PROXY_SUPPORT
188 char *translated;
189 static char *previous = NULL;
190 static size_t len;
192 /* This can happen, for instance, during `cvs init'. */
193 if (!config) return root_in;
195 if (config->PrimaryServer
196 && !strncmp (root_in, config->PrimaryServer->directory,
197 strlen (config->PrimaryServer->directory))
198 && (ISSLASH (root_in[strlen (config->PrimaryServer->directory)])
199 || root_in[strlen (config->PrimaryServer->directory)] == '\0')
202 translated =
203 Xasnprintf (previous, &len,
204 "%s%s", current_parsed_root->directory,
205 root_in + strlen (config->PrimaryServer->directory));
206 if (previous && previous != translated)
207 free (previous);
208 return previous = translated;
210 #endif
212 /* There is no primary root configured or it didn't match. */
213 return root_in;
218 /* Translate a primary root in reverse for PATHNAMEs in responses.
220 * INPUTS
221 * root_in The root to be translated.
223 * RETURNS
224 * A translated string this function owns, or a pointer to the original
225 * string passed in if no translation was necessary.
227 * If the returned string is the translated one, it may be overwritten
228 * by the next call to this function.
230 const char *
231 primary_root_inverse_translate (const char *root_in)
233 #ifdef PROXY_SUPPORT
234 char *translated;
235 static char *previous = NULL;
236 static size_t len;
238 /* This can happen, for instance, during `cvs init'. */
239 if (!config) return root_in;
241 if (config->PrimaryServer
242 && !strncmp (root_in, current_parsed_root->directory,
243 strlen (current_parsed_root->directory))
244 && (ISSLASH (root_in[strlen (current_parsed_root->directory)])
245 || root_in[strlen (current_parsed_root->directory)] == '\0')
248 translated =
249 Xasnprintf (previous, &len,
250 "%s%s", config->PrimaryServer->directory,
251 root_in + strlen (current_parsed_root->directory));
252 if (previous && previous != translated)
253 free (previous);
254 return previous = translated;
256 #endif
258 /* There is no primary root configured or it didn't match. */
259 return root_in;
264 /* The root_allow_* stuff maintains a list of valid CVSROOT
265 directories. Then we can check against them when a remote user
266 hands us a CVSROOT directory. */
267 static List *root_allow;
269 static void
270 delconfig (Node *n)
272 if (n->data) free_config (n->data);
277 void
278 root_allow_add (const char *arg, const char *configPath)
280 Node *n;
282 if (!root_allow) root_allow = getlist();
283 n = getnode();
284 n->key = xstrdup (arg);
285 n->data = parse_config (arg, configPath);
286 n->delproc = delconfig;
287 addnode (root_allow, n);
290 void
291 root_allow_free (void)
293 dellist (&root_allow);
296 bool
297 root_allow_ok (const char *arg)
299 if (!root_allow)
301 /* Probably someone upgraded from CVS before 1.9.10 to 1.9.10
302 or later without reading the documentation about
303 --allow-root. Printing an error here doesn't disclose any
304 particularly useful information to an attacker because a
305 CVS server configured in this way won't let *anyone* in. */
307 /* Note that we are called from a context where we can spit
308 back "error" rather than waiting for the next request which
309 expects responses. */
310 printf ("\
311 error 0 Server configuration missing --allow-root in inetd.conf\n");
312 exit (EXIT_FAILURE);
315 if (findnode (root_allow, arg))
316 return true;
317 return false;
322 /* Get a config we stored in response to root_allow.
324 * RETURNS
325 * The config associated with ARG.
327 struct config *
328 get_root_allow_config (const char *arg, const char *configPath)
330 Node *n;
332 TRACE (TRACE_FUNCTION, "get_root_allow_config (%s)", arg);
334 if (root_allow)
335 n = findnode (root_allow, arg);
336 else
337 n = NULL;
339 if (n) return n->data;
340 return parse_config (arg, configPath);
345 /* This global variable holds the global -d option. It is NULL if -d
346 was not used, which means that we must get the CVSroot information
347 from the CVSROOT environment variable or from a CVS/Root file. */
348 char *CVSroot_cmdline;
352 /* FIXME - Deglobalize this. */
353 cvsroot_t *current_parsed_root = NULL;
354 /* Used to save the original root being processed so that we can still find it
355 * in lists and the like after a `Redirect' response. Also set to mirror
356 * current_parsed_root in server mode so that code which runs on both the
357 * client and server but which wants to use original data on the client can
358 * just always reference the original_parsed_root.
360 const cvsroot_t *original_parsed_root;
363 /* allocate and initialize a cvsroot_t
365 * We must initialize the strings to NULL so we know later what we should
366 * free
368 * Some of the other zeroes remain meaningful as, "never set, use default",
369 * or the like
371 /* Functions which allocate memory are not pure. */
372 static cvsroot_t *new_cvsroot_t(void)
373 __attribute__( (__malloc__) );
374 static cvsroot_t *
375 new_cvsroot_t (void)
377 cvsroot_t *newroot;
379 /* gotta store it somewhere */
380 newroot = xmalloc(sizeof(cvsroot_t));
382 newroot->original = NULL;
383 newroot->directory = NULL;
384 newroot->method = null_method;
385 newroot->isremote = false;
386 #ifdef CLIENT_SUPPORT
387 newroot->username = NULL;
388 newroot->password = NULL;
389 newroot->hostname = NULL;
390 newroot->cvs_rsh = NULL;
391 newroot->cvs_server = NULL;
392 newroot->port = 0;
393 newroot->proxy_hostname = NULL;
394 newroot->proxy_port = 0;
395 newroot->redirect = true; /* Advertise Redirect support */
396 #endif /* CLIENT_SUPPORT */
398 return newroot;
403 /* Dispose of a cvsroot_t and its component parts.
405 * NOTE
406 * It is dangerous for most code to call this function since parse_cvsroot
407 * maintains a cache of parsed roots.
409 static void
410 free_cvsroot_t (cvsroot_t *root)
412 assert (root);
413 if (root->original != NULL)
414 free (root->original);
415 if (root->directory != NULL)
416 free (root->directory);
417 #ifdef CLIENT_SUPPORT
418 if (root->username != NULL)
419 free (root->username);
420 if (root->password != NULL)
422 /* I like to be paranoid */
423 memset (root->password, 0, strlen (root->password));
424 free (root->password);
426 if (root->hostname != NULL)
427 free (root->hostname);
428 if (root->cvs_rsh != NULL)
429 free (root->cvs_rsh);
430 if (root->cvs_server != NULL)
431 free (root->cvs_server);
432 if (root->proxy_hostname != NULL)
433 free (root->proxy_hostname);
434 #endif /* CLIENT_SUPPORT */
435 free (root);
441 * Parse a CVSROOT string to allocate and return a new cvsroot_t structure.
442 * Valid specifications are:
444 * :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path
445 * [:(ext|server):][[user]@]host[:]/path
446 * [:local:[e:]]/path
447 * :fork:/path
449 * INPUTS
450 * root_in C String containing the CVSROOT to be parsed.
452 * RETURNS
453 * A pointer to a newly allocated cvsroot_t structure upon success and
454 * NULL upon failure. The caller should never dispose of this structure,
455 * as it is stored in a cache, but the caller may rely on it not to
456 * change.
458 * NOTES
459 * This would have been a lot easier to write in Perl.
461 * Would it make sense to reimplement the root and config file parsing
462 * gunk in Lex/Yacc?
464 * SEE ALSO
465 * free_cvsroot_t()
467 cvsroot_t *
468 parse_cvsroot (const char *root_in)
470 cvsroot_t *newroot; /* the new root to be returned */
471 char *cvsroot_save; /* what we allocated so we can dispose
472 * it when finished */
473 char *cvsroot_copy, *p; /* temporary pointers for parsing */
474 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
475 char *q; /* temporary pointer for parsing */
476 char *firstslash; /* save where the path spec starts
477 * while we parse
478 * [[user][:password]@]host[:[port]]
480 int check_hostname, no_port, no_password, no_proxy;
481 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
482 static List *cache = NULL;
483 Node *node;
485 assert (root_in != NULL);
487 /* This message is TRACE_FLOW since this function is called repeatedly by
488 * the recursion routines.
490 TRACE (TRACE_FLOW, "parse_cvsroot (%s)", root_in);
492 if ((node = findnode (cache, root_in)))
493 return node->data;
495 assert (root_in);
497 /* allocate some space */
498 newroot = new_cvsroot_t();
500 /* save the original string */
501 newroot->original = xstrdup (root_in);
503 /* and another copy we can munge while parsing */
504 cvsroot_save = cvsroot_copy = xstrdup (root_in);
506 if (*cvsroot_copy == ':')
508 char *method = ++cvsroot_copy;
510 /* Access method specified, as in
511 * "cvs -d :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path",
512 * "cvs -d [:(ext|server):][[user]@]host[:]/path",
513 * "cvs -d :local:e:\path",
514 * "cvs -d :fork:/path".
515 * We need to get past that part of CVSroot before parsing the
516 * rest of it.
519 if (! (p = strchr (method, ':')))
521 error (0, 0, "No closing `:' on method in CVSROOT.");
522 goto error_exit;
524 *p = '\0';
525 cvsroot_copy = ++p;
527 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
528 /* Look for method options, for instance, proxy, proxyport.
529 * Calling strtok again is saved until after parsing the method.
531 method = strtok (method, ";");
532 if (!method)
533 /* Could just exit now, but this keeps the error message in sync.
535 method = "";
536 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
538 /* Now we have an access method -- see if it's valid. */
540 if (!strcasecmp (method, "local"))
541 newroot->method = local_method;
542 else if (!strcasecmp (method, "pserver"))
543 newroot->method = pserver_method;
544 else if (!strcasecmp (method, "kserver"))
545 newroot->method = kserver_method;
546 else if (!strcasecmp (method, "gserver"))
547 newroot->method = gserver_method;
548 else if (!strcasecmp (method, "server"))
549 newroot->method = server_method;
550 else if (!strcasecmp (method, "ext"))
551 newroot->method = ext_method;
552 else if (!strcasecmp (method, "fork"))
553 newroot->method = fork_method;
554 else
556 error (0, 0, "Unknown method (`%s') in CVSROOT.", method);
557 goto error_exit;
560 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
561 /* Parse the method options, for instance, proxy, proxyport */
562 while ((p = strtok (NULL, ";")))
564 char *q = strchr (p, '=');
565 if (q == NULL)
567 error (0, 0, "Option (`%s') has no argument in CVSROOT.",
569 goto error_exit;
572 *q++ = '\0';
573 TRACE (TRACE_DATA, "CVSROOT option=`%s' value=`%s'", p, q);
574 if (!strcasecmp (p, "proxy"))
576 newroot->proxy_hostname = xstrdup (q);
578 else if (!strcasecmp (p, "proxyport"))
580 char *r = q;
581 if (*r == '-') r++;
582 while (*r)
584 if (!isdigit(*r++))
586 error (0, 0,
587 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
589 goto error_exit;
592 if ((newroot->proxy_port = atoi (q)) <= 0)
593 error (0, 0,
594 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
597 else if (!strcasecmp (p, "CVS_RSH"))
599 /* override CVS_RSH environment variable */
600 if (newroot->method == ext_method)
601 newroot->cvs_rsh = xstrdup (q);
603 else if (!strcasecmp (p, "CVS_SERVER"))
605 /* override CVS_SERVER environment variable */
606 if (newroot->method == ext_method
607 || newroot->method == fork_method)
608 newroot->cvs_server = xstrdup (q);
610 else if (!strcasecmp (p, "Redirect"))
611 readBool ("CVSROOT", "Redirect", q, &newroot->redirect);
612 else
614 error (0, 0, "Unknown option (`%s') in CVSROOT.", p);
615 goto error_exit;
618 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
620 else
622 /* If the method isn't specified, assume EXT_METHOD if the string looks
623 like a relative path and LOCAL_METHOD otherwise. */
625 newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
626 ? ext_method
627 : local_method);
631 * There are a few sanity checks we can do now, only knowing the
632 * method of this root.
635 newroot->isremote = (newroot->method != local_method);
637 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
638 if (readonlyfs && newroot->isremote) {
639 /* downgrade readonlyfs settings via environment */
640 if (readonlyfs < 0)
641 error (1, 0,
642 "Read-only repository feature unavailable with remote roots (cvsroot = %s)",
643 cvsroot_copy);
644 readonlyfs = 0;
647 if ((newroot->method != local_method)
648 && (newroot->method != fork_method)
651 /* split the string into [[user][:password]@]host[:[port]] & /path
653 * this will allow some characters such as '@' & ':' to remain unquoted
654 * in the path portion of the spec
656 if ((p = strchr (cvsroot_copy, '/')) == NULL)
658 error (0, 0, "CVSROOT requires a path spec:");
659 error (0, 0,
660 ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
661 error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
662 goto error_exit;
664 firstslash = p; /* == NULL if '/' not in string */
665 *p = '\0';
667 /* Check to see if there is a username[:password] in the string. */
668 if ((p = strchr (cvsroot_copy, '@')) != NULL)
670 *p = '\0';
671 /* check for a password */
672 if ((q = strchr (cvsroot_copy, ':')) != NULL)
674 *q = '\0';
675 newroot->password = xstrdup (++q);
676 /* Don't check for *newroot->password == '\0' since
677 * a user could conceivably wish to specify a blank password
679 * (newroot->password == NULL means to use the
680 * password from .cvspass)
684 /* copy the username */
685 if (*cvsroot_copy != '\0')
686 /* a blank username is impossible, so leave it NULL in that
687 * case so we know to use the default username
689 newroot->username = xstrdup (cvsroot_copy);
691 cvsroot_copy = ++p;
694 /* now deal with host[:[port]] */
696 /* the port */
697 if ((p = strchr (cvsroot_copy, ':')) != NULL)
699 *p++ = '\0';
700 if (strlen(p))
702 q = p;
703 if (*q == '-') q++;
704 while (*q)
706 if (!isdigit(*q++))
708 error (0, 0,
709 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
711 error (0, 0,
712 "Perhaps you entered a relative pathname?");
713 goto error_exit;
716 if ((newroot->port = atoi (p)) <= 0)
718 error (0, 0,
719 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
721 error (0, 0, "Perhaps you entered a relative pathname?");
722 goto error_exit;
727 /* copy host */
728 if (*cvsroot_copy != '\0')
729 /* blank hostnames are invalid, but for now leave the field NULL
730 * and catch the error during the sanity checks later
732 newroot->hostname = xstrdup (cvsroot_copy);
734 /* restore the '/' */
735 cvsroot_copy = firstslash;
736 *cvsroot_copy = '/';
738 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
741 * Parse the path for all methods.
743 /* Here & local_cvsroot() should be the only places this needs to be
744 * called on a CVSROOT now. cvsroot->original is saved for error messages
745 * and, otherwise, we want no trailing slashes.
747 Sanitize_Repository_Name (cvsroot_copy);
748 newroot->directory = xstrdup (cvsroot_copy);
751 * Do various sanity checks.
754 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
755 if (newroot->username && ! newroot->hostname)
757 error (0, 0, "Missing hostname in CVSROOT.");
758 goto error_exit;
761 /* We won't have attempted to parse these without CLIENT_SUPPORT or
762 * SERVER_SUPPORT.
764 check_hostname = 0;
765 no_password = 1;
766 no_proxy = 1;
767 no_port = 0;
768 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
769 switch (newroot->method)
771 case local_method:
772 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
773 if (newroot->username || newroot->hostname)
775 error (0, 0, "Can't specify hostname and username in CVSROOT");
776 error (0, 0, "when using local access method.");
777 goto error_exit;
779 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
780 /* cvs.texinfo has always told people that CVSROOT must be an
781 absolute pathname. Furthermore, attempts to use a relative
782 pathname produced various errors (I couldn't get it to work),
783 so there would seem to be little risk in making this a fatal
784 error. */
785 if (!ISABSOLUTE (newroot->directory))
787 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
788 newroot->directory);
789 error (0, 0, "when using local access method.");
790 goto error_exit;
792 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
793 /* We don't need to check for these in :local: mode, really, since
794 * we shouldn't be able to hit the code above which parses them, but
795 * I'm leaving them here in lieu of assertions.
797 no_port = 1;
798 /* no_password already set */
799 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
800 break;
801 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
802 case fork_method:
803 /* We want :fork: to behave the same as other remote access
804 methods. Therefore, don't check to see that the repository
805 name is absolute -- let the server do it. */
806 if (newroot->username || newroot->hostname)
808 error (0, 0, "Can't specify hostname and username in CVSROOT");
809 error (0, 0, "when using fork access method.");
810 goto error_exit;
812 newroot->hostname = xstrdup("server"); /* for error messages */
813 if (!ISABSOLUTE (newroot->directory))
815 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
816 newroot->directory);
817 error (0, 0, "when using fork access method.");
818 goto error_exit;
820 no_port = 1;
821 /* no_password already set */
822 break;
823 case kserver_method:
824 check_hostname = 1;
825 /* no_password already set */
826 break;
827 case gserver_method:
828 check_hostname = 1;
829 no_proxy = 0;
830 /* no_password already set */
831 break;
832 case server_method:
833 case ext_method:
834 no_port = 1;
835 /* no_password already set */
836 check_hostname = 1;
837 break;
838 case pserver_method:
839 no_password = 0;
840 no_proxy = 0;
841 check_hostname = 1;
842 break;
843 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
844 default:
845 error (1, 0, "Invalid method found in parse_cvsroot");
848 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
849 if (no_password && newroot->password)
851 error (0, 0, "CVSROOT password specification is only valid for");
852 error (0, 0, "pserver connection method.");
853 goto error_exit;
855 if (no_proxy && (newroot->proxy_hostname || newroot->proxy_port))
857 error (0, 0,
858 "CVSROOT proxy specification is only valid for gserver and");
859 error (0, 0, "pserver connection methods.");
860 goto error_exit;
863 if (!newroot->proxy_hostname && newroot->proxy_port)
865 error (0, 0, "Proxy port specified in CVSROOT without proxy host.");
866 goto error_exit;
869 if (check_hostname && !newroot->hostname)
871 error (0, 0, "Didn't specify hostname in CVSROOT.");
872 goto error_exit;
875 if (no_port && newroot->port)
877 error (0, 0,
878 "CVSROOT port specification is only valid for gserver, kserver,");
879 error (0, 0, "and pserver connection methods.");
880 goto error_exit;
882 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
884 if (*newroot->directory == '\0')
886 error (0, 0, "Missing directory in CVSROOT.");
887 goto error_exit;
890 /* Hooray! We finally parsed it! */
891 free (cvsroot_save);
893 if (!cache) cache = getlist();
894 node = getnode();
895 node->key = xstrdup (newroot->original);
896 node->data = newroot;
897 addnode (cache, node);
898 return newroot;
900 error_exit:
901 free (cvsroot_save);
902 free_cvsroot_t (newroot);
903 return NULL;
908 #ifdef AUTH_CLIENT_SUPPORT
909 /* Use root->username, root->hostname, root->port, and root->directory
910 * to create a normalized CVSROOT fit for the .cvspass file
912 * username defaults to the result of getcaller()
913 * port defaults to the result of get_cvs_port_number()
915 * FIXME - we could cache the canonicalized version of a root inside the
916 * cvsroot_t, but we'd have to un'const the input here and stop expecting the
917 * caller to be responsible for our return value
919 * ASSUMPTIONS
920 * ROOT->method == pserver_method
922 char *
923 normalize_cvsroot (const cvsroot_t *root)
925 char *cvsroot_canonical;
926 char *p, *hostname;
928 assert (root && root->hostname && root->directory);
930 /* use a lower case hostname since we know hostnames are case insensitive */
931 /* Some logic says we should be tacking our domain name on too if it isn't
932 * there already, but for now this works. Reverse->Forward lookups are
933 * almost certainly too much since that would make CVS immune to some of
934 * the DNS trickery that makes life easier for sysadmins when they want to
935 * move a repository or the like
937 p = hostname = xstrdup (root->hostname);
938 while (*p)
940 *p = tolower (*p);
941 p++;
944 cvsroot_canonical = Xasprintf (":pserver:%s@%s:%d%s",
945 root->username ? root->username
946 : getcaller(),
947 hostname, get_cvs_port_number (root),
948 root->directory);
950 free (hostname);
951 return cvsroot_canonical;
953 #endif /* AUTH_CLIENT_SUPPORT */
957 #ifdef PROXY_SUPPORT
958 /* A walklist() function to walk the root_allow list looking for a PrimaryServer
959 * configuration with a directory matching the requested directory.
961 * If found, replace it.
963 static bool get_local_root_dir_done;
964 static int
965 get_local_root_dir (Node *p, void *root_in)
967 struct config *c = p->data;
968 char **r = root_in;
970 if (get_local_root_dir_done)
971 return 0;
973 if (c->PrimaryServer && !strcmp (*r, c->PrimaryServer->directory))
975 free (*r);
976 *r = xstrdup (p->key);
977 get_local_root_dir_done = true;
979 return 0;
981 #endif /* PROXY_SUPPORT */
985 /* allocate and return a cvsroot_t structure set up as if we're using the local
986 * repository DIR. */
987 cvsroot_t *
988 local_cvsroot (const char *dir)
990 cvsroot_t *newroot = new_cvsroot_t();
992 newroot->original = xstrdup(dir);
993 newroot->method = local_method;
994 newroot->directory = xstrdup(dir);
995 /* Here and parse_cvsroot() should be the only places this needs to be
996 * called on a CVSROOT now. cvsroot->original is saved for error messages
997 * and, otherwise, we want no trailing slashes.
999 Sanitize_Repository_Name (newroot->directory);
1001 #ifdef PROXY_SUPPORT
1002 /* Translate the directory to a local one in the case that we are
1003 * configured as a secondary. If root_allow has not been initialized,
1004 * nothing happens.
1006 get_local_root_dir_done = false;
1007 walklist (root_allow, get_local_root_dir, &newroot->directory);
1008 #endif /* PROXY_SUPPORT */
1010 return newroot;
1015 #ifdef DEBUG
1016 /* This is for testing the parsing function. Use
1018 gcc -I. -I.. -I../lib -DDEBUG root.c -o root
1020 to compile. */
1022 #include <stdio.h>
1024 char *program_name = "testing";
1025 char *cvs_cmd_name = "parse_cvsroot"; /* XXX is this used??? */
1027 void
1028 main (int argc, char *argv[])
1030 program_name = argv[0];
1032 if (argc != 2)
1034 fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
1035 exit (2);
1038 if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
1040 fprintf (stderr, "%s: Parsing failed.\n", program_name);
1041 exit (1);
1043 printf ("CVSroot: %s\n", argv[1]);
1044 printf ("current_parsed_root->method: %s\n",
1045 method_names[current_parsed_root->method]);
1046 printf ("current_parsed_root->username: %s\n",
1047 current_parsed_root->username
1048 ? current_parsed_root->username : "NULL");
1049 printf ("current_parsed_root->hostname: %s\n",
1050 current_parsed_root->hostname
1051 ? current_parsed_root->hostname : "NULL");
1052 printf ("current_parsed_root->directory: %s\n",
1053 current_parsed_root->directory);
1055 exit (0);
1056 /* NOTREACHED */
1058 #endif