ruserpass.c 8.3 KB

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