2016-03-17 21:23:27 +00:00
|
|
|
/* authd/providers/ident.c - ident lookup provider for authd
|
|
|
|
* Copyright (c) 2016 Elizabeth Myers <elizabeth@interlinked.me>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice is present in all copies.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
|
|
|
|
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
|
|
|
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2016-03-26 20:39:55 +00:00
|
|
|
/* Largely adapted from old s_auth.c, but reworked for authd. rDNS code
|
|
|
|
* moved to its own provider.
|
|
|
|
*
|
|
|
|
* --Elizafox 13 March 2016
|
|
|
|
*/
|
|
|
|
|
2016-03-17 21:23:27 +00:00
|
|
|
#include "stdinc.h"
|
2016-04-07 09:47:48 +00:00
|
|
|
#include "defaults.h"
|
2016-03-17 21:23:27 +00:00
|
|
|
#include "match.h"
|
|
|
|
#include "authd.h"
|
2016-03-26 02:29:44 +00:00
|
|
|
#include "notice.h"
|
2016-03-17 21:23:27 +00:00
|
|
|
#include "provider.h"
|
|
|
|
#include "res.h"
|
|
|
|
|
2016-04-05 09:31:22 +00:00
|
|
|
#define SELF_PID (ident_provider.id)
|
|
|
|
|
2016-03-17 21:23:27 +00:00
|
|
|
#define IDENT_BUFSIZE 128
|
|
|
|
|
|
|
|
struct ident_query
|
|
|
|
{
|
|
|
|
rb_fde_t *F; /* Our FD */
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Goinked from old s_auth.c --Elizafox */
|
|
|
|
static const char *messages[] =
|
|
|
|
{
|
2016-03-26 23:01:14 +00:00
|
|
|
"*** Checking Ident",
|
|
|
|
"*** Got Ident response",
|
|
|
|
"*** No Ident response",
|
2016-03-27 00:18:54 +00:00
|
|
|
"*** Cannot verify ident validity, ignoring ident",
|
2016-03-28 21:46:52 +00:00
|
|
|
"*** Ident disabled, not checking ident",
|
2016-03-17 21:23:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
{
|
|
|
|
REPORT_LOOKUP,
|
|
|
|
REPORT_FOUND,
|
|
|
|
REPORT_FAIL,
|
2016-03-27 00:18:54 +00:00
|
|
|
REPORT_INVALID,
|
2016-03-28 21:46:52 +00:00
|
|
|
REPORT_DISABLED,
|
2016-03-17 21:23:27 +00:00
|
|
|
} ident_message;
|
|
|
|
|
|
|
|
static CNCB ident_connected;
|
|
|
|
static PF read_ident_reply;
|
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
static void client_fail(struct auth_client *auth, ident_message message);
|
|
|
|
static void client_success(struct auth_client *auth);
|
2016-03-17 21:23:27 +00:00
|
|
|
static char * get_valid_ident(char *buf);
|
|
|
|
|
2016-04-07 09:47:48 +00:00
|
|
|
static int ident_timeout = IDENT_TIMEOUT_DEFAULT;
|
2016-03-28 04:57:06 +00:00
|
|
|
static bool ident_enable = true;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ident_connected() - deal with the result of rb_connect_tcp()
|
|
|
|
*
|
|
|
|
* If the connection failed, we simply close the auth fd and report
|
|
|
|
* a failure. If the connection suceeded send the ident server a query
|
|
|
|
* giving "theirport , ourport". The write is only attempted *once* so
|
|
|
|
* it is deemed to be a fail if the entire write doesn't write all the
|
|
|
|
* data given. This shouldnt be a problem since the socket should have
|
|
|
|
* a write buffer far greater than this message to store it in should
|
|
|
|
* problems arise. -avalon
|
|
|
|
*/
|
2016-03-26 20:39:55 +00:00
|
|
|
static void
|
2016-03-26 23:40:17 +00:00
|
|
|
ident_connected(rb_fde_t *F __unused, int error, void *data)
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
struct auth_client *auth = data;
|
2016-03-26 23:40:17 +00:00
|
|
|
struct ident_query *query;
|
2016-03-17 21:23:27 +00:00
|
|
|
char authbuf[32];
|
|
|
|
int authlen;
|
|
|
|
|
2016-04-04 08:48:42 +00:00
|
|
|
lrb_assert(auth != NULL);
|
2016-04-05 09:31:22 +00:00
|
|
|
query = get_provider_data(auth, SELF_PID);
|
2016-04-04 08:48:42 +00:00
|
|
|
lrb_assert(query != NULL);
|
2016-03-26 23:01:14 +00:00
|
|
|
|
2016-03-17 21:23:27 +00:00
|
|
|
/* Check the error */
|
|
|
|
if(error != RB_OK)
|
|
|
|
{
|
|
|
|
/* We had an error during connection :( */
|
2016-03-23 00:13:54 +00:00
|
|
|
client_fail(auth, REPORT_FAIL);
|
2016-03-17 21:23:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(authbuf, sizeof(authbuf), "%u , %u\r\n",
|
|
|
|
auth->c_port, auth->l_port);
|
|
|
|
authlen = strlen(authbuf);
|
|
|
|
|
|
|
|
if(rb_write(query->F, authbuf, authlen) != authlen)
|
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
client_fail(auth, REPORT_FAIL);
|
2016-03-17 21:23:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
read_ident_reply(query->F, auth);
|
2016-03-17 21:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_ident_reply(rb_fde_t *F, void *data)
|
|
|
|
{
|
2016-03-23 00:13:54 +00:00
|
|
|
struct auth_client *auth = data;
|
2016-03-27 00:18:54 +00:00
|
|
|
char buf[IDENT_BUFSIZE + 1]; /* buffer to read auth reply into */
|
|
|
|
ident_message message = REPORT_FAIL;
|
2016-03-17 21:23:27 +00:00
|
|
|
char *s = NULL;
|
|
|
|
char *t = NULL;
|
2016-03-27 00:18:54 +00:00
|
|
|
ssize_t len;
|
2016-03-17 21:23:27 +00:00
|
|
|
int count;
|
|
|
|
|
|
|
|
len = rb_read(F, buf, IDENT_BUFSIZE);
|
|
|
|
if(len < 0 && rb_ignore_errno(errno))
|
|
|
|
{
|
2016-03-26 23:40:17 +00:00
|
|
|
rb_setselect(F, RB_SELECT_READ, read_ident_reply, auth);
|
2016-03-17 21:23:27 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(len > 0)
|
|
|
|
{
|
2016-03-27 00:18:54 +00:00
|
|
|
if((s = get_valid_ident(buf)) != NULL)
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
|
|
|
t = auth->username;
|
|
|
|
|
|
|
|
while (*s == '~' || *s == '^')
|
|
|
|
s++;
|
|
|
|
|
|
|
|
for (count = USERLEN; *s && count; s++)
|
|
|
|
{
|
2016-03-27 00:18:54 +00:00
|
|
|
if(*s == '@' || *s == '\r' || *s == '\n')
|
2016-03-17 21:23:27 +00:00
|
|
|
break;
|
2016-03-27 00:18:54 +00:00
|
|
|
|
2016-03-17 21:23:27 +00:00
|
|
|
if(*s != ' ' && *s != ':' && *s != '[')
|
|
|
|
{
|
|
|
|
*t++ = *s;
|
|
|
|
count--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*t = '\0';
|
|
|
|
}
|
2016-03-27 00:18:54 +00:00
|
|
|
else
|
|
|
|
message = REPORT_INVALID;
|
2016-03-17 21:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(s == NULL)
|
2016-03-27 00:18:54 +00:00
|
|
|
client_fail(auth, message);
|
2016-03-17 21:23:27 +00:00
|
|
|
else
|
2016-03-23 00:13:54 +00:00
|
|
|
client_success(auth);
|
2016-03-17 21:23:27 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 20:39:55 +00:00
|
|
|
static void
|
|
|
|
client_fail(struct auth_client *auth, ident_message report)
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-04-05 09:31:22 +00:00
|
|
|
struct ident_query *query = get_provider_data(auth, SELF_PID);
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-04-04 08:48:42 +00:00
|
|
|
lrb_assert(query != NULL);
|
2016-03-26 23:01:14 +00:00
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
rb_strlcpy(auth->username, "*", sizeof(auth->username));
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-03-26 23:01:14 +00:00
|
|
|
if(query->F != NULL)
|
|
|
|
rb_close(query->F);
|
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
rb_free(query);
|
2016-04-05 09:31:22 +00:00
|
|
|
set_provider_data(auth, SELF_PID, NULL);
|
|
|
|
set_provider_timeout_absolute(auth, SELF_PID, 0);
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-03-26 02:29:44 +00:00
|
|
|
notice_client(auth->cid, messages[report]);
|
2016-04-05 09:31:22 +00:00
|
|
|
provider_done(auth, SELF_PID);
|
2016-05-01 10:31:05 +00:00
|
|
|
|
|
|
|
auth_client_unref(auth);
|
2016-03-17 21:23:27 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 20:39:55 +00:00
|
|
|
static void
|
|
|
|
client_success(struct auth_client *auth)
|
2016-03-17 21:23:27 +00:00
|
|
|
{
|
2016-04-05 09:31:22 +00:00
|
|
|
struct ident_query *query = get_provider_data(auth, SELF_PID);
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-04-04 08:48:42 +00:00
|
|
|
lrb_assert(query != NULL);
|
2016-03-26 23:01:14 +00:00
|
|
|
|
|
|
|
if(query->F != NULL)
|
|
|
|
rb_close(query->F);
|
|
|
|
|
2016-03-23 00:13:54 +00:00
|
|
|
rb_free(query);
|
2016-04-05 09:31:22 +00:00
|
|
|
set_provider_data(auth, SELF_PID, NULL);
|
|
|
|
set_provider_timeout_absolute(auth, SELF_PID, 0);
|
2016-03-17 21:23:27 +00:00
|
|
|
|
2016-03-26 02:29:44 +00:00
|
|
|
notice_client(auth->cid, messages[REPORT_FOUND]);
|
2016-04-05 09:31:22 +00:00
|
|
|
provider_done(auth, SELF_PID);
|
2016-05-01 10:31:05 +00:00
|
|
|
|
|
|
|
auth_client_unref(auth);
|
2016-03-17 21:23:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get_valid_ident
|
|
|
|
* parse ident query reply from identd server
|
|
|
|
*
|
2016-03-26 20:39:55 +00:00
|
|
|
* Taken from old s_auth.c --Elizafox
|
2016-03-17 21:23:27 +00:00
|
|
|
*
|
|
|
|
* Inputs - pointer to ident buf
|
|
|
|
* Outputs - NULL if no valid ident found, otherwise pointer to name
|
|
|
|
* Side effects - None
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
get_valid_ident(char *buf)
|
|
|
|
{
|
|
|
|
int remp = 0;
|
|
|
|
int locp = 0;
|
|
|
|
char *colon1Ptr;
|
|
|
|
char *colon2Ptr;
|
|
|
|
char *colon3Ptr;
|
|
|
|
char *commaPtr;
|
|
|
|
char *remotePortString;
|
|
|
|
|
|
|
|
/* All this to get rid of a sscanf() fun. */
|
|
|
|
remotePortString = buf;
|
|
|
|
|
|
|
|
colon1Ptr = strchr(remotePortString, ':');
|
|
|
|
if(!colon1Ptr)
|
2016-03-27 00:18:54 +00:00
|
|
|
return NULL;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
*colon1Ptr = '\0';
|
|
|
|
colon1Ptr++;
|
|
|
|
colon2Ptr = strchr(colon1Ptr, ':');
|
|
|
|
if(!colon2Ptr)
|
2016-03-27 00:18:54 +00:00
|
|
|
return NULL;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
*colon2Ptr = '\0';
|
|
|
|
colon2Ptr++;
|
|
|
|
commaPtr = strchr(remotePortString, ',');
|
|
|
|
|
|
|
|
if(!commaPtr)
|
2016-03-27 00:18:54 +00:00
|
|
|
return NULL;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
*commaPtr = '\0';
|
|
|
|
commaPtr++;
|
|
|
|
|
|
|
|
remp = atoi(remotePortString);
|
|
|
|
if(!remp)
|
2016-03-27 00:18:54 +00:00
|
|
|
return NULL;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
locp = atoi(commaPtr);
|
|
|
|
if(!locp)
|
2016-03-27 00:18:54 +00:00
|
|
|
return NULL;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
/* look for USERID bordered by first pair of colons */
|
|
|
|
if(!strstr(colon1Ptr, "USERID"))
|
2016-03-27 00:18:54 +00:00
|
|
|
return NULL;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
colon3Ptr = strchr(colon2Ptr, ':');
|
|
|
|
if(!colon3Ptr)
|
2016-03-27 00:18:54 +00:00
|
|
|
return NULL;
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
*colon3Ptr = '\0';
|
|
|
|
colon3Ptr++;
|
|
|
|
return (colon3Ptr);
|
|
|
|
}
|
|
|
|
|
2016-03-26 20:39:55 +00:00
|
|
|
static void
|
|
|
|
ident_destroy(void)
|
|
|
|
{
|
2016-05-01 00:20:12 +00:00
|
|
|
struct auth_client *auth;
|
|
|
|
rb_dictionary_iter iter;
|
2016-03-26 20:39:55 +00:00
|
|
|
|
|
|
|
/* Nuke all ident queries */
|
2016-05-01 00:20:12 +00:00
|
|
|
RB_DICTIONARY_FOREACH(auth, &iter, auth_clients)
|
2016-03-26 20:39:55 +00:00
|
|
|
{
|
2016-04-05 09:31:22 +00:00
|
|
|
if(get_provider_data(auth, SELF_PID) != NULL)
|
2016-03-26 20:39:55 +00:00
|
|
|
client_fail(auth, REPORT_FAIL);
|
2016-05-01 10:31:05 +00:00
|
|
|
/* auth is now invalid as we have no reference */
|
2016-03-26 20:39:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-31 04:30:09 +00:00
|
|
|
static bool
|
|
|
|
ident_start(struct auth_client *auth)
|
2016-03-26 20:39:55 +00:00
|
|
|
{
|
|
|
|
struct ident_query *query = rb_malloc(sizeof(struct ident_query));
|
|
|
|
struct rb_sockaddr_storage l_addr, c_addr;
|
2016-03-26 22:35:48 +00:00
|
|
|
int family = GET_SS_FAMILY(&auth->c_addr);
|
|
|
|
|
2016-04-05 09:31:22 +00:00
|
|
|
lrb_assert(get_provider_data(auth, SELF_PID) == NULL);
|
2016-04-04 08:39:22 +00:00
|
|
|
|
|
|
|
if(!ident_enable)
|
2016-03-28 21:46:52 +00:00
|
|
|
{
|
2016-04-04 08:39:22 +00:00
|
|
|
rb_free(query);
|
2016-03-28 21:46:52 +00:00
|
|
|
notice_client(auth->cid, messages[REPORT_DISABLED]);
|
2017-08-20 11:53:44 +00:00
|
|
|
provider_done(auth, SELF_PID);
|
2016-03-28 21:46:52 +00:00
|
|
|
return true;
|
|
|
|
}
|
2016-03-27 01:33:35 +00:00
|
|
|
|
2016-05-01 10:31:05 +00:00
|
|
|
auth_client_ref(auth);
|
|
|
|
|
2016-03-26 22:35:48 +00:00
|
|
|
notice_client(auth->cid, messages[REPORT_LOOKUP]);
|
2016-03-26 20:39:55 +00:00
|
|
|
|
2016-04-05 09:31:22 +00:00
|
|
|
set_provider_data(auth, SELF_PID, query);
|
|
|
|
set_provider_timeout_relative(auth, SELF_PID, ident_timeout);
|
2016-03-26 20:39:55 +00:00
|
|
|
|
2016-03-26 22:35:48 +00:00
|
|
|
if((query->F = rb_socket(family, SOCK_STREAM, 0, "ident")) == NULL)
|
2016-03-26 20:39:55 +00:00
|
|
|
{
|
2016-03-26 22:35:48 +00:00
|
|
|
warn_opers(L_DEBUG, "Could not create ident socket: %s", strerror(errno));
|
2016-03-26 20:39:55 +00:00
|
|
|
client_fail(auth, REPORT_FAIL);
|
|
|
|
return true; /* Not a fatal error */
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Build sockaddr_storages for rb_connect_tcp below */
|
2016-03-31 07:18:02 +00:00
|
|
|
l_addr = auth->l_addr;
|
|
|
|
c_addr = auth->c_addr;
|
2016-03-26 20:39:55 +00:00
|
|
|
|
2016-03-31 08:00:29 +00:00
|
|
|
SET_SS_PORT(&l_addr, 0);
|
|
|
|
SET_SS_PORT(&c_addr, htons(113));
|
2016-03-26 20:39:55 +00:00
|
|
|
|
2016-03-26 22:35:48 +00:00
|
|
|
rb_connect_tcp(query->F, (struct sockaddr *)&c_addr,
|
2016-03-26 20:39:55 +00:00
|
|
|
(struct sockaddr *)&l_addr,
|
2016-04-24 16:11:20 +00:00
|
|
|
ident_connected,
|
2016-03-26 23:40:17 +00:00
|
|
|
auth, ident_timeout);
|
2016-03-26 20:39:55 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ident_cancel(struct auth_client *auth)
|
|
|
|
{
|
2016-04-05 09:31:22 +00:00
|
|
|
struct ident_query *query = get_provider_data(auth, SELF_PID);
|
2016-03-26 20:39:55 +00:00
|
|
|
|
|
|
|
if(query != NULL)
|
|
|
|
client_fail(auth, REPORT_FAIL);
|
|
|
|
}
|
|
|
|
|
2016-03-26 21:36:50 +00:00
|
|
|
static void
|
2016-03-26 23:40:17 +00:00
|
|
|
add_conf_ident_timeout(const char *key __unused, int parc __unused, const char **parv)
|
2016-03-26 21:36:50 +00:00
|
|
|
{
|
|
|
|
int timeout = atoi(parv[0]);
|
|
|
|
|
|
|
|
if(timeout < 0)
|
|
|
|
{
|
2016-03-29 00:22:02 +00:00
|
|
|
warn_opers(L_CRIT, "Ident: ident timeout < 0 (value: %d)", timeout);
|
|
|
|
exit(EX_PROVIDER_ERROR);
|
2016-03-26 21:36:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ident_timeout = timeout;
|
|
|
|
}
|
|
|
|
|
2016-03-28 04:57:06 +00:00
|
|
|
static void
|
|
|
|
set_ident_enabled(const char *key __unused, int parc __unused, const char **parv)
|
|
|
|
{
|
2016-03-28 21:46:52 +00:00
|
|
|
ident_enable = (*parv[0] == '1');
|
2016-03-28 04:57:06 +00:00
|
|
|
}
|
|
|
|
|
2016-03-26 21:36:50 +00:00
|
|
|
struct auth_opts_handler ident_options[] =
|
|
|
|
{
|
|
|
|
{ "ident_timeout", 1, add_conf_ident_timeout },
|
2016-03-28 04:57:06 +00:00
|
|
|
{ "ident_enabled", 1, set_ident_enabled },
|
2016-03-26 21:36:50 +00:00
|
|
|
{ NULL, 0, NULL },
|
|
|
|
};
|
|
|
|
|
2016-03-17 21:23:27 +00:00
|
|
|
|
|
|
|
struct auth_provider ident_provider =
|
|
|
|
{
|
2016-04-05 09:31:22 +00:00
|
|
|
.name = "ident",
|
|
|
|
.letter = 'I',
|
2016-03-31 04:30:09 +00:00
|
|
|
.start = ident_start,
|
2016-03-17 21:23:27 +00:00
|
|
|
.destroy = ident_destroy,
|
|
|
|
.cancel = ident_cancel,
|
2016-03-30 22:20:27 +00:00
|
|
|
.timeout = ident_cancel,
|
2016-03-26 21:36:50 +00:00
|
|
|
.opt_handlers = ident_options,
|
2016-03-17 21:23:27 +00:00
|
|
|
};
|