Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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  * @(#) from_local.c 1.2 93/11/16 21:50:02
38  * $FreeBSD: src/usr.sbin/portmap/from_local.c,v 1.10.2.1 2000/08/16 14:04:37 brian Exp $
39  * $DragonFly: src/usr.sbin/portmap/from_local.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
40  */
41
42 #ifdef TEST
43 #undef perror
44 #endif
45
46 #include <sys/types.h>
47 #include <sys/ioctl.h>
48 #include <sys/socket.h>
49 #include <sys/sysctl.h>
50 #include <sys/time.h>
51
52 #include <netdb.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <syslog.h>
58 #include <unistd.h>
59
60 #include <net/if.h>
61 #include <net/if_dl.h>
62 #include <net/route.h>
63 #include <netinet/in.h>
64
65 #include "pmap_check.h"
66
67 #ifndef TRUE
68 #define TRUE    1
69 #define FALSE   0
70 #endif
71
72 #define ROUNDUP(x) ((x) ? (1 + (((x) - 1) | (sizeof(long) - 1))) : sizeof(long))
73
74 /* How many interfaces could there be on a computer? */
75
76 #define ESTIMATED_LOCAL 20
77 static int num_local = -1;
78 static struct in_addr *addrs;
79
80 static void
81 rtiparse(struct ifa_msghdr *ifam, struct rt_addrinfo *ai)
82 {
83   char *wp;
84   int rtax;
85
86   wp = (char *)(ifam + 1);
87
88   ai->rti_addrs = ifam->ifam_addrs;
89   for (rtax = 0; rtax < sizeof ai->rti_info / sizeof *ai->rti_info; rtax++)
90     if (ifam->ifam_addrs & (1 << rtax)) {
91       ai->rti_info[rtax] = (struct sockaddr *)wp;
92       wp += ROUNDUP(ai->rti_info[rtax]->sa_len);
93     } else
94       ai->rti_info[rtax] = NULL;
95 }
96
97 /* find_local - find all IP addresses for this host */
98
99 static int
100 find_local()
101 {
102   int mib[6], n, s, alloced;
103   size_t needed;
104   char *buf, *end, *ptr;
105   struct if_msghdr *ifm;
106   struct ifa_msghdr *ifam;
107   struct rt_addrinfo ai;
108   struct ifreq ifr;
109   struct sockaddr_dl *dl;
110
111   mib[0] = CTL_NET;
112   mib[1] = PF_ROUTE;
113   mib[4] = NET_RT_IFLIST;
114   mib[2] = mib[3] = mib[5] = 0;
115
116   if ((s = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
117     perror("socket");
118     return (0);
119   }
120   if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) {
121     close(s);
122     perror("sysctl(NET_RT_IFLIST)");
123     return 0;
124   }
125   if ((buf = (char *)malloc(needed)) == NULL) {
126     close(s);
127     perror("malloc");
128     return 0;
129   }
130   if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) {
131     close(s);
132     free(buf);
133     perror("sysctl(NET_RT_IFLIST)(after malloc)");
134     return 0;
135   }
136
137   if (addrs) {
138     free(addrs);
139     addrs = NULL;
140   }
141   num_local = 0;
142   alloced = 0;
143   end = buf + needed;
144
145   for (ptr = buf; ptr < end; ptr += ifm->ifm_msglen) {
146     ifm = (struct if_msghdr *)ptr;
147     dl = (struct sockaddr_dl *)(ifm + 1);
148
149     if (ifm->ifm_index != dl->sdl_index || dl->sdl_nlen == 0)
150       /* Skip over remaining ifa_msghdrs */
151       continue;
152
153     n = dl->sdl_nlen > sizeof ifr.ifr_name ?
154         sizeof ifr.ifr_name : dl->sdl_nlen;
155     strncpy(ifr.ifr_name, dl->sdl_data, n);
156     if (n < sizeof ifr.ifr_name)
157       ifr.ifr_name[n] = '\0';
158
159     /* we only want the first address from each interface */
160     if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0)
161       fprintf(stderr, "%.*s: SIOCGIFFLAGS: %s\n", n, ifr.ifr_name,
162               strerror(errno));
163     else if (ifr.ifr_flags & IFF_UP) {    /* active interface */
164       ifam = (struct ifa_msghdr *)(ptr + ifm->ifm_msglen);
165       while ((char *)ifam < end && ifam->ifam_type == RTM_NEWADDR) {
166         rtiparse(ifam, &ai);
167
168         if (ai.rti_info[RTAX_IFA] != NULL &&
169             ai.rti_info[RTAX_IFA]->sa_family == AF_INET) {
170           if (alloced < num_local + 1) {
171             alloced += ESTIMATED_LOCAL;
172             addrs = (struct in_addr *)realloc(addrs, alloced * sizeof addrs[0]);
173             if (addrs == NULL) {
174               perror("malloc/realloc");
175               num_local = 0;
176               break;
177             }
178           }
179           addrs[num_local++] = ((struct sockaddr_in *)
180             ai.rti_info[RTAX_IFA])->sin_addr;
181
182         }
183         ifam = (struct ifa_msghdr *)((char *)ifam + ifam->ifam_msglen);
184       }
185     }
186   }
187   free(buf);
188   close(s);
189
190   return num_local;
191 }
192
193 /* from_local - determine whether request comes from the local system */
194
195 int
196 from_local(addr)
197     struct sockaddr_in *addr;
198 {
199     int     i;
200
201     if (num_local == -1 && find_local() == 0)
202         syslog(LOG_ERR, "cannot find any active local network interfaces");
203
204     for (i = 0; i < num_local; i++) {
205         if (memcmp((char *) &(addr->sin_addr), (char *) &(addrs[i]),
206                    sizeof(struct in_addr)) == 0)
207             return (TRUE);
208     }
209     return (FALSE);
210 }
211
212 #ifdef TEST
213
214 int
215 main(argc, argv)
216     int argc;
217     char **argv;
218 {
219     char   *inet_ntoa();
220     int     i;
221
222     find_local();
223     for (i = 0; i < num_local; i++)
224         printf("%s\n", inet_ntoa(addrs[i]));
225
226     return 0;
227 }
228
229 #endif