Allow a user-configurable minimum password length.
[oweals/busybox.git] / libbb / inet_common.c
1 /*
2  * stolen from net-tools-1.59 and stripped down for busybox by
3  *                      Erik Andersen <andersen@codepoet.org>
4  *
5  * Heavily modified by Manuel Novoa III       Mar 12, 2001
6  *
7  * Version:     $Id: inet_common.c,v 1.8 2004/03/10 07:42:38 mjn3 Exp $
8  *
9  */
10
11 #include "libbb.h"
12 #include "inet_common.h"
13 #include <stdio.h>
14 #include <errno.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #ifdef DEBUG
20 # include <resolv.h>
21 #endif
22
23
24 const char bb_INET_default[] = "default";
25
26 int INET_resolve(const char *name, struct sockaddr_in *s_in, int hostfirst)
27 {
28         struct hostent *hp;
29         struct netent *np;
30
31         /* Grmpf. -FvK */
32         s_in->sin_family = AF_INET;
33         s_in->sin_port = 0;
34
35         /* Default is special, meaning 0.0.0.0. */
36         if (!strcmp(name, bb_INET_default)) {
37                 s_in->sin_addr.s_addr = INADDR_ANY;
38                 return (1);
39         }
40         /* Look to see if it's a dotted quad. */
41         if (inet_aton(name, &s_in->sin_addr)) {
42                 return 0;
43         }
44         /* If we expect this to be a hostname, try hostname database first */
45 #ifdef DEBUG
46         if (hostfirst) {
47                 bb_error_msg("gethostbyname (%s)", name);
48         }
49 #endif
50         if (hostfirst && (hp = gethostbyname(name)) != (struct hostent *) NULL) {
51                 memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
52                            sizeof(struct in_addr));
53                 return 0;
54         }
55         /* Try the NETWORKS database to see if this is a known network. */
56 #ifdef DEBUG
57         bb_error_msg("getnetbyname (%s)", name);
58 #endif
59         if ((np = getnetbyname(name)) != (struct netent *) NULL) {
60                 s_in->sin_addr.s_addr = htonl(np->n_net);
61                 return 1;
62         }
63         if (hostfirst) {
64                 /* Don't try again */
65                 return -1;
66         }
67 #ifdef DEBUG
68         res_init();
69         _res.options |= RES_DEBUG;
70 #endif
71
72 #ifdef DEBUG
73         bb_error_msg("gethostbyname (%s)", name);
74 #endif
75         if ((hp = gethostbyname(name)) == (struct hostent *) NULL) {
76                 return -1;
77         }
78         memcpy((char *) &s_in->sin_addr, (char *) hp->h_addr_list[0],
79                    sizeof(struct in_addr));
80
81         return 0;
82 }
83
84 /* cache */
85 struct addr {
86         struct sockaddr_in addr;
87         char *name;
88         int host;
89         struct addr *next;
90 };
91
92 static struct addr *INET_nn = NULL;     /* addr-to-name cache           */
93
94 /* numeric: & 0x8000: default instead of *,
95  *          & 0x4000: host instead of net,
96  *          & 0x0fff: don't resolve
97  */
98 int INET_rresolve(char *name, size_t len, struct sockaddr_in *s_in,
99                                   int numeric, unsigned int netmask)
100 {
101         struct hostent *ent;
102         struct netent *np;
103         struct addr *pn;
104         unsigned long ad, host_ad;
105         int host = 0;
106
107         /* Grmpf. -FvK */
108         if (s_in->sin_family != AF_INET) {
109 #ifdef DEBUG
110                 bb_error_msg("rresolve: unsupport address family %d !",
111                                   s_in->sin_family);
112 #endif
113                 errno = EAFNOSUPPORT;
114                 return (-1);
115         }
116         ad = (unsigned long) s_in->sin_addr.s_addr;
117 #ifdef DEBUG
118         bb_error_msg("rresolve: %08lx, mask %08x, num %08x", ad, netmask, numeric);
119 #endif
120         if (ad == INADDR_ANY) {
121                 if ((numeric & 0x0FFF) == 0) {
122                         if (numeric & 0x8000)
123                                 safe_strncpy(name, bb_INET_default, len);
124                         else
125                                 safe_strncpy(name, "*", len);
126                         return (0);
127                 }
128         }
129         if (numeric & 0x0FFF) {
130                 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
131                 return (0);
132         }
133
134         if ((ad & (~netmask)) != 0 || (numeric & 0x4000))
135                 host = 1;
136         pn = INET_nn;
137         while (pn != NULL) {
138                 if (pn->addr.sin_addr.s_addr == ad && pn->host == host) {
139                         safe_strncpy(name, pn->name, len);
140 #ifdef DEBUG
141                         bb_error_msg("rresolve: found %s %08lx in cache",
142                                           (host ? "host" : "net"), ad);
143 #endif
144                         return (0);
145                 }
146                 pn = pn->next;
147         }
148
149         host_ad = ntohl(ad);
150         np = NULL;
151         ent = NULL;
152         if (host) {
153 #ifdef DEBUG
154                 bb_error_msg("gethostbyaddr (%08lx)", ad);
155 #endif
156                 ent = gethostbyaddr((char *) &ad, 4, AF_INET);
157                 if (ent != NULL) {
158                         safe_strncpy(name, ent->h_name, len);
159                 }
160         } else {
161 #ifdef DEBUG
162                 bb_error_msg("getnetbyaddr (%08lx)", host_ad);
163 #endif
164                 np = getnetbyaddr(host_ad, AF_INET);
165                 if (np != NULL) {
166                         safe_strncpy(name, np->n_name, len);
167                 }
168         }
169         if ((ent == NULL) && (np == NULL)) {
170                 safe_strncpy(name, inet_ntoa(s_in->sin_addr), len);
171         }
172         pn = (struct addr *) xmalloc(sizeof(struct addr));
173         pn->addr = *s_in;
174         pn->next = INET_nn;
175         pn->host = host;
176         pn->name = bb_xstrdup(name);
177         INET_nn = pn;
178
179         return (0);
180 }
181
182 #ifdef CONFIG_FEATURE_IPV6
183
184 int INET6_resolve(const char *name, struct sockaddr_in6 *sin6)
185 {
186         struct addrinfo req, *ai;
187         int s;
188
189         memset(&req, '\0', sizeof req);
190         req.ai_family = AF_INET6;
191         if ((s = getaddrinfo(name, NULL, &req, &ai))) {
192                 bb_error_msg("getaddrinfo: %s: %d", name, s);
193                 return -1;
194         }
195         memcpy(sin6, ai->ai_addr, sizeof(struct sockaddr_in6));
196
197         freeaddrinfo(ai);
198
199         return (0);
200 }
201
202 #ifndef IN6_IS_ADDR_UNSPECIFIED
203 # define IN6_IS_ADDR_UNSPECIFIED(a) \
204         (((uint32_t *) (a))[0] == 0 && ((uint32_t *) (a))[1] == 0 && \
205          ((uint32_t *) (a))[2] == 0 && ((uint32_t *) (a))[3] == 0)
206 #endif
207
208
209 int INET6_rresolve(char *name, size_t len, struct sockaddr_in6 *sin6,
210                                    int numeric)
211 {
212         int s;
213
214         /* Grmpf. -FvK */
215         if (sin6->sin6_family != AF_INET6) {
216 #ifdef DEBUG
217                 bb_error_msg(_("rresolve: unsupport address family %d !\n"),
218                                   sin6->sin6_family);
219 #endif
220                 errno = EAFNOSUPPORT;
221                 return (-1);
222         }
223         if (numeric & 0x7FFF) {
224                 inet_ntop(AF_INET6, &sin6->sin6_addr, name, len);
225                 return (0);
226         }
227         if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr)) {
228                 if (numeric & 0x8000) {
229                         strcpy(name, "default");
230                 } else {
231                         strcpy(name, "*");
232                 }
233                 return (0);
234         }
235
236         s = getnameinfo((struct sockaddr *) sin6, sizeof(struct sockaddr_in6), name, len, NULL, 0, 0);
237         if (s) {
238                 bb_error_msg("getnameinfo failed");
239                 return -1;
240         }
241         return (0);
242 }
243
244 #endif                                                  /* CONFIG_FEATURE_IPV6 */