]> git.decadent.org.uk Git - nfs-utils.git/blob - support/misc/tcpwrapper.c
2000-08-25 H.J. Lu <hjl@lucon.org>
[nfs-utils.git] / support / misc / tcpwrapper.c
1 /* This is copied from portmap 4.0-29 in RedHat. */
2
3  /*
4   * pmap_check - additional portmap security.
5   * 
6   * Always reject non-local requests to update the portmapper tables.
7   * 
8   * Refuse to forward mount requests to the nfs mount daemon. Otherwise, the
9   * requests would appear to come from the local system, and nfs export
10   * restrictions could be bypassed.
11   * 
12   * Refuse to forward requests to the nfsd process.
13   * 
14   * Refuse to forward requests to NIS (YP) daemons; The only exception is the
15   * YPPROC_DOMAIN_NONACK broadcast rpc call that is used to establish initial
16   * contact with the NIS server.
17   * 
18   * Always allocate an unprivileged port when forwarding a request.
19   * 
20   * If compiled with -DCHECK_PORT, require that requests to register or
21   * unregister a privileged port come from a privileged port. This makes it
22   * more difficult to replace a critical service by a trojan.
23   * 
24   * If compiled with -DHOSTS_ACCESS, reject requests from hosts that are not
25   * authorized by the /etc/hosts.{allow,deny} files. The local system is
26   * always treated as an authorized host. The access control tables are never
27   * consulted for requests from the local system, and are always consulted
28   * for requests from other hosts. Access control is based on IP addresses
29   * only; attempts to map an address to a host name might cause the
30   * portmapper to hang.
31   * 
32   * Author: Wietse Venema (wietse@wzv.win.tue.nl), dept. of Mathematics and
33   * Computing Science, Eindhoven University of Technology, The Netherlands.
34   */
35
36 #include "tcpwrapper.h"
37
38 #include <unistd.h>
39 #include <string.h>
40 #include <rpc/rpc.h>
41 #include <rpc/pmap_prot.h>
42 #include <syslog.h>
43 #include <netdb.h>
44 #include <pwd.h>
45 #include <sys/types.h>
46 #include <sys/signal.h>
47 #ifdef SYSV40
48 #include <netinet/in.h>
49 #include <rpc/rpcent.h>
50 #endif
51
52 static void logit();
53 static void toggle_verboselog();
54 int     verboselog = 0;
55 int     allow_severity = LOG_INFO;
56 int     deny_severity = LOG_WARNING;
57
58 /* A handful of macros for "readability". */
59
60 /* coming from libwrap.a (tcp_wrappers) */
61 extern int hosts_ctl(char *daemon, char *name, char *addr, char *user);
62
63 #define legal_port(a,p) \
64   (ntohs((a)->sin_port) < IPPORT_RESERVED || (p) >= IPPORT_RESERVED)
65
66 #define log_bad_port(addr, proc, prog) \
67   logit(deny_severity, addr, proc, prog, ": request from unprivileged port")
68
69 #define log_bad_host(addr, proc, prog) \
70   logit(deny_severity, addr, proc, prog, ": request from unauthorized host")
71
72 #define log_bad_owner(addr, proc, prog) \
73   logit(deny_severity, addr, proc, prog, ": request from non-local host")
74
75 #define log_no_forward(addr, proc, prog) \
76   logit(deny_severity, addr, proc, prog, ": request not forwarded")
77
78 #define log_client(addr, proc, prog) \
79   logit(allow_severity, addr, proc, prog, "")
80
81 int
82 good_client(daemon, addr)
83 char *daemon;
84 struct sockaddr_in *addr;
85 {
86     struct hostent *hp;
87     char **sp;
88     char *tmpname;
89
90     /* Check the IP address first. */
91     if (hosts_ctl(daemon, "", inet_ntoa(addr->sin_addr), ""))
92         return 1;
93
94     /* Check the hostname. */
95     hp = gethostbyaddr ((const char *) &(addr->sin_addr),
96                         sizeof (addr->sin_addr), AF_INET);
97
98     if (!hp)
99         return 0;
100
101     /* must make sure the hostent is authorative. */
102     tmpname = alloca (strlen (hp->h_name) + 1);
103     strcpy (tmpname, hp->h_name);
104     hp = gethostbyname(tmpname);
105     if (hp) {
106         /* now make sure the "addr->sin_addr" is on the list */
107         for (sp = hp->h_addr_list ; *sp ; sp++) {
108             if (memcmp(*sp, &(addr->sin_addr), hp->h_length)==0)
109                 break;
110         }
111         if (!*sp)
112             /* it was a FAKE. */
113             return 0;
114     }
115     else
116            /* never heard of it. misconfigured DNS? */
117            return 0;
118
119    /* Check the official name first. */
120    if (hosts_ctl(daemon, "", hp->h_name, ""))
121         return 1;
122
123    /* Check aliases. */
124    for (sp = hp->h_aliases; *sp ; sp++) {
125         if (hosts_ctl(daemon, "", *sp, ""))
126             return 1;
127    }
128
129    /* No match */
130    return 0;
131 }
132
133 /* check_startup - additional startup code */
134
135 void    check_startup()
136 {
137
138     /*
139      * Give up root privileges so that we can never allocate a privileged
140      * port when forwarding an rpc request.
141      *
142      * Fix 8/3/00 Philipp Knirsch: First lookup our rpc user. If we find it,
143      * switch to that uid, otherwise simply resue the old bin user and print
144      * out a warning in syslog.
145      */
146
147     struct passwd *pwent;
148
149     pwent = getpwnam("rpc");
150     if (pwent == NULL) {
151         syslog(LOG_WARNING, "user rpc not found, reverting to user bin");
152         if (setuid(1) == -1) {
153             syslog(LOG_ERR, "setuid(1) failed: %m");
154             exit(1);
155         }
156     }
157     else {
158         if (setuid(pwent->pw_uid) == -1) {
159             syslog(LOG_WARNING, "setuid() to rpc user failed: %m");
160             if (setuid(1) == -1) {
161                 syslog(LOG_ERR, "setuid(1) failed: %m");
162                 exit(1);
163             }
164         }
165     }
166
167     (void) signal(SIGINT, toggle_verboselog);
168 }
169
170 /* check_default - additional checks for NULL, DUMP, GETPORT and unknown */
171
172 int
173 check_default(daemon, addr, proc, prog)
174 char *daemon;
175 struct sockaddr_in *addr;
176 u_long  proc;
177 u_long  prog;
178 {
179     if (!(from_local(addr) || good_client(daemon, addr))) {
180         log_bad_host(addr, proc, prog);
181         return (FALSE);
182     }
183     if (verboselog)
184         log_client(addr, proc, prog);
185     return (TRUE);
186 }
187
188 /* check_privileged_port - additional checks for privileged-port updates */
189 int
190 check_privileged_port(addr, proc, prog, port)
191 struct sockaddr_in *addr;
192 u_long  proc;
193 u_long  prog;
194 u_long  port;
195 {
196 #ifdef CHECK_PORT
197     if (!legal_port(addr, port)) {
198         log_bad_port(addr, proc, prog);
199         return (FALSE);
200     }
201 #endif
202     return (TRUE);
203 }
204
205 /* toggle_verboselog - toggle verbose logging flag */
206
207 static void toggle_verboselog(sig)
208 int     sig;
209 {
210     (void) signal(sig, toggle_verboselog);
211     verboselog = !verboselog;
212 }
213
214 /* logit - report events of interest via the syslog daemon */
215
216 static void logit(severity, addr, procnum, prognum, text)
217 int     severity;
218 struct sockaddr_in *addr;
219 u_long  procnum;
220 u_long  prognum;
221 char   *text;
222 {
223     char   *procname;
224     char    procbuf[4 * sizeof(u_long)];
225     char   *progname;
226     char    progbuf[4 * sizeof(u_long)];
227     struct rpcent *rpc;
228
229     /*
230      * Fork off a process or the portmap daemon might hang while
231      * getrpcbynumber() or syslog() does its thing.
232      */
233
234     if (fork() == 0) {
235
236         /* Try to map program number to name. */
237
238         if (prognum == 0) {
239             progname = "";
240         } else if ((rpc = getrpcbynumber((int) prognum))) {
241             progname = rpc->r_name;
242         } else {
243             sprintf(progname = progbuf, "%lu", prognum);
244         }
245
246         /* Try to map procedure number to name. */
247
248         sprintf(procname = procbuf, "%lu", (u_long) procnum);
249
250         /* Write syslog record. */
251
252         syslog(severity, "connect from %s to %s(%s)%s",
253                inet_ntoa(addr->sin_addr), procname, progname, text);
254         exit(0);
255     }
256 }