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