ruserpass.c 8.3 KB

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