X-Git-Url: https://git.distorted.org.uk/~mdw/fwd/blobdiff_plain/e82f7154f65062d9ac8b9677862774498b331058..096c89c34505cadba1fc6e7a5367273d5c5bea47:/identify.c diff --git a/identify.c b/identify.c index db6df93..dd37a6a 100644 --- a/identify.c +++ b/identify.c @@ -1,71 +1,30 @@ /* -*-c-*- * - * $Id: identify.c,v 1.1 1999/07/01 08:56:23 mdw Exp $ - * * Identifies and logs the client of a connection * - * (c) 1999 Mark Wooding + * (c) 1999 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * - * This file is part of the `fw' port forwarder. + * This file is part of the `fwd' port forwarder. * - * `fw' is free software; you can redistribute it and/or modify + * `fwd' is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. - * - * `fw' is distributed in the hope that it will be useful, + * + * `fwd' is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - * + * * You should have received a copy of the GNU General Public License - * along with `fw'; if not, write to the Free Software Foundation, + * along with `fwd'; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/*----- Revision history --------------------------------------------------* - * - * $Log: identify.c,v $ - * Revision 1.1 1999/07/01 08:56:23 mdw - * Initial revision - * - */ - -/*----- Header files ------------------------------------------------------*/ - -#include "config.h" - -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include - -#include -#include -#include -#include -#include -#include -#include - -#include "bres.h" -#include "fw.h" -#include "ident.h" -#include "identify.h" +#include "fwd.h" /*----- Magic numbers -----------------------------------------------------*/ @@ -77,22 +36,19 @@ typedef struct id { id_req q; /* Copy of client's request block */ - void (*func)(void */*p*/); /* Function to call when done */ - void *p; /* Argument to pass to function */ time_t when; /* When the connection occurred */ conn c; /* Connection selector */ unsigned state; /* Current state of the world */ - bres_client r; /* Backgd resolver client block */ - char host[64]; /* Resolved hostname */ - char user[32]; /* Authenticated client user */ sel_timer t; /* Timeout selector */ - selbuf id; /* Reader for the RFC931 client */ + bres_client r; /* Backgd resolver client block */ + ident_request i; /* Ident client block */ + char host[128]; /* Resolved hostname */ + char user[64]; /* Authenticated client user */ } id; #define S_HOST 1u /* Read the hostname from resolver */ #define S_USER 2u /* Read the username from RFC931 */ -#define S_UCONN 4u /* Connected to remote RFC931 */ -#define S_TIMER 8u /* Timeout has completed */ +#define S_TIMER 4u /* Timeout has completed */ /*----- Main code ---------------------------------------------------------*/ @@ -107,36 +63,31 @@ typedef struct id { static void id_done(id *i) { - char buf[64]; - struct tm *tm; - /* --- Close down the various dependent bits --- */ if (!(i->state & S_HOST)) bres_abort(&i->r); - if (!(i->state & S_UCONN)) - conn_kill(&i->c); - else if (!(i->state & S_USER)) - selbuf_disable(&i->id); + if (!(i->state & S_USER)) + ident_abort(&i->i); if (!(i->state & S_TIMER)) sel_rmtimer(&i->t); /* --- Report the final result --- */ - tm = localtime(&i->when); - strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm); - syslog(LOG_NOTICE, "%s %s %s from %s@%s\n", - buf, i->q.desc, i->q.act, i->user, i->host); + fw_log(i->when, "[%s] %s from %s@%s [%s:%u]", + i->q.desc, i->q.act, + i->user, i->host, + inet_ntoa(i->q.rsin.sin_addr), (unsigned)ntohs(i->q.rsin.sin_port)); /* --- Dispose of the block --- */ - i->func(i->p); - free(i); + REFFD_DEC(i->q.r); + xfree(i); } /* --- @id_res@ --- * * - * Arguments: @const char *host@ = name of the resolved host + * Arguments: @struct hostent *h@ = name of the resolved host * @void *vp@ = pointer to identification block * * Returns: --- @@ -144,10 +95,11 @@ static void id_done(id *i) * Use: Responds to a completed reverse name resolution. */ -static void id_res(const char *host, void *vp) +static void id_res(struct hostent *h, void *vp) { id *i = vp; - str_sanitize(i->host, host, sizeof(i->host)); + if (h) + str_sanitize(i->host, h->h_name, sizeof(i->host)); i->state |= S_HOST; if (i->state & S_USER) id_done(i); @@ -155,7 +107,7 @@ static void id_res(const char *host, void *vp) /* --- @id_ident@ --- * * - * Arguments: @char *p@ = pointer to string read from server + * Arguments: @ident_reply *i@ = pointer to string read from server * @void *vp@ = pointer to identification block * * Returns: --- @@ -163,61 +115,22 @@ static void id_res(const char *host, void *vp) * Use: Responds to a line read from the remote RFC931 server. */ -static void id_ident(char *p, void *vp) +static void id_ident(ident_reply *ir, void *vp) { id *i = vp; - /* --- Get rid of the connection --- */ - - i->state |= S_USER; - selbuf_disable(&i->id); - close(i->id.reader.fd); - - /* --- Read the information from the returned line --- */ + /* --- Read the information from the client --- */ - if (p) { - ident idbuf; - ident_parse(p, &idbuf); - if (idbuf.type == ident_userid) - str_sanitize(i->user, idbuf.u.userid.user, sizeof(i->user)); - } + if (ir && ir->type == IDENT_USERID) + str_sanitize(i->user, ir->u.userid.user, sizeof(i->user)); /* --- Maybe finish off this identification --- */ + i->state |= S_USER; if (i->state & S_HOST) id_done(i); } -/* --- @id_conn@ --- * - * - * Arguments: @int fd@ = file descriptor connected - * @void *vp@ = pointer to identification block - * - * Returns: --- - * - * Use: Responds to a completed connection to the remote RFC931 - * server. - */ - -static void id_conn(int fd, void *vp) -{ - id *i = vp; - - if (fd == -1) { - i->state |= S_USER | S_UCONN; - if (i->state & S_HOST) - id_done(i); - } else { - dstr d = DSTR_INIT; - dstr_putf(&d, "%u, %u\n", - ntohs(i->q.rsin.sin_port), ntohs(i->q.lsin.sin_port)); - write(fd, d.buf, d.len); - dstr_destroy(&d); - i->state |= S_UCONN; - selbuf_init(&i->id, sel, fd, id_ident, i); - } -} - /* --- @id_timer@ --- * * * Arguments: @struct timeval *tv@ = pointer to the current time @@ -231,14 +144,13 @@ static void id_conn(int fd, void *vp) static void id_timer(struct timeval *tv, void *vp) { id *i = vp; + i->state |= S_TIMER; id_done(i); } /* --- @identify@ --- * * * Arguments: @const id_req *q@ = pointer to request block - * @void (*func)(void *p)@ = function to call when done - * @void *p@ = argument to pass to function * * Returns: --- * @@ -246,8 +158,7 @@ static void id_timer(struct timeval *tv, void *vp) * which will, eventually, report a message to the system log. */ -void identify(const id_req *q, - void (*func)(void */*p*/), void *p) +void identify(const id_req *q) { id *i; @@ -255,8 +166,7 @@ void identify(const id_req *q, i = xmalloc(sizeof(*i)); i->q = *q; - i->func = func; - i->p = p; + REFFD_INC(i->q.r); str_sanitize(i->host, inet_ntoa(q->rsin.sin_addr), sizeof(i->host)); strcpy(i->user, ""); @@ -265,33 +175,11 @@ void identify(const id_req *q, /* --- Set up the connection to the identity server --- */ - { - int fd; - struct sockaddr_in sin; - - if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) - id_conn(-1, i); - else { - sin.sin_family = AF_INET; - sin.sin_addr = q->lsin.sin_addr; - sin.sin_port = 0; - if (bind(fd, (struct sockaddr *)&sin, sizeof(sin))) { - close(fd); - id_conn(-1, i); - } else { - sin.sin_family = AF_INET; - sin.sin_addr = q->rsin.sin_addr; - sin.sin_port = htons(113); - conn_init(&i->c, sel, fd, - (struct sockaddr *)&sin, sizeof(sin), - id_conn, i); - } - } - } + ident(&i->i, sel, &q->lsin, &q->rsin, id_ident, i); /* --- Set up the name resolver --- */ - bres_resolve(&i->r, q->rsin.sin_addr, id_res, i); + bres_byaddr(&i->r, q->rsin.sin_addr, id_res, i); /* --- Set up the time limiter --- */