ruserpass.c 9.0 KB

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