1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
|
/*
* Copyright (c) 1997, 1998 Motoyuki Kasahara
*
* This program 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, or (at your option)
* any later version.
*
* This program 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.
*/
/*
* This program requires the following Autoconf macros:
* AC_C_CONST
* AC_TYPE_SIZE_T
* AC_HEADER_STDC
* AC_CHECK_HEADERS(string.h, memory.h, stdlib.h, unistd.h)
* AC_CHECK_FUNCS(memcpy, strtol, sigprocmask, sigsetjmp)
* AC_TYPE_SIGNAL
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <signal.h>
#include <syslog.h>
#include <setjmp.h>
#include <errno.h>
#if defined(STDC_HEADERS) || defined(HAVE_STRING_H)
#include <string.h>
#if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#include <memory.h>
#endif /* not STDC_HEADERS and HAVE_MEMORY_H */
#else /* not STDC_HEADERS and not HAVE_STRING_H */
#include <strings.h>
#endif /* not STDC_HEADERS and not HAVE_STRING_H */
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include "ident.h"
#ifdef USE_FAKELOG
#include "fakelog.h"
#endif
#ifndef HAVE_MEMCPY
#define memcpy(d, s, n) bcopy((s), (d), (n))
#ifdef __STDC__
void *memchr(const void *, int, size_t);
int memcmp(const void *, const void *, size_t);
void *memmove(void *, const void *, size_t);
void *memset(void *, int, size_t);
#else /* not __STDC__ */
char *memchr();
int memcmp();
char *memmove();
char *memset();
#endif /* not __STDC__ */
#endif /* not HAVE_MEMCPY */
#ifndef HAVE_STRTOL
#ifdef __STDC__
long strtol(const char *, char **, int);
#else /* not __STDC__ */
long strtol();
#endif /* not __STDC__ */
#endif /* not HAVE_STRTOL */
/*
* Unexported functions.
*/
#ifdef __STDC__
static RETSIGTYPE ident_timeout(int);
#else
static RETSIGTYPE ident_timeout();
#endif
/*
* The port number of Identification protocol.
*/
#ifndef RFC1413_PORT
#define RFC1413_PORT 113
#endif
/*
* The maximum length of request and response lines in Identification
* protocol. (default: 1023)
*/
#ifndef MAXLEN_RFC1413_LINE
#define MAXLEN_RFC1413_LINE 511
#endif
/*
* The maximum number of arguments in a response line in Identification
* protocol. The get_ideint() function assumes that arguments in a
* response line consist of following 5 fields.
*
* <port-on-server>, <port-on-client> : <status> : <opsys> : <user-id>
*/
#define MAX_RFC1413_ARGS 5
/*
* Argument separators in a response line in Identification protocol.
*/
static char separators[MAX_RFC1413_ARGS] = {',', ':', ':', ':', '\0'};
/*
* The buffer for setjmp() or sigsetjmp().
*/
#ifdef HAVE_SIGSETJMP
sigjmp_buf jump_buffer;
#else
jmp_buf jump_buffer;
#endif
/*
* Get a remote user-id of the socket `file' by Identification protocol
* described in RFC1413.
*
* The user-id is put into `user'.
* `Timeout' specifies seconds until the server gives up to get an user-id.
*
* If succeed, 0 is returned. Otherwise -1 is returned and `user' is
* set to UNKNOWN_USER.
*/
int
identify_user(file, user, timeout)
int file;
char *user;
int timeout;
{
#ifdef HAVE_SIGPROCMASK
struct sigaction act;
#endif
static int idfile;
struct sockaddr_in myaddr, hisaddr;
char linebuf[MAXLEN_RFC1413_LINE + 1];
char *args[MAX_RFC1413_ARGS];
char *bufp, *wsp;
int dstport, srcport;
int size;
int opt;
int len;
int i;
/*
* Initialize static variables.
*/
idfile = -1;
/*
* Get an address of mine and remote.
*/
size = sizeof(myaddr);
if (getsockname(file, (struct sockaddr *)&myaddr, &size) < 0) {
syslog(LOG_ERR, "RFC1413 getsockname() failed, %m");
goto failed;
}
srcport = ntohs(myaddr.sin_port);
myaddr.sin_port = htons(0);
size = sizeof(hisaddr);
if (getpeername(file, (struct sockaddr *)&hisaddr, &size) < 0) {
syslog(LOG_ERR, "RFC1413 getpeername() failed, %m");
goto failed;
}
dstport = ntohs(hisaddr.sin_port);
hisaddr.sin_port = htons(RFC1413_PORT);
/*
* Set an alarm for timeout.
*/
#ifdef HAVE_SIGSETJMP
if (sigsetjmp(jump_buffer, 1) != 0)
goto failed;
#else
if (setjmp(jump_buffer) != 0)
goto failed;
#endif
#ifdef HAVE_SIGPROCMASK
act.sa_handler = ident_timeout;
#ifdef SA_INTERRUPT
act.sa_flags = SA_INTERRUPT;
#else /* not SA_INTERRUPT */
act.sa_flags = 0;
#endif /* not SA_INTERRUPT */
sigemptyset(&act.sa_mask);
sigaction(SIGALRM, &act, NULL);
#else /* not HAVE_SIGPROCMASK */
signal(SIGALRM, ident_timeout);
#endif /* not HAVE_SIGPROCMASK */
alarm(timeout);
/*
* Establish a connection to the RFC1413_PORT on the remote host.
*/
idfile = socket(AF_INET, SOCK_STREAM, 0);
if (idfile < 0) {
syslog(LOG_ERR, "RFC1413 socket() failed, %m");
goto failed;
}
#ifdef SO_REUSEADDR
opt = 1;
if (setsockopt(idfile, SOL_SOCKET, SO_REUSEADDR, (char *)&opt,
sizeof(opt)) < 0) {
syslog(LOG_ERR, "RFC1413 setsockopt() failed, %m");
goto failed;
}
#endif
if (bind(idfile, (struct sockaddr *)&myaddr, sizeof(myaddr)) < 0) {
syslog(LOG_ERR, "RFC1413 bind() failed, %m");
goto failed;
}
if (connect(idfile, (struct sockaddr *)&hisaddr, sizeof(hisaddr)) < 0) {
syslog(LOG_INFO, "RFC1413 connect() failed, %m");
goto failed;
}
/*
* Send a request to the remote host.
*/
sprintf(linebuf, "%d, %d\r\n", dstport, srcport);
write(idfile, linebuf, strlen(linebuf));
/*
* Receive an answer from the remote host.
*/
len = 0;
bufp = linebuf;
for (;;) {
int n;
char *nl;
/*
* Read a response line.
*/
n = read(idfile, bufp, MAXLEN_RFC1413_LINE - len);
if (n < 0) {
if (errno == EINTR)
continue;
syslog(LOG_INFO, "RFC1413 read() failed, %m");
goto failed;
} else if (n == 0) {
syslog(LOG_INFO, "RFC1413 receives unexpected EOF");
goto failed;
}
/*
* If `<CR>' or `<CR><LF>' is found in the line, remove it and
* break the loop.
*/
nl = (char *)memchr(bufp, '\n', n);
if (nl != NULL) {
*nl = '\0';
if (linebuf[0] != '\0' && *(nl - 1) == '\r')
*(nl - 1) = '\0';
break;
}
len += n;
bufp += n;
if (MAXLEN_RFC1413_LINE < len) {
*(linebuf + MAXLEN_RFC1413_LINE) = '\0';
syslog(LOG_INFO, "RFC1413 receives too long line: %s", linebuf);
goto failed;
}
}
syslog(LOG_DEBUG, "debug: RFC1413 receives the line: %s", linebuf);
/*
* Close the RFC1413 socket.
*/
if (shutdown(idfile, 2) < 0) {
idfile = -1;
goto failed;
}
/*
* Split the response line to fields.
*/
i = 0;
bufp = linebuf;
for (;;) {
/*
* Skip preceeding spaces and tabs.
*/
while (*bufp == ' ' || *bufp == '\t')
bufp++;
args[i] = bufp;
/*
* Skip following spaces and tabs.
*/
wsp = NULL;
while (*bufp != separators[i] && *bufp != '\0') {
if (*bufp == ' ' || *bufp == '\t')
wsp = bufp;
else
wsp = NULL;
bufp++;
}
if (wsp != NULL)
*wsp = '\0';
i++;
if (*bufp == '\0')
break;
*bufp++ = '\0';
}
/*
* Check for the fields.
*/
if (i < 4) {
syslog(LOG_INFO, "RFC1413 responce has too few argument");
goto failed;
}
if ((int)strtol(args[0], &bufp, 10) != dstport || *bufp != '\0') {
syslog(LOG_INFO, "RFC1413 responce has unmatched port-on-server");
goto failed;
}
if ((int)strtol(args[1], &bufp, 10) != srcport || *bufp != '\0') {
syslog(LOG_INFO, "RFC1413 response has unmatched port-on-client");
goto failed;
}
if (strcmp(args[2], "ERROR") == 0) {
syslog(LOG_INFO, "RFC1413 responce indicates error status: %s",
args[3]);
goto failed;
} else if (strcmp(args[2], "USERID") != 0) {
syslog(LOG_INFO, "RFC1413 response has unknown status: %s", args[3]);
goto failed;
}
if (i < 5) {
syslog(LOG_INFO, "RFC1413 response has too many arguments");
goto failed;
}
if (MAXLEN_USER_ID < strlen(args[4])) {
*(args[4] + MAXLEN_USER_ID) = '\0';
syslog(LOG_INFO, "RFC1413 response has too long user-id: %s...",
args[4]);
goto failed;
}
/*
* Put the user-id to `user'.
*/
strcpy(user, args[4]);
syslog(LOG_INFO, "RFC1413 get user-id: %s", user);
return 0;
/*
* An error occurs...
*/
failed:
/*
* Close the file for identification.
*/
if (0 <= idfile) {
if (shutdown(idfile, 2) < 0)
syslog(LOG_INFO, "RFC1413 shutdown() failed, %m");
}
/*
* Set `user' to unknown.
*/
strncpy(user, UNKNOWN_USER, MAXLEN_USER_ID);
*(user + MAXLEN_USER_ID) = '\0';
return 0;
}
/*
* The signal handler for SIGALRM.
*/
static RETSIGTYPE
ident_timeout(sig)
int sig;
{
alarm(0);
syslog(LOG_INFO, "RFC1413 timeout");
#ifdef HAVE_SIGSETJMP
siglongjmp(jump_buffer, 1);
#else
longjmp(jump_buffer, 1);
#endif
#ifdef RETSIGTYPE_VOID
return;
#else
return 0;
#endif
}
#ifdef TEST
int
main()
{
char user[512];
openlog("test", LOG_PID|LOG_NDELAY, LOG_DAEMON);
get_ident(0, user, 511, 10);
write(1, user, strlen(user));
write(1, "\r\n", 2);
closelog();
}
#endif /* TEST */
|