ruserpass.c 8.1 KB

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