]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/opie/libopie/accessfile.c
Merge lldb trunk r338150, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / opie / libopie / accessfile.c
1 /* accessfile.c: Handle trusted network access file and per-user 
2         overrides.
3
4 %%% portions-copyright-cmetz-96
5 Portions of this software are Copyright 1996-1999 by Craig Metz, All Rights
6 Reserved. The Inner Net License Version 2 applies to these portions of
7 the software.
8 You should have received a copy of the license with this software. If
9 you didn't get a copy, you may request one from <license@inner.net>.
10
11 Portions of this software are Copyright 1995 by Randall Atkinson and Dan
12 McDonald, All Rights Reserved. All Rights under this copyright are assigned
13 to the U.S. Naval Research Laboratory (NRL). The NRL Copyright Notice and
14 License Agreement applies to this software.
15
16         History:
17
18         Modified by cmetz for OPIE 2.31. Include syslog.h on debug.
19         Modified by cmetz for OPIE 2.3. Send debug info to syslog.
20         Modified by cmetz for OPIE 2.2. Use FUNCTION declaration et al.
21                 Ifdef around some headers. Remove extra semicolon.
22         Modified at NRL for OPIE 2.2. Moved from accessfile.c to
23                 libopie/opieaccessfile.c.
24         Modified at NRL for OPIE 2.0.
25         Written at Bellcore for the S/Key Version 1 software distribution
26                 (login.c).
27 */
28 #include "opie_cfg.h"
29
30 #include <stdio.h>
31 #include <ctype.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <netdb.h>
37 #if HAVE_STRING_H
38 #include <string.h>
39 #endif /* HAVE_STRING_H */
40 #if HAVE_UNISTD_H
41 #include <unistd.h>
42 #endif /* HAVE_UNISTD_H */
43 #if HAVE_STDLIB_H
44 #include <stdlib.h>
45 #endif /* HAVE_STDLIB_H */
46
47 #ifdef DEBUG
48 #include <syslog.h>
49 #endif /* DEBUG */
50
51 #include "opie.h"
52
53 int opieaccessfile FUNCTION((host), char *host)
54 {
55 #ifdef PATH_ACCESS_FILE
56 /* Turn host into an IP address and then look it up in the authorization
57  * database to determine if ordinary password logins are OK
58  */
59   long n;
60   struct hostent *hp;
61   FILE *fp;
62   char buf[128], **lp;
63
64 #ifdef DEBUG
65   syslog(LOG_DEBUG, "accessfile: host=%s", host);
66 #endif /* DEBUG */
67   if (!host[0])
68     /* Local login, okay */
69     return (1);
70   if (isaddr(host)) {
71     n = inet_addr(host);
72     return rdnets(n);
73   } else {
74     hp = gethostbyname(host);
75     if (!hp) {
76       printf("Unknown host %s\n", host);
77       return 0;
78     }
79     for (lp = hp->h_addr_list; *lp; lp++) {
80       memcpy((char *) &n, *lp, sizeof(n));
81       if (rdnets(n))
82         return (1);
83     }
84     return (0);
85   }
86 }
87
88 int rdnets FUNCTION((host), long host)
89 {
90   FILE *fp;
91   char buf[128], *cp;
92   long pattern, mask;
93   int permit_it;
94
95   if (!(fp = fopen(PATH_ACCESS_FILE, "r")))
96     return 0;
97
98   while (fgets(buf, sizeof(buf), fp), !feof(fp)) {
99     if (buf[0] == '#')
100       continue; /* Comment */
101     if (!(cp = strtok(buf, " \t")))
102       continue;
103     /* two choices permit of deny */
104     if (strncasecmp(cp, "permit", 4) == 0) {
105       permit_it = 1;
106     } else {
107       if (strncasecmp(cp, "deny", 4) == 0) {
108         permit_it = 0;
109       } else {
110         continue;       /* ignore; it is not permit/deny */
111       }
112     }
113     if (!(cp = strtok(NULL, " \t")))
114       continue; /* Invalid line */
115     pattern = inet_addr(cp);
116     if (!(cp = strtok(NULL, " \t")))
117       continue; /* Invalid line */
118     mask = inet_addr(cp);
119 #ifdef DEBUG
120     syslog(LOG_DEBUG, "accessfile: %08x & %08x == %08x (%s)", host, mask, pattern, ((host & mask) == pattern) ? "true" : "false");
121 #endif /* DEBUG */
122     if ((host & mask) == pattern) {
123       fclose(fp);
124       return permit_it;
125     }
126   }
127   fclose(fp);
128   return 0;
129 }
130
131
132 /* Return TRUE if string appears to be an IP address in dotted decimal;
133  * return FALSE otherwise (i.e., if string is a domain name)
134  */
135 int isaddr FUNCTION((s), register char *s)
136 {
137   char c;
138
139   if (!s)
140     return 1;   /* Can't happen */
141
142   while ((c = *s++) != '\0') {
143     if (c != '[' && c != ']' && !isdigit(c) && c != '.')
144       return 0;
145   }
146   return 1;
147 #else   /* PATH_ACCESS_FILE */
148   return !host[0];
149 #endif  /* PATH_ACCESS_FILE */
150 }
151
152 /* Returns the opposite of what you might expect */
153 /* Returns 1 on error (allow)... this might not be what you want */
154 int opiealways FUNCTION((homedir), char *homedir)
155 {
156   char *opiealwayspath;
157   int i;
158
159   if (!homedir)
160     return 1;
161
162   if (!(opiealwayspath = malloc(strlen(homedir) + sizeof(OPIE_ALWAYS_FILE) + 1)))
163     return 1;
164
165   strcpy(opiealwayspath, homedir);
166   strcat(opiealwayspath, "/");
167   strcat(opiealwayspath, OPIE_ALWAYS_FILE);
168   i = access(opiealwayspath, F_OK);
169   free(opiealwayspath);
170   return (i);
171 }