]>
Commit | Line | Data |
---|---|---|
2b0cc3d3 EM |
1 | /* authd/providers/ident.c - ident lookup provider for authd |
2 | * Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me> | |
3 | * | |
4 | * Permission to use, copy, modify, and/or distribute this software for any | |
5 | * purpose with or without fee is hereby granted, provided that the above | |
6 | * copyright notice and this permission notice is present in all copies. | |
7 | * | |
8 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR | |
9 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
10 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
11 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, | |
12 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
13 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
14 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
15 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
16 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | |
17 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
18 | * POSSIBILITY OF SUCH DAMAGE. | |
19 | */ | |
20 | ||
21 | #include "stdinc.h" | |
22 | #include "match.h" | |
23 | #include "authd.h" | |
db821ee9 | 24 | #include "notice.h" |
2b0cc3d3 EM |
25 | #include "provider.h" |
26 | #include "res.h" | |
27 | ||
28 | #define IDENT_BUFSIZE 128 | |
29 | ||
30 | struct ident_query | |
31 | { | |
2b0cc3d3 EM |
32 | time_t timeout; /* Timeout interval */ |
33 | rb_fde_t *F; /* Our FD */ | |
34 | }; | |
35 | ||
36 | /* Goinked from old s_auth.c --Elizafox */ | |
37 | static const char *messages[] = | |
38 | { | |
39 | ":*** Checking Ident", | |
40 | ":*** Got Ident response", | |
41 | ":*** No Ident response", | |
42 | }; | |
43 | ||
44 | typedef enum | |
45 | { | |
46 | REPORT_LOOKUP, | |
47 | REPORT_FOUND, | |
48 | REPORT_FAIL, | |
49 | } ident_message; | |
50 | ||
51 | static EVH timeout_ident_queries_event; | |
52 | static CNCB ident_connected; | |
53 | static PF read_ident_reply; | |
54 | ||
3e875f62 EM |
55 | static void client_fail(struct auth_client *auth, ident_message message); |
56 | static void client_success(struct auth_client *auth); | |
2b0cc3d3 EM |
57 | static char * get_valid_ident(char *buf); |
58 | ||
2b0cc3d3 EM |
59 | static struct ev_entry *timeout_ev; |
60 | static int ident_timeout = 5; | |
61 | ||
62 | ||
63 | bool ident_init(void) | |
64 | { | |
65 | timeout_ev = rb_event_addish("timeout_ident_queries_event", timeout_ident_queries_event, NULL, 1); | |
66 | return (timeout_ev != NULL); | |
67 | } | |
68 | ||
69 | void ident_destroy(void) | |
70 | { | |
3e875f62 | 71 | struct auth_client *auth; |
aba29d5a | 72 | rb_dictionary_iter iter; |
2b0cc3d3 EM |
73 | |
74 | /* Nuke all ident queries */ | |
ab33d608 | 75 | RB_DICTIONARY_FOREACH(auth, &iter, auth_clients) |
2b0cc3d3 | 76 | { |
3e875f62 EM |
77 | if(auth->data[PROVIDER_IDENT] != NULL) |
78 | client_fail(auth, REPORT_FAIL); | |
2b0cc3d3 EM |
79 | } |
80 | } | |
81 | ||
82 | bool ident_start(struct auth_client *auth) | |
83 | { | |
84 | struct ident_query *query = rb_malloc(sizeof(struct ident_query)); | |
85 | struct rb_sockaddr_storage l_addr, c_addr; | |
86 | int family; | |
87 | rb_fde_t *F; | |
88 | ||
3e875f62 | 89 | auth->data[PROVIDER_IDENT] = query; |
2b0cc3d3 EM |
90 | query->timeout = rb_current_time() + ident_timeout; |
91 | ||
92 | if((F = rb_socket(family, SOCK_STREAM, 0, "ident")) == NULL) | |
93 | { | |
3e875f62 | 94 | client_fail(auth, REPORT_FAIL); |
2b0cc3d3 EM |
95 | return true; /* Not a fatal error */ |
96 | } | |
97 | ||
98 | query->F = F; | |
99 | ||
100 | /* Build sockaddr_storages for rb_connect_tcp below */ | |
32f8c78b EM |
101 | memcpy(&l_addr, &auth->l_addr, sizeof(l_addr)); |
102 | memcpy(&c_addr, &auth->c_addr, sizeof(c_addr)); | |
2b0cc3d3 EM |
103 | |
104 | /* Set the ports correctly */ | |
105 | #ifdef RB_IPV6 | |
106 | if(GET_SS_FAMILY(&l_addr) == AF_INET6) | |
107 | ((struct sockaddr_in6 *)&l_addr)->sin6_port = 0; | |
108 | else | |
109 | #endif | |
110 | ((struct sockaddr_in *)&l_addr)->sin_port = 0; | |
111 | ||
112 | #ifdef RB_IPV6 | |
113 | if(GET_SS_FAMILY(&c_addr) == AF_INET6) | |
114 | ((struct sockaddr_in6 *)&c_addr)->sin6_port = htons(113); | |
115 | else | |
116 | #endif | |
117 | ((struct sockaddr_in *)&c_addr)->sin_port = htons(113); | |
118 | ||
119 | rb_connect_tcp(F, (struct sockaddr *)&c_addr, | |
120 | (struct sockaddr *)&l_addr, | |
121 | GET_SS_LEN(&l_addr), ident_connected, | |
122 | query, ident_timeout); | |
123 | ||
db821ee9 | 124 | notice_client(auth->cid, messages[REPORT_LOOKUP]); |
a7d5aea1 | 125 | set_provider_on(auth, PROVIDER_IDENT); |
2b0cc3d3 EM |
126 | |
127 | return true; | |
128 | } | |
129 | ||
130 | void ident_cancel(struct auth_client *auth) | |
131 | { | |
3e875f62 | 132 | struct ident_query *query = auth->data[PROVIDER_IDENT]; |
2b0cc3d3 | 133 | |
3e875f62 EM |
134 | if(query != NULL) |
135 | client_fail(auth, REPORT_FAIL); | |
2b0cc3d3 EM |
136 | } |
137 | ||
138 | /* Timeout outstanding queries */ | |
139 | static void timeout_ident_queries_event(void *notused) | |
140 | { | |
3e875f62 | 141 | struct auth_client *auth; |
aba29d5a | 142 | rb_dictionary_iter iter; |
2b0cc3d3 | 143 | |
ab33d608 | 144 | RB_DICTIONARY_FOREACH(auth, &iter, auth_clients) |
2b0cc3d3 | 145 | { |
3e875f62 | 146 | struct ident_query *query = auth->data[PROVIDER_IDENT]; |
2b0cc3d3 | 147 | |
3e875f62 EM |
148 | if(query != NULL && query->timeout < rb_current_time()) |
149 | client_fail(auth, REPORT_FAIL); | |
2b0cc3d3 EM |
150 | } |
151 | } | |
152 | ||
153 | /* | |
154 | * ident_connected() - deal with the result of rb_connect_tcp() | |
155 | * | |
156 | * If the connection failed, we simply close the auth fd and report | |
157 | * a failure. If the connection suceeded send the ident server a query | |
158 | * giving "theirport , ourport". The write is only attempted *once* so | |
159 | * it is deemed to be a fail if the entire write doesn't write all the | |
160 | * data given. This shouldnt be a problem since the socket should have | |
161 | * a write buffer far greater than this message to store it in should | |
162 | * problems arise. -avalon | |
163 | */ | |
164 | static void ident_connected(rb_fde_t *F, int error, void *data) | |
165 | { | |
3e875f62 EM |
166 | struct auth_client *auth = data; |
167 | struct ident_query *query = auth->data[PROVIDER_IDENT]; | |
2b0cc3d3 EM |
168 | char authbuf[32]; |
169 | int authlen; | |
170 | ||
171 | /* Check the error */ | |
172 | if(error != RB_OK) | |
173 | { | |
174 | /* We had an error during connection :( */ | |
3e875f62 | 175 | client_fail(auth, REPORT_FAIL); |
2b0cc3d3 EM |
176 | return; |
177 | } | |
178 | ||
179 | snprintf(authbuf, sizeof(authbuf), "%u , %u\r\n", | |
180 | auth->c_port, auth->l_port); | |
181 | authlen = strlen(authbuf); | |
182 | ||
183 | if(rb_write(query->F, authbuf, authlen) != authlen) | |
184 | { | |
3e875f62 | 185 | client_fail(auth, REPORT_FAIL); |
2b0cc3d3 EM |
186 | return; |
187 | } | |
188 | ||
3e875f62 | 189 | read_ident_reply(query->F, auth); |
2b0cc3d3 EM |
190 | } |
191 | ||
192 | static void | |
193 | read_ident_reply(rb_fde_t *F, void *data) | |
194 | { | |
3e875f62 EM |
195 | struct auth_client *auth = data; |
196 | struct ident_query *query = auth->data[PROVIDER_IDENT]; | |
2b0cc3d3 EM |
197 | char *s = NULL; |
198 | char *t = NULL; | |
199 | int len; | |
200 | int count; | |
201 | char buf[IDENT_BUFSIZE + 1]; /* buffer to read auth reply into */ | |
202 | ||
203 | len = rb_read(F, buf, IDENT_BUFSIZE); | |
204 | if(len < 0 && rb_ignore_errno(errno)) | |
205 | { | |
206 | rb_setselect(F, RB_SELECT_READ, read_ident_reply, query); | |
207 | return; | |
208 | } | |
209 | ||
210 | if(len > 0) | |
211 | { | |
212 | buf[len] = '\0'; | |
213 | ||
214 | if((s = get_valid_ident(buf))) | |
215 | { | |
216 | t = auth->username; | |
217 | ||
218 | while (*s == '~' || *s == '^') | |
219 | s++; | |
220 | ||
221 | for (count = USERLEN; *s && count; s++) | |
222 | { | |
223 | if(*s == '@') | |
224 | { | |
225 | break; | |
226 | } | |
227 | if(*s != ' ' && *s != ':' && *s != '[') | |
228 | { | |
229 | *t++ = *s; | |
230 | count--; | |
231 | } | |
232 | } | |
233 | *t = '\0'; | |
234 | } | |
235 | } | |
236 | ||
237 | if(s == NULL) | |
3e875f62 | 238 | client_fail(auth, REPORT_FAIL); |
2b0cc3d3 | 239 | else |
3e875f62 | 240 | client_success(auth); |
2b0cc3d3 EM |
241 | } |
242 | ||
3e875f62 | 243 | static void client_fail(struct auth_client *auth, ident_message report) |
2b0cc3d3 | 244 | { |
3e875f62 | 245 | struct ident_query *query = auth->data[PROVIDER_IDENT]; |
2b0cc3d3 | 246 | |
3e875f62 | 247 | rb_strlcpy(auth->username, "*", sizeof(auth->username)); |
2b0cc3d3 | 248 | |
3e875f62 EM |
249 | rb_close(query->F); |
250 | rb_free(query); | |
251 | auth->data[PROVIDER_IDENT] = NULL; | |
2b0cc3d3 | 252 | |
db821ee9 | 253 | notice_client(auth->cid, messages[report]); |
3e875f62 | 254 | provider_done(auth, PROVIDER_IDENT); |
2b0cc3d3 EM |
255 | } |
256 | ||
3e875f62 | 257 | static void client_success(struct auth_client *auth) |
2b0cc3d3 | 258 | { |
3e875f62 | 259 | struct ident_query *query = auth->data[PROVIDER_IDENT]; |
2b0cc3d3 | 260 | |
3e875f62 EM |
261 | rb_close(query->F); |
262 | rb_free(query); | |
263 | auth->data[PROVIDER_IDENT] = NULL; | |
2b0cc3d3 | 264 | |
db821ee9 | 265 | notice_client(auth->cid, messages[REPORT_FOUND]); |
3e875f62 | 266 | provider_done(auth, PROVIDER_IDENT); |
2b0cc3d3 EM |
267 | } |
268 | ||
269 | /* get_valid_ident | |
270 | * parse ident query reply from identd server | |
271 | * | |
272 | * Torn out of old s_auth.c because there was nothing wrong with it | |
273 | * --Elizafox | |
274 | * | |
275 | * Inputs - pointer to ident buf | |
276 | * Outputs - NULL if no valid ident found, otherwise pointer to name | |
277 | * Side effects - None | |
278 | */ | |
279 | static char * | |
280 | get_valid_ident(char *buf) | |
281 | { | |
282 | int remp = 0; | |
283 | int locp = 0; | |
284 | char *colon1Ptr; | |
285 | char *colon2Ptr; | |
286 | char *colon3Ptr; | |
287 | char *commaPtr; | |
288 | char *remotePortString; | |
289 | ||
290 | /* All this to get rid of a sscanf() fun. */ | |
291 | remotePortString = buf; | |
292 | ||
293 | colon1Ptr = strchr(remotePortString, ':'); | |
294 | if(!colon1Ptr) | |
295 | return 0; | |
296 | ||
297 | *colon1Ptr = '\0'; | |
298 | colon1Ptr++; | |
299 | colon2Ptr = strchr(colon1Ptr, ':'); | |
300 | if(!colon2Ptr) | |
301 | return 0; | |
302 | ||
303 | *colon2Ptr = '\0'; | |
304 | colon2Ptr++; | |
305 | commaPtr = strchr(remotePortString, ','); | |
306 | ||
307 | if(!commaPtr) | |
308 | return 0; | |
309 | ||
310 | *commaPtr = '\0'; | |
311 | commaPtr++; | |
312 | ||
313 | remp = atoi(remotePortString); | |
314 | if(!remp) | |
315 | return 0; | |
316 | ||
317 | locp = atoi(commaPtr); | |
318 | if(!locp) | |
319 | return 0; | |
320 | ||
321 | /* look for USERID bordered by first pair of colons */ | |
322 | if(!strstr(colon1Ptr, "USERID")) | |
323 | return 0; | |
324 | ||
325 | colon3Ptr = strchr(colon2Ptr, ':'); | |
326 | if(!colon3Ptr) | |
327 | return 0; | |
328 | ||
329 | *colon3Ptr = '\0'; | |
330 | colon3Ptr++; | |
331 | return (colon3Ptr); | |
332 | } | |
333 | ||
334 | ||
335 | struct auth_provider ident_provider = | |
336 | { | |
337 | .id = PROVIDER_IDENT, | |
338 | .init = ident_init, | |
339 | .destroy = ident_destroy, | |
340 | .start = ident_start, | |
341 | .cancel = ident_cancel, | |
342 | .completed = NULL, | |
343 | }; |