]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/zshutdown_notify/zshutdown_notify.c
Initial revision
[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: zshutdown_notify.c,v 1.14 1997/10/25 21:47:15 ghudson Exp $
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: zshutdown_notify.c,v 1.14 1997/10/25 21:47:15 ghudson Exp $";
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 main(argc,argv)
45     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     extern char *krb_get_phost();
61 #endif
62
63     if (gethostname(hostname, MAXHOSTNAMELEN) < 0) {
64         com_err(argv[0], errno, "while finding hostname");
65         exit(1);
66     }
67
68     if ((hp = gethostbyname(hostname)) != NULL)
69             (void) strcpy(hostname, hp->h_name);
70
71     msg[0] = hostname;
72     msg[1] = message;
73     sprintf(scratch, warning, hostname);
74     msg[2] = scratch;
75
76 #ifdef HAVE_KRB4
77     (void) sprintf(tkt_filename, "/tmp/tkt_zshut_%d", getpid());
78     krb_set_tkt_string(tkt_filename);
79
80     cp = krb_get_phost(hostname);
81     if (cp)
82         (void) strcpy(hn2, cp);
83     else {
84         fprintf(stderr, "%s: can't figure out canonical hostname\n",argv[0]);
85         exit(1);
86     }
87     if (retval = krb_get_lrealm(rlm, 1)) {
88         fprintf(stderr, "%s: can't get local realm: %s\n",
89                 argv[0], krb_get_err_text(retval));
90         exit(1);
91     }
92     if (retval = krb_get_svc_in_tkt(SVC_NAME, hn2, rlm,
93                                     SERVER_SERVICE, SERVER_INSTANCE, 1,
94                                     KEYFILE)) {
95         fprintf(stderr, "%s: can't get tickets: %s\n",
96                 argv[0], krb_get_err_text(retval));
97         exit(1);
98     }
99 #endif
100
101     if ((retval = ZInitialize()) != ZERR_NONE) {
102         com_err(argv[0], retval, "while initializing");
103         exit(1);
104     } 
105
106     ptr = message;
107
108     for (;;) {
109         if (!fgets(msgbuff, sizeof(msgbuff), stdin))
110             break;
111         if ((strlen(msgbuff) + (ptr - message)) > Z_MAXPKTLEN){
112             break;
113         }
114         (void) strcpy(ptr, msgbuff);
115         ptr += strlen(ptr);
116     }
117
118     (void) memset((char *)&notice, 0, sizeof(notice));
119
120     notice.z_kind = N_KIND;
121     notice.z_port = 0;
122     notice.z_class = N_CLASS;
123     notice.z_class_inst = hostname;
124     notice.z_opcode = N_OPCODE;
125     notice.z_sender = 0;
126     notice.z_message_len = 0;
127     notice.z_recipient = "";
128     notice.z_default_format = N_DEF_FORMAT;
129
130     retval = ZSendList(&notice, msg, N_FIELD_CNT, ZAUTH);
131 #ifdef HAVE_KRB4
132     (void) dest_tkt();
133 #endif
134
135     if (retval != ZERR_NONE) {
136         com_err(argv[0], retval, "while sending notice");
137         exit(1);
138     } 
139     return 0;
140 }