]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/clients/zshutdown_notify/zshutdown_notify.c
r4275@bucket (orig r265): kcr | 2008-01-21 02:57:32 -0500
[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     retval = krb_get_lrealm(rlm, 1);
87     if (retval) {
88         fprintf(stderr, "%s: can't get local realm: %s\n",
89                 argv[0], krb_get_err_text(retval));
90         exit(1);
91     }
92     retval = krb_get_svc_in_tkt(SVC_NAME, hn2, rlm,
93                                 SERVER_SERVICE, SERVER_INSTANCE, 1,
94                                 KEYFILE);
95     if (retval) {
96         fprintf(stderr, "%s: can't get tickets: %s\n",
97                 argv[0], krb_get_err_text(retval));
98         exit(1);
99     }
100 #endif
101
102     if ((retval = ZInitialize()) != ZERR_NONE) {
103         com_err(argv[0], retval, "while initializing");
104         exit(1);
105     } 
106
107     ptr = message;
108
109     for (;;) {
110         if (!fgets(msgbuff, sizeof(msgbuff), stdin))
111             break;
112         if ((strlen(msgbuff) + (ptr - message)) > Z_MAXPKTLEN){
113             break;
114         }
115         (void) strcpy(ptr, msgbuff);
116         ptr += strlen(ptr);
117     }
118
119     (void) memset((char *)&notice, 0, sizeof(notice));
120
121     notice.z_kind = N_KIND;
122     notice.z_port = 0;
123     notice.z_class = N_CLASS;
124     notice.z_class_inst = hostname;
125     notice.z_opcode = N_OPCODE;
126     notice.z_sender = 0;
127     notice.z_message_len = 0;
128     notice.z_recipient = "";
129     notice.z_default_format = N_DEF_FORMAT;
130
131     retval = ZSendList(&notice, msg, N_FIELD_CNT, ZAUTH);
132 #ifdef HAVE_KRB4
133     (void) dest_tkt();
134 #endif
135
136     if (retval != ZERR_NONE) {
137         com_err(argv[0], retval, "while sending notice");
138         exit(1);
139     } 
140     return 0;
141 }