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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
|
/* This file is part of the Project Athena Zephyr Notification System.
* It is one of the source files comprising zwgc, the Zephyr WindowGram
* client.
*
* Created by: Marc Horowitz <marc@athena.mit.edu>
*
* $Id$
*
* Copyright (c) 1989 by the Massachusetts Institute of Technology.
* For copying and distribution information, see the file
* "mit-copyright.h".
*/
#include <sysdep.h>
#ifdef HAVE_ARES
#include <ares.h>
#endif
#if (!defined(lint) && !defined(SABER))
static const char rcsid_main_c[] = "$Id$";
#endif
#include <netdb.h>
#include <arpa/nameser.h>
#ifdef HAVE_ARPA_NAMESER_COMPAT_H
#include <arpa/nameser_compat.h>
#endif
#include <sys/socket.h>
#include <sys/resource.h>
#include <locale.h>
#include <zephyr/mit-copyright.h>
#include <zephyr/zephyr.h>
#include "new_memory.h"
#include "zwgc.h"
#include "parser.h"
#include "node.h"
#include "exec.h"
#include "zephyr.h"
#include "notice.h"
#include "subscriptions.h"
#include "file.h"
#include "mux.h"
#include "port.h"
#include "variables.h"
#include "main.h"
#ifdef CMU_ZCTL_PUNT
#include "int_dictionary.h"
#endif
#ifdef CMU_ZWGCPLUS
#include "plus.h"
int zwgcplus = 1;
#endif
void notice_handler(ZNotice_t *);
static void process_notice(ZNotice_t *, char *);
static void setup_signals(int);
static void detach(void);
static void signal_exit(int);
#ifdef HAVE_ARES
static void notice_callback(void *, int, int, char *, char *);
#endif
/*
* Global zwgc-wide variables:
*/
#ifdef DEBUG
int zwgc_debug = 0;
#endif
static char *zwgc_version_string = "1.0";
/*
* description_filename_override - <<<>>>
*/
static char *description_filename_override = NULL;
/*
* progname - <<<>>> export!
*/
char *progname = NULL;
/*
* subscriptions_filename_override - <<<>>> export!
*/
char *subscriptions_filename_override = NULL;
/*
* location_override - <<<>>> export!
*/
char *location_override = NULL;
#ifdef HAVE_ARES
/*
* achannel - <<<>>> export!
*/
ares_channel achannel;
#endif
/****************************************************************************/
/* */
/* Code to deal with reading in the description file: */
/* */
/****************************************************************************/
/*
* program - this holds a pointer to the node representation of the
* description file once it has been read in.
*/
static struct _Node *program = NULL;
/*
* <<<>>>
*/
static void
fake_startup_packet(void)
{
ZNotice_t *notice = (ZNotice_t *)malloc(sizeof(ZNotice_t));
struct timezone tz;
char msgbuf[BUFSIZ];
extern void Z_gettimeofday(struct _ZTimeval *, struct timezone *);
var_set_variable("version", zwgc_version_string);
(void) memset(notice, 0, sizeof(ZNotice_t));
notice->z_version = "";
notice->z_class = "WG_CTL_CLASS";
notice->z_class_inst = "WG_CTL_USER<<<>>>";
notice->z_opcode = "WG_STARTUP";
notice->z_default_format = "Zwgc mark II version $version now running...\n";
notice->z_recipient = "";
notice->z_sender = "ZWGC";
Z_gettimeofday(¬ice->z_time, &tz);
notice->z_port = 0;
notice->z_kind = ACKED;
notice->z_auth = ZAUTH_YES;
notice->z_charset = ZCHARSET_UNKNOWN;
sprintf(msgbuf,"Zwgc mark II version %s now running...",
zwgc_version_string);
notice->z_message = msgbuf;
notice->z_message_len = strlen(notice->z_message)+1;
#ifdef CMU_ZWGCPLUS
list_add_notice(notice);
set_notice_fake(notice, 1);
#endif
process_notice(notice, NULL);
#ifdef CMU_ZWGCPLUS
list_del_notice(notice);
#else
free(notice);
#endif
}
static void
read_in_description_file(void)
{
FILE *input_file;
char defdesc[128];
/* var_clear_all_variables(); <<<>>> */
sprintf(defdesc, "%s/zephyr/%s", DATADIR, DEFDESC);
input_file = locate_file(description_filename_override, USRDESC, defdesc);
if (input_file)
program = parse_file(input_file);
else
program = NULL;
fake_startup_packet();
}
/****************************************************************************/
/* */
/* Code to deal with argument parsing & overall control: */
/* */
/****************************************************************************/
/*
* void usage()
* Effects: Prints out an usage message on stderr then exits the
* program with error code 1.
*/
void
usage(void)
{
#ifdef DEBUG
fprintf(stderr, "\
zwgc: usage: zwgc [-debug] [-f <filename>] [-subfile <filename>]\n\
[-ttymode] [-nofork] [-reenter] [-loc text]\n\
[-default <driver>] {-disable <driver>}*\n\
[output driver options]\n");
#else
fprintf(stderr, "\
zwgc: usage: zwgc [-f <filename>] [-subfile <filename>]\n\
[-ttymode] [-nofork] [-reenter] [-loc text]\n\
[-default <driver>] {-disable <driver>}*\n\
[output driver options]\n");
#endif
exit(1);
}
/*
* <<<>>>
*/
static void
run_initprogs(void)
{
/*
* This code stolen from old zwgc: yuck. Clean up & fix. <<<>>>
* Should this fork instead of just systeming?
*/
int status;
char *iprogname = ZGetVariable("initprogs");
if (!iprogname)
return;
status = system(iprogname);
if (status == 127) {
perror("zwgc initprog exec");
fprintf(stderr,"zwgc initprog of <%s> failed: no shell.\n",
iprogname);
} else if (status!=-1 && status>>8) {
perror("zwgc initprog exec");
fprintf(stderr,"zwgc initprog of <%s> failed with status [%d].\n",
iprogname, status>>8);
}
}
/*
* main -- the program entry point. Does parsing & top level control.
*/
int
main(int argc, char **argv)
{
char **new;
register char **current;
int dofork = 1;
#ifdef HAVE_ARES
int status;
#endif
setlocale(LC_ALL, "");
progname = argv[0];
/*
* Process "-f <filename>", "-subfile <filename>", "-nofork",
* "-reenter" (which is ignored) and (if DEBUG) "-debug"
* arguments, removing then from argc, argv:
*/
for (new=current=argv+1; *current; current++) {
if (string_Eq(*current, "-debug")) {
argc--;
#ifdef DEBUG
zwgc_debug = 1;
#endif
} else if (string_Eq(*current, "-f")) {
argc -= 2; current++;
if (!*current)
usage();
description_filename_override = *current;
} else if (string_Eq(*current, "-subfile")) {
argc -= 2; current++;
if (!*current)
usage();
subscriptions_filename_override = *current;
} else if (string_Eq(*current, "-nofork")) {
argc--;
dofork = 0;
} else if (string_Eq(*current, "-reenter")) {
argc--; /* just throw it away */
} else if (string_Eq(*current, "-loc")) {
argc -= 2; current++;
if (!*current)
usage();
location_override = *current;
} else
*(new)++ = *current;
}
*new = *current;
#ifdef HAVE_ARES
/*
* Initialize resolver library
*/
status = ares_init(&achannel);
if (status != ARES_SUCCESS) {
fprintf(stderr, "Couldn't initialize resolver: %s\n",
ares_strerror(status));
return(1);
}
#endif
/*
* Initialize various subsystems in proper order:
*/
dprintf("Initializing subsystems...\n"); /*<<<>>>*/
#ifdef CMU_ZWGCPLUS
init_noticelist();
#endif
mux_init();
var_clear_all_variables(); /* <<<>>> */
init_ports(); /* <<<>>> */
dprintf("Initializing standard ports...\n");
init_standard_ports(&argc, argv);
if (argc>1)
usage();
dprintf("Initializing zephyr...\n");
setup_signals(dofork);
zephyr_init(notice_handler);
if (dofork)
detach();
/*
* Run the initprogs program(s) now that we are all set to deal:
*/
dprintf("Running initprogs program...\n");
run_initprogs();
dprintf("Test Zwgc parser.\n\n");
read_in_description_file();
dprintf("Entering main loop\n");
mux_loop();
dprintf("Returning from main loop\n");
finalize_zephyr();
return(0);
}
/****************************************************************************/
/* */
/* : */
/* */
/****************************************************************************/
#define USER_SUPPRESS "SUPPRESS"
#define USER_UNSUPPRESS "UNSUPPRESS"
#ifdef CMU_ZCTL_PUNT
#define USER_LIST_SUPPRESSED "LIST-SUPPRESSED"
#define PUNT_INC 1024
extern int_dictionary puntable_addresses_dict;
ZNotice_t punt_reply;
static void
create_punt_reply(int_dictionary_binding *punt_ent)
{
int key_len = strlen(punt_ent->key);
char *tmp;
if (!punt_reply.z_message) {
punt_reply.z_message = (char *)malloc(PUNT_INC);
punt_reply.z_message[0] = 0;
}
if ((punt_reply.z_message_len + key_len + 1) / PUNT_INC >
(punt_reply.z_message_len + PUNT_INC - 1) / PUNT_INC) {
char *new_message = (char *)malloc((punt_reply.z_message_len
/ PUNT_INC + 1) * PUNT_INC);
strcpy(new_message, punt_reply.z_message);
free(punt_reply.z_message);
punt_reply.z_message = new_message;
}
tmp = punt_reply.z_message + strlen(punt_reply.z_message);
strcat (punt_reply.z_message, punt_ent->key);
strcat (punt_reply.z_message, "\n");
punt_reply.z_message_len += key_len + 1;
while (*tmp != '\001') tmp++;
*tmp = ',';
while (*tmp != '\001') tmp++;
*tmp = ',';
}
#endif /* CMU_ZCTL_PUNT */
void
notice_handler(ZNotice_t *notice)
{
#ifndef HAVE_ARES
int ret;
char node[NS_MAXDNAME];
#endif
#if defined(CMU_ZWGCPLUS)
list_add_notice(notice);
#endif
#ifdef HAVE_ARES
ares_getnameinfo(achannel,
(const struct sockaddr *)&(notice->z_sender_sockaddr),
notice->z_sender_sockaddr.sa.sa_family == AF_INET ?
sizeof(struct sockaddr_in) :
notice->z_sender_sockaddr.sa.sa_family == AF_INET6 ?
sizeof(struct sockaddr_in6) :
sizeof(notice->z_sender_sockaddr), ARES_NI_LOOKUPHOST,
notice_callback, notice);
#else
ret = getnameinfo((const struct sockaddr *)&(notice->z_sender_sockaddr),
notice->z_sender_sockaddr.sa.sa_family == AF_INET ?
sizeof(struct sockaddr_in) :
notice->z_sender_sockaddr.sa.sa_family == AF_INET6 ?
sizeof(struct sockaddr_in6) :
sizeof(notice->z_sender_sockaddr),
node, sizeof(node), NULL, 0, 0);
if (ret != 0)
strcpy(node, "?");
process_notice(notice, node);
#ifdef CMU_ZWGCPLUS
/* Let list_del_notice clean up for us. */
#else
ZFreeNotice(notice);
free(notice);
#endif
#endif
}
#ifdef HAVE_ARES
/*
static void
notice_callback(void *arg,
int status,
int timeouts,
struct hostent *fromhost)
*/
static void
notice_callback(void *arg,
int status,
int timeouts,
char *node,
char *service)
{
ZNotice_t *notice = (ZNotice_t *) arg;
#ifdef CMU_ZWGCPLUS
plus_set_hname(notice, node);
#endif
process_notice(notice, node);
#ifdef CMU_ZWGCPLUS
list_del_notice(notice);
#else
ZFreeNotice(notice);
free(notice);
#endif
}
#endif
static void
process_notice(ZNotice_t *notice,
char *hostname)
{
char *control_opcode;
dprintf("Got a message\n");
control_opcode = decode_notice(notice, hostname);
if (control_opcode) {
#ifdef DEBUG
printf("got control opcode <%s>.\n", control_opcode);
#endif
if (!strcasecmp(control_opcode, USER_REREAD)) {
read_in_description_file();
} else if (!strcasecmp(control_opcode, USER_SHUTDOWN))
zwgc_shutdown();
else if (!strcasecmp(control_opcode, USER_STARTUP)) {
#ifdef DEBUG_MEMORY
report_memory_usage(); /* <<<>>> */
#endif
zwgc_startup();
} else if (!strcasecmp(control_opcode, USER_SUPPRESS)) {
string class = get_field(notice->z_message,
notice->z_message_len, 1);
string instance = get_field(notice->z_message,
notice->z_message_len, 2);
string recipient = get_field(notice->z_message,
notice->z_message_len, 3);
punt(class, instance, recipient);
free(class);
free(instance);
free(recipient);
} else if (!strcasecmp(control_opcode, USER_UNSUPPRESS)) {
string class = get_field(notice->z_message,
notice->z_message_len, 1);
string instance = get_field(notice->z_message,
notice->z_message_len, 2);
string recipient = get_field(notice->z_message,
notice->z_message_len, 3);
unpunt(class, instance, recipient);
free(class);
free(instance);
free(recipient);
#ifdef CMU_ZCTL_PUNT
} else if (!strcasecmp(control_opcode, USER_LIST_SUPPRESSED)) {
struct sockaddr_in old, to;
int retval;
if (!notice->z_port) {
printf("zwgc: can't reply to LIST-SUPPRESSED request\n");
return;
}
memset((char *) &punt_reply, 0, sizeof(ZNotice_t));
punt_reply.z_kind = CLIENTACK;
punt_reply.z_class = WG_CTL_CLASS;
punt_reply.z_class_inst = "WG_REPLY";
punt_reply.z_recipient = "zctl?";
punt_reply.z_sender = "Zwgc";
punt_reply.z_default_format = "";
punt_reply.z_opcode = USER_LIST_SUPPRESSED;
punt_reply.z_port = notice->z_port;
punt_reply.z_message = NULL;
punt_reply.z_message_len = 0;
if (puntable_addresses_dict) {
int_dictionary_Enumerate(puntable_addresses_dict,
create_punt_reply);
}
old = ZGetDestAddr();
to = old;
to.sin_port = notice->z_port;
if ((retval = ZSetDestAddr(&to)) != ZERR_NONE) {
com_err("zwgc",retval,"while setting destination address");
exit(1);
}
ZSendNotice(&punt_reply, ZNOAUTH);
if ((retval = ZSetDestAddr(&old)) != ZERR_NONE) {
com_err("zwgc",retval,"while resetting destination address");
exit(1);
}
if (punt_reply.z_message) {
free(punt_reply.z_message);
punt_reply.z_message = NULL;
}
#endif
} else if (!strcasecmp(control_opcode, USER_EXIT)) {
signal_exit(0);
} else
printf("zwgc: unknown control opcode %s.\n", control_opcode);
goto cleanup;
}
if (!zwgc_active) {
#ifdef DEBUG
if (zwgc_debug)
printf("NON-ACTIVE: PUNTED <%s>!!!!\n", notice->z_class_inst);
#endif
goto cleanup;
}
if (puntable_address_p(notice->z_class,
notice->z_class_inst,
notice->z_recipient)) {
#ifdef DEBUG
if (zwgc_debug)
printf("PUNTED <%s>!!!!\n", notice->z_class_inst);
#endif
goto cleanup;
}
exec_process_packet(program, notice);
cleanup:
return;
}
#ifdef CMU_ZWGCPLUS
void
reprocess_notice(ZNotice_t *notice, char *hostname)
{
list_add_notice(notice);
process_notice(notice, hostname);
list_del_notice(notice);
}
#endif
/***************************************************************************/
/*
*
*/
static void
signal_exit(int ignored)
{
mux_end_loop_p = 1;
}
/* clean up ALL the waiting children, in case we get hit with
multiple SIGCHLD's at once, and don't process in time. */
static RETSIGTYPE
signal_child(int ignored)
{
#ifdef HAVE_WAITPID
int status;
#else
union wait status;
#endif
int pid, old_errno = errno;
do {
#ifdef HAVE_WAITPID
pid = waitpid(-1, &status, WNOHANG);
#else
pid = wait3(&status, WNOHANG, (struct rusage *)0);
#endif
} while (pid != 0 && pid != -1);
errno = old_errno;
}
/* rewrite the wgfile in case it has gone away */
static RETSIGTYPE
signal_usr1(int ignored)
{
write_wgfile();
}
static void
setup_signals(int dofork)
{
#ifdef _POSIX_VERSION
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
if (dofork) {
sa.sa_handler = SIG_IGN;
sigaction(SIGINT, &sa, (struct sigaction *)0);
sigaction(SIGTSTP, &sa, (struct sigaction *)0);
sigaction(SIGQUIT, &sa, (struct sigaction *)0);
sigaction(SIGTTOU, &sa, (struct sigaction *)0);
} else {
/* clean up on SIGINT; exiting on logout is the user's problem, now. */
sa.sa_handler = signal_exit;
sigaction(SIGINT, &sa, (struct sigaction *)0);
}
/* behavior never changes */
sa.sa_handler = signal_exit;
sigaction(SIGTERM, &sa, (struct sigaction *)0);
sigaction(SIGHUP, &sa, (struct sigaction *)0);
sa.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &sa, (struct sigaction *)0);
sa.sa_handler = signal_child;
sigaction(SIGCHLD, &sa, (struct sigaction *)0);
sa.sa_handler = signal_usr1;
sigaction(SIGUSR1, &sa, (struct sigaction *)0);
#else /* !POSIX */
if (dofork) {
/* Ignore keyboard signals if forking. Bad things will happen. */
signal(SIGINT, SIG_IGN);
signal(SIGTSTP, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
} else {
/* clean up on SIGINT; exiting on logout is the user's problem, now. */
signal(SIGINT, signal_exit);
}
/* behavior never changes */
signal(SIGTERM, signal_exit);
signal(SIGHUP, signal_exit);
signal(SIGCHLD, signal_child);
signal(SIGPIPE, SIG_IGN); /* so that Xlib gets an error */
signal(SIGUSR1, signal_usr1);
#endif
}
/* detach() taken from old zwgc, with lots of stuff ripped out */
static void
detach(void)
{
/* detach from terminal and fork. */
register int i;
/* Attempt to join the process group of the session leader. This
* will get us a HUP if the session leader is in the foreground at
* logout time (which is often the case) or if the shell is running
* under telnetd or xterm (both of which HUP the process group of
* their child process). If we have getsid(), that's the best way
* of finding the session leader; otherwise use the process group of
* the parent process, which is a good guess. */
#if defined(HAVE_GETSID)
setpgid(0, getsid(0));
#elif defined(HAVE_GETPGID)
setpgid(0, getpgid(getppid()));
#elif !defined(GETPGRP_VOID)
setpgid(0, getpgrp(getppid()));
#endif
/* fork off and let parent exit... */
i = fork();
if (i) {
if (i < 0) {
perror("zwgc: cannot fork, aborting:");
exit(1);
}
exit(0);
}
}
|