Whitespace fixing.
[fwd] / identify.c
index 8f0bf4c..dd37a6a 100644 (file)
@@ -1,80 +1,30 @@
 /* -*-c-*-
  *
- * $Id: identify.c,v 1.4 1999/07/27 18:30:53 mdw Exp $
- *
  * Identifies and logs the client of a connection
  *
  * (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.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.
- *
- * Revision 1.1.1.1  1999/07/01 08:56:23  mdw
- * Initial revision.
- *
- */
-
-/*----- Header files ------------------------------------------------------*/
-
-#include "config.h"
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-
-#include <sys/types.h>
-#include <sys/time.h>
-#include <unistd.h>
-#include <syslog.h>
-
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <netdb.h>
-
-#include <mLib/alloc.h>
-#include <mLib/conn.h>
-#include <mLib/dstr.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"
+#include "fwd.h"
 
 /*----- Magic numbers -----------------------------------------------------*/
 
@@ -89,17 +39,16 @@ typedef struct id {
   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 ---------------------------------------------------------*/
 
@@ -118,28 +67,27 @@ static void id_done(id *i)
 
   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 --- */
 
-  fw_log(i->when, "[%s] %s from %s@%s [%s]",
+  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));
+        i->user, i->host,
+        inet_ntoa(i->q.rsin.sin_addr), (unsigned)ntohs(i->q.rsin.sin_port));
 
   /* --- Dispose of the block --- */
 
   REFFD_DEC(i->q.r);
-  free(i);
+  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:    ---
@@ -147,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);
@@ -158,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:    ---
@@ -166,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
@@ -234,6 +144,7 @@ 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);
 }
 
@@ -264,36 +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 {
-      memset(&sin, 0, sizeof(sin));
-      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 {
-       int opt = 1;
-       sin.sin_family = AF_INET;
-       sin.sin_addr = q->rsin.sin_addr;
-       sin.sin_port = htons(113);
-       setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt));
-       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 --- */