ruserpass.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #define _(X) (X)
  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. /* ruserpass - remote password check.
  82. This function also exists in glibc but is undocumented */
  83. int ruserpass(const char *host, const char **aname, const char **apass)
  84. {
  85. char *hdir, *buf, *tmp;
  86. char myname[1024], *mydomain;
  87. int t, usedefault = 0;
  88. struct stat stb;
  89. /* Give up when running a setuid or setgid app. */
  90. if ((getuid() != geteuid()) || getgid() != getegid())
  91. return -1;
  92. hdir = getenv("HOME");
  93. if (hdir == NULL) {
  94. /* If we can't get HOME, fail instead of trying ".",
  95. which is no improvement. */
  96. return -1;
  97. }
  98. buf = alloca (strlen(hdir) + 8);
  99. strcpy(buf, hdir);
  100. strcat(buf, "/.netrc");
  101. cfile = fopen(buf, "r");
  102. if (cfile == NULL) {
  103. if (errno != ENOENT)
  104. printf("%s", buf);
  105. return (0);
  106. }
  107. /* No threads use this stream. */
  108. #ifdef __UCLIBC_HAS_THREADS__
  109. __fsetlocking (cfile, FSETLOCKING_BYCALLER);
  110. #endif
  111. if (gethostname(myname, sizeof(myname)) < 0)
  112. myname[0] = '\0';
  113. mydomain = strchr(myname, '.');
  114. if (mydomain==NULL) {
  115. mydomain=myname + strlen(myname);
  116. }
  117. next:
  118. while ((t = token())) switch(t) {
  119. case DEFAULT:
  120. usedefault = 1;
  121. /* FALL THROUGH */
  122. case MACHINE:
  123. if (!usedefault) {
  124. if (token() != ID)
  125. continue;
  126. /*
  127. * Allow match either for user's input host name
  128. * or official hostname. Also allow match of
  129. * incompletely-specified host in local domain.
  130. */
  131. if (strcasecmp(host, tokval) == 0)
  132. goto match;
  133. if ((tmp = strchr(host, '.')) != NULL &&
  134. strcasecmp(tmp, mydomain) == 0 &&
  135. strncasecmp(host, tokval, tmp - host) == 0 &&
  136. tokval[tmp - host] == '\0')
  137. goto match;
  138. continue;
  139. }
  140. match:
  141. while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
  142. case LOGIN:
  143. if (token()) {
  144. if (*aname == 0) {
  145. char *newp;
  146. newp = malloc((unsigned) strlen(tokval) + 1);
  147. if (newp == NULL)
  148. {
  149. printf(_("out of memory"));
  150. goto bad;
  151. }
  152. *aname = strcpy(newp, tokval);
  153. } else {
  154. if (strcmp(*aname, tokval))
  155. goto next;
  156. }
  157. }
  158. break;
  159. case PASSWD:
  160. if (strcmp(*aname, "anonymous") &&
  161. fstat(fileno(cfile), &stb) >= 0 &&
  162. (stb.st_mode & 077) != 0) {
  163. printf(_("Error: .netrc file is readable by others."));
  164. printf(_("Remove password or make file unreadable by others."));
  165. goto bad;
  166. }
  167. if (token() && *apass == 0) {
  168. char *newp;
  169. newp = malloc((unsigned) strlen(tokval) + 1);
  170. if (newp == NULL)
  171. {
  172. printf(_("out of memory"));
  173. goto bad;
  174. }
  175. *apass = strcpy(newp, tokval);
  176. }
  177. break;
  178. case ACCOUNT:
  179. #if 0
  180. if (fstat(fileno(cfile), &stb) >= 0
  181. && (stb.st_mode & 077) != 0) {
  182. printf("Error: .netrc file is readable by others.");
  183. printf("Remove account or make file unreadable by others.");
  184. goto bad;
  185. }
  186. if (token() && *aacct == 0) {
  187. *aacct = malloc((unsigned) strlen(tokval) + 1);
  188. (void) strcpy(*aacct, tokval);
  189. }
  190. #endif
  191. break;
  192. case MACDEF:
  193. #if 0
  194. if (proxy) {
  195. (void) fclose(cfile);
  196. return (0);
  197. }
  198. while ((c=getc_unlocked(cfile)) != EOF && c == ' '
  199. || c == '\t');
  200. if (c == EOF || c == '\n') {
  201. printf("Missing macdef name argument.\n");
  202. goto bad;
  203. }
  204. if (macnum == 16) {
  205. printf("Limit of 16 macros have already been defined\n");
  206. goto bad;
  207. }
  208. tmp = macros[macnum].mac_name;
  209. *tmp++ = c;
  210. for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
  211. !isspace(c); ++i) {
  212. *tmp++ = c;
  213. }
  214. if (c == EOF) {
  215. printf("Macro definition missing null line terminator.\n");
  216. goto bad;
  217. }
  218. *tmp = '\0';
  219. if (c != '\n') {
  220. while ((c=getc_unlocked(cfile)) != EOF
  221. && c != '\n');
  222. }
  223. if (c == EOF) {
  224. printf("Macro definition missing null line terminator.\n");
  225. goto bad;
  226. }
  227. if (macnum == 0) {
  228. macros[macnum].mac_start = macbuf;
  229. }
  230. else {
  231. macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
  232. }
  233. tmp = macros[macnum].mac_start;
  234. while (tmp != macbuf + 4096) {
  235. if ((c=getc_unlocked(cfile)) == EOF) {
  236. printf("Macro definition missing null line terminator.\n");
  237. goto bad;
  238. }
  239. *tmp = c;
  240. if (*tmp == '\n') {
  241. if (*(tmp-1) == '\0') {
  242. macros[macnum++].mac_end = tmp - 1;
  243. break;
  244. }
  245. *tmp = '\0';
  246. }
  247. tmp++;
  248. }
  249. if (tmp == macbuf + 4096) {
  250. printf("4K macro buffer exceeded\n");
  251. goto bad;
  252. }
  253. #endif
  254. break;
  255. default:
  256. printf(_("Unknown .netrc keyword %s"), tokval);
  257. break;
  258. }
  259. goto done;
  260. }
  261. done:
  262. (void) fclose(cfile);
  263. return (0);
  264. bad:
  265. (void) fclose(cfile);
  266. return (-1);
  267. }
  268. libc_hidden_def(ruserpass)
  269. static int
  270. token(void)
  271. {
  272. char *cp;
  273. int c;
  274. int i;
  275. if (feof_unlocked(cfile) || ferror_unlocked(cfile))
  276. return (0);
  277. while ((c = getc_unlocked(cfile)) != EOF &&
  278. (c == '\n' || c == '\t' || c == ' ' || c == ','))
  279. continue;
  280. if (c == EOF)
  281. return (0);
  282. cp = tokval;
  283. if (c == '"') {
  284. while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
  285. if (c == '\\')
  286. c = getc_unlocked(cfile);
  287. *cp++ = c;
  288. }
  289. } else {
  290. *cp++ = c;
  291. while ((c = getc_unlocked(cfile)) != EOF
  292. && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  293. if (c == '\\')
  294. c = getc_unlocked(cfile);
  295. *cp++ = c;
  296. }
  297. }
  298. *cp = 0;
  299. if (tokval[0] == 0)
  300. return (0);
  301. for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
  302. if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
  303. return toktab[i].tval;
  304. return (ID);
  305. }