]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/zshutdown_notify/zshutdown_notify.c
f2b493289959577ade261a9b99cc80ffa4840e6b
[1ts-debian.git] / zephyr / clients / zshutdown_notify / zshutdown_notify.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains code for "zshutdown_notify", a utility called by
3  * shutdown(8) to do Zephyr notification on shutdown.
4  *
5  *      Created by:     C. Anthony Della Fera
6  *
7  *      $Id$
8  *
9  *      Copyright (c) 1987, 1993 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h". 
12  */
13
14 #include <sysdep.h>
15 #include <zephyr/mit-copyright.h>
16 #include <zephyr/zephyr.h>
17
18 #include <sys/socket.h>
19 #include <netdb.h>
20
21 #ifndef lint
22 static const char *rcsid_zshutdown_notify_c =
23     "$Id$";
24 #endif
25
26 #define N_KIND          UNSAFE
27 #define N_CLASS         "FILSRV"
28 #define N_OPCODE        "SHUTDOWN"
29 #define N_DEF_FORMAT    "From $sender:\n@bold(Shutdown message from $1 at $time)\n@center(System going down, message is:)\n\n$2\n\n@center(@bold($3))"
30 #define N_FIELD_CNT     3
31
32 #ifdef HAVE_KRB4
33 #define SVC_NAME        "rcmd"
34 #endif
35
36 /*
37  * Standard warning strings appended as extra fields to
38  * the message body.
39  */
40
41 static char warning[] = "Please detach any filesystems you may have\nattached from this host by typing detach -host %s";
42
43 /*ARGSUSED*/
44 int
45 main(int argc,
46      char *argv[])
47 {
48     ZNotice_t notice;
49     struct hostent *hp;
50     int retval;
51     char hostname[MAXHOSTNAMELEN];
52     char msgbuff[BUFSIZ], message[Z_MAXPKTLEN], *ptr;
53     char scratch[BUFSIZ];
54     char *msg[N_FIELD_CNT];
55 #ifdef HAVE_KRB4
56     char tkt_filename[MAXPATHLEN];
57     char rlm[REALM_SZ];
58     char hn2[MAXHOSTNAMELEN];
59     char *cp;
60 #endif
61
62     if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
63         com_err(argv[0], errno, "while finding hostname");
64         exit(1);
65     }
66
67     if ((hp = gethostbyname(hostname)) != NULL)
68             (void) strcpy(hostname, hp->h_name);
69
70     msg[0] = hostname;
71     msg[1] = message;
72     sprintf(scratch, warning, hostname);
73     msg[2] = scratch;
74
75 #ifdef HAVE_KRB4
76     (void) sprintf(tkt_filename, "/tmp/tkt_zshut_%d", getpid());
77     krb_set_tkt_string(tkt_filename);
78
79     cp = krb_get_phost(hostname);
80     if (cp)
81         (void) strcpy(hn2, cp);
82     else {
83         fprintf(stderr, "%s: can't figure out canonical hostname\n",argv[0]);
84         exit(1);
85     }
86     if (retval = krb_get_lrealm(rlm, 1)) {
87         fprintf(stderr, "%s: can't get local realm: %s\n",
88                 argv[0], krb_get_err_text(retval));
89         exit(1);
90     }
91     if (retval = krb_get_svc_in_tkt(SVC_NAME, hn2, rlm,
92                                     SERVER_SERVICE, SERVER_INSTANCE, 1,
93                                     KEYFILE)) {
94         fprintf(stderr, "%s: can't get tickets: %s\n",
95                 argv[0], krb_get_err_text(retval));
96         exit(1);
97     }
98 #endif
99
100     if ((retval = ZInitialize()) != ZERR_NONE) {
101         com_err(argv[0], retval, "while initializing");
102         exit(1);
103     } 
104
105     ptr = message;
106
107     for (;;) {
108         if (!fgets(msgbuff, sizeof(msgbuff), stdin))
109             break;
110         if ((strlen(msgbuff) + (ptr - message)) > Z_MAXPKTLEN){
111             break;
112         }
113         (void) strcpy(ptr, msgbuff);
114         ptr += strlen(ptr);
115     }
116
117     (void) memset((char *)&notice, 0, sizeof(notice));
118
119     notice.z_kind = N_KIND;
120     notice.z_port = 0;
121     notice.z_class = N_CLASS;
122     notice.z_class_inst = hostname;
123     notice.z_opcode = N_OPCODE;
124     notice.z_sender = 0;
125     notice.z_message_len = 0;
126     notice.z_recipient = "";
127     notice.z_default_format = N_DEF_FORMAT;
128
129     retval = ZSendList(&notice, msg, N_FIELD_CNT, ZAUTH);
130 #ifdef HAVE_KRB4
131     (void) dest_tkt();
132 #endif
133
134     if (retval != ZERR_NONE) {
135         com_err(argv[0], retval, "while sending notice");
136         exit(1);
137     } 
138     return 0;
139 }