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
31 time_t timeout
; /* Timeout interval */
32 rb_fde_t
*F
; /* Our FD */
35 /* Goinked from old s_auth.c --Elizafox */
36 static const char *messages
[] =
38 ":*** Checking Ident",
39 ":*** Got Ident response",
40 ":*** No Ident response",
50 static EVH timeout_ident_queries_event
;
51 static CNCB ident_connected
;
52 static PF read_ident_reply
;
54 static void client_fail(struct auth_client
*auth
, ident_message message
);
55 static void client_success(struct auth_client
*auth
);
56 static char * get_valid_ident(char *buf
);
58 static struct ev_entry
*timeout_ev
;
59 static int ident_timeout
= 5;
64 timeout_ev
= rb_event_addish("timeout_ident_queries_event", timeout_ident_queries_event
, NULL
, 1);
65 return (timeout_ev
!= NULL
);
68 void ident_destroy(void)
70 struct auth_client
*auth
;
71 rb_dictionary_iter iter
;
73 /* Nuke all ident queries */
74 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
76 if(auth
->data
[PROVIDER_IDENT
] != NULL
)
77 client_fail(auth
, REPORT_FAIL
);
81 bool ident_start(struct auth_client
*auth
)
83 struct ident_query
*query
= rb_malloc(sizeof(struct ident_query
));
84 struct rb_sockaddr_storage l_addr
, c_addr
;
88 auth
->data
[PROVIDER_IDENT
] = query
;
89 query
->timeout
= rb_current_time() + ident_timeout
;
91 if((F
= rb_socket(family
, SOCK_STREAM
, 0, "ident")) == NULL
)
93 client_fail(auth
, REPORT_FAIL
);
94 return true; /* Not a fatal error */
99 /* Build sockaddr_storages for rb_connect_tcp below */
100 if(!rb_inet_pton_sock(auth
->l_ip
, (struct sockaddr
*)&l_addr
, sizeof(l_addr
)) ||
101 !rb_inet_pton_sock(auth
->c_ip
, (struct sockaddr
)&c_addr
, sizeof(c_addr
)))
103 client_fail(auth
, REPORT_FAIL
);
107 /* Set the ports correctly */
109 if(GET_SS_FAMILY(&l_addr
) == AF_INET6
)
110 ((struct sockaddr_in6
*)&l_addr
)->sin6_port
= 0;
113 ((struct sockaddr_in
*)&l_addr
)->sin_port
= 0;
116 if(GET_SS_FAMILY(&c_addr
) == AF_INET6
)
117 ((struct sockaddr_in6
*)&c_addr
)->sin6_port
= htons(113);
120 ((struct sockaddr_in
*)&c_addr
)->sin_port
= htons(113);
122 rb_connect_tcp(F
, (struct sockaddr
*)&c_addr
,
123 (struct sockaddr
*)&l_addr
,
124 GET_SS_LEN(&l_addr
), ident_connected
,
125 query
, ident_timeout
);
127 notice_client(auth
, messages
[REPORT_LOOKUP
]);
128 set_provider(auth
, PROVIDER_IDENT
);
133 void ident_cancel(struct auth_client
*auth
)
135 struct ident_query
*query
= auth
->data
[PROVIDER_IDENT
];
138 client_fail(auth
, REPORT_FAIL
);
141 /* Timeout outstanding queries */
142 static void timeout_ident_queries_event(void *notused
)
144 struct auth_client
*auth
;
145 rb_dictionary_iter iter
;
147 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
149 struct ident_query
*query
= auth
->data
[PROVIDER_IDENT
];
151 if(query
!= NULL
&& query
->timeout
< rb_current_time())
152 client_fail(auth
, REPORT_FAIL
);
157 * ident_connected() - deal with the result of rb_connect_tcp()
159 * If the connection failed, we simply close the auth fd and report
160 * a failure. If the connection suceeded send the ident server a query
161 * giving "theirport , ourport". The write is only attempted *once* so
162 * it is deemed to be a fail if the entire write doesn't write all the
163 * data given. This shouldnt be a problem since the socket should have
164 * a write buffer far greater than this message to store it in should
165 * problems arise. -avalon
167 static void ident_connected(rb_fde_t
*F
, int error
, void *data
)
169 struct auth_client
*auth
= data
;
170 struct ident_query
*query
= auth
->data
[PROVIDER_IDENT
];
174 /* Check the error */
177 /* We had an error during connection :( */
178 client_fail(auth
, REPORT_FAIL
);
182 snprintf(authbuf
, sizeof(authbuf
), "%u , %u\r\n",
183 auth
->c_port
, auth
->l_port
);
184 authlen
= strlen(authbuf
);
186 if(rb_write(query
->F
, authbuf
, authlen
) != authlen
)
188 client_fail(auth
, REPORT_FAIL
);
192 read_ident_reply(query
->F
, auth
);
196 read_ident_reply(rb_fde_t
*F
, void *data
)
198 struct auth_client
*auth
= data
;
199 struct ident_query
*query
= auth
->data
[PROVIDER_IDENT
];
204 char buf
[IDENT_BUFSIZE
+ 1]; /* buffer to read auth reply into */
206 len
= rb_read(F
, buf
, IDENT_BUFSIZE
);
207 if(len
< 0 && rb_ignore_errno(errno
))
209 rb_setselect(F
, RB_SELECT_READ
, read_ident_reply
, query
);
217 if((s
= get_valid_ident(buf
)))
221 while (*s
== '~' || *s
== '^')
224 for (count
= USERLEN
; *s
&& count
; s
++)
230 if(*s
!= ' ' && *s
!= ':' && *s
!= '[')
241 client_fail(auth
, REPORT_FAIL
);
243 client_success(auth
);
246 static void client_fail(struct auth_client
*auth
, ident_message report
)
248 struct ident_query
*query
= auth
->data
[PROVIDER_IDENT
];
250 rb_strlcpy(auth
->username
, "*", sizeof(auth
->username
));
254 auth
->data
[PROVIDER_IDENT
] = NULL
;
256 notice_client(auth
, messages
[report
]);
257 provider_done(auth
, PROVIDER_IDENT
);
260 static void client_success(struct auth_client
*auth
)
262 struct ident_query
*query
= auth
->data
[PROVIDER_IDENT
];
266 auth
->data
[PROVIDER_IDENT
] = NULL
;
268 notice_client(auth
, messages
[REPORT_FOUND
]);
269 provider_done(auth
, PROVIDER_IDENT
);
273 * parse ident query reply from identd server
275 * Torn out of old s_auth.c because there was nothing wrong with it
278 * Inputs - pointer to ident buf
279 * Outputs - NULL if no valid ident found, otherwise pointer to name
280 * Side effects - None
283 get_valid_ident(char *buf
)
291 char *remotePortString
;
293 /* All this to get rid of a sscanf() fun. */
294 remotePortString
= buf
;
296 colon1Ptr
= strchr(remotePortString
, ':');
302 colon2Ptr
= strchr(colon1Ptr
, ':');
308 commaPtr
= strchr(remotePortString
, ',');
316 remp
= atoi(remotePortString
);
320 locp
= atoi(commaPtr
);
324 /* look for USERID bordered by first pair of colons */
325 if(!strstr(colon1Ptr
, "USERID"))
328 colon3Ptr
= strchr(colon2Ptr
, ':');
338 struct auth_provider ident_provider
=
340 .id
= PROVIDER_IDENT
,
342 .destroy
= ident_destroy
,
343 .start
= ident_start
,
344 .cancel
= ident_cancel
,