Initial import from FreeBSD RELENG_4:
[dragonfly.git] / usr.sbin / portmap / from_local.c
1  /*
2   * Check if an address belongs to the local system. Adapted from:
3   *
4   * @(#)pmap_svc.c 1.32 91/03/11 Copyright 1984,1990 Sun Microsystems, Inc.
5   * @(#)get_myaddress.c  2.1 88/07/29 4.0 RPCSRC.
6   */
7
8 /*
9  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
10  * unrestricted use provided that this legend is included on all tape
11  * media and as a part of the software program in whole or part.  Users
12  * may copy or modify Sun RPC without charge, but are not authorized
13  * to license or distribute it to anyone else except as part of a product or
14  * program developed by the user or with the express written consent of
15  * Sun Microsystems, Inc.
16  *
17  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
18  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
20  *
21  * Sun RPC is provided with no support and without any obligation on the
22  * part of Sun Microsystems, Inc. to assist in its use, correction,
23  * modification or enhancement.
24  *
25  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
26  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
27  * OR ANY PART THEREOF.
28  *
29  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
30  * or profits or other special, indirect and consequential damages, even if
31  * Sun has been advised of the possibility of such damages.
32  *
33  * Sun Microsystems, Inc.
34  * 2550 Garcia Avenue
35  * Mountain View, California  94043
36  */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#) from_local.c 1.2 93/11/16 21:50:02";
41 #endif
42 static const char rcsid[] =
43   "$FreeBSD: src/usr.sbin/portmap/from_local.c,v 1.10.2.1 2000/08/16 14:04:37 brian Exp $";
44 #endif
45
46 #ifdef TEST
47 #undef perror
48 #endif
49
50 #include <sys/types.h>
51 #include <sys/ioctl.h>
52 #include <sys/socket.h>
53 #include <sys/sysctl.h>
54 #include <sys/time.h>
55
56 #include <netdb.h>
57 #include <errno.h>
58 #include <stdio.h>
59 #include <stdlib.h>
60 #include <string.h>
61 #include <syslog.h>
62 #include <unistd.h>
63
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/route.h>
67 #include <netinet/in.h>
68
69 #include "pmap_check.h"
70
71 #ifndef TRUE
72 #define TRUE    1
73 #define FALSE   0
74 #endif
75
76 #define ROUNDUP(x) ((x) ? (1 + (((x) - 1) | (sizeof(long) - 1))) : sizeof(long))
77
78 /* How many interfaces could there be on a computer? */
79
80 #define ESTIMATED_LOCAL 20
81 static int num_local = -1;
82 static struct in_addr *addrs;
83
84 static void
85 rtiparse(struct ifa_msghdr *ifam, struct rt_addrinfo *ai)
86 {
87   char *wp;
88   int rtax;
89
90   wp = (char *)(ifam + 1);
91
92   ai->rti_addrs = ifam->ifam_addrs;
93   for (rtax = 0; rtax < sizeof ai->rti_info / sizeof *ai->rti_info; rtax++)
94     if (ifam->ifam_addrs & (1 << rtax)) {
95       ai->rti_info[rtax] = (struct sockaddr *)wp;
96       wp += ROUNDUP(ai->rti_info[rtax]->sa_len);
97     } else
98       ai->rti_info[rtax] = NULL;
99 }
100
101 /* find_local - find all IP addresses for this host */
102
103 static int
104 find_local()
105 {
106   int mib[6], n, s, alloced;
107   size_t needed;
108   char *buf, *end, *ptr;
109   struct if_msghdr *ifm;
110   struct ifa_msghdr *ifam;
111   struct rt_addrinfo ai;
112   struct ifreq ifr;
113   struct sockaddr_dl *dl;
114
115   mib[0] = CTL_NET;
116   mib[1] = PF_ROUTE;
117   mib[4] = NET_RT_IFLIST;
118   mib[2] = mib[3] = mib[5] = 0;
119
120   if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
121     perror("socket");
122     return (0);
123   }
124   if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
125     close(s);
126     perror("sysctl(NET_RT_IFLIST)");
127     return 0;
128   }
129   if ((buf = (char *)malloc(needed)) == NULL) {
130     close(s);
131     perror("malloc");
132     return 0;
133   }
134   if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
135     close(s);
136     free(buf);
137     perror("sysctl(NET_RT_IFLIST)(after malloc)");
138     return 0;
139   }
140
141   if (addrs) {
142     free(addrs);
143     addrs = NULL;
144   }
145   num_local = 0;
146   alloced = 0;
147   end = buf + needed;
148
149   for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) {
150     ifm = (struct if_msghdr *)ptr;
151     dl = (struct sockaddr_dl *)(ifm + 1);
152
153     if (ifm->ifm_index != dl->sdl_index || dl->sdl_nlen == 0)
154       /* Skip over remaining ifa_msghdrs */
155       continue;
156
157     n = dl->sdl_nlen > sizeof ifr.ifr_name ?
158         sizeof ifr.ifr_name : dl->sdl_nlen;
159     strncpy(ifr.ifr_name, dl->sdl_data, n);
160     if (n < sizeof ifr.ifr_name)
161       ifr.ifr_name[n] = '\0';
162
163     /* we only want the first address from each interface */
164     if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
165       fprintf(stderr, "%.*s: SIOCGIFFLAGS: %s\n", n, ifr.ifr_name,
166               strerror(errno));
167     else if (ifr.ifr_flags & IFF_UP) {    /* active interface */
168       ifam = (struct ifa_msghdr *)(ptr + ifm->ifm_msglen);
169       while ((char *)ifam < end && ifam->ifam_type == RTM_NEWADDR) {
170         rtiparse(ifam, &ai);
171
172         if (ai.rti_info[RTAX_IFA] != NULL &&
173             ai.rti_info[RTAX_IFA]->sa_family == AF_INET) {
174           if (alloced < num_local + 1) {
175             alloced += ESTIMATED_LOCAL;
176             addrs = (struct in_addr *)realloc(addrs, alloced * sizeof addrs[0]);
177             if (addrs == NULL) {
178               perror("malloc/realloc");
179               num_local = 0;
180               break;
181             }
182           }
183           addrs[num_local++] = ((struct sockaddr_in *)
184             ai.rti_info[RTAX_IFA])->sin_addr;
185
186         }
187         ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
188       }
189     }
190   }
191   free(buf);
192   close(s);
193
194   return num_local;
195 }
196
197 /* from_local - determine whether request comes from the local system */
198
199 int
200 from_local(addr)
201     struct sockaddr_in *addr;
202 {
203     int     i;
204
205     if (num_local == -1 && find_local() == 0)
206         syslog(LOG_ERR, "cannot find any active local network interfaces");
207
208     for (i = 0; i < num_local; i++) {
209         if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
210                    sizeof(struct in_addr)) == 0)
211             return (TRUE);
212     }
213     return (FALSE);
214 }
215
216 #ifdef TEST
217
218 int
219 main(argc, argv)
220     int argc;
221     char **argv;
222 {
223     char   *inet_ntoa();
224     int     i;
225
226     find_local();
227     for (i = 0; i < num_local; i++)
228         printf("%s\n", inet_ntoa(addrs[i]));
229
230     return 0;
231 }
232
233 #endif