ruserpass.c 8.5 KB

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