ruserpass.c 9.0 KB

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