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.
21 /* Largely adapted from old s_auth.c, but reworked for authd. rDNS code
22 * moved to its own provider.
24 * --Elizafox 13 March 2016
34 #define IDENT_BUFSIZE 128
38 rb_fde_t
*F
; /* Our FD */
41 /* Goinked from old s_auth.c --Elizafox */
42 static const char *messages
[] =
45 "*** Got Ident response",
46 "*** No Ident response",
47 "*** Cannot verify ident validity, ignoring ident",
48 "*** Ident disabled, not checking ident",
60 static CNCB ident_connected
;
61 static PF read_ident_reply
;
63 static void client_fail(struct auth_client
*auth
, ident_message message
);
64 static void client_success(struct auth_client
*auth
);
65 static char * get_valid_ident(char *buf
);
67 static int ident_timeout
= 5;
68 static bool ident_enable
= true;
72 * ident_connected() - deal with the result of rb_connect_tcp()
74 * If the connection failed, we simply close the auth fd and report
75 * a failure. If the connection suceeded send the ident server a query
76 * giving "theirport , ourport". The write is only attempted *once* so
77 * it is deemed to be a fail if the entire write doesn't write all the
78 * data given. This shouldnt be a problem since the socket should have
79 * a write buffer far greater than this message to store it in should
80 * problems arise. -avalon
83 ident_connected(rb_fde_t
*F __unused
, int error
, void *data
)
85 struct auth_client
*auth
= data
;
86 struct ident_query
*query
;
93 query
= auth
->data
[PROVIDER_IDENT
];
101 /* We had an error during connection :( */
102 client_fail(auth
, REPORT_FAIL
);
106 snprintf(authbuf
, sizeof(authbuf
), "%u , %u\r\n",
107 auth
->c_port
, auth
->l_port
);
108 authlen
= strlen(authbuf
);
110 if(rb_write(query
->F
, authbuf
, authlen
) != authlen
)
112 client_fail(auth
, REPORT_FAIL
);
116 read_ident_reply(query
->F
, auth
);
120 read_ident_reply(rb_fde_t
*F
, void *data
)
122 struct auth_client
*auth
= data
;
123 struct ident_query
*query
;
124 char buf
[IDENT_BUFSIZE
+ 1]; /* buffer to read auth reply into */
125 ident_message message
= REPORT_FAIL
;
134 query
= auth
->data
[PROVIDER_IDENT
];
139 len
= rb_read(F
, buf
, IDENT_BUFSIZE
);
140 if(len
< 0 && rb_ignore_errno(errno
))
142 rb_setselect(F
, RB_SELECT_READ
, read_ident_reply
, auth
);
148 if((s
= get_valid_ident(buf
)) != NULL
)
152 while (*s
== '~' || *s
== '^')
155 for (count
= USERLEN
; *s
&& count
; s
++)
157 if(*s
== '@' || *s
== '\r' || *s
== '\n')
160 if(*s
!= ' ' && *s
!= ':' && *s
!= '[')
169 message
= REPORT_INVALID
;
173 client_fail(auth
, message
);
175 client_success(auth
);
179 client_fail(struct auth_client
*auth
, ident_message report
)
181 struct ident_query
*query
= auth
->data
[PROVIDER_IDENT
];
186 rb_strlcpy(auth
->username
, "*", sizeof(auth
->username
));
192 auth
->data
[PROVIDER_IDENT
] = NULL
;
193 auth
->timeout
[PROVIDER_IDENT
] = 0;
195 notice_client(auth
->cid
, messages
[report
]);
196 provider_done(auth
, PROVIDER_IDENT
);
200 client_success(struct auth_client
*auth
)
202 struct ident_query
*query
= auth
->data
[PROVIDER_IDENT
];
211 auth
->data
[PROVIDER_IDENT
] = NULL
;
212 auth
->timeout
[PROVIDER_IDENT
] = 0;
214 notice_client(auth
->cid
, messages
[REPORT_FOUND
]);
215 provider_done(auth
, PROVIDER_IDENT
);
219 * parse ident query reply from identd server
221 * Taken from old s_auth.c --Elizafox
223 * Inputs - pointer to ident buf
224 * Outputs - NULL if no valid ident found, otherwise pointer to name
225 * Side effects - None
228 get_valid_ident(char *buf
)
236 char *remotePortString
;
238 /* All this to get rid of a sscanf() fun. */
239 remotePortString
= buf
;
241 colon1Ptr
= strchr(remotePortString
, ':');
247 colon2Ptr
= strchr(colon1Ptr
, ':');
253 commaPtr
= strchr(remotePortString
, ',');
261 remp
= atoi(remotePortString
);
265 locp
= atoi(commaPtr
);
269 /* look for USERID bordered by first pair of colons */
270 if(!strstr(colon1Ptr
, "USERID"))
273 colon3Ptr
= strchr(colon2Ptr
, ':');
285 struct auth_client
*auth
;
286 rb_dictionary_iter iter
;
288 /* Nuke all ident queries */
289 RB_DICTIONARY_FOREACH(auth
, &iter
, auth_clients
)
291 if(auth
->data
[PROVIDER_IDENT
] != NULL
)
292 client_fail(auth
, REPORT_FAIL
);
297 ident_start(struct auth_client
*auth
)
299 struct ident_query
*query
= rb_malloc(sizeof(struct ident_query
));
300 struct rb_sockaddr_storage l_addr
, c_addr
;
301 int family
= GET_SS_FAMILY(&auth
->c_addr
);
303 if(auth
->data
[PROVIDER_IDENT
] != NULL
)
305 set_provider_done(auth
, PROVIDER_IDENT
); /* for blacklists */
308 else if(!ident_enable
)
310 notice_client(auth
->cid
, messages
[REPORT_DISABLED
]);
311 set_provider_done(auth
, PROVIDER_IDENT
);
315 notice_client(auth
->cid
, messages
[REPORT_LOOKUP
]);
317 auth
->data
[PROVIDER_IDENT
] = query
;
318 auth
->timeout
[PROVIDER_IDENT
] = rb_current_time() + ident_timeout
;
320 if((query
->F
= rb_socket(family
, SOCK_STREAM
, 0, "ident")) == NULL
)
322 warn_opers(L_DEBUG
, "Could not create ident socket: %s", strerror(errno
));
323 client_fail(auth
, REPORT_FAIL
);
324 return true; /* Not a fatal error */
327 /* Build sockaddr_storages for rb_connect_tcp below */
328 l_addr
= auth
->l_addr
;
329 c_addr
= auth
->c_addr
;
331 SET_SS_PORT(&l_addr
, 0);
332 SET_SS_PORT(&c_addr
, htons(113));
334 rb_connect_tcp(query
->F
, (struct sockaddr
*)&c_addr
,
335 (struct sockaddr
*)&l_addr
,
336 GET_SS_LEN(&l_addr
), ident_connected
,
337 auth
, ident_timeout
);
339 set_provider_on(auth
, PROVIDER_IDENT
);
345 ident_cancel(struct auth_client
*auth
)
347 struct ident_query
*query
= auth
->data
[PROVIDER_IDENT
];
350 client_fail(auth
, REPORT_FAIL
);
354 add_conf_ident_timeout(const char *key __unused
, int parc __unused
, const char **parv
)
356 int timeout
= atoi(parv
[0]);
360 warn_opers(L_CRIT
, "Ident: ident timeout < 0 (value: %d)", timeout
);
361 exit(EX_PROVIDER_ERROR
);
364 ident_timeout
= timeout
;
368 set_ident_enabled(const char *key __unused
, int parc __unused
, const char **parv
)
370 ident_enable
= (*parv
[0] == '1');
373 struct auth_opts_handler ident_options
[] =
375 { "ident_timeout", 1, add_conf_ident_timeout
},
376 { "ident_enabled", 1, set_ident_enabled
},
381 struct auth_provider ident_provider
=
383 .id
= PROVIDER_IDENT
,
384 .start
= ident_start
,
385 .destroy
= ident_destroy
,
386 .cancel
= ident_cancel
,
387 .timeout
= ident_cancel
,
388 .opt_handlers
= ident_options
,