ruserpass.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. #define __fsetlocking __fsetlocking_internal
  30. #define getgid __getgid
  31. #define getuid __getuid
  32. #define getegid __getegid
  33. #define geteuid __geteuid
  34. #define gethostname __gethostname
  35. #define fileno __fileno
  36. #define __FORCE_GLIBC
  37. #include <features.h>
  38. #include <sys/types.h>
  39. #include <sys/stat.h>
  40. #include <ctype.h>
  41. #include <err.h>
  42. #include <errno.h>
  43. #include <netdb.h>
  44. #include <stdio.h>
  45. #include <stdio_ext.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #include <unistd.h>
  49. extern int __getc_unlocked (FILE *__stream) attribute_hidden;
  50. #define _(X) (X)
  51. /* #include "ftp_var.h" */
  52. static int token (void);
  53. static FILE *cfile;
  54. #define DEFAULT 1
  55. #define LOGIN 2
  56. #define PASSWD 3
  57. #define ACCOUNT 4
  58. #define MACDEF 5
  59. #define ID 10
  60. #define MACHINE 11
  61. static char tokval[100];
  62. static const char tokstr[] =
  63. {
  64. #define TOK_DEFAULT_IDX 0
  65. "default\0"
  66. #define TOK_LOGIN_IDX (TOK_DEFAULT_IDX + sizeof "default")
  67. "login\0"
  68. #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
  69. "password\0"
  70. #define TOK_PASSWD_IDX (TOK_PASSWORD_IDX + sizeof "password")
  71. "passwd\0"
  72. #define TOK_ACCOUNT_IDX (TOK_PASSWD_IDX + sizeof "passwd")
  73. "account\0"
  74. #define TOK_MACHINE_IDX (TOK_ACCOUNT_IDX + sizeof "account")
  75. "machine\0"
  76. #define TOK_MACDEF_IDX (TOK_MACHINE_IDX + sizeof "machine")
  77. "macdef"
  78. };
  79. static const struct toktab {
  80. int tokstr_off;
  81. int tval;
  82. } toktab[]= {
  83. { TOK_DEFAULT_IDX, DEFAULT },
  84. { TOK_LOGIN_IDX, LOGIN },
  85. { TOK_PASSWORD_IDX, PASSWD },
  86. { TOK_PASSWD_IDX, PASSWD },
  87. { TOK_ACCOUNT_IDX, ACCOUNT },
  88. { TOK_MACHINE_IDX, MACHINE },
  89. { TOK_MACDEF_IDX, MACDEF }
  90. };
  91. int attribute_hidden __ruserpass(const char *host, const char **aname, const char **apass)
  92. {
  93. char *hdir, *buf, *tmp;
  94. char myname[1024], *mydomain;
  95. int t, usedefault = 0;
  96. struct stat stb;
  97. /* Give up when running a setuid or setgid app. */
  98. if ((getuid() != geteuid()) || getgid() != getegid())
  99. return -1;
  100. hdir = __getenv("HOME");
  101. if (hdir == NULL) {
  102. /* If we can't get HOME, fail instead of trying ".",
  103. which is no improvement. */
  104. return -1;
  105. }
  106. buf = alloca (__strlen(hdir) + 8);
  107. __strcpy(buf, hdir);
  108. __strcat(buf, "/.netrc");
  109. cfile = fopen(buf, "r");
  110. if (cfile == NULL) {
  111. if (errno != ENOENT)
  112. __printf("%s", buf);
  113. return (0);
  114. }
  115. /* No threads use this stream. */
  116. #ifdef __UCLIBC_HAS_THREADS__
  117. __fsetlocking (cfile, FSETLOCKING_BYCALLER);
  118. #endif
  119. if (gethostname(myname, sizeof(myname)) < 0)
  120. myname[0] = '\0';
  121. mydomain = __strchr(myname, '.');
  122. if (mydomain==NULL) {
  123. mydomain=myname + __strlen(myname);
  124. }
  125. next:
  126. while ((t = token())) switch(t) {
  127. case DEFAULT:
  128. usedefault = 1;
  129. /* FALL THROUGH */
  130. case MACHINE:
  131. if (!usedefault) {
  132. if (token() != ID)
  133. continue;
  134. /*
  135. * Allow match either for user's input host name
  136. * or official hostname. Also allow match of
  137. * incompletely-specified host in local domain.
  138. */
  139. if (__strcasecmp(host, tokval) == 0)
  140. goto match;
  141. if ((tmp = __strchr(host, '.')) != NULL &&
  142. __strcasecmp(tmp, mydomain) == 0 &&
  143. __strncasecmp(host, tokval, tmp - host) == 0 &&
  144. tokval[tmp - host] == '\0')
  145. goto match;
  146. continue;
  147. }
  148. match:
  149. while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
  150. case LOGIN:
  151. if (token()) {
  152. if (*aname == 0) {
  153. char *newp;
  154. newp = malloc((unsigned) __strlen(tokval) + 1);
  155. if (newp == NULL)
  156. {
  157. __printf(_("out of memory"));
  158. goto bad;
  159. }
  160. *aname = __strcpy(newp, tokval);
  161. } else {
  162. if (__strcmp(*aname, tokval))
  163. goto next;
  164. }
  165. }
  166. break;
  167. case PASSWD:
  168. if (__strcmp(*aname, "anonymous") &&
  169. __fstat(fileno(cfile), &stb) >= 0 &&
  170. (stb.st_mode & 077) != 0) {
  171. __printf(_("Error: .netrc file is readable by others."));
  172. __printf(_("Remove password or make file unreadable by others."));
  173. goto bad;
  174. }
  175. if (token() && *apass == 0) {
  176. char *newp;
  177. newp = malloc((unsigned) __strlen(tokval) + 1);
  178. if (newp == NULL)
  179. {
  180. __printf(_("out of memory"));
  181. goto bad;
  182. }
  183. *apass = __strcpy(newp, tokval);
  184. }
  185. break;
  186. case ACCOUNT:
  187. #if 0
  188. if (__fstat(fileno(cfile), &stb) >= 0
  189. && (stb.st_mode & 077) != 0) {
  190. __printf("Error: .netrc file is readable by others.");
  191. __printf("Remove account or make file unreadable by others.");
  192. goto bad;
  193. }
  194. if (token() && *aacct == 0) {
  195. *aacct = malloc((unsigned) __strlen(tokval) + 1);
  196. (void) __strcpy(*aacct, tokval);
  197. }
  198. #endif
  199. break;
  200. case MACDEF:
  201. #if 0
  202. if (proxy) {
  203. (void) fclose(cfile);
  204. return (0);
  205. }
  206. while ((c=__getc_unlocked(cfile)) != EOF && c == ' '
  207. || c == '\t');
  208. if (c == EOF || c == '\n') {
  209. __printf("Missing macdef name argument.\n");
  210. goto bad;
  211. }
  212. if (macnum == 16) {
  213. __printf("Limit of 16 macros have already been defined\n");
  214. goto bad;
  215. }
  216. tmp = macros[macnum].mac_name;
  217. *tmp++ = c;
  218. for (i=0; i < 8 && (c=__getc_unlocked(cfile)) != EOF &&
  219. !isspace(c); ++i) {
  220. *tmp++ = c;
  221. }
  222. if (c == EOF) {
  223. __printf("Macro definition missing null line terminator.\n");
  224. goto bad;
  225. }
  226. *tmp = '\0';
  227. if (c != '\n') {
  228. while ((c=__getc_unlocked(cfile)) != EOF
  229. && c != '\n');
  230. }
  231. if (c == EOF) {
  232. __printf("Macro definition missing null line terminator.\n");
  233. goto bad;
  234. }
  235. if (macnum == 0) {
  236. macros[macnum].mac_start = macbuf;
  237. }
  238. else {
  239. macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
  240. }
  241. tmp = macros[macnum].mac_start;
  242. while (tmp != macbuf + 4096) {
  243. if ((c=__getc_unlocked(cfile)) == EOF) {
  244. __printf("Macro definition missing null line terminator.\n");
  245. goto bad;
  246. }
  247. *tmp = c;
  248. if (*tmp == '\n') {
  249. if (*(tmp-1) == '\0') {
  250. macros[macnum++].mac_end = tmp - 1;
  251. break;
  252. }
  253. *tmp = '\0';
  254. }
  255. tmp++;
  256. }
  257. if (tmp == macbuf + 4096) {
  258. __printf("4K macro buffer exceeded\n");
  259. goto bad;
  260. }
  261. #endif
  262. break;
  263. default:
  264. __printf(_("Unknown .netrc keyword %s"), tokval);
  265. break;
  266. }
  267. goto done;
  268. }
  269. done:
  270. (void) fclose(cfile);
  271. return (0);
  272. bad:
  273. (void) fclose(cfile);
  274. return (-1);
  275. }
  276. strong_alias(__ruserpass,ruserpass)
  277. static int
  278. token()
  279. {
  280. char *cp;
  281. int c;
  282. int i;
  283. if (feof_unlocked(cfile) || ferror_unlocked(cfile))
  284. return (0);
  285. while ((c = __getc_unlocked(cfile)) != EOF &&
  286. (c == '\n' || c == '\t' || c == ' ' || c == ','))
  287. continue;
  288. if (c == EOF)
  289. return (0);
  290. cp = tokval;
  291. if (c == '"') {
  292. while ((c = __getc_unlocked(cfile)) != EOF && c != '"') {
  293. if (c == '\\')
  294. c = __getc_unlocked(cfile);
  295. *cp++ = c;
  296. }
  297. } else {
  298. *cp++ = c;
  299. while ((c = __getc_unlocked(cfile)) != EOF
  300. && c != '\n' && c != '\t' && c != ' ' && c != ',') {
  301. if (c == '\\')
  302. c = __getc_unlocked(cfile);
  303. *cp++ = c;
  304. }
  305. }
  306. *cp = 0;
  307. if (tokval[0] == 0)
  308. return (0);
  309. for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
  310. if (!__strcmp(&tokstr[toktab[i].tokstr_off], tokval))
  311. return toktab[i].tval;
  312. return (ID);
  313. }