]> asedeno.scripts.mit.edu Git - 1ts-debian.git/blob - zephyr/server/zserver.h
need automake as a build-dep, even though we don't use most of it
[1ts-debian.git] / zephyr / server / zserver.h
1 #ifndef __ZSERVER_H__
2 #define __ZSERVER_H__
3 /* This file is part of the Project Athena Zephyr Notification System.
4  * It contains declarations for use in the server.
5  *
6  *      Created by:     John T. Kohl
7  *
8  *      $Id$
9  *
10  *      Copyright (c) 1987,1988,1991 by the Massachusetts Institute of Technology.
11  *      For copying and distribution information, see the file
12  *      "mit-copyright.h".
13  */
14
15 #include <zephyr/mit-copyright.h>
16
17 #include <internal.h>
18
19 #include <arpa/inet.h>
20
21 #include "zsrv_err.h"
22
23 #include "timer.h"
24 #include "zsrv_conf.h"                  /* configuration params */
25
26 #include "zstring.h"
27 #include "access.h"
28 #include "acl.h"
29 #include "utf8proc.h"
30
31
32 /* Kerberos-specific library interfaces used only by the server. */
33 #ifdef HAVE_KRB5
34 extern krb5_keyblock *__Zephyr_keyblock;
35 #define ZGetSession() (__Zephyr_keyblock)
36 void ZSetSession(krb5_keyblock *keyblock);
37 krb5_error_code Z_krb5_init_keyblock(krb5_context, krb5_enctype, size_t,
38         krb5_keyblock **);
39 #endif
40
41 #ifdef HAVE_KRB4
42 void ZSetSessionDES(C_Block *key);
43
44 Code_t ZFormatAuthenticNotice(ZNotice_t*, char*, int, int*, C_Block);
45
46 #ifndef HAVE_KRB5
47 extern C_Block __Zephyr_session;
48 #define ZGetSession() (__Zephyr_session)
49 #endif
50 #endif
51
52 /* For krb_rd_req prototype and definition. */
53 #ifndef KRB_INT32
54 #define KRB_INT32 ZEPHYR_INT32
55 #endif
56
57 /* Current time as cached by main(); use instead of time(). */
58 #define NOW t_local.tv_sec
59
60 #ifdef HAVE_KRB4
61 /* Kerberos shouldn't stick us with array types... */
62 typedef struct {
63     des_key_schedule s;
64 } Sched;
65 #endif
66
67 typedef struct _Destination Destination;
68 typedef struct _Destlist Destlist;
69 typedef struct _ZRealm ZRealm;
70 typedef struct _ZRealmname ZRealmname;
71 typedef enum _ZRealm_state ZRealm_state;
72 typedef struct _Client Client;
73 typedef struct _Triplet Triplet;
74 typedef enum _Server_state Server_state;
75 typedef struct _Unacked Unacked;
76 typedef struct _Pending Pending;
77 typedef struct _Server Server;
78 typedef enum _Sent_type Sent_type;
79 typedef struct _Statistic Statistic;
80
81 struct _Destination {
82     String              *classname;
83     String              *inst;
84     String              *recip;
85 };
86
87 struct _Destlist {
88     Destination dest;
89     struct _Destlist    *next, **prev_p;
90 };
91
92 enum _ZRealm_state {
93     REALM_UP,                           /* ZRealm is up */
94     REALM_TARDY,                        /* ZRealm due for a hello XXX */
95     REALM_DEAD,                         /* ZRealm is considered dead */
96     REALM_STARTING                      /* ZRealm is between dead and up */
97 };
98
99 struct _ZRealm {
100     char name[REALM_SZ];
101     int count;
102     struct sockaddr_in *addrs;
103     int idx;                            /* which server we are connected to */
104     Destlist *subs;                     /* what their clients sub to */
105     Destlist *remsubs;                  /* our subs on their end */
106     Client *client;
107     int child_pid;
108     int have_tkt;
109     ZRealm_state state;
110 };
111
112 struct _ZRealmname {
113     char name[REALM_SZ];
114     char **servers;
115     int nused;
116     int nservers;
117 };
118
119 struct _Client {
120     struct sockaddr_in  addr;           /* ipaddr/port of client */
121     Destlist            *subs   ;       /* subscriptions */
122 #ifdef HAVE_KRB5
123     krb5_keyblock       *session_keyblock;
124 #else
125 #ifdef HAVE_KRB4
126     C_Block             session_key;    /* session key for this client */
127 #endif /* HAVE_KRB4 */
128 #endif
129     String              *principal;     /* krb principal of user */
130     int                 last_send;      /* Counter for last sent packet. */
131     time_t              last_ack;       /* Time of last received ack */
132     ZRealm              *realm;
133     struct _Client      *next, **prev_p;
134 };
135
136 struct _Triplet {
137     Destination         dest;
138     Acl                 *acl;
139     Client              **clients;
140     int                 clients_size;
141     struct _Triplet     *next, **prev_p;
142 };
143
144 enum _Server_state {
145     SERV_UP,                            /* Server is up */
146     SERV_TARDY,                         /* Server due for a hello */
147     SERV_DEAD,                          /* Server is considered dead */
148     SERV_STARTING                       /* Server is between dead and up */
149 };
150
151 struct _Unacked {
152     Timer               *timer;         /* timer for retransmit */
153     Client              *client;        /* responsible client, or NULL */
154     short               rexmits;        /* number of retransmits */
155     short               packsz;         /* size of packet */
156     char                *packet;        /* ptr to packet */
157     ZUnique_Id_t        uid;            /* uid of packet */
158     struct sockaddr_in  ack_addr;
159     union {                             /* address to send to */
160         struct sockaddr_in addr;        /* client address */
161         int     srv_idx;                /* index of server */
162         struct {
163             int rlm_idx;                /* index of realm */
164             int rlm_srv_idx;            /* index of server in realm */
165         } rlm;
166     } dest;
167     struct _Unacked *next, **prev_p;
168 };
169
170 struct _Pending {
171     char                *packet;        /* the notice (in pkt form) */
172     short               len;            /* len of pkt */
173     unsigned int        auth;           /* whether it is authentic */
174     struct sockaddr_in who;             /* the addr of the sender */
175     struct _Pending *next;
176 };
177
178 struct _Server {
179     Server_state        state;          /* server's state */
180     struct sockaddr_in  addr;           /* server's address */
181     long                timeout;        /* Length of timeout in sec */
182     Timer               *timer;         /* timer for this server */
183     Pending             *queue;         /* queue of packets to send
184                                            to this server when done dumping */
185     Pending             *queue_last;    /* last packet on queue */
186     short               num_hello_sent; /* number of hello's sent */
187     unsigned int        dumping;        /* 1 if dumping, so we should queue */
188     char                addr_str[16];   /* text version of address */
189 };
190
191 enum _Sent_type {
192     NOT_SENT,                           /* message was not xmitted */
193     SENT,                               /* message was xmitted */
194     AUTH_FAILED,                        /* authentication failed */
195     NOT_FOUND                           /* user not found for uloc */
196 };
197
198 /* statistics gathering */
199 struct _Statistic {
200     int                 val;
201     char                *str;
202 };
203
204 /* Function declarations */
205
206 /* These macros instantiate inline functions that do the work of the formder
207    LIST_INSERT and LIST_DELETE functions, which unfortunately triggered gcc's
208    pedanticism.  The comment before the *former* macros was: */
209 /* These macros are for insertion into and deletion from a singly-linked list
210  * with back pointers to the previous element's next pointer.  In order to
211  * make these macros act like expressions, they use the comma operator for
212  * sequenced evaluations of assignment, and "a && b" for "evaluate assignment
213  * b if expression a is true". */
214
215 #define MAKE_LIST_INSERT(type) inline static void type##_insert(type **head, type *elem) \
216     {\
217         (elem)->next = *(head);                                 \
218         if(*head) (*(head))->prev_p = &(elem)->next;            \
219         (*head) = (elem);                                       \
220         (elem)->prev_p = (head);                                \
221     }
222
223 #define MAKE_LIST_DELETE(type) inline static void type##_delete(type *elem) \
224     {\
225         *(elem)->prev_p = (elem)->next;                         \
226         if((elem)->next) (elem)->next->prev_p = (elem)->prev_p; \
227     }
228
229 MAKE_LIST_INSERT(Destlist);
230 MAKE_LIST_DELETE(Destlist);
231 MAKE_LIST_INSERT(Client);
232 MAKE_LIST_DELETE(Client);
233 MAKE_LIST_INSERT(Triplet);
234 MAKE_LIST_DELETE(Triplet);
235 MAKE_LIST_INSERT(Unacked);
236 MAKE_LIST_DELETE(Unacked);
237
238 /* found in bdump.c */
239 void bdump_get(ZNotice_t *notice, int auth, struct sockaddr_in *who,
240                     Server *server);
241 void bdump_send(void);
242 void bdump_offer(struct sockaddr_in *who);
243 Code_t bdump_send_list_tcp(ZNotice_Kind_t kind, struct sockaddr_in *addr,
244                                 char *class_name, char *inst, char *opcode,
245                                 char *sender, char *recip, char **lyst,
246                                 int num);
247 int get_tgt(void);
248
249 /* found in class.c */
250 extern String *class_control, *class_admin, *class_hm;
251 extern String *class_ulogin, *class_ulocate;
252 int ZDest_eq(Destination *d1, Destination *d2);
253 Code_t triplet_register(Client *client, Destination *dest, ZRealm *realm);
254 Code_t triplet_deregister(Client *client, Destination *dest,
255                                ZRealm *realm);
256 Code_t class_restrict(char *class_name, Acl *acl);
257 Code_t class_setup_restricted(char *class_name, Acl *acl);
258 Client **triplet_lookup(Destination *dest);
259 Acl *class_get_acl(String *class_name);
260 int dest_eq(Destination *d1, Destination *d2);
261 int order_dest_strings(Destination *d1, Destination *d2);
262 void triplet_dump_subs(FILE *fp);
263
264 /* found in client.c */
265 Code_t client_register(ZNotice_t *notice, struct in_addr *host,
266                             Client **client_p, int wantdefaults);
267 void client_deregister(Client *client, int flush);
268 void client_flush_host(struct in_addr *host);
269 void client_dump_clients(FILE *fp);
270 Client *client_find(struct in_addr *host, unsigned int port);
271 Code_t client_send_clients(void);
272
273 /* found in common.c */
274 char *strsave(const char *str);
275 unsigned long hash (const char *);
276 void dump_quote(char *p, FILE *fp);
277 void notice_extract_address(ZNotice_t *notice, struct sockaddr_in *addr);
278
279 /* found in dispatch.c */
280 void handle_packet(void);
281 void clt_ack(ZNotice_t *notice, struct sockaddr_in *who, Sent_type sent);
282 void nack_release(Client *client);
283 void sendit(ZNotice_t *notice, int auth, struct sockaddr_in *who,
284                  int external);
285 void rexmit(void *);
286 void xmit(ZNotice_t *notice, struct sockaddr_in *dest, int auth,
287                Client *client);
288 Code_t hostm_dispatch(ZNotice_t *notice, int auth,
289                            struct sockaddr_in *who, Server *server);
290 Code_t control_dispatch(ZNotice_t *notice, int auth,
291                              struct sockaddr_in *who, Server *server);
292 Code_t xmit_frag(ZNotice_t *notice, char *buf, int len, int waitforack);
293 void hostm_shutdown(void);
294
295 /* found in kstuff.c */
296 Code_t ZCheckSrvAuthentication(ZNotice_t *notice, struct sockaddr_in *from, char *realm);
297 #if defined(HAVE_KRB4) || defined(HAVE_KRB5)
298 Code_t ReadKerberosData(int, int *, char **, int *);
299 void sweep_ticket_hash_table(void *);
300 #endif
301 #ifdef HAVE_KRB4
302 int GetKerberosData (int, struct in_addr, AUTH_DAT *, char *, char *);
303 Code_t SendKerberosData (int, KTEXT, char *, char *);
304 #endif
305 #ifdef HAVE_KRB5
306 Code_t SendKrb5Data(int, krb5_data *);
307 Code_t GetKrb5Data(int, krb5_data *);
308 #endif
309
310 /* found in server.c */
311 void server_timo(void *which);
312 void server_dump_servers(FILE *fp);
313 void server_init(void);
314 void server_shutdown(void);
315 void server_forward(ZNotice_t *notice, int auth,
316                          struct sockaddr_in *who);
317 void server_kill_clt(Client *client);
318 void server_pending_free(Pending *pending);
319 void server_self_queue(ZNotice_t *, int, struct sockaddr_in *);
320 void server_send_queue(Server *);
321 void server_reset(void);
322 Server *server_which_server(struct sockaddr_in *who);
323 Pending *server_dequeue(Server *server);
324 Code_t server_dispatch(ZNotice_t *notice, int auth,
325                             struct sockaddr_in *who);
326 Code_t server_adispatch(ZNotice_t *notice, int auth,
327                              struct sockaddr_in *who, Server *server);
328
329 /* found in subscr.c */
330 Code_t subscr_foreign_user(ZNotice_t *, struct sockaddr_in *, Server *, ZRealm *);
331 Code_t subscr_cancel(struct sockaddr_in *sin, ZNotice_t *notice);
332 Code_t subscr_subscribe(Client *who, ZNotice_t *notice, Server *server);
333 Code_t subscr_send_subs(Client *client);
334 void subscr_cancel_client(Client *client);
335 void subscr_sendlist(ZNotice_t *notice, int auth,
336                           struct sockaddr_in *who);
337 void subscr_dump_subs(FILE *fp, Destlist *subs);
338 void subscr_reset(void);
339 Code_t subscr_def_subs(Client *who);
340 Code_t subscr_realm(ZRealm *, ZNotice_t *);
341 Code_t subscr_send_realm_subs(ZRealm *);
342 Code_t subscr_realm_cancel(struct sockaddr_in *, ZNotice_t *, ZRealm *);
343
344 /* found in uloc.c */
345 void uloc_hflush(struct in_addr *addr);
346 void uloc_flush_client(struct sockaddr_in *sin);
347 void uloc_dump_locs(FILE *fp);
348 Code_t ulogin_dispatch(ZNotice_t *notice, int auth,
349                             struct sockaddr_in *who, Server *server);
350 Code_t ulocate_dispatch(ZNotice_t *notice, int auth,
351                              struct sockaddr_in *who, Server *server);
352 Code_t uloc_send_locations(void);
353 void ulogin_relay_locate(ZNotice_t *, struct sockaddr_in *);
354 void ulogin_realm_locate(ZNotice_t *, struct sockaddr_in *, ZRealm *);
355
356 /* found in realm.c */
357 int realm_sender_in_realm(const char *realm, char *sender);
358 int realm_bound_for_realm(const char *realm, char *recip);
359 ZRealm *realm_which_realm(struct sockaddr_in *who);
360 ZRealm *realm_get_realm_by_name(char *name);
361 ZRealm *realm_get_realm_by_pid(int);
362 void realm_handoff(ZNotice_t *, int, struct sockaddr_in *, ZRealm *, int);
363 const char *realm_expand_realm(char *);
364 void realm_init(void);
365 Code_t ZCheckZRealmAuthentication(ZNotice_t *, struct sockaddr_in *,
366                                       char *);
367 Code_t realm_control_dispatch(ZNotice_t *, int, struct sockaddr_in *,
368                                    Server *, ZRealm *);
369 void realm_shutdown(void);
370 void realm_deathgram(Server *);
371 Code_t realm_send_realms(void);
372 Code_t realm_dispatch(ZNotice_t *, int, struct sockaddr_in *, Server *);
373 void realm_wakeup(void);
374 void kill_realm_pids(void);
375 void realm_dump_realms(FILE *);
376
377 /* found in version.c */
378 char *get_version(void);
379
380 /* found in access.c */
381 int access_check(char *, Acl *, Access);
382
383 /* global identifiers */
384
385 /* found in main.c */
386 int packets_waiting(void);
387 extern struct sockaddr_in srv_addr;     /* server socket address */
388 extern unsigned short hm_port;          /* host manager receiver port */
389 extern unsigned short hm_srv_port;      /* host manager server sending port */
390 extern int srv_socket;                  /* dgram sockets for clients
391                                            and other servers */
392 extern int bdump_socket;                /* brain dump socket
393                                            (closed most of the time) */
394
395 extern fd_set interesting;              /* the file descrips we are listening
396                                          to right now */
397 extern int nfds;                        /* number to look at in select() */
398 extern int zdebug;
399 extern char myname[];                   /* domain name of this host */
400 extern char list_file[];
401 #ifdef HAVE_KRB5
402 extern char keytab_file[];
403 extern krb5_ccache Z_krb5_ccache;
404 #endif
405 #ifdef HAVE_KRB4
406 extern char srvtab_file[];
407 extern char my_realm[];
408 #endif
409 extern char acl_dir[];
410 extern char subs_file[];
411 extern const char version[];
412 extern u_long npackets;                 /* num of packets processed */
413 extern time_t uptime;                   /* time we started */
414 extern struct in_addr my_addr;
415 extern struct timeval t_local;          /* current time */
416
417 /* found in bdump.c */
418 extern int bdumping;                    /* are we processing a bdump packet? */
419 extern int bdump_concurrent;            /* set while processing a packet
420                                          * concurrently during a braindump. */
421
422 /* found in dispatch.c */
423 extern Statistic i_s_ctls, i_s_logins, i_s_admins, i_s_locates;
424 extern int rexmit_times[];
425
426 /* found in server.c */
427 extern Server *otherservers;            /* array of servers */
428 extern int me_server_idx;               /* me (in the array of servers) */
429 extern int nservers;                    /* number of other servers*/
430
431 /* found in subscr.c */
432 extern String *empty;
433 extern String *wildcard_instance;
434
435 extern ZRealm *otherrealms;
436 extern int nrealms;
437
438 extern struct in_addr my_addr;  /* my inet address */
439
440 #define class_is_control(classname) (classname == class_control)
441 #define class_is_admin(classname) (classname == class_admin)
442 #define class_is_hm(classname) (classname == class_hm)
443 #define class_is_ulogin(classname) (classname == class_ulogin)
444 #define class_is_ulocate(classname) (classname == class_ulocate)
445
446 #define ADMIN_HELLO     "HELLO"         /* Opcode: hello, are you there */
447 #define ADMIN_IMHERE    "IHEARDYOU"     /* Opcode: yes, I am here */
448 #define ADMIN_SHUTDOWN  "GOODBYE"       /* Opcode: I am shutting down */
449 #define ADMIN_BDUMP     "DUMP_AVAIL"    /* Opcode: I will give you a dump */
450 #define ADMIN_DONE      "DUMP_DONE"     /* Opcode: brain dump for this server
451                                            is complete */
452 #define ADMIN_NEWCLT    "NEXT_CLIENT"   /* Opcode: this is a new client */
453 #define ADMIN_KILL_CLT  "KILL_CLIENT"   /* Opcode: client is dead, remove */
454 #define ADMIN_STATUS    "STATUS"        /* Opcode: please send status */
455
456 #define ADMIN_NEWREALM  "NEXT_REALM"    /* Opcode: this is a new realm */
457 #define REALM_REQ_LOCATE "REQ_LOCATE"   /* Opcode: request a location */
458 #define REALM_ANS_LOCATE "ANS_LOCATE"   /* Opcode: answer to location */
459 #define REALM_BOOT      "SENDSUBS"      /* Opcode: first server in realm */
460
461 /* me_server_idx is the index into otherservers of this server descriptor. */
462 /* the 'limbo' server is always the first server */
463
464 #define me_server       (&otherservers[me_server_idx])
465 #define limbo_server_idx()      (0)
466 #define limbo_server    (&otherservers[limbo_server_idx()])
467
468 #define msgs_queued()   (ZQLength() || otherservers[me_server_idx].queue)
469
470 #define ack(a,b)        clt_ack(a,b,SENT)
471 #define nack(a,b)       clt_ack(a,b,NOT_SENT)
472
473 #define min(a,b)        ((a) < (b) ? (a) : (b))
474 #define max(a,b)        ((a) > (b) ? (a) : (b))
475
476 #define START_CRITICAL_CODE
477 #define END_CRITICAL_CODE
478
479 /* the instance that matches all instances */
480 #define WILDCARD_INSTANCE       "*"
481
482 /* debugging macros */
483 #ifdef DEBUG
484 #define zdbug(s1)       if (zdebug) syslog s1;
485 #else /* !DEBUG */
486 #define zdbug(s1)
487 #endif /* DEBUG */
488
489 #endif /* !__ZSERVER_H__ */