ruserpass.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 1985, 1993, 1994
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <ctype.h>
  32. #include <err.h>
  33. #include <errno.h>
  34. #include <netdb.h>
  35. #include <stdio.h>
  36. #include <stdio_ext.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <unistd.h>
  40. #include <libintl.h>
  41. /* #include "ftp_var.h" */
  42. static int token (void);
  43. static FILE *cfile;
  44. #define DEFAULT 1
  45. #define LOGIN 2
  46. #define PASSWD 3
  47. #define ACCOUNT 4
  48. #define MACDEF 5
  49. #define ID 10
  50. #define MACHINE 11
  51. static char tokval[100];
  52. static const char tokstr[] =
  53. {
  54. #define TOK_DEFAULT_IDX 0
  55. "default\0"
  56. #define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
  57. "login\0"
  58. #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
  59. "password\0"
  60. #define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
  61. "passwd\0"
  62. #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
  63. "account\0"
  64. #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
  65. "machine\0"
  66. #define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
  67. "macdef"
  68. };
  69. static const struct toktab {
  70. int tokstr_off;
  71. int tval;
  72. } toktab[]= {
  73. { TOK_DEFAULT_IDX, DEFAULT },
  74. { TOK_LOGIN_IDX, LOGIN },
  75. { TOK_PASSWORD_IDX, PASSWD },
  76. { TOK_PASSWD_IDX, PASSWD },
  77. { TOK_ACCOUNT_IDX, ACCOUNT },
  78. { TOK_MACHINE_IDX, MACHINE },
  79. { TOK_MACDEF_IDX, MACDEF }
  80. };
  81. int
  82. ruserpass (const char *host, const char **aname, const char **apass)
  83. {
  84. char *hdir, *buf, *tmp;
  85. char myname[1024], *mydomain;
  86. int t, usedefault = 0;
  87. struct __stat64_t64 stb;
  88. hdir = __libc_secure_getenv("HOME");
  89. if (hdir == NULL) {
  90. /* If we can't get HOME, fail instead of trying ".",
  91. which is no improvement. This really should call
  92. getpwuid(getuid()). */
  93. /*hdir = ".";*/
  94. return -1;
  95. }
  96. buf = alloca (strlen (hdir) + 8);
  97. __stpcpy (__stpcpy (buf, hdir), "/.netrc");
  98. cfile = fopen(buf, "rce");
  99. if (cfile == NULL) {
  100. if (errno != ENOENT)
  101. warn("%s", buf);
  102. return (0);
  103. }
  104. /* No threads use this stream. */
  105. __fsetlocking (cfile, FSETLOCKING_BYCALLER);
  106. if (__gethostname(myname, sizeof(myname)) < 0)
  107. myname[0] = '\0';
  108. mydomain = __strchrnul(myname, '.');
  109. next:
  110. while ((t = token())) switch(t) {
  111. case DEFAULT:
  112. usedefault = 1;
  113. [[fallthrough]];
  114. case MACHINE:
  115. if (!usedefault) {
  116. if (token() != ID)
  117. continue;
  118. /*
  119. * Allow match either for user's input host name
  120. * or official hostname. Also allow match of
  121. * incompletely-specified host in local domain.
  122. */
  123. if (__strcasecmp(host, tokval) == 0)
  124. goto match;
  125. /* if (__strcasecmp(hostname, tokval) == 0)
  126. goto match;
  127. if ((tmp = strchr(hostname, '.')) != NULL &&
  128. __strcasecmp(tmp, mydomain) == 0 &&
  129. __strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
  130. tokval[tmp - hostname] == '\0')
  131. goto match; */
  132. if ((tmp = strchr(host, '.')) != NULL &&
  133. __strcasecmp(tmp, mydomain) == 0 &&
  134. __strncasecmp(host, tokval, tmp - host) == 0 &&
  135. tokval[tmp - host] == '\0')
  136. goto match;
  137. continue;
  138. }
  139. match:
  140. while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
  141. case LOGIN:
  142. if (token()) {
  143. if (*aname == NULL) {
  144. char *newp;
  145. newp = malloc((unsigned) strlen(tokval) + 1);
  146. if (newp == NULL)
  147. {
  148. warnx(_("out of memory"));
  149. goto bad;
  150. }
  151. *aname = strcpy(newp, tokval);
  152. } else {
  153. if (strcmp(*aname, tokval))
  154. goto next;
  155. }
  156. }
  157. break;
  158. case PASSWD:
  159. if (strcmp(*aname, "anonymous") &&
  160. __fstat64_time64(fileno(cfile), &stb) >= 0 &&
  161. (stb.st_mode & 077) != 0) {
  162. warnx(_("Error: .netrc file is readable by others."));
  163. warnx(_("Remove 'password' line or make file unreadable by others."));
  164. goto bad;
  165. }
  166. if (token() && *apass == NULL) {
  167. char *newp;
  168. newp = malloc((unsigned) strlen(tokval) + 1);
  169. if (newp == NULL)
  170. {
  171. warnx(_("out of memory"));
  172. goto bad;
  173. }
  174. *apass = strcpy(newp, tokval);
  175. }
  176. break;
  177. case ACCOUNT:
  178. break;
  179. case MACDEF:
  180. break;
  181. default:
  182. warnx(_("Unknown .netrc keyword %s"), tokval);
  183. break;
  184. }
  185. goto done;
  186. }
  187. done:
  188. (void) fclose(cfile);
  189. return (0);
  190. bad:
  191. (void) fclose(cfile);
  192. return (-1);
  193. }
  194. libc_hidden_def (ruserpass)
  195. static int
  196. token (void)
  197. {
  198. char *cp;
  199. int c;
  200. int i;
  201. if (feof_unlocked(cfile) || ferror_unlocked(cfile))
  202. return (0);
  203. while ((c = getc_unlocked(cfile)) != EOF &&
  204. (c == '\n' || c == '\t' || c == ' ' || c == ','))
  205. continue;
  206. if (c == EOF)
  207. return (0);
  208. cp = tokval;
  209. if (c == '"') {
  210. while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
  211. if (c == '\\')
  212. c = getc_unlocked(cfile);
  213. *cp++ = c;
  214. }
  215. } else {
  216. *cp++ = c;
  217. while ((c = getc_unlocked(cfile)) != EOF
  218. && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  219. if (c == '\\')
  220. c = getc_unlocked(cfile);
  221. *cp++ = c;
  222. }
  223. }
  224. *cp = 0;
  225. if (tokval[0] == 0)
  226. return (0);
  227. for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
  228. if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
  229. return toktab[i].tval;
  230. return (ID);
  231. }