ruserpass.c 8.9 KB

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