/contrib/cvs/src/root.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 864 lines · 555 code · 110 blank · 199 comment · 135 complexity · 6dc18e8432f810257b885d55e16c6e3d MD5 · raw file

  1. /*
  2. * Copyright (C) 1986-2008 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. #include "cvs.h"
  17. #include <assert.h>
  18. #include "getline.h"
  19. /* Printable names for things in the current_parsed_root->method enum variable.
  20. Watch out if the enum is changed in cvs.h! */
  21. const char method_names[][16] = {
  22. "undefined", "local", "server (rsh)", "pserver",
  23. "kserver", "gserver", "ext", "extssh", "fork"
  24. };
  25. #ifndef DEBUG
  26. cvsroot_t *
  27. Name_Root (dir, update_dir)
  28. const char *dir;
  29. const char *update_dir;
  30. {
  31. FILE *fpin;
  32. cvsroot_t *ret;
  33. const char *xupdate_dir;
  34. char *root = NULL;
  35. size_t root_allocated = 0;
  36. char *tmp;
  37. char *cvsadm;
  38. char *cp;
  39. int len;
  40. if (update_dir && *update_dir)
  41. xupdate_dir = update_dir;
  42. else
  43. xupdate_dir = ".";
  44. if (dir != NULL)
  45. {
  46. cvsadm = xmalloc (strlen (dir) + sizeof (CVSADM) + 10);
  47. (void) sprintf (cvsadm, "%s/%s", dir, CVSADM);
  48. tmp = xmalloc (strlen (dir) + sizeof (CVSADM_ROOT) + 10);
  49. (void) sprintf (tmp, "%s/%s", dir, CVSADM_ROOT);
  50. }
  51. else
  52. {
  53. cvsadm = xstrdup (CVSADM);
  54. tmp = xstrdup (CVSADM_ROOT);
  55. }
  56. /*
  57. * Do not bother looking for a readable file if there is no cvsadm
  58. * directory present.
  59. *
  60. * It is possible that not all repositories will have a CVS/Root
  61. * file. This is ok, but the user will need to specify -d
  62. * /path/name or have the environment variable CVSROOT set in
  63. * order to continue. */
  64. if ((!isdir (cvsadm)) || (!isreadable (tmp)))
  65. {
  66. ret = NULL;
  67. goto out;
  68. }
  69. /*
  70. * The assumption here is that the CVS Root is always contained in the
  71. * first line of the "Root" file.
  72. */
  73. fpin = open_file (tmp, "r");
  74. if ((len = getline (&root, &root_allocated, fpin)) < 0)
  75. {
  76. int saved_errno = errno;
  77. /* FIXME: should be checking for end of file separately; errno
  78. is not set in that case. */
  79. error (0, 0, "in directory %s:", xupdate_dir);
  80. error (0, saved_errno, "cannot read %s", CVSADM_ROOT);
  81. error (0, 0, "please correct this problem");
  82. ret = NULL;
  83. goto out;
  84. }
  85. fclose (fpin);
  86. cp = root + len - 1;
  87. if (*cp == '\n')
  88. *cp = '\0'; /* strip the newline */
  89. /*
  90. * root now contains a candidate for CVSroot. It must be an
  91. * absolute pathname or specify a remote server.
  92. */
  93. ret = parse_cvsroot (root);
  94. if (ret == NULL)
  95. {
  96. error (0, 0, "in directory %s:", xupdate_dir);
  97. error (0, 0,
  98. "ignoring %s because it does not contain a valid root.",
  99. CVSADM_ROOT);
  100. goto out;
  101. }
  102. if (!ret->isremote && !isdir (ret->directory))
  103. {
  104. error (0, 0, "in directory %s:", xupdate_dir);
  105. error (0, 0,
  106. "ignoring %s because it specifies a non-existent repository %s",
  107. CVSADM_ROOT, root);
  108. free_cvsroot_t (ret);
  109. ret = NULL;
  110. goto out;
  111. }
  112. out:
  113. free (cvsadm);
  114. free (tmp);
  115. if (root != NULL)
  116. free (root);
  117. return ret;
  118. }
  119. /*
  120. * Write the CVS/Root file so that the environment variable CVSROOT
  121. * and/or the -d option to cvs will be validated or not necessary for
  122. * future work.
  123. */
  124. void
  125. Create_Root (dir, rootdir)
  126. const char *dir;
  127. const char *rootdir;
  128. {
  129. FILE *fout;
  130. char *tmp;
  131. if (noexec)
  132. return;
  133. /* record the current cvs root */
  134. if (rootdir != NULL)
  135. {
  136. if (dir != NULL)
  137. {
  138. tmp = xmalloc (strlen (dir) + sizeof (CVSADM_ROOT) + 10);
  139. (void) sprintf (tmp, "%s/%s", dir, CVSADM_ROOT);
  140. }
  141. else
  142. tmp = xstrdup (CVSADM_ROOT);
  143. fout = open_file (tmp, "w+");
  144. if (fprintf (fout, "%s\n", rootdir) < 0)
  145. error (1, errno, "write to %s failed", tmp);
  146. if (fclose (fout) == EOF)
  147. error (1, errno, "cannot close %s", tmp);
  148. free (tmp);
  149. }
  150. }
  151. #endif /* ! DEBUG */
  152. /* The root_allow_* stuff maintains a list of legal CVSROOT
  153. directories. Then we can check against them when a remote user
  154. hands us a CVSROOT directory. */
  155. static int root_allow_count;
  156. static char **root_allow_vector;
  157. static int root_allow_size;
  158. int
  159. root_allow_used ()
  160. {
  161. return root_allow_count;
  162. }
  163. void
  164. root_allow_add (arg)
  165. char *arg;
  166. {
  167. char *p;
  168. if (root_allow_size <= root_allow_count)
  169. {
  170. if (root_allow_size == 0)
  171. {
  172. root_allow_size = 1;
  173. root_allow_vector =
  174. (char **) xmalloc (root_allow_size * sizeof (char *));
  175. }
  176. else
  177. {
  178. root_allow_size *= 2;
  179. root_allow_vector =
  180. (char **) xrealloc (root_allow_vector,
  181. root_allow_size * sizeof (char *));
  182. }
  183. if (root_allow_vector == NULL)
  184. {
  185. no_memory:
  186. /* Strictly speaking, we're not supposed to output anything
  187. now. But we're about to exit(), give it a try. */
  188. printf ("E Fatal server error, aborting.\n\
  189. error ENOMEM Virtual memory exhausted.\n");
  190. error_exit ();
  191. }
  192. }
  193. p = xmalloc (strlen (arg) + 1);
  194. if (p == NULL)
  195. goto no_memory;
  196. strcpy (p, arg);
  197. root_allow_vector[root_allow_count++] = p;
  198. }
  199. void
  200. root_allow_free ()
  201. {
  202. if (root_allow_vector != NULL)
  203. free_names (&root_allow_count, root_allow_vector);
  204. root_allow_size = 0;
  205. }
  206. int
  207. root_allow_ok (arg)
  208. char *arg;
  209. {
  210. int i;
  211. if (root_allow_count == 0)
  212. {
  213. /* Probably someone upgraded from CVS before 1.9.10 to 1.9.10
  214. or later without reading the documentation about
  215. --allow-root. Printing an error here doesn't disclose any
  216. particularly useful information to an attacker because a
  217. CVS server configured in this way won't let *anyone* in. */
  218. /* Note that we are called from a context where we can spit
  219. back "error" rather than waiting for the next request which
  220. expects responses. */
  221. printf ("\
  222. error 0 Server configuration missing --allow-root in inetd.conf\n");
  223. error_exit ();
  224. }
  225. for (i = 0; i < root_allow_count; ++i)
  226. if (strcmp (root_allow_vector[i], arg) == 0)
  227. return 1;
  228. return 0;
  229. }
  230. /* This global variable holds the global -d option. It is NULL if -d
  231. was not used, which means that we must get the CVSroot information
  232. from the CVSROOT environment variable or from a CVS/Root file. */
  233. char *CVSroot_cmdline;
  234. /* FIXME - Deglobalize this. */
  235. cvsroot_t *current_parsed_root = NULL;
  236. /* allocate and initialize a cvsroot_t
  237. *
  238. * We must initialize the strings to NULL so we know later what we should
  239. * free
  240. *
  241. * Some of the other zeroes remain meaningful as, "never set, use default",
  242. * or the like
  243. */
  244. static cvsroot_t *
  245. new_cvsroot_t ()
  246. {
  247. cvsroot_t *newroot;
  248. /* gotta store it somewhere */
  249. newroot = xmalloc(sizeof(cvsroot_t));
  250. newroot->original = NULL;
  251. newroot->method = null_method;
  252. newroot->isremote = 0;
  253. #ifdef CLIENT_SUPPORT
  254. newroot->username = NULL;
  255. newroot->password = NULL;
  256. newroot->hostname = NULL;
  257. newroot->port = 0;
  258. newroot->directory = NULL;
  259. newroot->proxy_hostname = NULL;
  260. newroot->proxy_port = 0;
  261. #endif /* CLIENT_SUPPORT */
  262. return newroot;
  263. }
  264. /* Dispose of a cvsroot_t and its component parts */
  265. void
  266. free_cvsroot_t (root)
  267. cvsroot_t *root;
  268. {
  269. if (root->original != NULL)
  270. free (root->original);
  271. if (root->directory != NULL)
  272. free (root->directory);
  273. #ifdef CLIENT_SUPPORT
  274. if (root->username != NULL)
  275. free (root->username);
  276. if (root->password != NULL)
  277. {
  278. /* I like to be paranoid */
  279. memset (root->password, 0, strlen (root->password));
  280. free (root->password);
  281. }
  282. if (root->hostname != NULL)
  283. free (root->hostname);
  284. if (root->proxy_hostname != NULL)
  285. free (root->proxy_hostname);
  286. #endif /* CLIENT_SUPPORT */
  287. free (root);
  288. }
  289. /*
  290. * Parse a CVSROOT string to allocate and return a new cvsroot_t structure.
  291. * Valid specifications are:
  292. *
  293. * :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path
  294. * [:(ext|server):][[user]@]host[:]/path
  295. * [:local:[e:]]/path
  296. * :fork:/path
  297. *
  298. * INPUTS
  299. * root_in C String containing the CVSROOT to be parsed.
  300. *
  301. * RETURNS
  302. * A pointer to a newly allocated cvsroot_t structure upon success and
  303. * NULL upon failure. The caller is responsible for disposing of
  304. * new structures with a call to free_cvsroot_t().
  305. *
  306. * NOTES
  307. * This would have been a lot easier to write in Perl.
  308. *
  309. * SEE ALSO
  310. * free_cvsroot_t()
  311. */
  312. cvsroot_t *
  313. parse_cvsroot (root_in)
  314. const char *root_in;
  315. {
  316. cvsroot_t *newroot; /* the new root to be returned */
  317. char *cvsroot_save; /* what we allocated so we can dispose
  318. * it when finished */
  319. char *firstslash; /* save where the path spec starts
  320. * while we parse
  321. * [[user][:password]@]host[:[port]]
  322. */
  323. char *cvsroot_copy, *p, *q; /* temporary pointers for parsing */
  324. #ifdef CLIENT_SUPPORT
  325. int check_hostname, no_port, no_password;
  326. #endif /* CLIENT_SUPPORT */
  327. assert (root_in);
  328. /* allocate some space */
  329. newroot = new_cvsroot_t();
  330. /* save the original string */
  331. newroot->original = xstrdup (root_in);
  332. /* and another copy we can munge while parsing */
  333. cvsroot_save = cvsroot_copy = xstrdup (root_in);
  334. if (*cvsroot_copy == ':')
  335. {
  336. char *method = ++cvsroot_copy;
  337. /* Access method specified, as in
  338. * "cvs -d :(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path",
  339. * "cvs -d [:(ext|server):][[user]@]host[:]/path",
  340. * "cvs -d :local:e:\path",
  341. * "cvs -d :fork:/path".
  342. * We need to get past that part of CVSroot before parsing the
  343. * rest of it.
  344. */
  345. if (! (p = strchr (method, ':')))
  346. {
  347. error (0, 0, "No closing `:' on method in CVSROOT.");
  348. goto error_exit;
  349. }
  350. *p = '\0';
  351. cvsroot_copy = ++p;
  352. #ifdef CLIENT_SUPPORT
  353. /* Look for method options, for instance, proxy, proxyport.
  354. * We don't handle these, but we like to try and warn the user that
  355. * they are being ignored.
  356. */
  357. if ((p = strchr (method, ';')) != NULL)
  358. {
  359. *p++ = '\0';
  360. if (!really_quiet)
  361. {
  362. error (0, 0,
  363. "WARNING: Ignoring method options found in CVSROOT: `%s'.",
  364. p);
  365. error (0, 0,
  366. "Use CVS version 1.12.7 or later to handle method options.");
  367. }
  368. }
  369. #endif /* CLIENT_SUPPORT */
  370. /* Now we have an access method -- see if it's valid. */
  371. if (strcmp (method, "local") == 0)
  372. newroot->method = local_method;
  373. else if (strcmp (method, "pserver") == 0)
  374. newroot->method = pserver_method;
  375. else if (strcmp (method, "kserver") == 0)
  376. newroot->method = kserver_method;
  377. else if (strcmp (method, "gserver") == 0)
  378. newroot->method = gserver_method;
  379. else if (strcmp (method, "server") == 0)
  380. newroot->method = server_method;
  381. else if (strcmp (method, "ext") == 0)
  382. newroot->method = ext_method;
  383. else if (strcmp (method, "extssh") == 0)
  384. newroot->method = extssh_method;
  385. else if (strcmp (method, "fork") == 0)
  386. newroot->method = fork_method;
  387. else
  388. {
  389. error (0, 0, "Unknown method (`%s') in CVSROOT.", method);
  390. goto error_exit;
  391. }
  392. }
  393. else
  394. {
  395. /* If the method isn't specified, assume EXT_METHOD if the string looks
  396. like a relative path and LOCAL_METHOD otherwise. */
  397. newroot->method = ((*cvsroot_copy != '/' && strchr (cvsroot_copy, '/'))
  398. ? ext_method
  399. : local_method);
  400. }
  401. newroot->isremote = (newroot->method != local_method);
  402. if ((newroot->method != local_method)
  403. && (newroot->method != fork_method))
  404. {
  405. /* split the string into [[user][:password]@]host[:[port]] & /path
  406. *
  407. * this will allow some characters such as '@' & ':' to remain unquoted
  408. * in the path portion of the spec
  409. */
  410. if ((p = strchr (cvsroot_copy, '/')) == NULL)
  411. {
  412. error (0, 0, "CVSROOT requires a path spec:");
  413. error (0, 0,
  414. ":(gserver|kserver|pserver):[[user][:password]@]host[:[port]]/path");
  415. error (0, 0, "[:(ext|server):][[user]@]host[:]/path");
  416. goto error_exit;
  417. }
  418. firstslash = p; /* == NULL if '/' not in string */
  419. *p = '\0';
  420. /* Don't parse username, password, hostname, or port without client
  421. * support.
  422. */
  423. #ifdef CLIENT_SUPPORT
  424. /* Check to see if there is a username[:password] in the string. */
  425. if ((p = strchr (cvsroot_copy, '@')) != NULL)
  426. {
  427. *p = '\0';
  428. /* check for a password */
  429. if ((q = strchr (cvsroot_copy, ':')) != NULL)
  430. {
  431. *q = '\0';
  432. newroot->password = xstrdup (++q);
  433. /* Don't check for *newroot->password == '\0' since
  434. * a user could conceivably wish to specify a blank password
  435. *
  436. * (newroot->password == NULL means to use the
  437. * password from .cvspass)
  438. */
  439. }
  440. /* copy the username */
  441. if (*cvsroot_copy != '\0')
  442. /* a blank username is impossible, so leave it NULL in that
  443. * case so we know to use the default username
  444. */
  445. newroot->username = xstrdup (cvsroot_copy);
  446. cvsroot_copy = ++p;
  447. }
  448. /* now deal with host[:[port]] */
  449. /* the port */
  450. if ((p = strchr (cvsroot_copy, ':')) != NULL)
  451. {
  452. *p++ = '\0';
  453. if (strlen(p))
  454. {
  455. q = p;
  456. if (*q == '-') q++;
  457. while (*q)
  458. {
  459. if (!isdigit(*q++))
  460. {
  461. error (0, 0,
  462. "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
  463. p);
  464. error (0, 0,
  465. "Perhaps you entered a relative pathname?");
  466. goto error_exit;
  467. }
  468. }
  469. if ((newroot->port = atoi (p)) <= 0)
  470. {
  471. error (0, 0,
  472. "CVSROOT may only specify a positive, non-zero, integer port (not `%s').",
  473. p);
  474. error (0, 0, "Perhaps you entered a relative pathname?");
  475. goto error_exit;
  476. }
  477. }
  478. }
  479. /* copy host */
  480. if (*cvsroot_copy != '\0')
  481. /* blank hostnames are invalid, but for now leave the field NULL
  482. * and catch the error during the sanity checks later
  483. */
  484. newroot->hostname = xstrdup (cvsroot_copy);
  485. /* restore the '/' */
  486. cvsroot_copy = firstslash;
  487. *cvsroot_copy = '/';
  488. #endif /* CLIENT_SUPPORT */
  489. }
  490. /*
  491. * Parse the path for all methods.
  492. */
  493. /* Here & local_cvsroot() should be the only places this needs to be
  494. * called on a CVSROOT now. cvsroot->original is saved for error messages
  495. * and, otherwise, we want no trailing slashes.
  496. */
  497. Sanitize_Repository_Name( cvsroot_copy );
  498. newroot->directory = xstrdup(cvsroot_copy);
  499. /*
  500. * Do various sanity checks.
  501. */
  502. #if ! defined (CLIENT_SUPPORT) && ! defined (DEBUG)
  503. if (newroot->method != local_method)
  504. {
  505. error (0, 0, "CVSROOT is set for a remote access method but your");
  506. error (0, 0, "CVS executable doesn't support it.");
  507. goto error_exit;
  508. }
  509. #endif
  510. #if ! defined (SERVER_SUPPORT) && ! defined (DEBUG)
  511. if (newroot->method == fork_method)
  512. {
  513. error (0, 0, "CVSROOT is set to use the :fork: access method but your");
  514. error (0, 0, "CVS executable doesn't support it.");
  515. goto error_exit;
  516. }
  517. #endif
  518. #ifdef CLIENT_SUPPORT
  519. if (newroot->username && ! newroot->hostname)
  520. {
  521. error (0, 0, "Missing hostname in CVSROOT.");
  522. goto error_exit;
  523. }
  524. check_hostname = 0;
  525. no_password = 1;
  526. no_port = 0;
  527. #endif /* CLIENT_SUPPORT */
  528. switch (newroot->method)
  529. {
  530. case local_method:
  531. #ifdef CLIENT_SUPPORT
  532. if (newroot->username || newroot->hostname)
  533. {
  534. error (0, 0, "Can't specify hostname and username in CVSROOT");
  535. error (0, 0, "when using local access method.");
  536. goto error_exit;
  537. }
  538. no_port = 1;
  539. /* no_password already set */
  540. #endif /* CLIENT_SUPPORT */
  541. /* cvs.texinfo has always told people that CVSROOT must be an
  542. absolute pathname. Furthermore, attempts to use a relative
  543. pathname produced various errors (I couldn't get it to work),
  544. so there would seem to be little risk in making this a fatal
  545. error. */
  546. if (!isabsolute (newroot->directory))
  547. {
  548. error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
  549. newroot->directory);
  550. error (0, 0, "when using local access method.");
  551. goto error_exit;
  552. }
  553. break;
  554. #ifdef CLIENT_SUPPORT
  555. case fork_method:
  556. /* We want :fork: to behave the same as other remote access
  557. methods. Therefore, don't check to see that the repository
  558. name is absolute -- let the server do it. */
  559. if (newroot->username || newroot->hostname)
  560. {
  561. error (0, 0, "Can't specify hostname and username in CVSROOT");
  562. error (0, 0, "when using fork access method.");
  563. goto error_exit;
  564. }
  565. newroot->hostname = xstrdup("server"); /* for error messages */
  566. if (!isabsolute (newroot->directory))
  567. {
  568. error (0, 0, "CVSROOT must be an absolute pathname (not `%s')",
  569. newroot->directory);
  570. error (0, 0, "when using fork access method.");
  571. goto error_exit;
  572. }
  573. no_port = 1;
  574. /* no_password already set */
  575. break;
  576. case kserver_method:
  577. # ifndef HAVE_KERBEROS
  578. error (0, 0, "CVSROOT is set for a kerberos access method but your");
  579. error (0, 0, "CVS executable doesn't support it.");
  580. goto error_exit;
  581. # else
  582. check_hostname = 1;
  583. /* no_password already set */
  584. break;
  585. # endif
  586. case gserver_method:
  587. # ifndef HAVE_GSSAPI
  588. error (0, 0, "CVSROOT is set for a GSSAPI access method but your");
  589. error (0, 0, "CVS executable doesn't support it.");
  590. goto error_exit;
  591. # else
  592. check_hostname = 1;
  593. /* no_password already set */
  594. break;
  595. # endif
  596. case server_method:
  597. case ext_method:
  598. case extssh_method:
  599. no_port = 1;
  600. /* no_password already set */
  601. check_hostname = 1;
  602. break;
  603. case pserver_method:
  604. no_password = 0;
  605. check_hostname = 1;
  606. break;
  607. #endif /* CLIENT_SUPPORT */
  608. default:
  609. error (1, 0, "Invalid method found in parse_cvsroot");
  610. }
  611. #ifdef CLIENT_SUPPORT
  612. if (no_password && newroot->password)
  613. {
  614. error (0, 0, "CVSROOT password specification is only valid for");
  615. error (0, 0, "pserver connection method.");
  616. goto error_exit;
  617. }
  618. if (check_hostname && !newroot->hostname)
  619. {
  620. error (0, 0, "Didn't specify hostname in CVSROOT.");
  621. goto error_exit;
  622. }
  623. if (no_port && newroot->port)
  624. {
  625. error (0, 0, "CVSROOT port specification is only valid for gserver, kserver,");
  626. error (0, 0, "and pserver connection methods.");
  627. goto error_exit;
  628. }
  629. #endif /* CLIENT_SUPPORT */
  630. if (*newroot->directory == '\0')
  631. {
  632. error (0, 0, "Missing directory in CVSROOT.");
  633. goto error_exit;
  634. }
  635. /* Hooray! We finally parsed it! */
  636. free (cvsroot_save);
  637. return newroot;
  638. error_exit:
  639. free (cvsroot_save);
  640. free_cvsroot_t (newroot);
  641. return NULL;
  642. }
  643. #ifdef AUTH_CLIENT_SUPPORT
  644. /* Use root->username, root->hostname, root->port, and root->directory
  645. * to create a normalized CVSROOT fit for the .cvspass file
  646. *
  647. * username defaults to the result of getcaller()
  648. * port defaults to the result of get_cvs_port_number()
  649. *
  650. * FIXME - we could cache the canonicalized version of a root inside the
  651. * cvsroot_t, but we'd have to un'const the input here and stop expecting the
  652. * caller to be responsible for our return value
  653. */
  654. char *
  655. normalize_cvsroot (root)
  656. const cvsroot_t *root;
  657. {
  658. char *cvsroot_canonical;
  659. char *p, *hostname, *username;
  660. char port_s[64];
  661. assert (root && root->hostname && root->directory);
  662. /* get the appropriate port string */
  663. sprintf (port_s, "%d", get_cvs_port_number (root));
  664. /* use a lower case hostname since we know hostnames are case insensitive */
  665. /* Some logic says we should be tacking our domain name on too if it isn't
  666. * there already, but for now this works. Reverse->Forward lookups are
  667. * almost certainly too much since that would make CVS immune to some of
  668. * the DNS trickery that makes life easier for sysadmins when they want to
  669. * move a repository or the like
  670. */
  671. p = hostname = xstrdup(root->hostname);
  672. while (*p)
  673. {
  674. *p = tolower(*p);
  675. p++;
  676. }
  677. /* get the username string */
  678. username = root->username ? root->username : getcaller();
  679. cvsroot_canonical = xmalloc ( strlen(username)
  680. + strlen(hostname) + strlen(port_s)
  681. + strlen(root->directory) + 12);
  682. sprintf (cvsroot_canonical, ":pserver:%s@%s:%s%s",
  683. username, hostname, port_s, root->directory);
  684. free (hostname);
  685. return cvsroot_canonical;
  686. }
  687. #endif /* AUTH_CLIENT_SUPPORT */
  688. /* allocate and return a cvsroot_t structure set up as if we're using the local
  689. * repository DIR. */
  690. cvsroot_t *
  691. local_cvsroot (dir)
  692. const char *dir;
  693. {
  694. cvsroot_t *newroot = new_cvsroot_t();
  695. newroot->original = xstrdup(dir);
  696. newroot->method = local_method;
  697. newroot->directory = xstrdup(dir);
  698. /* Here and parse_cvsroot() should be the only places this needs to be
  699. * called on a CVSROOT now. cvsroot->original is saved for error messages
  700. * and, otherwise, we want no trailing slashes.
  701. */
  702. Sanitize_Repository_Name( newroot->directory );
  703. return newroot;
  704. }
  705. #ifdef DEBUG
  706. /* This is for testing the parsing function. Use
  707. gcc -I. -I.. -I../lib -DDEBUG root.c -o root
  708. to compile. */
  709. #include <stdio.h>
  710. char *program_name = "testing";
  711. char *cvs_cmd_name = "parse_cvsroot"; /* XXX is this used??? */
  712. /* Toy versions of various functions when debugging under unix. Yes,
  713. these make various bad assumptions, but they're pretty easy to
  714. debug when something goes wrong. */
  715. void
  716. error_exit PROTO ((void))
  717. {
  718. exit (1);
  719. }
  720. int
  721. isabsolute (dir)
  722. const char *dir;
  723. {
  724. return (dir && (*dir == '/'));
  725. }
  726. void
  727. main (argc, argv)
  728. int argc;
  729. char *argv[];
  730. {
  731. program_name = argv[0];
  732. if (argc != 2)
  733. {
  734. fprintf (stderr, "Usage: %s <CVSROOT>\n", program_name);
  735. exit (2);
  736. }
  737. if ((current_parsed_root = parse_cvsroot (argv[1])) == NULL)
  738. {
  739. fprintf (stderr, "%s: Parsing failed.\n", program_name);
  740. exit (1);
  741. }
  742. printf ("CVSroot: %s\n", argv[1]);
  743. printf ("current_parsed_root->method: %s\n", method_names[current_parsed_root->method]);
  744. printf ("current_parsed_root->username: %s\n",
  745. current_parsed_root->username ? current_parsed_root->username : "NULL");
  746. printf ("current_parsed_root->hostname: %s\n",
  747. current_parsed_root->hostname ? current_parsed_root->hostname : "NULL");
  748. printf ("current_parsed_root->directory: %s\n", current_parsed_root->directory);
  749. exit (0);
  750. /* NOTREACHED */
  751. }
  752. #endif