1 /* authd/providers/ident.c - ident lookup provider for authd
2 * Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
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.
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.
27 #define IDENT_BUFSIZE 128
33 struct auth_client
*auth
; /* Our client */
34 time_t timeout
; /* Timeout interval */
35 rb_fde_t
*F
; /* Our FD */
38 /* Goinked from old s_auth.c --Elizafox */
39 static const char *messages
[] =
41 ":*** Checking Ident",
42 ":*** Got Ident response",
43 ":*** No Ident response",
53 static EVH timeout_ident_queries_event
;
54 static CNCB ident_connected
;
55 static PF read_ident_reply
;
57 static void client_fail(struct ident_query
*query
, ident_message message
);
58 static void client_success(struct ident_query
*query
);
59 static void cleanup_query(struct ident_query
*query
);
60 static char * get_valid_ident(char *buf
);
62 static rb_dlink_list queries
;
63 static struct ev_entry
*timeout_ev
;
64 static int ident_timeout
= 5;
69 timeout_ev
= rb_event_addish("timeout_ident_queries_event", timeout_ident_queries_event
, NULL
, 1);
70 return (timeout_ev
!= NULL
);
73 void ident_destroy(void)
75 rb_dlink_node
*ptr
, *nptr
;
77 /* Nuke all ident queries */
78 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, queries
.head
)
80 struct ident_query
*query
= ptr
->data
;
82 notice_client(query
->auth
, messages
[REPORT_FAIL
]);
86 rb_dlinkDelete(ptr
, &queries
);
90 bool ident_start(struct auth_client
*auth
)
92 struct ident_query
*query
= rb_malloc(sizeof(struct ident_query
));
93 struct rb_sockaddr_storage l_addr
, c_addr
;
98 query
->timeout
= rb_current_time() + ident_timeout
;
100 if((F
= rb_socket(family
, SOCK_STREAM
, 0, "ident")) == NULL
)
102 client_fail(query
, REPORT_FAIL
);
103 return true; /* Not a fatal error */
108 /* Build sockaddr_storages for rb_connect_tcp below */
109 if(!rb_inet_ntop_sock((struct sockaddr
*)&l_addr
, auth
->l_ip
, sizeof(l_addr
)) ||
110 !rb_inet_ntop_sock((struct sockaddr
*)&c_addr
, auth
->c_ip
, sizeof(c_addr
)))
112 client_fail(query
, REPORT_FAIL
);
116 /* Set the ports correctly */
118 if(GET_SS_FAMILY(&l_addr
) == AF_INET6
)
119 ((struct sockaddr_in6
*)&l_addr
)->sin6_port
= 0;
122 ((struct sockaddr_in
*)&l_addr
)->sin_port
= 0;
125 if(GET_SS_FAMILY(&c_addr
) == AF_INET6
)
126 ((struct sockaddr_in6
*)&c_addr
)->sin6_port
= htons(113);
129 ((struct sockaddr_in
*)&c_addr
)->sin_port
= htons(113);
131 rb_connect_tcp(F
, (struct sockaddr
*)&c_addr
,
132 (struct sockaddr
*)&l_addr
,
133 GET_SS_LEN(&l_addr
), ident_connected
,
134 query
, ident_timeout
);
136 rb_dlinkAdd(query
, &query
->node
, &queries
);
138 set_provider(auth
, PROVIDER_IDENT
);
139 notice_client(auth
, messages
[REPORT_LOOKUP
]);
144 void ident_cancel(struct auth_client
*auth
)
146 rb_dlink_node
*ptr
, *nptr
;
148 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, queries
.head
)
150 struct ident_query
*query
= ptr
->data
;
152 if(query
->auth
== auth
)
154 client_fail(query
, REPORT_FAIL
);
158 rb_dlinkDelete(ptr
, &queries
);
165 /* Timeout outstanding queries */
166 static void timeout_ident_queries_event(void *notused
)
168 rb_dlink_node
*ptr
, *nptr
;
170 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, queries
.head
)
172 struct ident_query
*query
= ptr
->data
;
174 if(query
->timeout
< rb_current_time())
176 client_fail(query
, REPORT_FAIL
);
180 rb_dlinkDelete(ptr
, &queries
);
186 * ident_connected() - deal with the result of rb_connect_tcp()
188 * If the connection failed, we simply close the auth fd and report
189 * a failure. If the connection suceeded send the ident server a query
190 * giving "theirport , ourport". The write is only attempted *once* so
191 * it is deemed to be a fail if the entire write doesn't write all the
192 * data given. This shouldnt be a problem since the socket should have
193 * a write buffer far greater than this message to store it in should
194 * problems arise. -avalon
196 static void ident_connected(rb_fde_t
*F
, int error
, void *data
)
198 struct ident_query
*query
= data
;
199 struct auth_client
*auth
= query
->auth
;
203 /* Check the error */
206 /* We had an error during connection :( */
207 client_fail(query
, REPORT_FAIL
);
208 cleanup_query(query
);
212 snprintf(authbuf
, sizeof(authbuf
), "%u , %u\r\n",
213 auth
->c_port
, auth
->l_port
);
214 authlen
= strlen(authbuf
);
216 if(rb_write(query
->F
, authbuf
, authlen
) != authlen
)
218 client_fail(query
, REPORT_FAIL
);
222 read_ident_reply(query
->F
, query
);
226 read_ident_reply(rb_fde_t
*F
, void *data
)
228 struct ident_query
*query
= data
;
229 struct auth_client
*auth
= query
->auth
;
234 char buf
[IDENT_BUFSIZE
+ 1]; /* buffer to read auth reply into */
236 len
= rb_read(F
, buf
, IDENT_BUFSIZE
);
237 if(len
< 0 && rb_ignore_errno(errno
))
239 rb_setselect(F
, RB_SELECT_READ
, read_ident_reply
, query
);
247 if((s
= get_valid_ident(buf
)))
251 while (*s
== '~' || *s
== '^')
254 for (count
= USERLEN
; *s
&& count
; s
++)
260 if(*s
!= ' ' && *s
!= ':' && *s
!= '[')
271 client_fail(query
, REPORT_FAIL
);
273 client_success(query
);
275 cleanup_query(query
);
278 static void client_fail(struct ident_query
*query
, ident_message report
)
280 struct auth_client
*auth
= query
->auth
;
284 rb_strlcpy(auth
->username
, "*", sizeof(auth
->username
));
285 notice_client(auth
, messages
[report
]);
286 provider_done(auth
, PROVIDER_IDENT
);
290 static void client_success(struct ident_query
*query
)
292 struct auth_client
*auth
= query
->auth
;
296 notice_client(auth
, messages
[REPORT_FOUND
]);
297 provider_done(auth
, PROVIDER_IDENT
);
301 static void cleanup_query(struct ident_query
*query
)
303 rb_dlink_node
*ptr
, *nptr
;
305 RB_DLINK_FOREACH_SAFE(ptr
, nptr
, queries
.head
)
307 struct ident_query
*query_l
= ptr
->data
;
313 rb_dlinkDelete(ptr
, &queries
);
319 * parse ident query reply from identd server
321 * Torn out of old s_auth.c because there was nothing wrong with it
324 * Inputs - pointer to ident buf
325 * Outputs - NULL if no valid ident found, otherwise pointer to name
326 * Side effects - None
329 get_valid_ident(char *buf
)
337 char *remotePortString
;
339 /* All this to get rid of a sscanf() fun. */
340 remotePortString
= buf
;
342 colon1Ptr
= strchr(remotePortString
, ':');
348 colon2Ptr
= strchr(colon1Ptr
, ':');
354 commaPtr
= strchr(remotePortString
, ',');
362 remp
= atoi(remotePortString
);
366 locp
= atoi(commaPtr
);
370 /* look for USERID bordered by first pair of colons */
371 if(!strstr(colon1Ptr
, "USERID"))
374 colon3Ptr
= strchr(colon2Ptr
, ':');
384 struct auth_provider ident_provider
=
386 .id
= PROVIDER_IDENT
,
388 .destroy
= ident_destroy
,
389 .start
= ident_start
,
390 .cancel
= ident_cancel
,