]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/zwrite/zwrite.c
r4275@bucket (orig r265): kcr | 2008-01-21 02:57:32 -0500
[1ts-debian.git] / zephyr / clients / zwrite / zwrite.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains code for the "zwrite" command.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id$
7  *
8  *      Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #include <sysdep.h>
14 #include <zephyr/mit-copyright.h>
15 #include <zephyr/zephyr.h>
16 #include <netdb.h>
17 #include <pwd.h>
18
19 #ifndef lint
20 static const char rcsid_zwrite_c[] = "$Id$";
21 #endif /* lint */
22
23 #define DEFAULT_CLASS "MESSAGE"
24 #define DEFAULT_INSTANCE "PERSONAL"
25 #define URGENT_INSTANCE "URGENT"
26 #define DEFAULT_OPCODE ""
27 #define FILSRV_CLASS "FILSRV"
28
29 #define MAXRECIPS 100
30
31 int nrecips, msgarg, verbose, quiet, nodot, cc;
32 char *whoami, *inst, *class, *opcode, *realm, *recips[MAXRECIPS];
33 Z_AuthProc auth;
34 void un_tabify(char **, int *);
35
36 char *fix_filsrv_inst(char *);
37 void usage(char *);
38 void send_off(ZNotice_t *, int);
39
40 int
41 main(int argc,
42      char *argv[])
43 {
44     int retval, arg, nocheck, nchars, msgsize, filsys, tabexpand;
45     char *message, *signature = NULL, *format = NULL;
46     static char bfr[BUFSIZ], classbfr[BUFSIZ], instbfr[BUFSIZ], sigbfr[BUFSIZ];
47     static char opbfr[BUFSIZ];
48     static ZNotice_t notice;
49
50     whoami = argv[0];
51
52     if ((retval = ZInitialize()) != ZERR_NONE) {
53         com_err(whoami, retval, "while initializing");
54         exit(1);
55     }
56
57     if (argc < 2)
58         usage(whoami);
59
60     auth = ZAUTH;
61     verbose = quiet = msgarg = nrecips = nocheck = filsys = nodot = 0;
62     tabexpand = 1;
63
64     class = ZGetVariable("zwrite-class");
65     if (class) {
66         (void) strcpy(classbfr, class);
67         class = classbfr;
68     }
69     else
70         class = DEFAULT_CLASS;
71     inst = ZGetVariable("zwrite-inst");
72     if (inst) {
73         (void) strcpy(instbfr, inst);
74         inst = instbfr;
75     }
76     else
77         inst = DEFAULT_INSTANCE;
78
79     opcode = ZGetVariable("zwrite-opcode");
80     if (opcode)
81       opcode = strcpy(opbfr, opcode);
82     else
83       opcode = DEFAULT_OPCODE;
84
85       signature = ZGetVariable("zwrite-signature");
86     if (signature) {
87         (void) strcpy(sigbfr, signature);
88         signature = sigbfr;
89     } 
90         
91     arg = 1;
92         
93     for (;arg<argc&&!msgarg;arg++) {
94         if (*argv[arg] != '-') {
95             recips[nrecips++] = argv[arg];
96             continue;
97         } 
98         if (strlen(argv[arg]) > 2)
99             usage(whoami);
100         switch (argv[arg][1]) {
101         case 'a':               /* Backwards compatibility */
102             auth = ZAUTH;
103             break;
104         case 'o':
105             class = DEFAULT_CLASS;
106             inst = DEFAULT_INSTANCE;
107             opcode = DEFAULT_OPCODE;
108             break;
109         case 'd':
110             auth = ZNOAUTH;
111             break;
112         case 'v':
113             verbose = 1;
114             break;
115         case 'q':
116             quiet = 1;
117             break;
118         case 'n':
119             nocheck = 1;
120             break;
121         case 't':
122             tabexpand = 0;
123             break;
124         case 'u':
125             inst = URGENT_INSTANCE;
126             break;
127         case 'O':
128             if (arg == argc-1)
129               usage(whoami);
130             arg++;
131             opcode = argv[arg];
132             break;
133         case 'i':
134             if (arg == argc-1 || filsys == 1)
135                 usage(whoami);
136             arg++;
137             inst = argv[arg];
138             filsys = -1;
139             break;
140         case 'c':
141             if (arg == argc-1 || filsys == 1)
142                 usage(whoami);
143             arg++;
144             class = argv[arg];
145             filsys = -1;
146             break;
147         case 'f':
148             if (arg == argc-1 || filsys == -1)
149                 usage(whoami);
150             arg++;
151             class = FILSRV_CLASS;
152             inst = fix_filsrv_inst(argv[arg]);
153             filsys = 1;
154             nocheck = 1;                /* implied -n (no ping) */
155             break;
156         case 's':
157             if (arg == argc-1)
158                 usage(whoami);
159             arg++;
160             signature = argv[arg];
161             break;
162         case 'm':
163             if (arg == argc-1)
164                 usage(whoami);
165             nocheck = 1;                /* implied -n (no ping) */
166             msgarg = arg+1;
167             break;
168         case 'l':                       /* literal */
169             nodot = 1;
170             break;
171         case 'F':
172             if (arg == argc-1)
173                 usage(whoami);
174             arg++;
175             format = argv[arg];
176             break;
177         case 'r':
178             if (arg == argc-1)
179                 usage(whoami);
180             arg++;
181             realm = argv[arg];
182             break;
183         case 'C':
184             cc = 1;
185             break;
186         default:
187             usage(whoami);
188         }
189     }
190
191     if (!nrecips && !(strcmp(class, DEFAULT_CLASS) ||
192                       (strcmp(inst, DEFAULT_INSTANCE) &&
193                        strcmp(inst, URGENT_INSTANCE)))) {
194         /* must specify recipient if using default class and
195            (default instance or urgent instance) */
196         fprintf(stderr, "No recipients specified.\n");
197         usage(whoami);
198     }
199
200     if (!signature) {
201         /* try to find name in the password file */
202         register struct passwd *pwd;
203         register char *cp = sigbfr;
204         register char *cp2, *pp;
205
206         pwd = getpwuid(getuid());
207         if (pwd) {
208             cp2 = pwd->pw_gecos;
209             for (; *cp2 && *cp2 != ',' ; cp2++) {
210                 if (*cp2 == '&') {
211                     pp = pwd->pw_name;
212                     *cp++ = islower(*pp) ? toupper(*pp) : *pp;
213                     pp++;
214                     while (*pp)
215                         *cp++ = *pp++;
216                 } else
217                     *cp++ = *cp2;
218             }
219             signature = sigbfr;
220         }
221     }   
222
223     notice.z_kind = ACKED;
224     notice.z_port = 0;
225     notice.z_class = class;
226     notice.z_class_inst = inst;
227     notice.z_opcode = "PING";
228     notice.z_sender = 0;
229     notice.z_message_len = 0;
230     notice.z_recipient = "";
231     if (format)
232             notice.z_default_format = format;
233     else if (filsys == 1)
234             notice.z_default_format = "@bold(Filesystem Operation Message for $instance:)\nFrom: @bold($sender) at $time $date\n$message";
235     else if (auth == ZAUTH) {
236         if (signature)
237             notice.z_default_format = "Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\nFrom: @bold($1) <$sender>\n\n$2";
238         else
239             notice.z_default_format = "Class $class, Instance $instance:\nTo: @bold($recipient) at $time $date\n$message";
240     } else {
241         if (signature)
242             notice.z_default_format = "@bold(UNAUTHENTIC) Class $class, Instance $instance at $time $date:\nFrom: @bold($1) <$sender>\n\n$2";
243         else
244             notice.z_default_format = "@bold(UNAUTHENTIC) Class $class, Instance $instance at $time $date:\n$message";
245     }
246     if (!nocheck && nrecips)
247         send_off(&notice, 0);
248         
249     if (!msgarg && isatty(0)) {
250         if (nodot)
251             printf("Type your message now.  End with the end-of-file character.\n");
252         else
253             printf("Type your message now.  End with control-D or a dot on a line by itself.\n");
254     }
255         
256     message = NULL;
257     msgsize = 0;
258     if (signature) {
259         message = malloc((unsigned)(strlen(signature)+2));
260         (void) strcpy(message, signature);
261         msgsize = strlen(message);
262         message[msgsize++] = '\0';
263     } else {
264         message = malloc(1);
265         message[msgsize++] = '\0';
266     }
267
268     if (cc && nrecips > 1) {
269         int size = msgsize;
270         for (arg=0;arg<nrecips;arg++)
271             size += (strlen(recips[arg]) + 2);
272         size += 6;                      /* for the newlines and "cc: " */
273         message = realloc(message, (unsigned) size);
274         (void) strcpy(message+msgsize, "CC: ");
275         msgsize += 4;
276         for (arg=0;arg<nrecips;arg++) {
277             (void) strcpy(message+msgsize, recips[arg]);
278             msgsize += strlen(recips[arg]);
279             if (arg != nrecips-1) {
280                 message[msgsize] = ' ';
281                 msgsize++;
282             }
283         }
284         message[msgsize] = '\n';
285         msgsize += 1;
286     }
287
288     if (msgarg) {
289         int size = msgsize;
290         for (arg=msgarg;arg<argc;arg++)
291                 size += (strlen(argv[arg]) + 1);
292         size++;                         /* for the newline */
293         message = realloc(message, (unsigned) size);
294         for (arg=msgarg;arg<argc;arg++) {
295             (void) strcpy(message+msgsize, argv[arg]);
296             msgsize += strlen(argv[arg]);
297             if (arg != argc-1) {
298                 message[msgsize] = ' ';
299                 msgsize++;
300             } 
301         }
302         message[msgsize] = '\n';
303         msgsize += 1;
304     } else {
305         if (isatty(0)) {
306             for (;;) {
307                 unsigned int l;
308                 if (!fgets(bfr, sizeof bfr, stdin))
309                     break;
310                 if (!nodot && bfr[0] == '.' &&
311                     (bfr[1] == '\n' || bfr[1] == '\0'))
312                     break;
313                 l = strlen(bfr);
314                 message = realloc(message, msgsize+l+1);
315                 (void) strcpy(message+msgsize, bfr);
316                 msgsize += l;
317             }
318             message = realloc(message, (unsigned)(msgsize+1));
319         }
320         else {  /* Use read so you can send binary messages... */
321             while ((nchars = read(fileno(stdin), bfr, sizeof bfr))) {
322                 if (nchars == -1) {
323                     fprintf(stderr, "Read error from stdin!  Can't continue!\n");
324                     exit(1);
325                 }
326                 message = realloc(message, (unsigned)(msgsize+nchars));
327                 (void) memcpy(message+msgsize, bfr, nchars);
328                 msgsize += nchars;
329             }
330             /* end of msg */
331             message = realloc(message, (unsigned)(msgsize+1));
332         } 
333     }
334
335     notice.z_opcode = opcode;
336     if (tabexpand)
337         un_tabify(&message, &msgsize);
338     notice.z_message = message;
339     notice.z_message_len = msgsize;
340
341     send_off(&notice, 1);
342     exit(0);
343 }
344
345 void
346 send_off(ZNotice_t *notice,
347          int real)
348 {
349     int i, success, retval;
350     char bfr[BUFSIZ], realm_recip[BUFSIZ], dest[3 * BUFSIZ];
351     ZNotice_t retnotice;
352
353     success = 0;
354         
355     for (i=0;i<nrecips || i==0;i++) {
356         if (realm) {
357             sprintf(realm_recip, "%s@%s", (nrecips) ? recips[i] : "", realm);
358             notice->z_recipient = realm_recip;
359         } else {
360             notice->z_recipient = (nrecips) ? recips[i] : "";
361         }
362         if (nrecips)
363             strcpy(dest, recips[i]);
364         else if (!strcmp(class, DEFAULT_CLASS))
365             sprintf(dest, "instance \"%s\"", inst);
366         else if (!strcmp(inst, DEFAULT_INSTANCE))
367             sprintf(dest, "class \"%s\"", class);
368         else
369             sprintf(dest, "class \"%s\", instance \"%s\"", class, inst);
370         if (verbose && real)
371             printf("Sending %smessage, class %s, instance %s, to %s\n", 
372                    auth?"authenticated ":"", 
373                    class, inst, 
374                    nrecips?notice->z_recipient:"everyone");
375         if ((retval = ZSendNotice(notice, auth)) != ZERR_NONE) {
376             (void) sprintf(bfr, "while sending notice to %s", dest);
377             com_err(whoami, retval, bfr);
378             break;
379         }
380         if (real && !quiet) {
381             if (verbose)
382                 printf("Queued... ");
383             else
384                 printf("Message queued for %s... ", dest);
385             fflush(stdout);
386         }
387         if ((retval = ZIfNotice(&retnotice, (struct sockaddr_in *) 0,
388                                 ZCompareUIDPred, 
389                                 (char *)&notice->z_uid)) !=
390             ZERR_NONE) {
391             ZFreeNotice(&retnotice);
392             (void) sprintf(bfr, "while waiting for acknowledgement for %s", 
393                     dest);
394             com_err(whoami, retval, bfr);
395             continue;
396         }
397         if (retnotice.z_kind == SERVNAK) {
398             if (!quiet) {
399                 printf("Received authorization failure while sending to %s\n", 
400                        dest);
401             }
402             ZFreeNotice(&retnotice);
403             break;                      /* if auth fails, punt */
404         } 
405         if (retnotice.z_kind != SERVACK || !retnotice.z_message_len) {
406             if (!quiet) {
407                 printf("Detected server failure while receiving acknowledgement for %s\n", 
408                        dest);
409             }
410             ZFreeNotice(&retnotice);
411             continue;
412         }
413         if (!strcmp(retnotice.z_message, ZSRVACK_SENT)) {
414             success = 1;
415             if (real && !quiet)
416                 printf("sent\n");
417         } else if (!strcmp(retnotice.z_message, ZSRVACK_NOTSENT)) {
418             if (verbose && real && !quiet) {
419                 if (strcmp(class, DEFAULT_CLASS)) {
420                     fprintf(stderr, "Not logged in or not subscribing to class %s, instance %s\n", 
421                            class, inst);
422                 } else {
423                     fprintf(stderr,
424                             "Not logged in or not subscribing to messages\n");
425                 }
426             } 
427             else if (!quiet) {
428                 if (!nrecips) {
429                     fprintf(stderr,
430                             "No one subscribing to class %s, instance %s\n", 
431                             class, inst);
432                 } else {
433                     if (strcmp(class, DEFAULT_CLASS)) {
434                         fprintf(stderr, "%s: Not logged in or not subscribing to class %s, instance %s\n", 
435                                notice->z_recipient, class, inst);
436                     } else {
437                         fprintf(stderr, "%s: Not logged in or not subscribing to messages\n", 
438                                notice->z_recipient);
439                     }
440                 }
441             }
442         } 
443         else
444             printf("Internal failure - illegal message field in server response\n");
445         ZFreeNotice(&retnotice);
446     }
447     if (!success)
448         exit(1);
449
450
451 void
452 usage(char *s)
453 {
454     fprintf(stderr,
455             "Usage: %s [-a] [-o] [-d] [-v] [-q] [-n] [-t] [-u] [-l]\n\
456 \t[-c class] [-i inst] [-O opcode] [-f fsname] [-s signature] [-C]\n\
457 \t[user ...] [-F format] [-r realm] [-m message]\n", s);
458     fprintf(stderr,"\t-f and -c are mutually exclusive\n\
459 \t-f and -i are mutually exclusive\n\
460 \trecipients must be specified unless -c or -f specifies a class\n\
461 \tother than the default class or -i or -f specifies an instance\n\
462 \tother than the default or urgent instance\n");
463     exit(1);
464
465
466 /*
467   if the -f option is specified, this routine is called to canonicalize
468   an instance of the form hostname[:pack].  It turns the hostname into the
469   name returned by gethostbyname(hostname)
470  */
471
472 char *
473 fix_filsrv_inst(char *str)
474 {
475         static char fsinst[BUFSIZ];
476         char *ptr;
477         struct hostent *hp;
478
479         ptr = strchr(str,':');
480         if (ptr)
481                 *ptr = '\0';
482         
483         hp = gethostbyname(str);
484         if (!hp) {
485                 if (ptr)
486                         *ptr = ':';
487                 return(str);
488         }
489         (void) strcpy(fsinst, hp->h_name);
490         if (ptr) {
491                 (void) strcat(fsinst, ":");
492                 ptr++;
493                 (void) strcat(fsinst, ptr);
494         }
495         return(fsinst);
496 }
497
498 /* convert tabs in the buffer into appropriate # of spaces.
499    slightly tricky since the buffer can have NUL's in it. */
500
501 #ifndef TABSTOP
502 #define TABSTOP 8                       /* #chars between tabstops */
503 #endif /* ! TABSTOP */
504
505 void
506 un_tabify(char **bufp,
507           int *sizep)
508 {
509     register char *cp, *cp2;
510     char *cp3;
511     register int i;
512     register int column;                /* column of next character */
513     register int size = *sizep;
514
515     for (cp = *bufp, i = 0; size; size--, cp++)
516         if (*cp == '\t')
517             i++;                        /* count tabs in buffer */
518
519     if (!i)
520         return;                         /* no tabs == no work */
521
522     /* To avoid allocation churning, allocate enough extra space to convert
523        every tab into TABSTOP spaces */
524     /* only add (TABSTOP-1)x because we re-use the cell holding the
525        tab itself */
526     cp = malloc((unsigned)(*sizep + (i * (TABSTOP-1))));
527     if (!cp)                            /* XXX */
528         return;                         /* punt expanding if memory fails */
529     cp3 = cp;
530     /* Copy buffer, converting tabs to spaces as we go */
531     for (cp2 = *bufp, column = 1, size = *sizep; size; cp2++, size--) {
532         switch (*cp2) {
533         case '\n':
534         case '\0':
535             /* newline or null: reset column */
536             column = 1;
537             *cp++ = *cp2;               /* copy the newline */
538             break;
539         default:
540             /* copy the character */
541             *cp = *cp2;
542             cp++;
543             column++;
544             break;
545         case '\t':
546             /* it's a tab, compute how many spaces to expand into. */
547             i = TABSTOP - ((column - 1) % TABSTOP);
548             for (; i > 0; i--) {
549                 *cp++ = ' ';            /* fill in the spaces */
550                 column++;
551                 (*sizep)++;             /* increment the size */
552             }
553             (*sizep)--;                 /* remove one (we replaced the tab) */
554             break;
555         }
556     }
557     free(*bufp);                        /* free the old buf */
558     *bufp = cp3;
559     return;
560 }