ruserpass.c 8.1 KB

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