1 /*
2 * Copyright (C) 1986-2005 The Free Software Foundation, Inc.
3 *
4 * Portions Copyright (C) 1998-2005 Derek Price, Ximbiot <http://ximbiot.com>,
5 * and others.
6 *
7 * Poritons Copyright (c) 1992, Mark D. Baushke
8 *
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.
11 *
12 * Name of Root
13 *
14 * Determine the path to the CVSROOT and set "Root" accordingly.
15 */
16
17 #include "cvs.h"
18 #include <assert.h>
19 #include "getline.h"
20
21 __RCSID("$MirOS: src/gnu/usr.bin/cvs/src/root.c,v 1.10 2011/06/11 00:39:38 tg Exp $");
22
23 /* Printable names for things in the current_parsed_root->method enum variable.
24 Watch out if the enum is changed in cvs.h! */
25
26 const char method_names[][16] = {
27 "undefined", "local", "server (rsh)", "pserver",
28 "kserver", "gserver", "ext", "fork"
29 };
30
31 #ifndef DEBUG
32
33 cvsroot_t *
Name_Root(const char * dir,const char * update_dir)34 Name_Root (const char *dir, const char *update_dir)
35 {
36 FILE *fpin;
37 cvsroot_t *ret;
38 const char *xupdate_dir;
39 char *root = NULL;
40 size_t root_allocated = 0;
41 char *tmp;
42 char *cvsadm;
43 char *cp;
44 int len;
45
46 TRACE (TRACE_FLOW, "Name_Root (%s, %s)",
47 dir ? dir : "(null)",
48 update_dir ? update_dir : "(null)");
49
50 if (update_dir && *update_dir)
51 xupdate_dir = update_dir;
52 else
53 xupdate_dir = ".";
54
55 if (dir != NULL)
56 {
57 cvsadm = Xasprintf ("%s/%s", dir, CVSADM);
58 tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
59 }
60 else
61 {
62 cvsadm = xstrdup (CVSADM);
63 tmp = xstrdup (CVSADM_ROOT);
64 }
65
66 /*
67 * Do not bother looking for a readable file if there is no cvsadm
68 * directory present.
69 *
70 * It is possible that not all repositories will have a CVS/Root
71 * file. This is ok, but the user will need to specify -d
72 * /path/name or have the environment variable CVSROOT set in
73 * order to continue. */
74 if ((!isdir (cvsadm)) || (!isreadable (tmp)))
75 {
76 ret = NULL;
77 goto out;
78 }
79
80 /*
81 * The assumption here is that the CVS Root is always contained in the
82 * first line of the "Root" file.
83 */
84 fpin = xfopen (tmp, "r");
85
86 if ((len = getline (&root, &root_allocated, fpin)) < 0)
87 {
88 int saved_errno = errno;
89 /* FIXME: should be checking for end of file separately; errno
90 is not set in that case. */
91 error (0, 0, "in directory %s:", xupdate_dir);
92 error (0, saved_errno, "cannot read %s", CVSADM_ROOT);
93 error (0, 0, "please correct this problem");
94 ret = NULL;
95 goto out;
96 }
97 fclose (fpin);
98 cp = root + len - 1;
99 if (*cp == '\n')
100 *cp = '\0'; /* strip the newline */
101
102 /*
103 * root now contains a candidate for CVSroot. It must be an
104 * absolute pathname or specify a remote server.
105 */
106
107 ret = parse_cvsroot (root);
108 if (ret == NULL)
109 {
110 error (0, 0, "in directory %s:", xupdate_dir);
111 error (0, 0,
112 "ignoring %s because it does not contain a valid root.",
113 CVSADM_ROOT);
114 goto out;
115 }
116
117 if (!ret->isremote && !isdir (ret->directory))
118 {
119 error (0, 0, "in directory %s:", xupdate_dir);
120 error (0, 0,
121 "ignoring %s because it specifies a non-existent repository %s",
122 CVSADM_ROOT, root);
123 ret = NULL;
124 goto out;
125 }
126
127
128 out:
129 free (cvsadm);
130 free (tmp);
131 if (root != NULL)
132 free (root);
133 return ret;
134 }
135
136
137
138 /*
139 * Write the CVS/Root file so that the environment variable CVSROOT
140 * and/or the -d option to cvs will be validated or not necessary for
141 * future work.
142 */
143 void
Create_Root(const char * dir,const char * rootdir)144 Create_Root (const char *dir, const char *rootdir)
145 {
146 FILE *fout;
147 char *tmp;
148
149 if (noexec)
150 return;
151
152 /* record the current cvs root */
153
154 if (rootdir != NULL)
155 {
156 if (dir != NULL)
157 tmp = Xasprintf ("%s/%s", dir, CVSADM_ROOT);
158 else
159 tmp = xstrdup (CVSADM_ROOT);
160
161 fout = xfopen (tmp, "w+");
162 if (fprintf (fout, "%s\n", rootdir) < 0)
163 error (1, errno, "write to %s failed", tmp);
164 if (fclose (fout) == EOF)
165 error (1, errno, "cannot close %s", tmp);
166 free (tmp);
167 }
168 }
169
170 #endif /* ! DEBUG */
171
172
173
174 /* Translate an absolute repository string for a primary server and return it.
175 *
176 * INPUTS
177 * root_in The root to be translated.
178 *
179 * RETURNS
180 * A translated string this function owns, or a pointer to the original
181 * string passed in if no translation was necessary.
182 *
183 * If the returned string is the translated one, it may be overwritten
184 * by the next call to this function.
185 */
186 const char *
primary_root_translate(const char * root_in)187 primary_root_translate (const char *root_in)
188 {
189 #ifdef PROXY_SUPPORT
190 char *translated;
191 static char *previous = NULL;
192 static size_t len;
193
194 /* This can happen, for instance, during `cvs init'. */
195 if (!config) return root_in;
196
197 if (config->PrimaryServer
198 && !strncmp (root_in, config->PrimaryServer->directory,
199 strlen (config->PrimaryServer->directory))
200 && (ISSLASH (root_in[strlen (config->PrimaryServer->directory)])
201 || root_in[strlen (config->PrimaryServer->directory)] == '\0')
202 )
203 {
204 translated =
205 Xasnprintf (previous, &len,
206 "%s%s", current_parsed_root->directory,
207 root_in + strlen (config->PrimaryServer->directory));
208 if (previous && previous != translated)
209 free (previous);
210 return previous = translated;
211 }
212 #endif
213
214 /* There is no primary root configured or it didn't match. */
215 return root_in;
216 }
217
218
219
220 /* Translate a primary root in reverse for PATHNAMEs in responses.
221 *
222 * INPUTS
223 * root_in The root to be translated.
224 *
225 * RETURNS
226 * A translated string this function owns, or a pointer to the original
227 * string passed in if no translation was necessary.
228 *
229 * If the returned string is the translated one, it may be overwritten
230 * by the next call to this function.
231 */
232 const char *
primary_root_inverse_translate(const char * root_in)233 primary_root_inverse_translate (const char *root_in)
234 {
235 #ifdef PROXY_SUPPORT
236 char *translated;
237 static char *previous = NULL;
238 static size_t len;
239
240 /* This can happen, for instance, during `cvs init'. */
241 if (!config) return root_in;
242
243 if (config->PrimaryServer
244 && !strncmp (root_in, current_parsed_root->directory,
245 strlen (current_parsed_root->directory))
246 && (ISSLASH (root_in[strlen (current_parsed_root->directory)])
247 || root_in[strlen (current_parsed_root->directory)] == '\0')
248 )
249 {
250 translated =
251 Xasnprintf (previous, &len,
252 "%s%s", config->PrimaryServer->directory,
253 root_in + strlen (current_parsed_root->directory));
254 if (previous && previous != translated)
255 free (previous);
256 return previous = translated;
257 }
258 #endif
259
260 /* There is no primary root configured or it didn't match. */
261 return root_in;
262 }
263
264
265
266 /* The root_allow_* stuff maintains a list of valid CVSROOT
267 directories. Then we can check against them when a remote user
268 hands us a CVSROOT directory. */
269 static List *root_allow;
270
271 static void
delconfig(Node * n)272 delconfig (Node *n)
273 {
274 if (n->data) free_config (n->data);
275 }
276
277
278
279 void
root_allow_add(const char * arg,const char * configPath)280 root_allow_add (const char *arg, const char *configPath)
281 {
282 Node *n;
283
284 if (!root_allow) root_allow = getlist();
285 n = getnode();
286 n->key = xstrdup (arg);
287 n->data = parse_config (arg, configPath);
288 n->delproc = delconfig;
289 addnode (root_allow, n);
290 }
291
292 void
root_allow_free(void)293 root_allow_free (void)
294 {
295 dellist (&root_allow);
296 }
297
298 int
root_allow_used(void)299 root_allow_used (void)
300 {
301 return (root_allow != NULL);
302 }
303
304 bool
root_allow_ok(const char * arg)305 root_allow_ok (const char *arg)
306 {
307 if (!root_allow)
308 {
309 /* Probably someone upgraded from CVS before 1.9.10 to 1.9.10
310 or later without reading the documentation about
311 --allow-root. Printing an error here doesn't disclose any
312 particularly useful information to an attacker because a
313 CVS server configured in this way won't let *anyone* in. */
314
315 /* Note that we are called from a context where we can spit
316 back "error" rather than waiting for the next request which
317 expects responses. */
318 printf ("\
319 error 0 Server configuration missing --allow-root in inetd.conf\n");
320 exit (EXIT_FAILURE);
321 }
322
323 if (findnode (root_allow, arg))
324 return true;
325 return false;
326 }
327
328
329
330 /* Get a config we stored in response to root_allow.
331 *
332 * RETURNS
333 * The config associated with ARG.
334 */
335 struct config *
get_root_allow_config(const char * arg,const char * configPath)336 get_root_allow_config (const char *arg, const char *configPath)
337 {
338 Node *n;
339
340 TRACE (TRACE_FUNCTION, "get_root_allow_config (%s)", arg);
341
342 if (root_allow)
343 n = findnode (root_allow, arg);
344 else
345 n = NULL;
346
347 if (n) return n->data;
348 return parse_config (arg, configPath);
349 }
350
351
352
353 /* This global variable holds the global -d option. It is NULL if -d
354 was not used, which means that we must get the CVSroot information
355 from the CVSROOT environment variable or from a CVS/Root file. */
356 char *CVSroot_cmdline;
357
358
359
360 /* FIXME - Deglobalize this. */
361 cvsroot_t *current_parsed_root = NULL;
362 /* Used to save the original root being processed so that we can still find it
363 * in lists and the like after a `Redirect' response. Also set to mirror
364 * current_parsed_root in server mode so that code which runs on both the
365 * client and server but which wants to use original data on the client can
366 * just always reference the original_parsed_root.
367 */
368 const cvsroot_t *original_parsed_root;
369
370
371 /* allocate and initialize a cvsroot_t
372 *
373 * We must initialize the strings to NULL so we know later what we should
374 * free
375 *
376 * Some of the other zeroes remain meaningful as, "never set, use default",
377 * or the like
378 */
379 /* Functions which allocate memory are not pure. */
380 static cvsroot_t *new_cvsroot_t(void)
381 __attribute__( (__malloc__) );
382 static cvsroot_t *
new_cvsroot_t(void)383 new_cvsroot_t (void)
384 {
385 cvsroot_t *newroot;
386
387 /* gotta store it somewhere */
388 newroot = xmalloc(sizeof(cvsroot_t));
389
390 newroot->original = NULL;
391 newroot->directory = NULL;
392 newroot->method = null_method;
393 newroot->isremote = false;
394 #ifdef CLIENT_SUPPORT
395 newroot->username = NULL;
396 newroot->password = NULL;
397 newroot->hostname = NULL;
398 newroot->cvs_rsh = NULL;
399 newroot->cvs_server = NULL;
400 newroot->port = 0;
401 newroot->proxy_hostname = NULL;
402 newroot->proxy_port = 0;
403 newroot->redirect = true; /* Advertise Redirect support */
404 #endif /* CLIENT_SUPPORT */
405
406 return newroot;
407 }
408
409
410
411 /* Dispose of a cvsroot_t and its component parts.
412 *
413 * NOTE
414 * It is dangerous for most code to call this function since parse_cvsroot
415 * maintains a cache of parsed roots.
416 */
417 static void
free_cvsroot_t(cvsroot_t * root)418 free_cvsroot_t (cvsroot_t *root)
419 {
420 assert (root);
421 if (root->original != NULL)
422 free (root->original);
423 if (root->directory != NULL)
424 free (root->directory);
425 #ifdef CLIENT_SUPPORT
426 if (root->username != NULL)
427 free (root->username);
428 if (root->password != NULL)
429 {
430 /* I like to be paranoid */
431 memset (root->password, 0, strlen (root->password));
432 free (root->password);
433 }
434 if (root->hostname != NULL)
435 free (root->hostname);
436 if (root->cvs_rsh != NULL)
437 free (root->cvs_rsh);
438 if (root->cvs_server != NULL)
439 free (root->cvs_server);
440 if (root->proxy_hostname != NULL)
441 free (root->proxy_hostname);
442 #endif /* CLIENT_SUPPORT */
443 free (root);
444 }
445
446
447
448 /*
449 * Parse a CVSROOT string to allocate and return a new cvsroot_t structure.
450 * Valid specifications are:
451 *
452 * :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path
453 * [:(ext|server):][[user]@]host[:]/path
454 * [:local:[e:]]/path
455 * :fork:/path
456 *
457 * INPUTS
458 * root_in C String containing the CVSROOT to be parsed.
459 *
460 * RETURNS
461 * A pointer to a newly allocated cvsroot_t structure upon success and
462 * NULL upon failure. The caller should never dispose of this structure,
463 * as it is stored in a cache, but the caller may rely on it not to
464 * change.
465 *
466 * NOTES
467 * This would have been a lot easier to write in Perl.
468 *
469 * Would it make sense to reimplement the root and config file parsing
470 * gunk in Lex/Yacc?
471 *
472 * SEE ALSO
473 * free_cvsroot_t()
474 */
475 cvsroot_t *
parse_cvsroot(const char * root_in)476 parse_cvsroot (const char *root_in)
477 {
478 cvsroot_t *newroot; /* the new root to be returned */
479 char *cvsroot_save; /* what we allocated so we can dispose
480 * it when finished */
481 char *cvsroot_copy, *p; /* temporary pointers for parsing */
482 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
483 char *q; /* temporary pointer for parsing */
484 char *firstslash; /* save where the path spec starts
485 * while we parse
486 * [[user][:password]@]host[:[port]]
487 */
488 int check_hostname, no_port, no_password, no_proxy;
489 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
490 static List *cache = NULL;
491 Node *node;
492
493 assert (root_in != NULL);
494
495 /* This message is TRACE_FLOW since this function is called repeatedly by
496 * the recursion routines.
497 */
498 TRACE (TRACE_FLOW, "parse_cvsroot (%s)", root_in);
499
500 if ((node = findnode (cache, root_in)))
501 return node->data;
502
503 assert (root_in);
504
505 /* allocate some space */
506 newroot = new_cvsroot_t();
507
508 /* save the original string */
509 newroot->original = xstrdup (root_in);
510
511 /* and another copy we can munge while parsing */
512 cvsroot_save = cvsroot_copy = xstrdup (root_in);
513
514 if (*cvsroot_copy == ':')
515 {
516 char *method = ++cvsroot_copy;
517
518 /* Access method specified, as in
519 * "cvs -d :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path",
520 * "cvs -d [:(ext|server):][[user]@]host[:]/path",
521 * "cvs -d :local:e:\path",
522 * "cvs -d :fork:/path".
523 * We need to get past that part of CVSroot before parsing the
524 * rest of it.
525 */
526
527 if (! (p = strchr (method, ':')))
528 {
529 error (0, 0, "No closing `:' on method in CVSROOT.");
530 goto error_exit;
531 }
532 *p = '\0';
533 cvsroot_copy = ++p;
534
535 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
536 /* Look for method options, for instance, proxy, proxyport.
537 * Calling strtok again is saved until after parsing the method.
538 */
539 method = strtok (method, ";");
540 if (!method)
541 /* Could just exit now, but this keeps the error message in sync.
542 */
543 method = "";
544 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
545
546 if (NULL == method)
547 {
548 error (0, 0, "Missing method in CVSROOT.");
549 goto error_exit;
550 }
551
552 /* Now we have an access method -- see if it's valid. */
553
554 if (!strcasecmp (method, "local"))
555 newroot->method = local_method;
556 else if (!strcasecmp (method, "pserver"))
557 newroot->method = pserver_method;
558 else if (!strcasecmp (method, "kserver"))
559 newroot->method = kserver_method;
560 else if (!strcasecmp (method, "gserver"))
561 newroot->method = gserver_method;
562 else if (!strcasecmp (method, "server"))
563 newroot->method = server_method;
564 else if (strncmp (method, "ext=", 4) == 0)
565 {
566 newroot->cvs_rsh = xstrdup(method + 4);
567 newroot->method = ext_method;
568 }
569 else if (!strcasecmp (method, "extssh"))
570 {
571 newroot->cvs_rsh = xstrdup("ssh");
572 newroot->method = extssh_method;
573 }
574 else if (!strcasecmp (method, "ext"))
575 newroot->method = ext_method;
576 else if (!strcasecmp (method, "fork"))
577 newroot->method = fork_method;
578 else
579 {
580 error (0, 0, "Unknown method (`%s') in CVSROOT.", method);
581 goto error_exit;
582 }
583
584 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
585 /* Parse the method options, for instance, proxy, proxyport */
586 while ((p = strtok (NULL, ";")))
587 {
588 char *q = strchr (p, '=');
589 if (q == NULL)
590 {
591 error (0, 0, "Option (`%s') has no argument in CVSROOT.",
592 p);
593 goto error_exit;
594 }
595
596 *q++ = '\0';
597 TRACE (TRACE_DATA, "CVSROOT option=`%s' value=`%s'", p, q);
598 if (!strcasecmp (p, "proxy"))
599 {
600 newroot->proxy_hostname = xstrdup (q);
601 }
602 else if (!strcasecmp (p, "proxyport"))
603 {
604 char *r = q;
605 if (*r == '-') r++;
606 while (*r)
607 {
608 if (!isdigit(*r++))
609 {
610 error (0, 0,
611 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
612 q);
613 goto error_exit;
614 }
615 }
616 if ((newroot->proxy_port = atoi (q)) <= 0)
617 error (0, 0,
618 "CVSROOT may only specify a positive, non-zero, integer proxy port (not `%s').",
619 q);
620 }
621 else if (!strcasecmp (p, "CVS_RSH"))
622 {
623 /* override CVS_RSH environment variable */
624 if (newroot->method == ext_method
625 || newroot->method == extssh_method)
626 newroot->cvs_rsh = xstrdup (q);
627 }
628 else if (!strcasecmp (p, "CVS_SERVER"))
629 {
630 /* override CVS_SERVER environment variable */
631 if (newroot->method == ext_method
632 || newroot->method == extssh_method
633 || newroot->method == fork_method)
634 newroot->cvs_server = xstrdup (q);
635 }
636 else if (!strcasecmp (p, "Redirect"))
637 readBool ("CVSROOT", "Redirect", q, &newroot->redirect);
638 else
639 {
640 error (0, 0, "Unknown option (`%s') in CVSROOT.", p);
641 goto error_exit;
642 }
643 }
644 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
645 }
646 else
647 {
648 /* If the method isn't specified, assume EXT_METHOD if the string looks
649 like a relative path and LOCAL_METHOD otherwise. */
650
651 newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
652 ? ext_method
653 : local_method);
654 }
655
656 /*
657 * There are a few sanity checks we can do now, only knowing the
658 * method of this root.
659 */
660
661 newroot->isremote = (newroot->method != local_method);
662
663 #if defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
664 if (readonlyfs && newroot->isremote && (newroot->method != ext_method)
665 && (newroot->method != extssh_method))
666 error (1, 0,
667 "Read-only repository feature unavailable with remote roots (cvsroot = %s)",
668 cvsroot_copy);
669
670 if ((newroot->method != local_method)
671 && (newroot->method != fork_method)
672 )
673 {
674 /* split the string into [[user][:password]@]host[:[port]] & /path
675 *
676 * this will allow some characters such as '@' & ':' to remain unquoted
677 * in the path portion of the spec
678 */
679 if ((p = strchr (cvsroot_copy, '/')) == NULL)
680 {
681 error (0, 0, "CVSROOT requires a path spec:");
682 error (0, 0,
683 ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
684 error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
685 goto error_exit;
686 }
687 firstslash = p; /* == NULL if '/' not in string */
688 *p = '\0';
689
690 /* Check to see if there is a username[:password] in the string. */
691 if ((p = strchr (cvsroot_copy, '@')) != NULL)
692 {
693 *p = '\0';
694 /* check for a password */
695 if ((q = strchr (cvsroot_copy, ':')) != NULL)
696 {
697 *q = '\0';
698 newroot->password = xstrdup (++q);
699 /* Don't check for *newroot->password == '\0' since
700 * a user could conceivably wish to specify a blank password
701 *
702 * (newroot->password == NULL means to use the
703 * password from .cvspass)
704 */
705 }
706
707 /* copy the username */
708 if (*cvsroot_copy != '\0')
709 /* a blank username is impossible, so leave it NULL in that
710 * case so we know to use the default username
711 */
712 newroot->username = xstrdup (cvsroot_copy);
713
714 cvsroot_copy = ++p;
715 }
716
717 /* now deal with host[:[port]] */
718
719 /* the port */
720 if ((p = strchr (cvsroot_copy, ':')) != NULL)
721 {
722 *p++ = '\0';
723 if (strlen(p))
724 {
725 q = p;
726 if (*q == '-') q++;
727 while (*q)
728 {
729 if (!isdigit(*q++))
730 {
731 error (0, 0,
732 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
733 p);
734 error (0, 0,
735 "Perhaps you entered a relative pathname?");
736 goto error_exit;
737 }
738 }
739 if ((newroot->port = atoi (p)) <= 0)
740 {
741 error (0, 0,
742 "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
743 p);
744 error (0, 0, "Perhaps you entered a relative pathname?");
745 goto error_exit;
746 }
747 }
748 }
749
750 /* copy host */
751 if (*cvsroot_copy != '\0')
752 /* blank hostnames are invalid, but for now leave the field NULL
753 * and catch the error during the sanity checks later
754 */
755 newroot->hostname = xstrdup (cvsroot_copy);
756
757 /* restore the '/' */
758 cvsroot_copy = firstslash;
759 *cvsroot_copy = '/';
760 }
761 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
762
763 /*
764 * Parse the path for all methods.
765 */
766 /* Here & local_cvsroot() should be the only places this needs to be
767 * called on a CVSROOT now. cvsroot->original is saved for error messages
768 * and, otherwise, we want no trailing slashes.
769 */
770 Sanitize_Repository_Name (cvsroot_copy);
771 newroot->directory = xstrdup (cvsroot_copy);
772
773 /*
774 * Do various sanity checks.
775 */
776
777 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
778 if (newroot->username && ! newroot->hostname)
779 {
780 error (0, 0, "Missing hostname in CVSROOT.");
781 goto error_exit;
782 }
783
784 /* We won't have attempted to parse these without CLIENT_SUPPORT or
785 * SERVER_SUPPORT.
786 */
787 check_hostname = 0;
788 no_password = 1;
789 no_proxy = 1;
790 no_port = 0;
791 #endif /* defined (CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
792 switch (newroot->method)
793 {
794 case local_method:
795 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
796 if (newroot->username || newroot->hostname)
797 {
798 error (0, 0, "Can't specify hostname and username in CVSROOT");
799 error (0, 0, "when using local access method.");
800 goto error_exit;
801 }
802 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
803 /* cvs.texinfo has always told people that CVSROOT must be an
804 absolute pathname. Furthermore, attempts to use a relative
805 pathname produced various errors (I couldn't get it to work),
806 so there would seem to be little risk in making this a fatal
807 error. */
808 if (!ISABSOLUTE (newroot->directory))
809 {
810 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
811 newroot->directory);
812 error (0, 0, "when using local access method.");
813 goto error_exit;
814 }
815 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
816 /* We don't need to check for these in :local: mode, really, since
817 * we shouldn't be able to hit the code above which parses them, but
818 * I'm leaving them here in lieu of assertions.
819 */
820 no_port = 1;
821 /* no_password already set */
822 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
823 break;
824 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
825 case fork_method:
826 /* We want :fork: to behave the same as other remote access
827 methods. Therefore, don't check to see that the repository
828 name is absolute -- let the server do it. */
829 if (newroot->username || newroot->hostname)
830 {
831 error (0, 0, "Can't specify hostname and username in CVSROOT");
832 error (0, 0, "when using fork access method.");
833 goto error_exit;
834 }
835 newroot->hostname = xstrdup("server"); /* for error messages */
836 if (!ISABSOLUTE (newroot->directory))
837 {
838 error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
839 newroot->directory);
840 error (0, 0, "when using fork access method.");
841 goto error_exit;
842 }
843 no_port = 1;
844 /* no_password already set */
845 break;
846 case kserver_method:
847 check_hostname = 1;
848 /* no_password already set */
849 break;
850 case gserver_method:
851 check_hostname = 1;
852 no_proxy = 0;
853 /* no_password already set */
854 break;
855 case server_method:
856 case ext_method:
857 no_port = 1;
858 case extssh_method:
859 /* no_password already set */
860 check_hostname = 1;
861 break;
862 case pserver_method:
863 no_password = 0;
864 no_proxy = 0;
865 check_hostname = 1;
866 break;
867 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
868 default:
869 error (1, 0, "Invalid method found in parse_cvsroot");
870 }
871
872 #if defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT)
873 if (no_password && newroot->password)
874 {
875 error (0, 0, "CVSROOT password specification is only valid for");
876 error (0, 0, "pserver connection method.");
877 goto error_exit;
878 }
879 if (no_proxy && (newroot->proxy_hostname || newroot->proxy_port))
880 {
881 error (0, 0,
882 "CVSROOT proxy specification is only valid for gserver and");
883 error (0, 0, "pserver connection methods.");
884 goto error_exit;
885 }
886
887 if (!newroot->proxy_hostname && newroot->proxy_port)
888 {
889 error (0, 0, "Proxy port specified in CVSROOT without proxy host.");
890 goto error_exit;
891 }
892
893 if (check_hostname && !newroot->hostname)
894 {
895 error (0, 0, "Didn't specify hostname in CVSROOT.");
896 goto error_exit;
897 }
898
899 if (no_port && newroot->port)
900 {
901 error (0, 0,
902 "CVSROOT port specification is only valid for extssh,");
903 error (0, 0, "gserver, kserver and pserver connection methods.");
904 goto error_exit;
905 }
906 #endif /* defined(CLIENT_SUPPORT) || defined (SERVER_SUPPORT) */
907
908 if (*newroot->directory == '\0')
909 {
910 error (0, 0, "Missing directory in CVSROOT.");
911 goto error_exit;
912 }
913
914 /* Hooray! We finally parsed it! */
915 free (cvsroot_save);
916
917 if (!cache) cache = getlist();
918 node = getnode();
919 node->key = xstrdup (newroot->original);
920 node->data = newroot;
921 addnode (cache, node);
922 return newroot;
923
924 error_exit:
925 free (cvsroot_save);
926 free_cvsroot_t (newroot);
927 return NULL;
928 }
929
930
931
932 #ifdef AUTH_CLIENT_SUPPORT
933 /* Use root->username, root->hostname, root->port, and root->directory
934 * to create a normalized CVSROOT fit for the .cvspass file
935 *
936 * username defaults to the result of getcaller()
937 * port defaults to the result of get_cvs_port_number()
938 *
939 * FIXME - we could cache the canonicalized version of a root inside the
940 * cvsroot_t, but we'd have to un'const the input here and stop expecting the
941 * caller to be responsible for our return value
942 *
943 * ASSUMPTIONS
944 * ROOT->method == pserver_method
945 */
946 char *
normalize_cvsroot(const cvsroot_t * root)947 normalize_cvsroot (const cvsroot_t *root)
948 {
949 char *cvsroot_canonical;
950 char *p, *hostname;
951
952 assert (root && root->hostname && root->directory);
953
954 /* use a lower case hostname since we know hostnames are case insensitive */
955 /* Some logic says we should be tacking our domain name on too if it isn't
956 * there already, but for now this works. Reverse->Forward lookups are
957 * almost certainly too much since that would make CVS immune to some of
958 * the DNS trickery that makes life easier for sysadmins when they want to
959 * move a repository or the like
960 */
961 p = hostname = xstrdup (root->hostname);
962 while (*p)
963 {
964 *p = tolower (*p);
965 p++;
966 }
967
968 cvsroot_canonical = Xasprintf (":pserver:%s@%s:%d%s",
969 root->username ? root->username
970 : getcaller(),
971 hostname, get_cvs_port_number (root),
972 root->directory);
973
974 free (hostname);
975 return cvsroot_canonical;
976 }
977 #endif /* AUTH_CLIENT_SUPPORT */
978
979
980
981 #ifdef PROXY_SUPPORT
982 /* A walklist() function to walk the root_allow list looking for a PrimaryServer
983 * configuration with a directory matching the requested directory.
984 *
985 * If found, replace it.
986 */
987 static bool get_local_root_dir_done;
988 static int
get_local_root_dir(Node * p,void * root_in)989 get_local_root_dir (Node *p, void *root_in)
990 {
991 struct config *c = p->data;
992 char **r = root_in;
993
994 if (get_local_root_dir_done)
995 return 0;
996
997 if (c->PrimaryServer && !strcmp (*r, c->PrimaryServer->directory))
998 {
999 free (*r);
1000 *r = xstrdup (p->key);
1001 get_local_root_dir_done = true;
1002 }
1003 return 0;
1004 }
1005 #endif /* PROXY_SUPPORT */
1006
1007
1008
1009 /* allocate and return a cvsroot_t structure set up as if we're using the local
1010 * repository DIR. */
1011 cvsroot_t *
local_cvsroot(const char * dir)1012 local_cvsroot (const char *dir)
1013 {
1014 cvsroot_t *newroot = new_cvsroot_t();
1015
1016 newroot->original = xstrdup(dir);
1017 newroot->method = local_method;
1018 newroot->directory = xstrdup(dir);
1019 /* Here and parse_cvsroot() should be the only places this needs to be
1020 * called on a CVSROOT now. cvsroot->original is saved for error messages
1021 * and, otherwise, we want no trailing slashes.
1022 */
1023 Sanitize_Repository_Name (newroot->directory);
1024
1025 #ifdef PROXY_SUPPORT
1026 /* Translate the directory to a local one in the case that we are
1027 * configured as a secondary. If root_allow has not been initialized,
1028 * nothing happens.
1029 */
1030 get_local_root_dir_done = false;
1031 walklist (root_allow, get_local_root_dir, &newroot->directory);
1032 #endif /* PROXY_SUPPORT */
1033
1034 return newroot;
1035 }
1036
1037
1038
1039 #ifdef DEBUG
1040 /* This is for testing the parsing function. Use
1041
1042 gcc -I. -I.. -I../lib -DDEBUG root.c -o root
1043
1044 to compile. */
1045
1046 #include <stdio.h>
1047
1048 char *program_name = "testing";
1049 char *cvs_cmd_name = "parse_cvsroot"; /* XXX is this used??? */
1050
1051 void
main(int argc,char * argv[])1052 main (int argc, char *argv[])
1053 {
1054 program_name = argv[0];
1055
1056 if (argc != 2)
1057 {
1058 fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
1059 exit (2);
1060 }
1061
1062 if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
1063 {
1064 fprintf (stderr, "%s: Parsing failed.\n", program_name);
1065 exit (1);
1066 }
1067 printf ("CVSroot: %s\n", argv[1]);
1068 printf ("current_parsed_root->method: %s\n",
1069 method_names[current_parsed_root->method]);
1070 printf ("current_parsed_root->username: %s\n",
1071 current_parsed_root->username
1072 ? current_parsed_root->username : "NULL");
1073 printf ("current_parsed_root->hostname: %s\n",
1074 current_parsed_root->hostname
1075 ? current_parsed_root->hostname : "NULL");
1076 printf ("current_parsed_root->directory: %s\n",
1077 current_parsed_root->directory);
1078
1079 exit (0);
1080 /* NOTREACHED */
1081 }
1082 #endif
1083