/* -*-c-*-
*
- * $Id: identify.c,v 1.2 1999/07/03 13:56:59 mdw Exp $
+ * $Id: identify.c,v 1.5 1999/10/10 16:45:34 mdw Exp $
*
* Identifies and logs the client of a connection
*
- * (c) 1999 Mark Wooding
+ * (c) 1999 Straylight/Edgeware
*/
/*----- Licensing notice --------------------------------------------------*
/*----- Revision history --------------------------------------------------*
*
* $Log: identify.c,v $
+ * Revision 1.5 1999/10/10 16:45:34 mdw
+ * Modified to use new mLib resolver and ident client.
+ *
+ * Revision 1.4 1999/07/27 18:30:53 mdw
+ * Various minor portability fixes.
+ *
+ * Revision 1.3 1999/07/26 23:26:21 mdw
+ * Minor modifications for new design.
+ *
* Revision 1.2 1999/07/03 13:56:59 mdw
* Log connections to syslog or stderr as appropriate.
*
#include <netdb.h>
#include <mLib/alloc.h>
+#include <mLib/bres.h>
#include <mLib/conn.h>
#include <mLib/dstr.h>
+#include <mLib/ident.h>
#include <mLib/report.h>
#include <mLib/sel.h>
#include <mLib/selbuf.h>
#include <mLib/str.h>
-#include "bres.h"
#include "fw.h"
-#include "ident.h"
#include "identify.h"
/*----- Magic numbers -----------------------------------------------------*/
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 */
+ sel_timer t; /* Timeout selector */
bres_client r; /* Backgd resolver client block */
+ ident_request i; /* Ident 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 */
} 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 ---------------------------------------------------------*/
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);
- if (flags & FW_SYSLOG) {
- syslog(LOG_NOTICE, "%s %s %s from %s@%s [%s]\n",
- buf, i->q.desc, i->q.act,
- i->user, i->host, inet_ntoa(i->q.rsin.sin_addr));
- } else {
- fprintf(stderr, "%s %s %s from %s@%s [%s]\n",
- buf, i->q.desc, i->q.act,
- i->user, i->host, inet_ntoa(i->q.rsin.sin_addr));
- }
+ fw_log(i->when, "[%s] %s from %s@%s [%s]",
+ i->q.desc, i->q.act,
+ i->user, i->host, inet_ntoa(i->q.rsin.sin_addr));
/* --- Dispose of the block --- */
- i->func(i->p);
+ REFFD_DEC(i->q.r);
free(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: ---
* 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);
/* --- @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: ---
* 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 --- */
+ /* --- Read the information from the client --- */
- i->state |= S_USER;
- selbuf_disable(&i->id);
- close(i->id.reader.fd);
-
- /* --- Read the information from the returned line --- */
-
- 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
/* --- @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: ---
*
* 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;
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, "<ANONYMOUS>");
/* --- 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 --- */