]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZLocations.c
11e4a7a9ab4d1bbe3caa155111a6bc2c0073fa67
[1ts-debian.git] / zephyr / lib / ZLocations.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for the ZSetLocation, ZUnsetLocation, and
3  * ZFlushMyLocations functions.
4  *
5  *      Created by:     Robert French
6  *
7  *      $Id$
8  *
9  *      Copyright (c) 1987,1988,1991 by the Massachusetts Institute of Technology.
10  *      For copying and distribution information, see the file
11  *      "mit-copyright.h". 
12  */
13
14 #ifndef lint
15 static char rcsid_ZLocations_c[] =
16     "$Zephyr: /afs/athena.mit.edu/astaff/project/zephyr/src/lib/RCS/ZLocations.c,v 1.30 90/12/20 03:04:39 raeburn Exp $";
17 #endif
18
19 #include <internal.h>
20
21 #include <pwd.h>
22
23 static char host[MAXHOSTNAMELEN], mytty[MAXPATHLEN];
24 static int location_info_set = 0;
25
26 Code_t ZInitLocationInfo(hostname, tty)
27     char *hostname;
28     char *tty;
29 {
30     char *ttyp, *p;
31     struct hostent *hent;
32
33     if (hostname) {
34         strcpy(host, hostname);
35     } else {
36         if (gethostname(host, MAXHOSTNAMELEN) < 0)
37             return (errno);
38         hent = gethostbyname(host);
39         if (hent) {
40            (void) strncpy(host, hent->h_name, sizeof(host));
41            host[sizeof(host) - 1] = '\0';
42         }
43     }
44     if (tty) {
45         strcpy(mytty, tty);
46     } else {
47         ttyp = ttyname(0);
48         if (ttyp && *ttyp) {
49             p = strchr(ttyp + 1, '/');
50             strcpy(mytty, (p) ? p + 1 : ttyp);
51         } else {
52             strcpy(mytty, "unknown");
53         }
54     }
55     location_info_set = 1;
56     return (ZERR_NONE);
57 }
58
59 Code_t ZSetLocation(exposure)
60     char *exposure;
61 {
62     return (Z_SendLocation(LOGIN_CLASS, exposure, ZAUTH, 
63                            "$sender logged in to $1 on $3 at $2"));
64 }
65
66 Code_t ZUnsetLocation()
67 {
68     return (Z_SendLocation(LOGIN_CLASS, LOGIN_USER_LOGOUT, ZNOAUTH, 
69                            "$sender logged out of $1 on $3 at $2"));
70 }
71
72 Code_t ZFlushMyLocations()
73 {
74     return (Z_SendLocation(LOGIN_CLASS, LOGIN_USER_FLUSH, ZAUTH, ""));
75 }
76
77 char *ZParseExposureLevel(text)
78      char *text;
79 {
80     if (!strcasecmp(text, EXPOSE_NONE))
81         return (EXPOSE_NONE);
82     else if (!strcasecmp(text, EXPOSE_OPSTAFF))
83         return (EXPOSE_OPSTAFF);
84     else if (!strcasecmp(text, EXPOSE_REALMVIS))
85         return (EXPOSE_REALMVIS);
86     else if (!strcasecmp(text, EXPOSE_REALMANN))
87         return (EXPOSE_REALMANN);
88     else if (!strcasecmp(text, EXPOSE_NETVIS))
89         return (EXPOSE_NETVIS);
90     else if (!strcasecmp(text, EXPOSE_NETANN))
91         return (EXPOSE_NETANN);
92     else
93         return(NULL);
94 }
95
96 /* lifted from lib/ZSendPkt.c wait_for_hmack, but waits for SERVACK instead */
97 static int wait_for_srvack(notice, uid)
98     ZNotice_t *notice;
99     ZUnique_Id_t *uid;
100 {
101     return (notice->z_kind == SERVACK && ZCompareUID(&notice->z_uid, uid));
102 }
103
104 Code_t Z_SendLocation(class, opcode, auth, format)
105     char *class;
106     char *opcode;
107     Z_AuthProc auth;
108     char *format;
109 {
110     int retval;
111     time_t ourtime;
112     ZNotice_t notice, retnotice;
113     char *bptr[3];
114     struct hostent *hent;
115     short wg_port = ZGetWGPort();
116
117     if (!location_info_set)
118         ZInitLocationInfo(NULL, NULL);
119
120     memset((char *)&notice, 0, sizeof(notice));
121     notice.z_kind = ACKED;
122     notice.z_port = (u_short) ((wg_port == -1) ? 0 : wg_port);
123     notice.z_class = class;
124     notice.z_class_inst = ZGetSender();
125     notice.z_opcode = opcode;
126     notice.z_sender = 0;
127     notice.z_recipient = "";
128     notice.z_num_other_fields = 0;
129     notice.z_default_format = format;
130
131     bptr[0] = host;
132     ourtime = time((time_t *)0);
133     bptr[1] = ctime(&ourtime);
134     bptr[1][strlen(bptr[1])-1] = '\0';
135     bptr[2] = mytty;
136
137     if ((retval = ZSendList(&notice, bptr, 3, auth)) != ZERR_NONE)
138         return (retval);
139
140     retval = Z_WaitForNotice (&retnotice, wait_for_srvack, &notice.z_uid,
141                               SRV_TIMEOUT);
142     if (retval != ZERR_NONE)
143       return retval;
144
145     if (retnotice.z_kind == SERVNAK) {
146         if (!retnotice.z_message_len) {
147             ZFreeNotice(&retnotice);
148             return (ZERR_SERVNAK);
149         }
150         if (!strcmp(retnotice.z_message, ZSRVACK_NOTSENT)) {
151             ZFreeNotice(&retnotice);
152             return (ZERR_AUTHFAIL);
153         }
154         if (!strcmp(retnotice.z_message, ZSRVACK_FAIL)) {
155             ZFreeNotice(&retnotice);
156             return (ZERR_LOGINFAIL);
157         }
158         ZFreeNotice(&retnotice);
159         return (ZERR_SERVNAK);
160     } 
161         
162     if (retnotice.z_kind != SERVACK) {
163         ZFreeNotice(&retnotice);
164         return (ZERR_INTERNAL);
165     }
166
167     if (!retnotice.z_message_len) {
168         ZFreeNotice(&retnotice);
169         return (ZERR_INTERNAL);
170     }
171
172     if (strcmp(retnotice.z_message, ZSRVACK_SENT) &&
173         strcmp(retnotice.z_message, ZSRVACK_NOTSENT)) {
174         ZFreeNotice(&retnotice);
175         return (ZERR_INTERNAL);
176     }
177
178     ZFreeNotice(&retnotice);
179         
180     return (ZERR_NONE);
181 }