| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Nonblocking RFC931 client |
| 4 | * |
| 5 | * (c) 1999 Mark Wooding |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of the mLib utilities library. |
| 11 | * |
| 12 | * mLib is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU Library General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of the |
| 15 | * License, or (at your option) any later version. |
| 16 | * |
| 17 | * mLib 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 Library General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU Library General Public |
| 23 | * License along with mLib; if not, write to the Free |
| 24 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 25 | * MA 02111-1307, USA. |
| 26 | */ |
| 27 | |
| 28 | /*----- Header files ------------------------------------------------------*/ |
| 29 | |
| 30 | #include <ctype.h> |
| 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #include <sys/types.h> |
| 36 | #include <unistd.h> |
| 37 | |
| 38 | #include <sys/socket.h> |
| 39 | #include <netinet/in.h> |
| 40 | #include <arpa/inet.h> |
| 41 | #include <netdb.h> |
| 42 | |
| 43 | #include "conn.h" |
| 44 | #include "dstr.h" |
| 45 | #include "exc.h" |
| 46 | #include "ident.h" |
| 47 | #include "macros.h" |
| 48 | #include "selbuf.h" |
| 49 | |
| 50 | /*----- Main code ---------------------------------------------------------*/ |
| 51 | |
| 52 | /* --- @next@ --- * |
| 53 | * |
| 54 | * Arguments: @char **pp@ = address of string pointer |
| 55 | * |
| 56 | * Returns: Address of next token. |
| 57 | * |
| 58 | * Use: Reads the next token from the result string. Tokens are |
| 59 | * terminated by whitespace, or `:' or `,' characters. The |
| 60 | * result string has had `\' escapes removed; it's stored in |
| 61 | * the same memory as the input string. The actual content |
| 62 | * of the delimiter doesn't seem to be interesting, so it |
| 63 | * gets thrown away. |
| 64 | */ |
| 65 | |
| 66 | static char *next(char **pp) |
| 67 | { |
| 68 | char *p, *q, *r; |
| 69 | |
| 70 | /* --- Deal with reads past the end of the string --- */ |
| 71 | |
| 72 | if (!*pp) |
| 73 | return (""); |
| 74 | |
| 75 | /* --- Initialize various pointers into the string --- */ |
| 76 | |
| 77 | p = q = r = *pp; |
| 78 | |
| 79 | /* --- Skip past any leading whitespace --- */ |
| 80 | |
| 81 | while (ISSPACE(*p)) |
| 82 | p++; |
| 83 | |
| 84 | /* --- Now start work on the string itself --- */ |
| 85 | |
| 86 | for (;;) { |
| 87 | if (*p == 0 || *p == ':' || *p == ',' || ISSPACE(*p)) |
| 88 | break; |
| 89 | else if (*p == '\\') { |
| 90 | p++; |
| 91 | if (!*p) { |
| 92 | *q++ = '\\'; |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | *q++ = *p++; |
| 97 | } |
| 98 | |
| 99 | /* --- Tidy up afterwards --- */ |
| 100 | |
| 101 | while (ISSPACE(*p)) |
| 102 | p++; |
| 103 | if (*p == 0) |
| 104 | *pp = 0; |
| 105 | else if (*p == ':' || *p == ',') |
| 106 | *pp = p + 1; |
| 107 | else |
| 108 | *pp = p; |
| 109 | *q = 0; |
| 110 | |
| 111 | return (r); |
| 112 | } |
| 113 | |
| 114 | /* --- @parse@ --- * |
| 115 | * |
| 116 | * Arguments: @char *p@ = pointer to input string from identd |
| 117 | * @ident_reply *i@ = pointer to output block |
| 118 | * |
| 119 | * Returns: --- |
| 120 | * |
| 121 | * Use: Parses a result string from an RFC931 (identd) server. |
| 122 | */ |
| 123 | |
| 124 | static void parse(char *p, ident_reply *i) |
| 125 | { |
| 126 | char *q; |
| 127 | |
| 128 | /* --- Read the source and destination port numbers --- */ |
| 129 | |
| 130 | i->sport = atoi(next(&p)); |
| 131 | i->dport = atoi(next(&p)); |
| 132 | |
| 133 | /* --- Find out what sort of a reply this is --- */ |
| 134 | |
| 135 | q = next(&p); |
| 136 | if (STRCMP(q, ==, "USERID")) { |
| 137 | i->type = IDENT_USERID; |
| 138 | i->u.userid.os = next(&p); |
| 139 | i->u.userid.user = next(&p); |
| 140 | } else if (STRCMP(q, ==, "ERROR")) { |
| 141 | i->type = IDENT_ERROR; |
| 142 | i->u.error = next(&p); |
| 143 | } else |
| 144 | i->type = IDENT_BAD; |
| 145 | } |
| 146 | |
| 147 | /* --- @line@ --- * |
| 148 | * |
| 149 | * Arguments: @char *s@ = pointer to string from ident server |
| 150 | * @size_t len@ = length of the line |
| 151 | * @void *p@ = pointer to my request block |
| 152 | * |
| 153 | * Returns: --- |
| 154 | * |
| 155 | * Use: Handles a string from an ident server. |
| 156 | */ |
| 157 | |
| 158 | static void line(char *s, size_t len, void *p) |
| 159 | { |
| 160 | ident_request *rq = p; |
| 161 | |
| 162 | rq->state = IDENT_DONE; |
| 163 | close(rq->b.reader.fd); |
| 164 | if (!s) |
| 165 | rq->func(0, rq->p); |
| 166 | else { |
| 167 | ident_reply i; |
| 168 | parse(s, &i); |
| 169 | rq->func(&i, rq->p); |
| 170 | } |
| 171 | selbuf_destroy(&rq->b); |
| 172 | } |
| 173 | |
| 174 | /* --- @connected@ --- * |
| 175 | * |
| 176 | * Arguments: @int fd@ = file descriptor |
| 177 | * @void *p@ = pointer to request block |
| 178 | * |
| 179 | * Returns: --- |
| 180 | * |
| 181 | * Use: Handles a connection to a remote ident server. |
| 182 | */ |
| 183 | |
| 184 | static void connected(int fd, void *p) |
| 185 | { |
| 186 | ident_request *rq = p; |
| 187 | dstr d = DSTR_INIT; |
| 188 | |
| 189 | /* --- Handle an error during the connect --- */ |
| 190 | |
| 191 | if (fd < 0) |
| 192 | goto fail_0; |
| 193 | |
| 194 | /* --- Initialize the string to send to the remote host --- */ |
| 195 | |
| 196 | TRY { |
| 197 | dstr_putf(&d, "%u, %u\r\n", |
| 198 | ntohs(rq->remote.sin_port), ntohs(rq->local.sin_port)); |
| 199 | } CATCH switch (exc_type) { |
| 200 | case EXC_NOMEM: |
| 201 | EXIT_TRY; |
| 202 | goto fail_1; |
| 203 | default: |
| 204 | RETHROW; |
| 205 | } END_TRY; |
| 206 | |
| 207 | /* --- Do the rest of the work --- */ |
| 208 | |
| 209 | if (write(fd, d.buf, d.len) < d.len) |
| 210 | goto fail_1; |
| 211 | dstr_destroy(&d); |
| 212 | rq->state = IDENT_READ; |
| 213 | selbuf_init(&rq->b, rq->s, fd, line, rq); |
| 214 | return; |
| 215 | |
| 216 | /* --- Tidy up after misfortunes --- */ |
| 217 | |
| 218 | fail_1: |
| 219 | dstr_destroy(&d); |
| 220 | close(fd); |
| 221 | fail_0: |
| 222 | rq->state = IDENT_DONE; |
| 223 | rq->func(0, rq->p); |
| 224 | } |
| 225 | |
| 226 | /* --- @ident_abort@ --- * |
| 227 | * |
| 228 | * Arguments: @ident_request *rq@ = pointer to request block |
| 229 | * |
| 230 | * Returns: --- |
| 231 | * |
| 232 | * Use: Cancels an ident request in progress. |
| 233 | */ |
| 234 | |
| 235 | void ident_abort(ident_request *rq) |
| 236 | { |
| 237 | switch (rq->state) { |
| 238 | case IDENT_CONN: |
| 239 | conn_kill(&rq->c); |
| 240 | break; |
| 241 | case IDENT_READ: |
| 242 | close(rq->b.reader.fd); |
| 243 | selbuf_destroy(&rq->b); |
| 244 | break; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | /* --- @go@ --- * |
| 249 | * |
| 250 | * Arguments: @ident_request *rq@ = pointer to request block |
| 251 | * |
| 252 | * Returns: --- |
| 253 | * |
| 254 | * Use: Starts a connection to the remote ident server. |
| 255 | */ |
| 256 | |
| 257 | static void go(ident_request *rq) |
| 258 | { |
| 259 | int fd; |
| 260 | struct sockaddr_in sin; |
| 261 | int opt; |
| 262 | |
| 263 | /* --- Create the socket I'll use --- */ |
| 264 | |
| 265 | if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0) |
| 266 | goto fail_0; |
| 267 | memset(&sin, 0, sizeof(sin)); |
| 268 | sin.sin_family = AF_INET; |
| 269 | sin.sin_port = 0; |
| 270 | sin.sin_addr = rq->local.sin_addr; |
| 271 | if (bind(fd, (struct sockaddr *)&sin, sizeof(sin))) |
| 272 | goto fail_1; |
| 273 | |
| 274 | /* --- Out-of-band data would confuse us --- */ |
| 275 | |
| 276 | opt = 1; |
| 277 | setsockopt(fd, SOL_SOCKET, SO_OOBINLINE, &opt, sizeof(opt)); |
| 278 | |
| 279 | /* --- Start a connection to the remote server --- */ |
| 280 | |
| 281 | sin.sin_family = AF_INET; |
| 282 | sin.sin_port = htons(113); |
| 283 | sin.sin_addr = rq->remote.sin_addr; |
| 284 | if (conn_init(&rq->c, rq->s, fd, (struct sockaddr *)&sin, sizeof(sin), |
| 285 | connected, rq)) |
| 286 | goto fail_0; |
| 287 | |
| 288 | /* --- Finish off initializing the block --- */ |
| 289 | |
| 290 | rq->state = IDENT_CONN; |
| 291 | return; |
| 292 | |
| 293 | /* --- Tidy up after lossage --- */ |
| 294 | |
| 295 | fail_1: |
| 296 | close(fd); |
| 297 | fail_0: |
| 298 | rq->state = IDENT_DONE; |
| 299 | rq->func(0, rq->p); |
| 300 | } |
| 301 | |
| 302 | /* --- @ident@ --- * |
| 303 | * |
| 304 | * Arguments: @ident_request *rq@ = pointer to request block |
| 305 | * @sel_state *s@ = I/O multiplexor |
| 306 | * @const struct sockaddr_in *local, *remote@ = addresses |
| 307 | * @void (*func)(ident_reply *i, void *p)@ = handler function |
| 308 | * @void *p@ = argument for handler |
| 309 | * |
| 310 | * Returns: --- |
| 311 | * |
| 312 | * Use: Initializes an ident request. |
| 313 | */ |
| 314 | |
| 315 | void ident(ident_request *rq, sel_state *s, |
| 316 | const struct sockaddr_in *local, |
| 317 | const struct sockaddr_in *remote, |
| 318 | void (*func)(ident_reply */*i*/, void */*p*/), |
| 319 | void *p) |
| 320 | { |
| 321 | memcpy(&rq->local, local, sizeof(rq->local)); |
| 322 | memcpy(&rq->remote, remote, sizeof(rq->remote)); |
| 323 | rq->func = func; |
| 324 | rq->p = p; |
| 325 | rq->s = s; |
| 326 | go(rq); |
| 327 | } |
| 328 | |
| 329 | /* --- @ident_socket@ --- * |
| 330 | * |
| 331 | * Arguments: @ident_request *rq@ = pointer to request block |
| 332 | * @sel_state *s@ = I/O multiplexor |
| 333 | * @int sk@ = connected socket file descriptor |
| 334 | * @void (*func)(ident_reply *i, void *p)@ = handler function |
| 335 | * @void *p@ = argument for handler |
| 336 | * |
| 337 | * Returns: --- |
| 338 | * |
| 339 | * Use: An alternative interface to @ident@. Initializes an ident |
| 340 | * request from a connected socket, rather than from an explicit |
| 341 | * address. This will call @getsockname@ and @getpeername@ to |
| 342 | * find out what the socket is actually connected to, which adds |
| 343 | * convenience but wastes time. |
| 344 | */ |
| 345 | |
| 346 | void ident_socket(ident_request *rq, sel_state *s, int sk, |
| 347 | void (*func)(ident_reply */*i*/, void */*p*/), |
| 348 | void *p) |
| 349 | { |
| 350 | socklen_t sinsz; |
| 351 | if ((sinsz = sizeof(struct sockaddr_in), |
| 352 | getsockname(sk, (struct sockaddr *)&rq->local, &sinsz)) || |
| 353 | (sinsz = sizeof(struct sockaddr_in), |
| 354 | getpeername(sk, (struct sockaddr *)&rq->remote, &sinsz))) { |
| 355 | rq->state = IDENT_DONE; |
| 356 | func(0, p); |
| 357 | return; |
| 358 | } |
| 359 | |
| 360 | rq->func = func; |
| 361 | rq->p = p; |
| 362 | rq->s = s; |
| 363 | go(rq); |
| 364 | } |
| 365 | |
| 366 | /*----- That's all, folks -------------------------------------------------*/ |