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