| 1 |
/* Spm: The PortMaster |
| 2 |
* This program is expected to run as a daemon on every machine |
| 3 |
* which uses Charles Campbell's and Terry McRoberts' Socket Library. |
| 4 |
* |
| 5 |
* Its purpose in life is to: |
| 6 |
* a) receive messages from processes starting up servers to maintain |
| 7 |
* a list of process names and associated ports, |
| 8 |
* b) to inform processes starting up client sockets about the port |
| 9 |
* numbers of designed servers. |
| 10 |
* |
| 11 |
* This program's services are generally hidden from the users, and is |
| 12 |
* accessed by the Sopen() functions. Since processes from anywhere |
| 13 |
* need to access this process, it has a fixed port number (PORTMASTER) |
| 14 |
* unless overridden from the command line. This information is hard-coded |
| 15 |
* into the <sockets.h> header file. |
| 16 |
* |
| 17 |
* Routines Include: <Spm.c> |
| 18 |
* int main( int argc, char **argv) |
| 19 |
* static Socket *Sopen_prtmstr() |
| 20 |
* static PortTable *makePortTable(SKTEVENT port,char *sharehost) |
| 21 |
* static void delPortTable(PortTable *p) |
| 22 |
* void pm_server(Socket *client) |
| 23 |
* void pm_client(Socket *client,int waiting) |
| 24 |
* void pm_close(Socket *client) |
| 25 |
* void pm_quit( Socket *server, Socket *client) |
| 26 |
* void pm_table(Socket *client) |
| 27 |
* void pm_rmserver(Socket *client) |
| 28 |
* int firewall(Socket *skt) |
| 29 |
* void firewall_init(char *fwfile) |
| 30 |
* void pm_fwinit(Socket *skt) |
| 31 |
*/ |
| 32 |
#include <stdio.h> |
| 33 |
#include "sockets.h" |
| 34 |
#include <setjmp.h> |
| 35 |
#ifdef __WIN32__ |
| 36 |
# include <windows.h> |
| 37 |
#endif |
| 38 |
#ifdef unix |
| 39 |
# include <sys/types.h> |
| 40 |
# include <unistd.h> |
| 41 |
#endif |
| 42 |
|
| 43 |
/* ---------------------------------------------------------------------- |
| 44 |
* Definitions: |
| 45 |
*/ |
| 46 |
#define SPMFIREWALL /* select PortMaster firewall code */ |
| 47 |
|
| 48 |
#define BUFSIZE 128 /* size of character buffers */ |
| 49 |
#define SPMFWDATA 6 /* SSL PortMaster Firewall data quantity */ |
| 50 |
#define TIMEOUT 20L /* wait no more than 20 seconds at a Sread... */ |
| 51 |
|
| 52 |
# ifdef unix |
| 53 |
/* # define CloseStdio */ |
| 54 |
# endif |
| 55 |
|
| 56 |
/* exceptions -- if char is normally unsigned */ |
| 57 |
#if !defined(sgi) |
| 58 |
# define SIGNEDHOSTADDR |
| 59 |
#endif |
| 60 |
|
| 61 |
/* ---------------------------------------------------------------------- |
| 62 |
* Typedefs: |
| 63 |
*/ |
| 64 |
typedef struct ClientWaiting_str ClientWaiting; |
| 65 |
typedef struct PortTable_str PortTable; |
| 66 |
#ifdef SPMFIREWALL |
| 67 |
typedef struct spmfw_str SpmFW; |
| 68 |
#endif |
| 69 |
|
| 70 |
/* ---------------------------------------------------------------------- |
| 71 |
* Data Structures: |
| 72 |
*/ |
| 73 |
|
| 74 |
/* PortTable: this data structure supports matching host/process name |
| 75 |
* to ports |
| 76 |
*/ |
| 77 |
struct PortTable_str { |
| 78 |
char *sktname; /* name of skt */ |
| 79 |
char *sharehost; /* optional Portmaster-sharing hostname */ |
| 80 |
SKTEVENT port; /* port of socket */ |
| 81 |
PortTable *nxt; /* linked list support */ |
| 82 |
PortTable *prv; /* linked list support */ |
| 83 |
}; |
| 84 |
struct ClientWaiting_str { |
| 85 |
Socket *client; /* waiting client socket */ |
| 86 |
char *sktname; /* name of server client is waiting for */ |
| 87 |
ClientWaiting *nxt; /* linked list support */ |
| 88 |
ClientWaiting *prv; /* linked list support */ |
| 89 |
}; |
| 90 |
|
| 91 |
#ifdef SPMFIREWALL |
| 92 |
struct spmfw_str { |
| 93 |
int data[SPMFWDATA]; |
| 94 |
SpmFW *nxt; |
| 95 |
}; |
| 96 |
#endif |
| 97 |
|
| 98 |
/* ---------------------------------------------------------------------- |
| 99 |
* Global Data: |
| 100 |
*/ |
| 101 |
static struct sockaddr_in sin; |
| 102 |
static char hostname[BUFSIZE]; |
| 103 |
static char hostaddr[4]; |
| 104 |
static char sktname[BUFSIZE]; |
| 105 |
static SKTEVENT pm_ok = 0; /* will be initialized to htons(PM_OK) */ |
| 106 |
static SKTEVENT pm_sorry = 0; /* will be initialized to htons(PM_SORRY) */ |
| 107 |
static SKTEVENT pm_resend = 0; /* will be initialized to htons(PM_RESEND) */ |
| 108 |
static SKTEVENT pm_okshare = 0; /* will be initialized to htons(PM_OKSHARE) */ |
| 109 |
static PortTable *phd = NULL; |
| 110 |
static PortTable *ptl = NULL; |
| 111 |
static jmp_buf timedout; |
| 112 |
static ClientWaiting *cwhd = NULL; |
| 113 |
static ClientWaiting *cwtl = NULL; |
| 114 |
|
| 115 |
#ifdef SPMFIREWALL |
| 116 |
static SpmFW *spmfwhd = NULL; |
| 117 |
#endif |
| 118 |
|
| 119 |
/* ---------------------------------------------------------------------- |
| 120 |
* Usage: |
| 121 |
*/ |
| 122 |
static char *usage[]={ |
| 123 |
XYELLOW,"Spm ",XGREEN,"[",XCYAN,"-f firewallfilename",XGREEN,"]\n\n", |
| 124 |
|
| 125 |
XUWHITE,"Author",XGREEN,": Dr. Charles E. Campbell, Jr.\n\n", |
| 126 |
|
| 127 |
XUWHITE,"Options:\n\n", |
| 128 |
|
| 129 |
XCYAN," -f firewallfilename",XGREEN,": optional firewall file\n", |
| 130 |
|
| 131 |
XUWHITE,"Explanation:\n\n",XGREEN, |
| 132 |
|
| 133 |
"The ",XYELLOW,"Spm",XGREEN," is the PortMaster for the Sockets library\n", |
| 134 |
"written by Dr. Charles E. Campbell, Jr. and Terry McRoberts of NASA.\n", |
| 135 |
"The Spm program provides a \"phonebook\" of server names and their\n", |
| 136 |
"associated port identifiers used by the Sopen function. The data\n", |
| 137 |
"is maintained in an internal \"PortTable\" linked list in memory.\n", |
| 138 |
"The PortMaster (Spm) must be run on any machine using the Sockets\n", |
| 139 |
"library.\n\n", |
| 140 |
|
| 141 |
"The PortMaster is not intended to be used directly by the user.\n", |
| 142 |
"Instead, Sopen, Sclose, and Srmsrvr are the primary interfaces to the\n", |
| 143 |
"PortMaster for the user. These functions follow a simple protocol and\n", |
| 144 |
"pass PortMaster \"event\"s to and from the PortMaster.\n\n", |
| 145 |
|
| 146 |
"There are some additional functions that the PortMaster provides: one\n", |
| 147 |
"may use PortMaster events to close down the PortMaster (PM_QUIT) and\n", |
| 148 |
"to query the PortMaster as to what servers are available (PM_TABLE).\n", |
| 149 |
"These functions do not have nice interfaces provided; one must be\n", |
| 150 |
"conversant with the PortMaster protocol to get them to work.\n\n", |
| 151 |
"\n",XUWHITE,"Version",XGREEN,": ", |
| 152 |
#ifdef unix |
| 153 |
"unix", |
| 154 |
#endif |
| 155 |
#ifdef vms |
| 156 |
"vms", |
| 157 |
#endif |
| 158 |
#ifdef msdos |
| 159 |
"msdos", |
| 160 |
#endif |
| 161 |
"\n", |
| 162 |
|
| 163 |
"",""}; |
| 164 |
|
| 165 |
/* ---------------------------------------------------------------------- |
| 166 |
* Prototypes: |
| 167 |
*/ |
| 168 |
#ifdef __PROTOTYPE__ |
| 169 |
|
| 170 |
int main( int, char **); /* Spm.c */ |
| 171 |
static Socket *Sopen_prtmstr(void); /* Spm.c */ |
| 172 |
static PortTable *makePortTable(SKTEVENT,char *); /* Spm.c */ |
| 173 |
static void delPortTable(PortTable *); /* Spm.c */ |
| 174 |
void pm_server(Socket *,SKTEVENT); /* Spm.c */ |
| 175 |
void pm_client(Socket *,int); /* Spm.c */ |
| 176 |
void pm_close(Socket *); /* Spm.c */ |
| 177 |
void pm_quit( Socket *, Socket *); /* Spm.c */ |
| 178 |
void pm_table(Socket *); /* Spm.c */ |
| 179 |
void pm_rmserver(Socket *); /* Spm.c */ |
| 180 |
#ifdef AS400 |
| 181 |
void pm_strtsv(char * ); /* SPMSTRTR.c */ |
| 182 |
#endif |
| 183 |
|
| 184 |
#ifdef SPMFIREWALL |
| 185 |
int firewall(Socket *); /* Spm.c */ |
| 186 |
void firewall_init(char *); /* Spm.c */ |
| 187 |
void pm_fwinit(Socket *); /* Spm.c */ |
| 188 |
#endif |
| 189 |
|
| 190 |
#else |
| 191 |
|
| 192 |
extern int main(); /* Spm.c */ |
| 193 |
static Socket *Sopen_prtmstr(); /* Spm.c */ |
| 194 |
static PortTable *makePortTable(); /* Spm.c */ |
| 195 |
static void delPortTable(); /* Spm.c */ |
| 196 |
extern void pm_server(); /* Spm.c */ |
| 197 |
extern void pm_client(); /* Spm.c */ |
| 198 |
extern void pm_close(); /* Spm.c */ |
| 199 |
extern void pm_quit(); /* Spm.c */ |
| 200 |
extern void pm_table(); /* Spm.c */ |
| 201 |
extern void pm_rmserver(); /* Spm.c */ |
| 202 |
#ifdef AS400 |
| 203 |
extern void pm_strtsv(); /* SPMSTRTR.c */ |
| 204 |
#endif |
| 205 |
|
| 206 |
#ifdef SPMFIREWALL |
| 207 |
extern int firewall(); /* Spm.c */ |
| 208 |
extern void firewall_init(); /* Spm.c */ |
| 209 |
extern void pm_fwinit(); /* Spm.c */ |
| 210 |
#endif |
| 211 |
|
| 212 |
#endif /* __PROTOTYPE__ */ |
| 213 |
|
| 214 |
/* ---------------------------------------------------------------------- */ |
| 215 |
|
| 216 |
/* main: this is where it starts, ladies and gentlemen! */ |
| 217 |
#ifdef __WIN32__ |
| 218 |
// WinMain: Entry point of all Windows programs |
| 219 |
int APIENTRY WinMain( |
| 220 |
HINSTANCE hInstance, |
| 221 |
HINSTANCE hPrevInstance, |
| 222 |
LPSTR lpCmdLine, |
| 223 |
int nCmdShow) |
| 224 |
|
| 225 |
#else |
| 226 |
|
| 227 |
# ifdef __PROTOTYPE__ |
| 228 |
int main( |
| 229 |
int argc, |
| 230 |
char **argv) |
| 231 |
# else |
| 232 |
int main( |
| 233 |
argc, |
| 234 |
argv) |
| 235 |
int argc; |
| 236 |
char **argv; |
| 237 |
# endif |
| 238 |
#endif |
| 239 |
{ |
| 240 |
SKTEVENT event; |
| 241 |
Socket *client = NULL; |
| 242 |
Socket *server = NULL; |
| 243 |
int resend_cnt = 0; |
| 244 |
struct hostent *he = NULL; |
| 245 |
|
| 246 |
#ifdef unix |
| 247 |
int daemon=0; |
| 248 |
|
| 249 |
daemon= fork(); |
| 250 |
if(daemon < 0) error(XTDIO_ERROR,"Spm: PortMaster cannot fork"); |
| 251 |
else if(daemon) { /* we're the parent, so bye-bye! */ |
| 252 |
|
| 253 |
/* give out credits */ |
| 254 |
fprintf(stderr,"\n"); |
| 255 |
fprintf(stderr," *** Simple Sockets PortMaster ***\n"); |
| 256 |
fprintf(stderr," Authors: Charles E. Campbell, Jr., Ph.D.\n"); |
| 257 |
fprintf(stderr," Terry McRoberts\n"); |
| 258 |
fprintf(stderr,"\n"); |
| 259 |
fprintf(stderr,"of Goddard Space Flight Center, Maryland, USA\n"); |
| 260 |
fprintf(stderr," (uses port %4d)\n",PORTMASTER); |
| 261 |
|
| 262 |
exit(0); |
| 263 |
} |
| 264 |
setsid(); /* release controlling terminal */ |
| 265 |
|
| 266 |
#elif defined(__WIN32__) |
| 267 |
static int argc = 1; |
| 268 |
static char argv[1][4]={"Spm"}; |
| 269 |
|
| 270 |
#else |
| 271 |
|
| 272 |
/* give out credits */ |
| 273 |
fprintf(stderr,"\n"); |
| 274 |
fprintf(stderr," *** Simple Sockets PortMaster ***\n"); |
| 275 |
fprintf(stderr," Authors: Charles E. Campbell, Jr., Ph.D.\n"); |
| 276 |
fprintf(stderr," Terry McRoberts\n"); |
| 277 |
fprintf(stderr,"\n"); |
| 278 |
fprintf(stderr,"of Goddard Space Flight Center, Maryland, USA\n"); |
| 279 |
fprintf(stderr," (uses port %4d)\n",PORTMASTER); |
| 280 |
#endif /* #if unix .. else if windoz ... endif */ |
| 281 |
|
| 282 |
rdcolor(); /* read color strings */ |
| 283 |
Sinit(); /* initialize sockets */ |
| 284 |
|
| 285 |
/* handle command line */ |
| 286 |
if(argc > 1 && !strcmp(argv[1],"?")) { /* give usage message */ |
| 287 |
char **u; |
| 288 |
|
| 289 |
for(u= usage; u[0][0] || u[1][0]; ++u) rdcputs(*u,stdout); |
| 290 |
exit(0); |
| 291 |
} |
| 292 |
|
| 293 |
/* initialize */ |
| 294 |
pm_ok = htons(PM_OK); |
| 295 |
pm_okshare= htons(PM_OKSHARE); |
| 296 |
pm_sorry = htons(PM_SORRY); |
| 297 |
pm_resend = htons(PM_RESEND); |
| 298 |
|
| 299 |
/* get the current hostname */ |
| 300 |
gethostname(hostname,BUFSIZE); |
| 301 |
|
| 302 |
#ifdef SPMFIREWALL |
| 303 |
he= gethostbyname(hostname); /* get host address */ |
| 304 |
|
| 305 |
if(he) { |
| 306 |
hostaddr[0]= he->h_addr_list[0][0]; |
| 307 |
hostaddr[1]= he->h_addr_list[0][1]; |
| 308 |
hostaddr[2]= he->h_addr_list[0][2]; |
| 309 |
hostaddr[3]= he->h_addr_list[0][3]; |
| 310 |
|
| 311 |
#ifdef SIGNEDHOSTADDR |
| 312 |
if(hostaddr[0] < 0) hostaddr[0]+= 256; |
| 313 |
if(hostaddr[1] < 0) hostaddr[1]+= 256; |
| 314 |
if(hostaddr[2] < 0) hostaddr[2]+= 256; |
| 315 |
if(hostaddr[3] < 0) hostaddr[3]+= 256; |
| 316 |
#endif |
| 317 |
|
| 318 |
|
| 319 |
/* initialize firewall: -f <firewallfile> |
| 320 |
* If no such option, then the "SPMFIREWALL" environment variable |
| 321 |
* is examined |
| 322 |
*/ |
| 323 |
if(argc > 1 && argv[1][0] == '-' && argv[1][1] == 'f') |
| 324 |
firewall_init(argv[1]+2); |
| 325 |
else firewall_init(""); |
| 326 |
} |
| 327 |
else hostaddr[0]= hostaddr[1]= hostaddr[2]= hostaddr[3]= '\0'; |
| 328 |
#endif |
| 329 |
|
| 330 |
/* open a server for the PortMaster */ |
| 331 |
server= Sopen_prtmstr(); |
| 332 |
if(!server) { |
| 333 |
error(XTDIO_ERROR,"%s PortMaster's port (#%d) already in use!!!\n", |
| 334 |
hostname, PORTMASTER); |
| 335 |
#ifdef vms |
| 336 |
exit(1); |
| 337 |
#else |
| 338 |
exit(0); |
| 339 |
#endif |
| 340 |
} |
| 341 |
|
| 342 |
#ifdef CloseStdio |
| 343 |
/* get rid of things we no longer need */ |
| 344 |
fflush(stdin); |
| 345 |
fflush(stdout); |
| 346 |
fflush(stderr); |
| 347 |
fclose(stdin); |
| 348 |
fclose(stdout); |
| 349 |
fclose(stderr); |
| 350 |
#endif |
| 351 |
|
| 352 |
/* set up a timeout return point */ |
| 353 |
(void) setjmp(timedout); |
| 354 |
|
| 355 |
/* accept clients */ |
| 356 |
while(1) { |
| 357 |
|
| 358 |
/* close out old connection */ |
| 359 |
if(client) { |
| 360 |
shutdown(client->skt,2); |
| 361 |
close(client->skt); |
| 362 |
client->skt= 0;; |
| 363 |
freeSocket(client); /* free client Socket */ |
| 364 |
client= NULL; /* paranoia is good medicine */ |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
/* accept a client connection */ |
| 369 |
client= Saccept(server); |
| 370 |
if(!client) { |
| 371 |
continue; |
| 372 |
} |
| 373 |
|
| 374 |
#ifdef SPMFIREWALL |
| 375 |
if(firewall(client)) { /* reject clients not inside firewall */ |
| 376 |
Swrite(client,(char *) &pm_sorry,sizeof(pm_sorry)); |
| 377 |
shutdown(client->skt,2); |
| 378 |
close(client->skt); client->skt= 0;; |
| 379 |
freeSocket(client); client = NULL; |
| 380 |
continue; |
| 381 |
} |
| 382 |
#endif /* SPMFIREWALL */ |
| 383 |
|
| 384 |
/* get a PrtMstrEvent */ |
| 385 |
resend_cnt= 0; |
| 386 |
do { |
| 387 |
|
| 388 |
/* get the event that PortMaster is to handle */ |
| 389 |
if(Stimeoutwait(client,TIMEOUT,0L) < 0) longjmp(timedout,1); |
| 390 |
Sreadbytes(client,(char *) &event,sizeof(event)); |
| 391 |
event= ntohs( event); |
| 392 |
|
| 393 |
/* The PortMaster handles several events: |
| 394 |
/===============================================\ |
| 395 |
| Client PortMaster | |
| 396 |
| Event Sends Sends | |
| 397 |
|===============================================| |
| 398 |
| PM_SERVER PM_OK / PM_RESEND | |
| 399 |
| sktname | |
| 400 |
| port | |
| 401 |
| PM_OK / PM_SORRY | |
| 402 |
| PM_OK | |
| 403 |
|-----------------------------------------------| |
| 404 |
| PM_CLIENT | PM_CLIENTWAIT acts just like PM_CLIENT, |
| 405 |
| PM_OK / PM_RESEND | except the second PM_OK will be deferred |
| 406 |
| sktname | until the requested server is available |
| 407 |
| PM_OK / PM_SORRY | |
| 408 |
| port | |
| 409 |
|-----------------------------------------------| |
| 410 |
| PM_CLOSE | |
| 411 |
| PM_OK / PM_RESEND | |
| 412 |
| port | |
| 413 |
| PM_OK / PM_SORRY | |
| 414 |
|-----------------------------------------------| |
| 415 |
| PM_TABLE | |
| 416 |
| PM_OK / PM_RESEND | |
| 417 |
| count of servers | |
| 418 |
| "server : port#" | |
| 419 |
|-----------------------------------------------| |
| 420 |
| PM_QUIT | |
| 421 |
| PM_OK / PM_RESEND | |
| 422 |
| "PortMaster" | |
| 423 |
|-----------------------------------------------| |
| 424 |
| PM_FWINIT | |
| 425 |
| PM_OK / PM_RESEND | |
| 426 |
|-----------------------------------------------| |
| 427 |
| PM_SHARE PM_OK / PM_RESEND | Supports new portmaster sharing mode. A |
| 428 |
| hostname | single Portmaster can support a number of |
| 429 |
| sktname | different machines. The main reason for |
| 430 |
| port | this is to support PC-only shops, but it |
| 431 |
| PM_OK / PM_SORRY | will work with any PortMaster. |
| 432 |
|-----------------------------------------------| |
| 433 |
| PM_CLIENT | |
| 434 |
| PM_OK / PM_RESEND | |
| 435 |
| sktname | PM_CLIENT + PM_OKSHARE handles |
| 436 |
| PM_OKSHARE / PM_SORRY| PortMaster sharing for clients. |
| 437 |
| hostname | |
| 438 |
| port | |
| 439 |
\===============================================/ |
| 440 |
*/ |
| 441 |
switch(event) { |
| 442 |
|
| 443 |
case PM_SERVER: /* enter port into database */ |
| 444 |
case PM_SHARE: /* enter hostname & port into database */ |
| 445 |
pm_server(client,event); |
| 446 |
resend_cnt= 0; |
| 447 |
break; |
| 448 |
|
| 449 |
case PM_CLIENTWAIT: /* return port from database or wait until server shows up */ |
| 450 |
pm_client(client,1); |
| 451 |
if(cwtl && client == cwtl->client) client= NULL; /* prevent premature closure of accept socket to client */ |
| 452 |
resend_cnt= 0; |
| 453 |
break; |
| 454 |
|
| 455 |
case PM_CLIENT: /* return port from database */ |
| 456 |
pm_client(client,0); |
| 457 |
resend_cnt= 0; |
| 458 |
break; |
| 459 |
|
| 460 |
case PM_CLOSE: /* delete port from database */ |
| 461 |
pm_close(client); |
| 462 |
resend_cnt= 0; |
| 463 |
break; |
| 464 |
|
| 465 |
case PM_QUIT: /* this function terminates the PortMaster */ |
| 466 |
pm_quit(server,client); |
| 467 |
resend_cnt= 0; |
| 468 |
break; |
| 469 |
|
| 470 |
case PM_TABLE: /* print out the PortTable */ |
| 471 |
pm_table(client); |
| 472 |
resend_cnt= 0; |
| 473 |
break; |
| 474 |
|
| 475 |
case PM_RMSERVER: /* remove a server entry from the PortMaster */ |
| 476 |
pm_rmserver(client); |
| 477 |
resend_cnt= 0; |
| 478 |
break; |
| 479 |
|
| 480 |
#ifdef SPMFIREWALL |
| 481 |
case PM_FWINIT: /* re-read firewall file */ |
| 482 |
pm_fwinit(client); |
| 483 |
resend_cnt= 0; |
| 484 |
break; |
| 485 |
#endif |
| 486 |
|
| 487 |
default: /* huh? whaddaIdonow? */ |
| 488 |
Swrite(client,(char *) &pm_resend,sizeof(pm_resend)); |
| 489 |
|
| 490 |
/* terminate resending after PM_MAXTRY attempts */ |
| 491 |
if(++resend_cnt > PM_MAXTRY) { |
| 492 |
resend_cnt= 0; |
| 493 |
shutdown(client->skt,2); |
| 494 |
close(client->skt); client->skt= 0;; |
| 495 |
freeSocket(client); client = NULL; |
| 496 |
} |
| 497 |
break; |
| 498 |
} |
| 499 |
} while(resend_cnt); |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
/* ---------------------------------------------------------------------- */ |
| 504 |
|
| 505 |
/* Sopen_prtmstr: this function opens a socket handle for the Portmaster */ |
| 506 |
static Socket *Sopen_prtmstr() |
| 507 |
{ |
| 508 |
socklen_t length; |
| 509 |
int status = 1; |
| 510 |
Socket *skt = NULL; |
| 511 |
|
| 512 |
|
| 513 |
/* allocate a Socket */ |
| 514 |
skt= makeSocket(hostname,"PortMaster",PM_SERVER); |
| 515 |
|
| 516 |
/* create a socket */ |
| 517 |
if((skt->skt= socket(AF_INET, SOCK_STREAM, 0)) <0 ) { |
| 518 |
freeSocket(skt); skt= NULL; |
| 519 |
return skt; |
| 520 |
} |
| 521 |
|
| 522 |
#ifndef SSLNOSETSOCKOPT |
| 523 |
/* allow PortMaster to be brought back up quickly (this should seldom be necessary!) */ |
| 524 |
status= 1; |
| 525 |
if(setsockopt(skt->skt,SOL_SOCKET,SO_REUSEADDR,(char *) &status,sizeof(status)) < 0) { |
| 526 |
} |
| 527 |
#endif |
| 528 |
|
| 529 |
/* initialize socket data structure */ |
| 530 |
sin.sin_family = AF_INET; |
| 531 |
sin.sin_addr.s_addr = INADDR_ANY; |
| 532 |
sin.sin_port = htons((u_short) PORTMASTER); |
| 533 |
|
| 534 |
/* bind socket data structure to this socket */ |
| 535 |
if(bind(skt->skt,(struct sockaddr *) &sin,sizeof(sin))) { |
| 536 |
freeSocket(skt); skt= NULL; |
| 537 |
return skt; |
| 538 |
} |
| 539 |
|
| 540 |
/* getsockname fills in the socket structure with information, |
| 541 |
* such as the port number assigned to this socket |
| 542 |
*/ |
| 543 |
length = sizeof(sin); |
| 544 |
if(getsockname(skt->skt,(struct sockaddr *) &sin,&length)) { |
| 545 |
freeSocket(skt); skt= NULL; |
| 546 |
return skt; |
| 547 |
} |
| 548 |
skt->port= ntohs(sin.sin_port); |
| 549 |
|
| 550 |
/* prepare socket queue for (up to) PM_MAXREQUESTS (nominally 10) connection requests. |
| 551 |
* Often, even though PM_MAXREQUESTS > 5, only 5 pending connection requests will be |
| 552 |
* permitted by your o/s settings. |
| 553 |
*/ |
| 554 |
listen(skt->skt,PM_MAXREQUESTS); |
| 555 |
|
| 556 |
#ifndef MSDOS |
| 557 |
#ifndef _AIX |
| 558 |
/* turn off TCP's buffering algorithm so small packets don't get |
| 559 |
* needlessly delayed |
| 560 |
*/ |
| 561 |
if(setsockopt(skt->skt,IPPROTO_TCP,TCP_NODELAY,(char *) &status,sizeof(status)) |
| 562 |
< 0) { |
| 563 |
} |
| 564 |
#endif /* #ifndef _AIX ... */ |
| 565 |
#endif /* #ifndef MSDOS ... */ |
| 566 |
|
| 567 |
/* return server socket */ |
| 568 |
return skt; |
| 569 |
} |
| 570 |
|
| 571 |
/* ---------------------------------------------------------------------- */ |
| 572 |
|
| 573 |
/* makePortTable: this function allocates, links, sets up a PortTable */ |
| 574 |
#ifdef __PROTOTYPE__ |
| 575 |
static PortTable *makePortTable( |
| 576 |
SKTEVENT port, |
| 577 |
char *sharehost) |
| 578 |
#else |
| 579 |
static PortTable *makePortTable(port,sharehost) |
| 580 |
SKTEVENT port; |
| 581 |
char *sharehost; |
| 582 |
#endif |
| 583 |
{ |
| 584 |
PortTable *p= NULL; |
| 585 |
|
| 586 |
|
| 587 |
/* allocate a PortTable */ |
| 588 |
p= (PortTable *) malloc(sizeof(PortTable)); |
| 589 |
outofmem((void *) p,"attempt to makePortTable(%s,%d)",sktname,port); |
| 590 |
|
| 591 |
/* double link a PortTable */ |
| 592 |
if(ptl) ptl->nxt= p; |
| 593 |
else phd = p; |
| 594 |
p->nxt= NULL; |
| 595 |
p->prv= ptl; |
| 596 |
ptl = p; |
| 597 |
|
| 598 |
/* set up PortTable */ |
| 599 |
p->sktname= (char *) calloc((size_t) strlen(sktname)+1,sizeof(char)); |
| 600 |
outofmem((void *) p->sktname,"attempt to allocate sktname<%s>\n",sktname); |
| 601 |
strcpy(p->sktname,sktname); |
| 602 |
|
| 603 |
p->sharehost= NULL; |
| 604 |
p->port = port; |
| 605 |
|
| 606 |
/* set up optional sharehost */ |
| 607 |
if(sharehost) { |
| 608 |
p->sharehost= (char *) calloc((size_t) strlen(sharehost)+1,sizeof(char)); |
| 609 |
outofmem((void *) p->sharehost,"attempt to allocate sharehost<%s>\n",sharehost); |
| 610 |
strcpy(p->sharehost,sharehost); |
| 611 |
} |
| 612 |
|
| 613 |
return p; |
| 614 |
} |
| 615 |
|
| 616 |
/* ---------------------------------------------------------------------- */ |
| 617 |
|
| 618 |
/* delPortTable: this function deletes a PortTable */ |
| 619 |
#ifdef __PROTOTYPE__ |
| 620 |
static void delPortTable(PortTable *p) |
| 621 |
#else |
| 622 |
static void delPortTable(p) |
| 623 |
PortTable *p; |
| 624 |
#endif |
| 625 |
{ |
| 626 |
|
| 627 |
/* free memory used by PortTable */ |
| 628 |
if(p->sktname) free(p->sktname); |
| 629 |
if(p->sharehost) free(p->sharehost); |
| 630 |
|
| 631 |
/* de-link the PortTable */ |
| 632 |
if(p->prv) p->prv->nxt= p->nxt; |
| 633 |
else phd = p->nxt; |
| 634 |
if(p->nxt) p->nxt->prv= p->prv; |
| 635 |
else ptl = p->prv; |
| 636 |
|
| 637 |
} |
| 638 |
|
| 639 |
/* ---------------------------------------------------------------------- */ |
| 640 |
|
| 641 |
/* pm_server: this function handles the PortMaster Server protocol */ |
| 642 |
#ifdef __PROTOTYPE__ |
| 643 |
void pm_server( |
| 644 |
Socket *client, /* temporary client->PortMaster socket */ |
| 645 |
SKTEVENT event) /* PM_SERVER or PM_SHARE */ |
| 646 |
#else |
| 647 |
void pm_server(client,event) |
| 648 |
Socket *client; |
| 649 |
SKTEVENT event; |
| 650 |
#endif |
| 651 |
{ |
| 652 |
PortTable *p = NULL; |
| 653 |
SKTEVENT port; |
| 654 |
SKTEVENT mesg; |
| 655 |
static char sharehost[BUFSIZE]; |
| 656 |
ClientWaiting *cw = NULL; |
| 657 |
ClientWaiting *cwnxt = NULL; |
| 658 |
|
| 659 |
|
| 660 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 661 |
|
| 662 |
if(event == PM_SHARE) { |
| 663 |
if(Stimeoutwait(client,TIMEOUT,0L) < 0) longjmp(timedout,1); |
| 664 |
Sgets(sharehost,BUFSIZE,client); |
| 665 |
} |
| 666 |
else sharehost[0]= '\0'; |
| 667 |
|
| 668 |
if(Stimeoutwait(client,TIMEOUT,0L) < 0) longjmp(timedout,1); |
| 669 |
Sgets(sktname,BUFSIZE,client); |
| 670 |
|
| 671 |
/* check if sktname already in table */ |
| 672 |
for(p= phd; p; p= p->nxt) if(!strcmp(sktname,p->sktname)) { |
| 673 |
Swrite(client,(char *) &pm_sorry,sizeof(pm_sorry)); |
| 674 |
return; |
| 675 |
} |
| 676 |
|
| 677 |
if(Stimeoutwait(client,TIMEOUT,0L) < 0) longjmp(timedout,1); |
| 678 |
Sreadbytes(client,(char *) &port,sizeof(port)); |
| 679 |
port= ntohs( port); |
| 680 |
|
| 681 |
/* make a PortTable entry for the new server */ |
| 682 |
p= makePortTable(port,(event == PM_SHARE)? sharehost : NULL); |
| 683 |
|
| 684 |
/* unable to make PortTable entry */ |
| 685 |
if(!p) { |
| 686 |
Swrite(client,(char *) &pm_sorry,sizeof(pm_sorry)); |
| 687 |
} |
| 688 |
else { |
| 689 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 690 |
|
| 691 |
if(Stimeoutwait(client,TIMEOUT,0L) < 0) longjmp(timedout,1); |
| 692 |
Sreadbytes(client,(char *) &mesg,sizeof(mesg)); |
| 693 |
mesg= ntohs(mesg); |
| 694 |
if(mesg != PM_OK) longjmp(timedout,1); |
| 695 |
|
| 696 |
/* go through ClientWaiting list to let any clients waiting |
| 697 |
* to talk to this server that the server is now up |
| 698 |
*/ |
| 699 |
for(cw= cwhd, cwnxt= NULL; cw; cw= cwnxt) if(!strcmp(cw->sktname,sktname)) { |
| 700 |
/* found a matching ClientWaiting */ |
| 701 |
cwnxt= cw->nxt; |
| 702 |
|
| 703 |
/* respond with PM_OK (normally) or PM_OKSHARE for PortMaster-sharing */ |
| 704 |
if(p->sharehost) { |
| 705 |
Swrite(cw->client,(char *) &pm_okshare,sizeof(pm_okshare)); |
| 706 |
Sputs(p->sharehost,cw->client); |
| 707 |
} |
| 708 |
else { |
| 709 |
Swrite(cw->client,(char *) &pm_ok,sizeof(pm_ok)); |
| 710 |
} |
| 711 |
|
| 712 |
/* return port number */ |
| 713 |
port= htons(p->port); |
| 714 |
Swrite(cw->client,(char *) &port,sizeof(port)); |
| 715 |
|
| 716 |
/* de-link ClientWaiting */ |
| 717 |
if(cw->prv) cw->prv->nxt= cw->nxt; |
| 718 |
else cwhd = cw->nxt; |
| 719 |
if(cw->nxt) cw->nxt->prv= cw->prv; |
| 720 |
else cwtl = cw->prv; |
| 721 |
free(cw->sktname); |
| 722 |
Sclose(cw->client); |
| 723 |
free((char *) cw); |
| 724 |
} |
| 725 |
} |
| 726 |
|
| 727 |
} |
| 728 |
|
| 729 |
/* ---------------------------------------------------------------------- */ |
| 730 |
|
| 731 |
/* pm_client: handle PortMaster client protocol (looks up server port) */ |
| 732 |
#ifdef __PROTOTYPE__ |
| 733 |
void pm_client(Socket *client,int waiting) |
| 734 |
#else |
| 735 |
void pm_client(client,waiting) |
| 736 |
Socket *client; |
| 737 |
int waiting; |
| 738 |
#endif |
| 739 |
{ |
| 740 |
PortTable *p = NULL; |
| 741 |
SKTEVENT port; |
| 742 |
|
| 743 |
|
| 744 |
/* send first PM_OK, letting client know that the PortMaster is alive */ |
| 745 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 746 |
|
| 747 |
/* get requested server's sktname from client */ |
| 748 |
if(Stimeoutwait(client,TIMEOUT,0L) < 0) longjmp(timedout,1); |
| 749 |
Sgets(sktname,BUFSIZE,client); |
| 750 |
|
| 751 |
/* attempt to look up sktname in PortTable linked list */ |
| 752 |
for(p= phd; p; p= p->nxt) { |
| 753 |
if(!strcmp(p->sktname,sktname)) { |
| 754 |
|
| 755 |
/* respond with PM_OK (normally) or PM_OKSHARE for PortMaster-sharing */ |
| 756 |
if(p->sharehost) { |
| 757 |
Swrite(client,(char *) &pm_okshare,sizeof(pm_okshare)); |
| 758 |
Sputs(p->sharehost,client); |
| 759 |
} |
| 760 |
else { |
| 761 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 762 |
} |
| 763 |
|
| 764 |
/* return port number */ |
| 765 |
port= htons(p->port); |
| 766 |
Swrite(client,(char *) &port,sizeof(port)); |
| 767 |
break; |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
/* if unable to find server */ |
| 772 |
if(!p) { |
| 773 |
if(waiting) { /* put request on ClientWaiting list */ |
| 774 |
ClientWaiting *cw= NULL; |
| 775 |
/* double-link and initialize a ClientWaiting structure */ |
| 776 |
cw= (ClientWaiting *) malloc(sizeof(ClientWaiting)); |
| 777 |
if(cw) { |
| 778 |
if(cwtl) cwtl->nxt= cw; |
| 779 |
else cwhd = cw; |
| 780 |
cw->prv = cwtl; |
| 781 |
cw->nxt = (ClientWaiting *) NULL; |
| 782 |
cwtl = cw; |
| 783 |
cw->client = client; |
| 784 |
cw->sktname = calloc(strlen(sktname)+1,sizeof(char)); |
| 785 |
strcpy(cw->sktname,sktname); |
| 786 |
return; |
| 787 |
} |
| 788 |
} |
| 789 |
|
| 790 |
/* send PM_SORRY to indicate to client that server cannot be located (yet) */ |
| 791 |
#ifdef AS400 |
| 792 |
pm_strtsv(sktname); |
| 793 |
#endif |
| 794 |
Swrite(client,(char *) &pm_sorry,sizeof(pm_sorry)); |
| 795 |
} |
| 796 |
|
| 797 |
} |
| 798 |
|
| 799 |
/* ---------------------------------------------------------------------- */ |
| 800 |
|
| 801 |
/* pm_close: this function handles a server close (removes server from |
| 802 |
* PortTable) |
| 803 |
*/ |
| 804 |
#ifdef __PROTOTYPE__ |
| 805 |
void pm_close(Socket *client) |
| 806 |
#else |
| 807 |
void pm_close(client) |
| 808 |
Socket *client; |
| 809 |
#endif |
| 810 |
{ |
| 811 |
PortTable *p = NULL; |
| 812 |
SKTEVENT port; |
| 813 |
|
| 814 |
|
| 815 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 816 |
|
| 817 |
/* get port of skt to be closed */ |
| 818 |
if(Stimeoutwait(client,TIMEOUT,0L) < 0) longjmp(timedout,1); |
| 819 |
Sreadbytes(client,(char *) &port,sizeof(port)); |
| 820 |
port= ntohs( port); |
| 821 |
|
| 822 |
for(p= phd; p; p= p->nxt) { |
| 823 |
if(p->port == port) { |
| 824 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 825 |
delPortTable(p); |
| 826 |
break; |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
/* report if unable to find requested port */ |
| 831 |
if(!p) { |
| 832 |
Swrite(client,(char *) &pm_sorry,sizeof(pm_sorry)); |
| 833 |
} |
| 834 |
|
| 835 |
} |
| 836 |
|
| 837 |
/* ---------------------------------------------------------------------- */ |
| 838 |
|
| 839 |
/* pm_quit: this function causes the PortMaster to quit (shutdown) */ |
| 840 |
#ifdef __PROTOTYPE__ |
| 841 |
void pm_quit( |
| 842 |
Socket *server, |
| 843 |
Socket *client) |
| 844 |
#else |
| 845 |
void pm_quit( |
| 846 |
server, |
| 847 |
client) |
| 848 |
Socket *server; |
| 849 |
Socket *client; |
| 850 |
#endif |
| 851 |
{ |
| 852 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 853 |
|
| 854 |
/* get sktname (should be PortMaster) */ |
| 855 |
if(Stimeoutwait(client,TIMEOUT,0L) < 0) longjmp(timedout,1); |
| 856 |
Sgets(sktname,BUFSIZE,client); |
| 857 |
|
| 858 |
/* if sktname was "PortMaster", then quit */ |
| 859 |
if(!strcmp(sktname,"PortMaster")) { |
| 860 |
if(client) { |
| 861 |
shutdown(client->skt,2); |
| 862 |
close(client->skt); |
| 863 |
client->skt= 0;; |
| 864 |
freeSocket(client); |
| 865 |
} |
| 866 |
if(server) { |
| 867 |
shutdown(client->skt,2); |
| 868 |
close(server->skt); |
| 869 |
server->skt= 0;; |
| 870 |
freeSocket(server); |
| 871 |
} |
| 872 |
#ifdef vms |
| 873 |
exit(1); |
| 874 |
#else |
| 875 |
exit(0); |
| 876 |
#endif |
| 877 |
} |
| 878 |
|
| 879 |
/* if not "PortMaster", then don't quit */ |
| 880 |
else { |
| 881 |
Swrite(client,(char *) &pm_sorry,sizeof(pm_sorry)); |
| 882 |
} |
| 883 |
|
| 884 |
} |
| 885 |
|
| 886 |
/* ---------------------------------------------------------------------- */ |
| 887 |
|
| 888 |
/* pm_table: this function returns the PortMaster's PortTable through the |
| 889 |
* socket to it |
| 890 |
*/ |
| 891 |
#ifdef __PROTOTYPE__ |
| 892 |
void pm_table(Socket *client) |
| 893 |
#else |
| 894 |
void pm_table(client) |
| 895 |
Socket *client; |
| 896 |
#endif |
| 897 |
{ |
| 898 |
PortTable *p = NULL; |
| 899 |
SKTEVENT cnt; |
| 900 |
|
| 901 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 902 |
|
| 903 |
/* count table entries */ |
| 904 |
for(p= phd, cnt= 0; p; p= p->nxt) ++cnt; |
| 905 |
|
| 906 |
cnt= htons(cnt); |
| 907 |
Swrite(client,(char *) &cnt,sizeof(cnt)); |
| 908 |
|
| 909 |
/* send PortMaster table out the socket */ |
| 910 |
for(p= phd; p; p= p->nxt) { |
| 911 |
if(p->sharehost) Sprintf(client,"%s@%s : %d",p->sktname,p->sharehost,p->port); |
| 912 |
else Sprintf(client,"%s : %d",p->sktname,p->port); |
| 913 |
} |
| 914 |
|
| 915 |
} |
| 916 |
|
| 917 |
/* ---------------------------------------------------------------------- */ |
| 918 |
|
| 919 |
/* pm_rmserver: this function removes a server entry from the PortTable */ |
| 920 |
#ifdef __PROTOTYPE__ |
| 921 |
void pm_rmserver(Socket *client) |
| 922 |
#else |
| 923 |
void pm_rmserver(client) |
| 924 |
Socket *client; |
| 925 |
#endif |
| 926 |
{ |
| 927 |
char locbuf[BUFSIZE]; |
| 928 |
PortTable *p = NULL; |
| 929 |
|
| 930 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 931 |
|
| 932 |
/* get name of server to be removed from PortTable */ |
| 933 |
if(Stimeoutwait(client,TIMEOUT,0L) < 0) longjmp(timedout,1); |
| 934 |
Sgets(locbuf,BUFSIZE,client); |
| 935 |
|
| 936 |
|
| 937 |
for(p= phd; p; p= p->nxt) { |
| 938 |
if(!strcmp(p->sktname,locbuf)) { |
| 939 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 940 |
delPortTable(p); |
| 941 |
break; |
| 942 |
} |
| 943 |
} |
| 944 |
|
| 945 |
/* report if unable to find requested port */ |
| 946 |
if(!p) { |
| 947 |
Swrite(client,(char *) &pm_sorry,sizeof(pm_sorry)); |
| 948 |
} |
| 949 |
|
| 950 |
} |
| 951 |
|
| 952 |
/* --------------------------------------------------------------------- */ |
| 953 |
#ifdef SPMFIREWALL |
| 954 |
|
| 955 |
/* firewall: how to keep out unwanted types |
| 956 |
* Environment Variable: SPMFIREWALL=spfirewall.dat file |
| 957 |
* |
| 958 |
* <spmfirewall.dat> format: |
| 959 |
* [#*] [#*] [#*] [#*] [#*] [#*] |
| 960 |
* |
| 961 |
* Use a number to restrict entry; an * for accept all |
| 962 |
* |
| 963 |
* Returns: 0=accept |
| 964 |
* 1=reject |
| 965 |
*/ |
| 966 |
#ifdef __PROTOTYPE__ |
| 967 |
int firewall(Socket *skt) |
| 968 |
#else |
| 969 |
int firewall(skt) |
| 970 |
Socket *skt; |
| 971 |
#endif |
| 972 |
{ |
| 973 |
int ret = 1; /* default: flag as reject */ |
| 974 |
static struct sockaddr name; |
| 975 |
static socklen_t namelen= sizeof(name.sa_data); |
| 976 |
SpmFW *spmfw = NULL; |
| 977 |
|
| 978 |
|
| 979 |
if(!spmfwhd) { |
| 980 |
return 0; |
| 981 |
} |
| 982 |
|
| 983 |
/* get connecting socket's internet address (peername) */ |
| 984 |
if(getpeername(skt->skt,&name,&namelen) == -1) { |
| 985 |
} |
| 986 |
else { /* check that connecting socket's internet address is approved */ |
| 987 |
int i; |
| 988 |
int datum; |
| 989 |
for(spmfw= spmfwhd; spmfw; spmfw= spmfw->nxt) { |
| 990 |
for(i= 0; i < SPMFWDATA; ++i) { |
| 991 |
datum= name.sa_data[i]; |
| 992 |
if(datum < 0) datum+= 256; |
| 993 |
if(spmfw->data[i] != -1 && spmfw->data[i] != datum) break; |
| 994 |
} |
| 995 |
if(i >= SPMFWDATA) { /* name in firewall */ |
| 996 |
ret= 0; /* accept */ |
| 997 |
break; |
| 998 |
} |
| 999 |
} |
| 1000 |
} |
| 1001 |
|
| 1002 |
return ret; |
| 1003 |
} |
| 1004 |
|
| 1005 |
/* --------------------------------------------------------------------- */ |
| 1006 |
|
| 1007 |
/* firewall_init: initialize PortMaster firewall */ |
| 1008 |
#ifdef __PROTOTYPE__ |
| 1009 |
void firewall_init(char *fwfile) |
| 1010 |
#else |
| 1011 |
void firewall_init(fwfile) |
| 1012 |
char *fwfile; |
| 1013 |
#endif |
| 1014 |
{ |
| 1015 |
char buf[BUFSIZE]; |
| 1016 |
char *b = NULL; |
| 1017 |
int i; |
| 1018 |
FILE *fp = NULL; |
| 1019 |
SpmFW *spmfw = NULL; |
| 1020 |
|
| 1021 |
|
| 1022 |
/* free up old firewall data (if any) */ |
| 1023 |
if(spmfwhd) { |
| 1024 |
SpmFW *spmfwnxt=NULL; |
| 1025 |
|
| 1026 |
for(spmfw= spmfwhd; spmfw; spmfw= spmfwnxt) { |
| 1027 |
spmfwnxt= spmfw->nxt; |
| 1028 |
free((char *) spmfw); |
| 1029 |
} |
| 1030 |
spmfwhd= NULL; |
| 1031 |
} |
| 1032 |
|
| 1033 |
if(!fwfile || !fwfile[0]) { /* get fwfile from environment SPMFIREWALL */ |
| 1034 |
fwfile= getenv("SPMFIREWALL"); |
| 1035 |
} |
| 1036 |
|
| 1037 |
if(fwfile) { |
| 1038 |
fp= fopen(fwfile,"r"); |
| 1039 |
if(fp) { |
| 1040 |
|
| 1041 |
/* allocate and forward-link a SpmFW */ |
| 1042 |
spmfw= (SpmFW *) malloc(sizeof(SpmFW)); |
| 1043 |
if(!spmfw) { |
| 1044 |
error(XTDIO_WARNING, |
| 1045 |
"unable to allocate a SpmFW in firewall_init()\n"); |
| 1046 |
return; |
| 1047 |
} |
| 1048 |
spmfw->nxt = spmfwhd; |
| 1049 |
spmfwhd = spmfw; |
| 1050 |
|
| 1051 |
/* insure that the PortMaster host is on the firewall list */ |
| 1052 |
spmfw->data[0]= -1; |
| 1053 |
spmfw->data[1]= -1; |
| 1054 |
spmfw->data[2]= hostaddr[0]; |
| 1055 |
spmfw->data[3]= hostaddr[1]; |
| 1056 |
spmfw->data[4]= hostaddr[2]; |
| 1057 |
spmfw->data[5]= hostaddr[3]; |
| 1058 |
|
| 1059 |
/* read in <spmfirewall.dat> file */ |
| 1060 |
while(fgets(buf,BUFSIZE,fp)) { |
| 1061 |
|
| 1062 |
/* ignore comments, ignore blank lines */ |
| 1063 |
b= strchr(buf,'#'); |
| 1064 |
if(b) *b= '\0'; |
| 1065 |
b= stpblk(buf); |
| 1066 |
if(!b[0]) continue; |
| 1067 |
|
| 1068 |
/* allocate and forward-link a SpmFW */ |
| 1069 |
spmfw= (SpmFW *) malloc(sizeof(SpmFW)); |
| 1070 |
if(!spmfw) { |
| 1071 |
error(XTDIO_WARNING, |
| 1072 |
"unable to allocate a SpmFW in firewall_init()\n"); |
| 1073 |
return; |
| 1074 |
} |
| 1075 |
spmfw->nxt= spmfwhd; |
| 1076 |
spmfwhd = spmfw; |
| 1077 |
|
| 1078 |
for(i= 0; i < SPMFWDATA; ++i) { |
| 1079 |
if(*b == '*') { |
| 1080 |
spmfw->data[i]= -1; |
| 1081 |
b = stpblk(b+1); |
| 1082 |
} |
| 1083 |
else { |
| 1084 |
sscanf(b,"%d",&spmfw->data[i]); |
| 1085 |
b= stpnxt(b,"%d%b"); |
| 1086 |
} |
| 1087 |
} |
| 1088 |
} |
| 1089 |
} |
| 1090 |
} |
| 1091 |
|
| 1092 |
} |
| 1093 |
|
| 1094 |
/* --------------------------------------------------------------------- */ |
| 1095 |
|
| 1096 |
/* pm_fwinit: handles PortMaster firewall re-initialize */ |
| 1097 |
#ifdef __PROTOTYPE__ |
| 1098 |
void pm_fwinit(Socket *client) |
| 1099 |
#else |
| 1100 |
void pm_fwinit(client) |
| 1101 |
Socket *client; |
| 1102 |
#endif |
| 1103 |
{ |
| 1104 |
|
| 1105 |
firewall_init(""); |
| 1106 |
|
| 1107 |
Swrite(client,(char *) &pm_ok,sizeof(pm_ok)); |
| 1108 |
|
| 1109 |
} |
| 1110 |
#endif /* SPMFIREWALL */ |
| 1111 |
|
| 1112 |
/* --------------------------------------------------------------------- */ |
| 1113 |
|
| 1114 |
#ifdef STRTSRVR_PGM |
| 1115 |
/* pm_strtsv: Starts server if it is not registered |
| 1116 |
* |
| 1117 |
* Author: Steve Richards |
| 1118 |
* |
| 1119 |
* We will take the name and spawn a program that will actually start the |
| 1120 |
* server so it can register the documenation can be adjusted to tell the |
| 1121 |
* client requester that if they get a pm_sorry to delay a bit (5 seconds say) and |
| 1122 |
* retry the request. |
| 1123 |
* |
| 1124 |
* I didn't want to change the protocol by sending a different response but it |
| 1125 |
* would be possible to send say a "retry" and keep track of how many times the |
| 1126 |
* server was tried to be started etc... |
| 1127 |
* I have added the server starting program name to the |
| 1128 |
*/ |
| 1129 |
#ifdef __PROTOTYPE__ |
| 1130 |
void pm_strtsv(char *sktname) |
| 1131 |
#else |
| 1132 |
void pm_strtsv(sktname) |
| 1133 |
char *sktname; |
| 1134 |
#endif |
| 1135 |
{ |
| 1136 |
char command_string[1024] = ""; |
| 1137 |
|
| 1138 |
strcpy (command_string,STRTSRVR_PGM); |
| 1139 |
strcat (command_string," "); |
| 1140 |
#ifdef AS400 |
| 1141 |
strcat (command_string,"("); |
| 1142 |
strcat (command_string,sktname); |
| 1143 |
strcat (command_string,")"); |
| 1144 |
#else |
| 1145 |
strcat (command_string,sktname); |
| 1146 |
#endif |
| 1147 |
|
| 1148 |
fprintf(stderr,"trying to start %s\n",command_string); |
| 1149 |
system(command_string); |
| 1150 |
} |
| 1151 |
#endif /* STRTSV */ |
| 1152 |
|
| 1153 |
/* --------------------------------------------------------------------- |
| 1154 |
* vim: ts=4 |
| 1155 |
*/ |