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 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
|
/*
* Program: $RCSfile: server.c,v $ $Revision: 4.12 $
*
* Purpose: UNIX application program that becomes a server for
* the Internet "youbin" service.
*
* Usage: See global variable usage.
*
* Author: K.Agusa agusa@nuie.nagoya-u.ac.jp
* S.Yamamoto yamamoto@nuie.nagoya-u.ac.jp
*
* Date: 1993/07/24
* Modified: $Date: 1996/05/06 14:38:45 $
*
* Copyright: K.Agusa and S.Yamamoto 1993 - 1995
*
* The X Consortium, and any party obtaining a copy of these files from
* the X Consortium, directly or indirectly, is granted, free of charge,
* a full and unrestricted irrevocable, world-wide, paid up, royalty-free,
* nonexclusive right and license to deal in this software and documentation
* files (the "Software"), including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons who receive copies from any such
* party to do so. This license includes without limitation a license to do
* the foregoing actions under any patents of the party supplying this
* software to the X Consortium.
*/
#ifndef lint
static char rcsid[] =
"$Id: server.c,v 4.12 1996/05/06 14:38:45 yamamoto Exp yamamoto $";
#endif
#if defined(__STD__)
#include <unistd.h> /* ANSI only. */
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h> /* For struct sockaddr_in. */
#include <arpa/inet.h> /* For inet_ntoa(). */
#include <sys/stat.h>
#include <sys/time.h> /* For struct timeval. */
#include <errno.h> /* For EINTR. */
#include <fcntl.h> /* For O_RDONLY. */
#include <netdb.h> /* For gethostbyname() and gethostbyaddr(). */
#include <pwd.h> /* For getpwuid(). */
#include <signal.h>
#include <stdio.h>
#include "youbin.h"
#include "server.h"
static char *usage[] = {
"NAME",
" youbind - server for the Internet \"youbin\" service",
"SYNOPSIS",
" youbind [-b header_list][-d][-h][-t][-A port]",
"OPTIONS",
" -b head if more than Body,From:,To:,Subject:,cc:,Date:.",
" -d Debug mode.",
" -h Help: print out the usage.",
" -t Trace: log the information of beginning and end of service.",
" -A port Auxiliary Service port name.",
"AUTHORS",
" K.Agusa agusa@nuie.nagoya-u.ac.jp",
" S.Yamamoto yamamoto@nuie.nagoya-u.ac.jp",
"Version 3.0 beta",
NULL
};
/*
* Global variables.
*/
SockAddr sa; /* Server address. */
SockAddr ca; /* Client address. */
int sockid; /* For sockets. */
char *prog_name; /* This program name. */
char *service_name = SERVICE_NAME; /* Service name. */
/* Port for non standard /bin/mail. For example, MIPS's /bin/mail uses
mbiff (6512/udp) port instead of biff (512/udp). */
char *aux_port_name = '\0';
char spool_path[MAXPATHLEN + 1]; /* Spool path. */
char *spool_file; /* File name part of spool path. */
char NAK_buff[MESS_LEN + 1];
/* Note, NAK_reason is modified with side effect of make_user(). */
char *NAK_reason;
int flag_timeout = OFF; /* Be set in sig_alarm(). */
int flag_hup = OFF; /* Be set in sig_hup(). */
int flag_quit = OFF; /* Be set in sig_quit(). */
/* Mode flags. */
int debug_mode = OFF;
int trace_mode = OFF;
int exitcode = EXIT_SUCCESS;
/* The followings are only for debug. */
char *state_name[] = {
"ACKED", "IDLE", "WAIT", "TO1", "TO2", "NOT_USED", "PW1", "PW2", "NOT_USED"
};
/*
* External variables.
*/
extern int errno;
/* For getopt(). */
extern char *optarg;
extern int optind, opterr;
int
main(argc, argv)
int argc;
char **argv;
{
int auxid = 0; /* Auxiliary fid. */
int max_fd; /* For select. */
fd_set fdset, fdset_tmp;
SockAddr aux_sa; /* Auxiliary server address. */
struct timeval dummy;
init(argc, argv); /* Check arguments. */
init_log(); /* Initialize syslog. */
init_users(); /* Initialize users and states. */
FD_ZERO(&fdset); /* Clear descriptor sets. */
sockid = open_udp(service_name, &sa); /* Port for clients. */
FD_SET(sockid, &fdset);
if (aux_port_name != NULL) {
auxid = open_udp(aux_port_name, &aux_sa);
FD_SET(auxid, &fdset);
}
max_fd = MAX(sockid, auxid) + 1;
signal(SIGINT, sig_quit); /* Set signal handlers. */
signal(SIGQUIT, sig_quit);
signal(SIGTERM, sig_quit);
signal(SIGHUP, sig_hup);
signal(SIGALRM, sig_alarm);
/* Dive into main loop. Don't use setjmp() and longjmp(),
because list maintenance routines are in critical section. */
alarm(UNIT_TIME);
while (1) {
if (flag_hup == ON) { /* First time call or catching SIGHUP. */
send_Quit("hup"); /* Kill all clients. */
init_users(); /* Initialize users and states. */
flag_hup = OFF;
}
if (flag_quit == ON) { /* Catch SIGINT, SIGTERM or SIGQUIT. */
send_Quit("quit");
exit(EXIT_SUCCESS);
}
if (flag_timeout == ON) { /* Timeout. */
periodic();
flag_timeout = OFF;
}
fdset_tmp = fdset;
/*
* Set dummy time out interval. The time out never happen,
* because SIGALARM is sent in every UNIT_TIME.
* In some OS, especially Linux, contents of timeval structure
* become undefined. So they should be set in this loop.
*/
dummy.tv_sec = (long)(UNIT_TIME * 10);
dummy.tv_usec = 0L;
if (SELECT(max_fd, &fdset_tmp, 0, 0, &dummy) < 0) {
if (errno != EINTR) {
sys_error_log("select");
}
continue;
}
if (FD_ISSET(sockid, &fdset_tmp)) {
get_mess(sockid);
}
if (auxid != 0 && FD_ISSET(auxid, &fdset_tmp)) {
get_mess(auxid);
}
}
}
void
init(argc, argv)
int argc;
char **argv;
{
int c, headlen;
char header_pattern[ 1000 ];
strcpy( header_pattern, BASIC_HEAD );
headlen = 1000 - strlen( header_pattern );
if ((prog_name = strrchr(argv[0], '/')) != NULL) {
prog_name++;
} else {
prog_name = argv[0];
}
strcpy(spool_path, MAIL_SPOOL);
spool_file = spool_path + strlen(spool_path);
strcpy(NAK_buff, "NACK ");
NAK_reason = NAK_buff + strlen(NAK_buff);
opterr = 0;
while ((c = getopt(argc, argv, "dhtb:A:S:")) != EOF) {
switch (c) {
case 'd': /* Debug mode. */
debug_mode = ON;
break;
case 'h': /* Help. */
print_usage(usage);
exit(EXIT_SUCCESS);
break;
case 't': /* Trace mode. */
trace_mode = ON;
break;
case 'b': /* biff headers */
strcat( header_pattern, "," );
strncat( header_pattern, optarg, headlen-1 );
header_pattern[999] = 0;
break;
case 'A': /* Auxiliary port name. */
aux_port_name = optarg;
break;
case 'S': /* Service port name. For debug. */
service_name = optarg;
break;
default:
exitcode++;
break;
}
}
if (optind != argc || exitcode != EXIT_SUCCESS) {
warn_log("Unknown options\n");
print_usage(usage);
exit(EXIT_FAILURE);
}
init_header_list( header_pattern );
}
void
get_mess(fid)
int fid;
{
char buff[MESS_LEN + 1];
int len, flen = sizeof(ca);
if ((len = RECVFROM(fid, buff, MESS_LEN, 0, &ca, &flen)) < 0) {
if (errno != EINTR) {
warn_log("Error in receiving packet\n");
}
return;
}
buff[len] = '\0'; /* Zero termination. */
if (len < 4) { /* a@0\n is the shortest mess.*/
debug_log("Too short length packet: %s\n", buff);
return;
}
if (flen != sizeof(ca) || ca.sin_family != AF_INET) {
char *cp = inet_ntoa(ca.sin_addr);
warn_log("Invalid source address: %s\n", buff);
warn_log(" IP = %s, port = %d\n",
((cp != NULL) ? cp : "unknown"), ntohs(ca.sin_port));
return;
}
#ifdef VER2_BC
if( buff[1] == ' ' ){
ver2_proc( buff, len );
return;
}
#endif
if (buff[4] != ' ') {
/* Trim the last newline. Packet from /bin/mail has a ending newline. */
if (buff[len - 2] == '\n') {
buff[len - 2] = '\0';
} else if (buff[len - 1] == '\n') {
buff[len - 1] = '\0';
}
debug_log("Recv: from /bin/mail on port %s: \"%s\"\n",
((fid == sockid) ? service_name : aux_port_name), buff);
proc_mail(buff);
} else {
debug_log("Recv: from client: \"%s\"\n", buff);
proc_client(buff, len);
}
}
void
proc_mail(mess)
char *mess;
{
/*
* This routine sends a part of mail like biff.
* Add a flag "B" to Wakeup packet if you want.
*/
char *cp;
off_t offset;
UserP up;
StateP sp;
if ((cp = strchr(mess, '@')) == NULL) {
warn_log("Invalid packet from /bin/mail: %s\n", mess);
return;
}
*cp = '\0';
if((up = find_user(mess)) == NULL) { /* Check if user is registered. */
return;
}
offset = atol(cp + 1); /* Get offset address. */
check_spool(up); /* Update size and time. */
for (sp = up->stat; sp != NULL; sp = sp->next) {
if( sp->state < NOT_USED ) {
if(sp->mode.head_list) {
send_header(sp, offset); /* Send header and so on. */
} else {
send_Status(sp); /* Notify of modification. */
}
}
}
}
void
send_header(sp, offset)
StateP sp;
off_t offset;
{
/*
* Some part of this function are copied from BSD source.
*/
FILE *fi;
int len, line_cnt, char_cnt, head, print_cont;
int flag_in_header;
char *line;
char buff[HEADER_LEN];
strcpy(spool_file, sp->parent->name);
if ((fi = fopen(spool_path, "r")) == NULL) { /* Check mail spool. */
sys_error_log("open"); /* Not created yet. */
return;
}
sprintf(buff, "STAT %d %d ", sp->parent->size, sp->parent->time);
#ifdef VER2_BC
if( sp->option.ver2_user ){
sprintf(buff, "S %d %d ", sp->parent->size, sp->parent->time);
}
#endif
line = buff + strlen(buff);
(void)fseek(fi, offset, 0); /* Locate to proper position. */
/*
* Print the first 7 lines or 560 characters of the new mail
* (whichever comes first). Skip header crap other than
* From, Subject, To, and Date.
*/
line_cnt = 7;
char_cnt = 560;
flag_in_header = ON;
head = sp->mode.head_list;
print_cont = OFF;
while (fgets(line, sizeof(buff) - (line - buff), fi) != NULL) {
if (flag_in_header == ON) {
if (line[0] == '\n') {
if(( head & 1 ) == 0 ) break; /* Check if "Body" request */
flag_in_header = OFF;
continue;
}
if (line[0] == ' ' || line[0] == '\t' ){
if( print_cont == OFF ) continue;
} else {
if( isPrintHead( line, head ) == FALSE ){
print_cont = OFF;
continue;
} else {
print_cont = ON;
}
}
}
if (line_cnt <= 0 || char_cnt <= 0) {
strcpy(line, "...more...\n");
break;
}
len = strlen(line);
line += len;
char_cnt -= len;
line_cnt--;
}
fclose(fi);
/*
* strcpy(line, "----\n");
*/
send_packet(buff, sp); /* I do not know if too large packet. */
}
void
proc_client(buff, len)
char *buff;
int len;
{
StateP id; /* User ID. */
if( strncmp( buff, "THNQ ", 5 ) == 0 ){ /* Thank you packet.*/
do_Thank(buff + PREFIX_LEN);
} else if( strncmp( buff, "UPDT ", 5 ) == 0 ){ /* Update packet. */
do_Update(buff + PREFIX_LEN );
} else if( strncmp( buff, "WAKE ", 5 ) == 0 ){ /* Wakeup packet. */
do_Wakeup( buff + PREFIX_LEN );
} else if( strncmp( buff, "PASS ", 5 ) == 0 ){ /* Password packet. */
do_Passwd( buff+PREFIX_LEN );
} else if( strncmp( buff, "QUIT ", 5 ) == 0 ){ /* Quit packet. */
do_Quit( buff + PREFIX_LEN );
} else {
warn_log("Unknown packet: %s\n", buff);
}
}
void
sig_alarm()
{
alarm(UNIT_TIME);
flag_timeout = ON;
signal(SIGALRM, sig_alarm);
}
void
sig_hup()
{
warn_log("Catch signal HUP\n");
flag_hup = ON;
signal(SIGHUP, sig_hup);
}
void
sig_quit()
{
warn_log("Catch signal INT, TERM or QUIT\n");
flag_quit = ON;
}
void
periodic()
{
UserP up, upp;
State dummy;
StateP sp, spp;
static unsigned int times;
debug_log("periodic: %d\n", times);
if (times++ & 1) { /* Check spool every two tic. */
for (up = UserList.next; up != NULL; up = up->next) {
check_spool(up);
}
}
for (upp = &UserList; (up = upp->next) != NULL; upp = up) {
for (dummy.next = up->stat, spp = &dummy; (sp = spp->next) != NULL;
spp = sp) {
debug_log(" %s [%ld]: %s -> %s\n", up->name, (long)sp,
state_name[ sp->state ], state_name[ sp->state + 1 ] );
switch (sp->state) {
case ACKED:
if( sp->option.no_KeepAlive == FALSE ) {
sp->state = IDLE;
}
break;
case IDLE:
sp->state = WAIT;
send_Status(sp);
break;
case WAIT:
sp->state = TO1;
send_Status(sp);
break;
case TO1:
sp->state = TO2;
send_Status(sp);
break;
case PW1:
sp->state = PW2;
break;
default: /* Something wrong. */
warn_log("%s [%ld]: Unknown state: %d\n",
up->name, (long)sp, sp->state);
/* Don't break. */
case PW2: /* Too late for PASS */
case TO2: /* TO2 and fatal error. */
spp->next = sp->next;
dispose_state(sp);
sp = spp;
break;
}
}
if ((up->stat = dummy.next) == NULL) {
upp->next = up->next;
dispose_user(up);
up = upp;
}
}
}
/*
* Check given sockaddr_in with lastly received sockaddr_in which is in
* global variable ca. Boolean function. TRUE means equal.
*/
int
check_ca(saddr)
SockAddr *saddr;
{
return (ca.sin_family == saddr->sin_family &&
ca.sin_port == saddr->sin_port &&
ca.sin_addr.s_addr == saddr->sin_addr.s_addr);
}
int
open_udp(service, saddr)
char *service;
SockAddr *saddr;
{
struct hostent *hp; /* Result of host name lookup. */
struct servent *sp; /* Result of service lookup. */
char localhost[MAXHOSTNAMELEN + 1]; /* Local host name. */
int id; /* socket id to be returned */
/*
* Look up the specified host name.
*/
gethostname(localhost, MAXHOSTNAMELEN);
if ((hp = gethostbyname(localhost)) == NULL) {
warn_log("No such host: %s\n", prog_name, localhost);
exit(1);
}
/*
* Look up the service.
*/
if ((sp = getservbyname(service, "udp")) == NULL) {
warn_log("No such service: %s\n", service);
exit(1);
}
/*
* Allocate an open socket.
*/
if ((id = socket(hp->h_addrtype, SOCK_DGRAM, 0)) < 0) {
sys_error_log("socket");
exit(EXIT_FAILURE);
}
/*
* Connect to the remote server.
*/
bzero((char *)saddr, sizeof(*saddr));
saddr->sin_family = hp->h_addrtype;
saddr->sin_addr.s_addr = htonl(INADDR_ANY);
saddr->sin_port = sp->s_port;
if (BIND(id, saddr, sizeof(*saddr)) < 0) {
sys_error_log("bind");
exit(EXIT_FAILURE);
}
return (id);
}
/*
* Set user spool's size and modified time.
*/
void
check_spool(up)
UserP up;
{
struct stat status; /* A structure for stat(). */
strcpy(spool_file, up->name);
/* Set file name part of spool_path[]. */
if (stat(spool_path, &status) < 0) { /* Get spool attributes. */
if (errno != ENOENT) {
sys_error_log("stat");
}
up->size = up->time = 0;
} else {
up->size = status.st_size;
up->time = status.st_mtime;
}
}
void
send_packet(mess, sp)
char *mess;
StateP sp;
{
SockAddr *saddr;
saddr = (sp == CA_ADDR) ? &ca : &(sp->ca);
/* Print first MAX_DEBUG_COLUMN characters of message. */
debug_log("Send: %s [%ld]: \"%.*s\"\n",
((sp == CA_ADDR) ? "" : sp->parent->name), (long)sp,
MAX_DEBUG_COLUMN, mess);
if (SENDTO(sockid, mess, strlen(mess), 0, saddr, sizeof(*saddr)) < 0) {
warn_log("Error in sending packet: %s\n", mess);
}
}
|