| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Identifies and logs the client of a connection |
| 4 | * |
| 5 | * (c) 1999 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the `fw' port forwarder. |
| 11 | * |
| 12 | * `fw' is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2 of the License, or |
| 15 | * (at your option) any later version. |
| 16 | * |
| 17 | * `fw' is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with `fw'; if not, write to the Free Software Foundation, |
| 24 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 25 | */ |
| 26 | |
| 27 | #include "fw.h" |
| 28 | |
| 29 | /*----- Magic numbers -----------------------------------------------------*/ |
| 30 | |
| 31 | #define TIMEOUT 15 /* Seconds to wait for answers */ |
| 32 | |
| 33 | /*----- Data structures ---------------------------------------------------*/ |
| 34 | |
| 35 | /* --- Structure to track the progress of an identification --- */ |
| 36 | |
| 37 | typedef struct id { |
| 38 | id_req q; /* Copy of client's request block */ |
| 39 | time_t when; /* When the connection occurred */ |
| 40 | conn c; /* Connection selector */ |
| 41 | unsigned state; /* Current state of the world */ |
| 42 | sel_timer t; /* Timeout selector */ |
| 43 | bres_client r; /* Backgd resolver client block */ |
| 44 | ident_request i; /* Ident client block */ |
| 45 | char host[128]; /* Resolved hostname */ |
| 46 | char user[64]; /* Authenticated client user */ |
| 47 | } id; |
| 48 | |
| 49 | #define S_HOST 1u /* Read the hostname from resolver */ |
| 50 | #define S_USER 2u /* Read the username from RFC931 */ |
| 51 | #define S_TIMER 4u /* Timeout has completed */ |
| 52 | |
| 53 | /*----- Main code ---------------------------------------------------------*/ |
| 54 | |
| 55 | /* --- @id_done@ --- * |
| 56 | * |
| 57 | * Arguments: @id *i@ = pointer to identification block |
| 58 | * |
| 59 | * Returns: --- |
| 60 | * |
| 61 | * Use: Finishes with an identification block. |
| 62 | */ |
| 63 | |
| 64 | static void id_done(id *i) |
| 65 | { |
| 66 | /* --- Close down the various dependent bits --- */ |
| 67 | |
| 68 | if (!(i->state & S_HOST)) |
| 69 | bres_abort(&i->r); |
| 70 | if (!(i->state & S_USER)) |
| 71 | ident_abort(&i->i); |
| 72 | if (!(i->state & S_TIMER)) |
| 73 | sel_rmtimer(&i->t); |
| 74 | |
| 75 | /* --- Report the final result --- */ |
| 76 | |
| 77 | fw_log(i->when, "[%s] %s from %s@%s [%s:%u]", |
| 78 | i->q.desc, i->q.act, |
| 79 | i->user, i->host, |
| 80 | inet_ntoa(i->q.rsin.sin_addr), (unsigned)ntohs(i->q.rsin.sin_port)); |
| 81 | |
| 82 | /* --- Dispose of the block --- */ |
| 83 | |
| 84 | REFFD_DEC(i->q.r); |
| 85 | xfree(i); |
| 86 | } |
| 87 | |
| 88 | /* --- @id_res@ --- * |
| 89 | * |
| 90 | * Arguments: @struct hostent *h@ = name of the resolved host |
| 91 | * @void *vp@ = pointer to identification block |
| 92 | * |
| 93 | * Returns: --- |
| 94 | * |
| 95 | * Use: Responds to a completed reverse name resolution. |
| 96 | */ |
| 97 | |
| 98 | static void id_res(struct hostent *h, void *vp) |
| 99 | { |
| 100 | id *i = vp; |
| 101 | if (h) |
| 102 | str_sanitize(i->host, h->h_name, sizeof(i->host)); |
| 103 | i->state |= S_HOST; |
| 104 | if (i->state & S_USER) |
| 105 | id_done(i); |
| 106 | } |
| 107 | |
| 108 | /* --- @id_ident@ --- * |
| 109 | * |
| 110 | * Arguments: @ident_reply *i@ = pointer to string read from server |
| 111 | * @void *vp@ = pointer to identification block |
| 112 | * |
| 113 | * Returns: --- |
| 114 | * |
| 115 | * Use: Responds to a line read from the remote RFC931 server. |
| 116 | */ |
| 117 | |
| 118 | static void id_ident(ident_reply *ir, void *vp) |
| 119 | { |
| 120 | id *i = vp; |
| 121 | |
| 122 | /* --- Read the information from the client --- */ |
| 123 | |
| 124 | if (ir && ir->type == IDENT_USERID) |
| 125 | str_sanitize(i->user, ir->u.userid.user, sizeof(i->user)); |
| 126 | |
| 127 | /* --- Maybe finish off this identification --- */ |
| 128 | |
| 129 | i->state |= S_USER; |
| 130 | if (i->state & S_HOST) |
| 131 | id_done(i); |
| 132 | } |
| 133 | |
| 134 | /* --- @id_timer@ --- * |
| 135 | * |
| 136 | * Arguments: @struct timeval *tv@ = pointer to the current time |
| 137 | * @void *vp@ = pointer to identification block |
| 138 | * |
| 139 | * Returns: --- |
| 140 | * |
| 141 | * Use: Times an identification job out. |
| 142 | */ |
| 143 | |
| 144 | static void id_timer(struct timeval *tv, void *vp) |
| 145 | { |
| 146 | id *i = vp; |
| 147 | i->state |= S_TIMER; |
| 148 | id_done(i); |
| 149 | } |
| 150 | |
| 151 | /* --- @identify@ --- * |
| 152 | * |
| 153 | * Arguments: @const id_req *q@ = pointer to request block |
| 154 | * |
| 155 | * Returns: --- |
| 156 | * |
| 157 | * Use: Starts a background ident lookup and reverse-resolve job |
| 158 | * which will, eventually, report a message to the system log. |
| 159 | */ |
| 160 | |
| 161 | void identify(const id_req *q) |
| 162 | { |
| 163 | id *i; |
| 164 | |
| 165 | /* --- Initialize the block with stuff --- */ |
| 166 | |
| 167 | i = xmalloc(sizeof(*i)); |
| 168 | i->q = *q; |
| 169 | REFFD_INC(i->q.r); |
| 170 | |
| 171 | str_sanitize(i->host, inet_ntoa(q->rsin.sin_addr), sizeof(i->host)); |
| 172 | strcpy(i->user, "<ANONYMOUS>"); |
| 173 | i->state = 0; |
| 174 | i->when = time(0); |
| 175 | |
| 176 | /* --- Set up the connection to the identity server --- */ |
| 177 | |
| 178 | ident(&i->i, sel, &q->lsin, &q->rsin, id_ident, i); |
| 179 | |
| 180 | /* --- Set up the name resolver --- */ |
| 181 | |
| 182 | bres_byaddr(&i->r, q->rsin.sin_addr, id_res, i); |
| 183 | |
| 184 | /* --- Set up the time limiter --- */ |
| 185 | |
| 186 | { |
| 187 | struct timeval tv; |
| 188 | gettimeofday(&tv, 0); |
| 189 | tv.tv_sec += TIMEOUT; |
| 190 | sel_addtimer(sel, &i->t, &tv, id_timer, i); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /*----- That's all, folks -------------------------------------------------*/ |