]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/lib/ZInit.c
024e2b29a291846728ddd9bf133553cc9cf03fab
[1ts-debian.git] / zephyr / lib / ZInit.c
1 /* This file is part of the Project Athena Zephyr Notification System.
2  * It contains source for the ZInitialize function.
3  *
4  *      Created by:     Robert French
5  *
6  *      $Id: ZInit.c,v 1.27 1999/01/22 23:19:14 ghudson Exp $
7  *
8  *      Copyright (c) 1987, 1991 by the Massachusetts Institute of Technology.
9  *      For copying and distribution information, see the file
10  *      "mit-copyright.h". 
11  */
12
13 #ifndef lint
14 static char rcsid_ZInitialize_c[] =
15     "$Zephyr: /afs/athena.mit.edu/astaff/project/zephyr/src/lib/RCS/ZInitialize.c,v 1.17 89/05/30 18:11:25 jtkohl Exp $";
16 #endif
17
18 #include <internal.h>
19
20 #include <sys/socket.h>
21 #ifdef HAVE_KRB4
22 #include <krb_err.h>
23 #endif
24 #ifdef HAVE_KRB5
25 #include <krb5.h>
26 #endif
27 #ifdef HAVE_KRB5_ERR_H
28 #include <krb5_err.h>
29 #endif
30
31 #ifndef INADDR_NONE
32 #define INADDR_NONE 0xffffffff
33 #endif
34
35 Code_t ZInitialize()
36 {
37     struct servent *hmserv;
38     struct hostent *hostent;
39     char addr[4], hostname[MAXHOSTNAMELEN];
40     struct in_addr servaddr;
41     struct sockaddr_in sin;
42     int s, sinsize = sizeof(sin);
43     Code_t code;
44     ZNotice_t notice;
45 #ifdef HAVE_KRB5
46     char **krealms = NULL;
47 #else
48 #ifdef HAVE_KRB4
49     char *krealm = NULL;
50     int krbval;
51     char d1[ANAME_SZ], d2[INST_SZ];
52 #endif
53 #endif
54
55 #ifdef HAVE_KRB4
56     initialize_krb_error_table();
57 #endif
58 #ifdef HAVE_KRB5
59     initialize_krb5_error_table();
60 #endif
61
62     initialize_zeph_error_table();
63     
64     (void) memset((char *)&__HM_addr, 0, sizeof(__HM_addr));
65
66     __HM_addr.sin_family = AF_INET;
67
68     /* Set up local loopback address for HostManager */
69     addr[0] = 127;
70     addr[1] = 0;
71     addr[2] = 0;
72     addr[3] = 1;
73
74     hmserv = (struct servent *)getservbyname(HM_SVCNAME, "udp");
75     __HM_addr.sin_port = (hmserv) ? hmserv->s_port : HM_SVC_FALLBACK;
76
77     (void) memcpy((char *)&__HM_addr.sin_addr, addr, 4);
78
79     __HM_set = 0;
80
81     /* Initialize the input queue */
82     __Q_Tail = NULL;
83     __Q_Head = NULL;
84     
85 #ifdef HAVE_KRB5
86     if ((code = krb5_init_context(&Z_krb5_ctx)))
87         return(code);
88 #endif
89
90     /* if the application is a server, there might not be a zhm.  The
91        code will fall back to something which might not be "right",
92        but this is is ok, since none of the servers call krb_rd_req. */
93
94     servaddr.s_addr = INADDR_NONE;
95     if (! __Zephyr_server) {
96        if ((code = ZOpenPort(NULL)) != ZERR_NONE)
97           return(code);
98
99        if ((code = ZhmStat(NULL, &notice)) != ZERR_NONE)
100           return(code);
101
102        ZClosePort();
103
104        /* the first field, which is NUL-terminated, is the server name.
105           If this code ever support a multiplexing zhm, this will have to
106           be made smarter, and probably per-message */
107
108 #ifdef HAVE_KRB5
109        code = krb5_get_host_realm(Z_krb5_ctx, notice.z_message, &krealms);
110        if (code)
111          return(code);
112 #else
113 #ifdef HAVE_KRB4
114        krealm = krb_realmofhost(notice.z_message);
115 #endif
116 #endif
117        hostent = gethostbyname(notice.z_message);
118        if (hostent && hostent->h_addrtype == AF_INET)
119            memcpy(&servaddr, hostent->h_addr, sizeof(servaddr));
120
121        ZFreeNotice(&notice);
122     }
123
124 #ifdef HAVE_KRB5
125     if (krealms) {
126       strcpy(__Zephyr_realm, krealms[0]);
127       krb5_free_host_realm(Z_krb5_ctx, krealms);
128     } else {
129       char *p; /* XXX define this somewhere portable */
130       /* XXX check ticket file here */
131       code = krb5_get_default_realm(Z_krb5_ctx, &p);
132       strcpy(__Zephyr_realm, p);
133       krb5_free_default_realm(Z_krb5_ctx, p);
134       if (code)
135         return code;
136     }
137 #else
138 #ifdef HAVE_KRB4
139     if (krealm) {
140         strcpy(__Zephyr_realm, krealm);
141     } else if ((krb_get_tf_fullname(TKT_FILE, d1, d2, __Zephyr_realm)
142                 != KSUCCESS) &&
143                ((krbval = krb_get_lrealm(__Zephyr_realm, 1)) != KSUCCESS)) {
144         return (krbval);
145     }
146 #else
147     strcpy(__Zephyr_realm, "local-realm");
148 #endif
149 #endif
150
151     __My_addr.s_addr = INADDR_NONE;
152     if (servaddr.s_addr != INADDR_NONE) {
153         /* Try to get the local interface address by connecting a UDP
154          * socket to the server address and getting the local address.
155          * Some broken operating systems (e.g. Solaris 2.0-2.5) yield
156          * INADDR_ANY (zero), so we have to check for that. */
157         s = socket(AF_INET, SOCK_DGRAM, 0);
158         if (s != -1) {
159             memset(&sin, 0, sizeof(sin));
160             sin.sin_family = AF_INET;
161             memcpy(&sin.sin_addr, &servaddr, sizeof(servaddr));
162             sin.sin_port = HM_SRV_SVC_FALLBACK;
163             if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) == 0
164                 && getsockname(s, (struct sockaddr *) &sin, &sinsize) == 0
165                 && sin.sin_addr.s_addr != 0)
166                 memcpy(&__My_addr, &sin.sin_addr, sizeof(__My_addr));
167             close(s);
168         }
169     }
170     if (__My_addr.s_addr == INADDR_NONE) {
171         /* We couldn't figure out the local interface address by the
172          * above method.  Try by resolving the local hostname.  (This
173          * is a pretty broken thing to do, and unfortunately what we
174          * always do on server machines.) */
175         if (gethostname(hostname, sizeof(hostname)) == 0) {
176             hostent = gethostbyname(hostname);
177             if (hostent && hostent->h_addrtype == AF_INET)
178                 memcpy(&__My_addr, hostent->h_addr, sizeof(__My_addr));
179         }
180     }
181     /* If the above methods failed, zero out __My_addr so things will
182      * sort of kind of work. */
183     if (__My_addr.s_addr == INADDR_NONE)
184         __My_addr.s_addr = 0;
185
186     /* Get the sender so we can cache it */
187     (void) ZGetSender();
188
189     return (ZERR_NONE);
190 }
191