Back to index

glibc  2.9
ruserpass.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1985, 1993, 1994
00003  *     The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #if defined(LIBC_SCCS) && !defined(lint)
00031 static char sccsid[] = "@(#)ruserpass.c   8.3 (Berkeley) 4/2/94";
00032 #endif /* not lint */
00033 
00034 #include <sys/types.h>
00035 #include <sys/stat.h>
00036 
00037 #include <ctype.h>
00038 #include <err.h>
00039 #include <errno.h>
00040 #include <netdb.h>
00041 #include <stdio.h>
00042 #include <stdio_ext.h>
00043 #include <stdlib.h>
00044 #include <string.h>
00045 #include <unistd.h>
00046 #include <libintl.h>
00047 
00048 /* #include "ftp_var.h" */
00049 
00050 static int token (void);
00051 static FILE *cfile;
00052 
00053 #define       DEFAULT       1
00054 #define       LOGIN  2
00055 #define       PASSWD 3
00056 #define       ACCOUNT 4
00057 #define MACDEF  5
00058 #define       ID     10
00059 #define       MACHINE       11
00060 
00061 static char tokval[100];
00062 
00063 static const char tokstr[] =
00064 {
00065 #define TOK_DEFAULT_IDX     0
00066   "default\0"
00067 #define TOK_LOGIN_IDX       (TOK_DEFAULT_IDX + sizeof "default")
00068   "login\0"
00069 #define TOK_PASSWORD_IDX (TOK_LOGIN_IDX + sizeof "login")
00070   "password\0"
00071 #define TOK_PASSWD_IDX      (TOK_PASSWORD_IDX + sizeof "password")
00072   "passwd\0"
00073 #define TOK_ACCOUNT_IDX     (TOK_PASSWD_IDX + sizeof "passwd")
00074   "account\0"
00075 #define TOK_MACHINE_IDX     (TOK_ACCOUNT_IDX + sizeof "account")
00076   "machine\0"
00077 #define TOK_MACDEF_IDX      (TOK_MACHINE_IDX + sizeof "machine")
00078   "macdef"
00079 };
00080 
00081 static const struct toktab {
00082        int tokstr_off;
00083        int tval;
00084 } toktab[]= {
00085        { TOK_DEFAULT_IDX,   DEFAULT },
00086        { TOK_LOGIN_IDX,     LOGIN },
00087        { TOK_PASSWORD_IDX,  PASSWD },
00088        { TOK_PASSWD_IDX,    PASSWD },
00089        { TOK_ACCOUNT_IDX,   ACCOUNT },
00090        { TOK_MACHINE_IDX,   MACHINE },
00091        { TOK_MACDEF_IDX,    MACDEF }
00092 };
00093 
00094 
00095 
00096 int
00097 ruserpass(host, aname, apass)
00098        const char *host, **aname, **apass;
00099 {
00100        char *hdir, *buf, *tmp;
00101        char myname[1024], *mydomain;
00102        int t, usedefault = 0;
00103        struct stat64 stb;
00104 
00105        hdir = __secure_getenv("HOME");
00106        if (hdir == NULL) {
00107               /* If we can't get HOME, fail instead of trying ".",
00108                  which is no improvement. This really should call
00109                  getpwuid(getuid()).  */
00110               /*hdir = ".";*/
00111               return -1;
00112        }
00113 
00114        buf = alloca (strlen (hdir) + 8);
00115 
00116        __stpcpy (__stpcpy (buf, hdir), "/.netrc");
00117        cfile = fopen(buf, "rc");
00118        if (cfile == NULL) {
00119               if (errno != ENOENT)
00120                      warn("%s", buf);
00121               return (0);
00122        }
00123        /* No threads use this stream.  */
00124        __fsetlocking (cfile, FSETLOCKING_BYCALLER);
00125        if (__gethostname(myname, sizeof(myname)) < 0)
00126               myname[0] = '\0';
00127        mydomain = __strchrnul(myname, '.');
00128 next:
00129        while ((t = token())) switch(t) {
00130 
00131        case DEFAULT:
00132               usedefault = 1;
00133               /* FALL THROUGH */
00134 
00135        case MACHINE:
00136               if (!usedefault) {
00137                      if (token() != ID)
00138                             continue;
00139                      /*
00140                       * Allow match either for user's input host name
00141                       * or official hostname.  Also allow match of
00142                       * incompletely-specified host in local domain.
00143                       */
00144                      if (__strcasecmp(host, tokval) == 0)
00145                             goto match;
00146 /*                   if (__strcasecmp(hostname, tokval) == 0)
00147                             goto match;
00148                      if ((tmp = strchr(hostname, '.')) != NULL &&
00149                          __strcasecmp(tmp, mydomain) == 0 &&
00150                          __strncasecmp(hostname, tokval, tmp-hostname) == 0 &&
00151                          tokval[tmp - hostname] == '\0')
00152                             goto match; */
00153                      if ((tmp = strchr(host, '.')) != NULL &&
00154                          __strcasecmp(tmp, mydomain) == 0 &&
00155                          __strncasecmp(host, tokval, tmp - host) == 0 &&
00156                          tokval[tmp - host] == '\0')
00157                             goto match;
00158                      continue;
00159               }
00160        match:
00161               while ((t = token()) && t != MACHINE && t != DEFAULT) switch(t) {
00162 
00163               case LOGIN:
00164                      if (token()) {
00165                             if (*aname == 0) {
00166                               char *newp;
00167                               newp = malloc((unsigned) strlen(tokval) + 1);
00168                               if (newp == NULL)
00169                                 {
00170                                   warnx(_("out of memory"));
00171                                   goto bad;
00172                                 }
00173                               *aname = strcpy(newp, tokval);
00174                             } else {
00175                                    if (strcmp(*aname, tokval))
00176                                           goto next;
00177                             }
00178                      }
00179                      break;
00180               case PASSWD:
00181                      if (strcmp(*aname, "anonymous") &&
00182                          fstat64(fileno(cfile), &stb) >= 0 &&
00183                          (stb.st_mode & 077) != 0) {
00184        warnx(_("Error: .netrc file is readable by others."));
00185        warnx(_("Remove password or make file unreadable by others."));
00186                             goto bad;
00187                      }
00188                      if (token() && *apass == 0) {
00189                             char *newp;
00190                             newp = malloc((unsigned) strlen(tokval) + 1);
00191                             if (newp == NULL)
00192                               {
00193                                 warnx(_("out of memory"));
00194                                 goto bad;
00195                               }
00196                             *apass = strcpy(newp, tokval);
00197                      }
00198                      break;
00199               case ACCOUNT:
00200 #if 0
00201                      if (fstat64(fileno(cfile), &stb) >= 0
00202                          && (stb.st_mode & 077) != 0) {
00203        warnx("Error: .netrc file is readable by others.");
00204        warnx("Remove account or make file unreadable by others.");
00205                             goto bad;
00206                      }
00207                      if (token() && *aacct == 0) {
00208                             *aacct = malloc((unsigned) strlen(tokval) + 1);
00209                             (void) strcpy(*aacct, tokval);
00210                      }
00211 #endif
00212                      break;
00213               case MACDEF:
00214 #if 0
00215                      if (proxy) {
00216                             (void) fclose(cfile);
00217                             return (0);
00218                      }
00219                      while ((c=getc_unlocked(cfile)) != EOF && c == ' '
00220                             || c == '\t');
00221                      if (c == EOF || c == '\n') {
00222                             printf("Missing macdef name argument.\n");
00223                             goto bad;
00224                      }
00225                      if (macnum == 16) {
00226                             printf("Limit of 16 macros have already been defined\n");
00227                             goto bad;
00228                      }
00229                      tmp = macros[macnum].mac_name;
00230                      *tmp++ = c;
00231                      for (i=0; i < 8 && (c=getc_unlocked(cfile)) != EOF &&
00232                          !isspace(c); ++i) {
00233                             *tmp++ = c;
00234                      }
00235                      if (c == EOF) {
00236                             printf("Macro definition missing null line terminator.\n");
00237                             goto bad;
00238                      }
00239                      *tmp = '\0';
00240                      if (c != '\n') {
00241                             while ((c=getc_unlocked(cfile)) != EOF
00242                                    && c != '\n');
00243                      }
00244                      if (c == EOF) {
00245                             printf("Macro definition missing null line terminator.\n");
00246                             goto bad;
00247                      }
00248                      if (macnum == 0) {
00249                             macros[macnum].mac_start = macbuf;
00250                      }
00251                      else {
00252                             macros[macnum].mac_start = macros[macnum-1].mac_end + 1;
00253                      }
00254                      tmp = macros[macnum].mac_start;
00255                      while (tmp != macbuf + 4096) {
00256                             if ((c=getc_unlocked(cfile)) == EOF) {
00257                             printf("Macro definition missing null line terminator.\n");
00258                                    goto bad;
00259                             }
00260                             *tmp = c;
00261                             if (*tmp == '\n') {
00262                                    if (*(tmp-1) == '\0') {
00263                                       macros[macnum++].mac_end = tmp - 1;
00264                                       break;
00265                                    }
00266                                    *tmp = '\0';
00267                             }
00268                             tmp++;
00269                      }
00270                      if (tmp == macbuf + 4096) {
00271                             printf("4K macro buffer exceeded\n");
00272                             goto bad;
00273                      }
00274 #endif
00275                      break;
00276               default:
00277                      warnx(_("Unknown .netrc keyword %s"), tokval);
00278                      break;
00279               }
00280               goto done;
00281        }
00282 done:
00283        (void) fclose(cfile);
00284        return (0);
00285 bad:
00286        (void) fclose(cfile);
00287        return (-1);
00288 }
00289 libc_hidden_def (ruserpass)
00290 
00291 static int
00292 token()
00293 {
00294        char *cp;
00295        int c;
00296        int i;
00297 
00298        if (feof_unlocked(cfile) || ferror_unlocked(cfile))
00299               return (0);
00300        while ((c = getc_unlocked(cfile)) != EOF &&
00301            (c == '\n' || c == '\t' || c == ' ' || c == ','))
00302               continue;
00303        if (c == EOF)
00304               return (0);
00305        cp = tokval;
00306        if (c == '"') {
00307               while ((c = getc_unlocked(cfile)) != EOF && c != '"') {
00308                      if (c == '\\')
00309                             c = getc_unlocked(cfile);
00310                      *cp++ = c;
00311               }
00312        } else {
00313               *cp++ = c;
00314               while ((c = getc_unlocked(cfile)) != EOF
00315                   && c != '\n' && c != '\t' && c != ' ' && c != ',') {
00316                      if (c == '\\')
00317                             c = getc_unlocked(cfile);
00318                      *cp++ = c;
00319               }
00320        }
00321        *cp = 0;
00322        if (tokval[0] == 0)
00323               return (0);
00324        for (i = 0; i < (int) (sizeof (toktab) / sizeof (toktab[0])); ++i)
00325               if (!strcmp(&tokstr[toktab[i].tokstr_off], tokval))
00326                      return toktab[i].tval;
00327        return (ID);
00328 }