1 /* This file is part of the Project Athena Zephyr Notification System.
2 * It contains functions for the User Locator service.
4 * Created by: John T. Kohl
8 * Copyright (c) 1987,1988 by the Massachusetts Institute of Technology.
9 * For copying and distribution information, see the file
13 #include <zephyr/mit-copyright.h>
15 #include <sys/socket.h>
19 static const char rcsid_uloc_c[] =
25 * The user locator functions.
29 * void ulocate_dispatch(notice, auth, who, server)
32 * struct sockaddr_in *who;
35 * void ulogin_dispatch(notice, auth, who, server)
38 * struct sockaddr_in *who;
41 * void uloc_hflush(addr)
42 * struct in_addr *addr;
44 * void uloc_flush_client(sin)
45 * struct sockaddr_in *sin;
47 * Code_t uloc_send_locations()
49 * void uloc_dump_locs(fp)
55 * We maintain an array of Location sorted by user (so we can do
56 * binary searches), growing and shrinking it as necessary.
59 /* WARNING: make sure this is the same as the number of strings you */
60 /* plan to hand back to the user in response to a locate request, */
61 /* else you will lose. See ulogin_locate() and uloc_send_locations() */
64 typedef enum _Exposure_type {
73 typedef struct _Location {
76 char *time; /* in ctime format */
78 struct sockaddr_in addr; /* IP address and port of location */
79 Exposure_type exposure;
86 static void ulogin_locate __P((ZNotice_t *notice, struct sockaddr_in *who,
88 ulogin_flush_user __P((ZNotice_t *notice));
89 static Location *ulogin_find __P((char *user, struct in_addr *host,
91 static Location *ulogin_find_user __P((char *user));
92 static int ulogin_setup __P((ZNotice_t *notice, Location *locs,
93 Exposure_type exposure, struct sockaddr_in *who)),
94 ulogin_add_user __P((ZNotice_t *notice, Exposure_type exposure,
95 struct sockaddr_in *who)),
96 ulogin_parse __P((ZNotice_t *notice, Location *locs));
97 static Exposure_type ulogin_remove_user __P((ZNotice_t *notice,
98 struct sockaddr_in *who,
100 static void login_sendit __P((ZNotice_t *notice, int auth,
101 struct sockaddr_in *who, int external));
102 static char **ulogin_marshal_locs __P((ZNotice_t *notice, int *found,
105 static int ul_equiv __P((Location *l1, Location *l2));
107 static void free_loc __P((Location *loc));
108 static void ulogin_locate_forward __P((ZNotice_t *notice,
109 struct sockaddr_in *who, ZRealm *realm));
111 static Location *locations = NULL; /* ptr to first in array */
112 static int num_locs = 0; /* number in array */
115 * Dispatch a LOGIN notice.
119 ulogin_dispatch(notice, auth, who, server)
122 struct sockaddr_in *who;
125 Exposure_type retval;
128 if (strcmp(notice->z_opcode, LOGIN_USER_LOGOUT) == 0) {
129 retval = ulogin_remove_user(notice, who, &err_ret);
132 if (err_ret == UNAUTH) {
133 if (server == me_server)
134 clt_ack(notice, who, AUTH_FAILED);
136 } else if (err_ret == NOLOC) {
137 if (server == me_server)
138 clt_ack(notice, who, NOT_FOUND);
141 syslog(LOG_ERR,"bogus location exposure NONE, %s",
146 /* he is not announced to people. Silently ack */
147 if (server == me_server)
152 if (server == me_server)
153 sendit(notice, 1, who, 0);
156 /* currently no distinction between these.
158 /* we assume that if this user is at a certain
159 IP address, we can trust the logout to be
160 authentic. ulogin_remove_user checks the
162 if (server == me_server)
163 sendit(notice, 1, who, 1);
166 syslog(LOG_ERR,"bogus location exposure %d/%s",
167 (int) retval, notice->z_sender);
170 if (server == me_server) /* tell the other servers */
171 server_forward(notice, auth, who);
175 (!auth || strcmp(notice->z_sender, notice->z_class_inst) != 0)) {
176 zdbug((LOG_DEBUG,"unauthentic ulogin: %d %s %s", auth,
177 notice->z_sender, notice->z_class_inst));
178 if (server == me_server)
179 clt_ack(notice, who, AUTH_FAILED);
182 if (strcmp(notice->z_opcode, LOGIN_USER_FLUSH) == 0) {
183 ulogin_flush_user(notice);
184 if (server == me_server)
186 } else if (strcmp(notice->z_opcode, EXPOSE_NONE) == 0) {
187 ulogin_remove_user(notice, who, &err_ret);
188 if (err_ret == UNAUTH) {
189 if (server == me_server)
190 clt_ack(notice, who, AUTH_FAILED);
192 } else if (err_ret == NOLOC) {
193 if (server == me_server)
194 clt_ack(notice, who, NOT_FOUND);
197 if (server == me_server) {
199 server_forward(notice, auth, who);
202 } else if (strcmp(notice->z_opcode, EXPOSE_OPSTAFF) == 0) {
203 err_ret = ulogin_add_user(notice, OPSTAFF_VIS, who);
204 if (server == me_server) {
210 } else if (strcmp(notice->z_opcode, EXPOSE_REALMVIS) == 0) {
211 err_ret = ulogin_add_user(notice, REALM_VIS, who);
212 if (server == me_server) { /* realm vis is not broadcast,
219 } else if (!strcmp(notice->z_opcode, EXPOSE_REALMANN)) {
220 err_ret = ulogin_add_user(notice, REALM_ANN, who);
221 if (server == me_server) { /* announce to the realm */
225 login_sendit(notice, auth, who, 0);
227 } else if (!strcmp(notice->z_opcode, EXPOSE_NETVIS)) {
228 err_ret = ulogin_add_user(notice, NET_VIS, who);
229 if (server == me_server) { /* announce to the realm */
233 login_sendit(notice, auth, who, 0);
235 } else if (!strcmp(notice->z_opcode, EXPOSE_NETANN)) {
236 err_ret = ulogin_add_user(notice, NET_ANN, who);
237 if (server == me_server) { /* tell the world */
241 login_sendit(notice, auth, who, 1);
244 if (!strcmp(notice->z_opcode, LOGIN_USER_LOGIN)) {
245 zdbug((LOG_DEBUG, "ulog opcode from unknown foreign realm %s",
248 syslog(LOG_ERR, "unknown ulog opcode %s", notice->z_opcode);
250 if (server == me_server)
254 if (server == me_server)
255 server_forward(notice, auth, who);
260 login_sendit(notice, auth, who, external)
263 struct sockaddr_in *who;
266 ZNotice_t log_notice;
268 /* we must copy the notice struct here because we need the original
269 for forwarding. We needn't copy the private data of the notice,
270 since that isn't modified by sendit and its subroutines. */
272 log_notice = *notice;
274 log_notice.z_opcode = LOGIN_USER_LOGIN;
275 sendit(&log_notice, auth, who, external);
280 * Dispatch a LOCATE notice.
283 ulocate_dispatch(notice, auth, who, server)
286 struct sockaddr_in *who;
292 if (!strcmp(notice->z_opcode, LOCATE_LOCATE)) {
293 /* we are talking to a current-rev client; send an ack */
295 cp = strchr(notice->z_class_inst, '@');
296 if (cp && (realm = realm_get_realm_by_name(cp + 1)))
297 ulogin_locate_forward(notice, who, realm);
299 ulogin_locate(notice, who, auth);
302 syslog(LOG_ERR, "unknown uloc opcode %s", notice->z_opcode);
303 if (server == me_server)
310 * Flush all locations at the address.
315 struct in_addr *addr;
318 int i = 0, new_num = 0;
321 return; /* none to flush */
323 /* slightly inefficient, assume the worst, and allocate enough space */
324 loc = (Location *) malloc(num_locs *sizeof(Location));
326 syslog(LOG_CRIT, "uloc_flush alloc");
330 /* copy entries which don't match */
331 while (i < num_locs) {
332 if (locations[i].addr.sin_addr.s_addr != addr->s_addr)
333 loc[new_num++] = locations[i];
335 free_loc(&locations[i]);
357 uloc_flush_client(sin)
358 struct sockaddr_in *sin;
361 int i = 0, new_num = 0;
364 return; /* none to flush */
366 /* slightly inefficient, assume the worst, and allocate enough space */
367 loc = (Location *) malloc(num_locs *sizeof(Location));
369 syslog(LOG_CRIT, "uloc_flush_clt alloc");
373 /* copy entries which don't match */
374 while (i < num_locs) {
375 if ((locations[i].addr.sin_addr.s_addr != sin->sin_addr.s_addr)
376 || (locations[i].addr.sin_port != sin->sin_port)) {
377 loc[new_num++] = locations[i];
379 free_loc(&locations[i]);
401 for (i = 0; i < num_locs; i++) {
402 syslog(LOG_DEBUG, "%s/%d", locations[i].user->string,
403 (int) locations[i].exposure);
412 * Send the locations for host for a brain dump
417 uloc_send_locations()
421 char *lyst[NUM_FIELDS];
422 char *exposure_level;
425 for (i = 0, loc = locations; i < num_locs; i++, loc++) {
426 lyst[0] = (char *) loc->machine->string;
427 lyst[1] = (char *) loc->time;
428 lyst[2] = (char *) loc->tty->string;
430 switch (loc->exposure) {
432 exposure_level = EXPOSE_OPSTAFF;
435 exposure_level = EXPOSE_REALMVIS;
438 exposure_level = EXPOSE_REALMANN;
441 exposure_level = EXPOSE_NETVIS;
444 exposure_level = EXPOSE_NETANN;
447 syslog(LOG_ERR,"broken location state %s/%d",
448 loc->user->string, (int) loc->exposure);
449 exposure_level = EXPOSE_OPSTAFF;
452 retval = bdump_send_list_tcp(ACKED, &loc->addr, LOGIN_CLASS,
453 loc->user->string, exposure_level, myname,
454 "", lyst, NUM_FIELDS);
455 if (retval != ZERR_NONE) {
456 syslog(LOG_ERR, "uloc_send_locs: %s", error_message(retval));
464 * Add the user to the internal table of locations.
468 ulogin_add_user(notice, exposure, who)
470 Exposure_type exposure;
471 struct sockaddr_in *who;
473 Location *loc, *oldlocs, newloc;
476 loc = ulogin_find(notice->z_class_inst, &who->sin_addr, notice->z_port);
478 /* Update the time, tty, and exposure on the existing location. */
479 loc->exposure = exposure;
480 if (ulogin_parse(notice, &newloc) == 0) {
481 free_string(loc->tty);
482 loc->tty = dup_string(newloc.tty);
484 loc->time = strsave(newloc.time);
492 locations = (Location *) malloc((num_locs + 1) * sizeof(Location));
494 syslog(LOG_ERR, "zloc mem alloc");
499 if (num_locs == 0) { /* first one */
500 if (ulogin_setup(notice, locations, exposure, who)) {
509 /* not the first one, insert him */
511 if (ulogin_setup(notice, &newloc, exposure, who)) {
520 while ((i < num_locs-1) &&
521 (comp_string(oldlocs[i].user,newloc.user) < 0)) {
522 locations[i] = oldlocs[i];
526 /* add him in here */
527 locations[i++] = newloc;
530 while (i < num_locs) {
531 locations[i] = oldlocs[i - 1];
542 * Set up the location locs with the information in the notice.
546 ulogin_setup(notice, locs, exposure, who)
549 Exposure_type exposure;
550 struct sockaddr_in *who;
552 if (ulogin_parse(notice, locs))
555 locs->exposure = exposure;
556 locs->addr.sin_family = AF_INET;
557 locs->addr.sin_addr.s_addr = who->sin_addr.s_addr;
558 locs->addr.sin_port = notice->z_port;
563 * Parse the location information in the notice, and fill it into *locs
567 ulogin_parse(notice, locs)
574 if (!notice->z_message_len) {
575 syslog(LOG_ERR, "short ulogin");
579 base = notice->z_message;
580 for (cp = base; cp < base + notice->z_message_len; cp++) {
585 syslog(LOG_ERR, "zloc bad format from user %s (only %d fields)",
586 notice->z_sender, nulls);
590 locs->user = make_string(notice->z_class_inst,0);
593 locs->machine = make_string(cp,0);
595 cp += (strlen(cp) + 1);
596 locs->time = strsave(cp);
598 /* This field might not be null-terminated */
599 cp += (strlen(cp) + 1);
600 locs->tty = make_string(cp, 0);
607 ulogin_find(user, host, port)
609 struct in_addr *host;
615 /* Find the first location for this user. */
616 loc = ulogin_find_user(user);
620 /* Look for a location which matches the host and port. */
621 str = make_string(user, 0);
622 while (loc < locations + num_locs && loc->user == str) {
623 if (loc->addr.sin_addr.s_addr == host->s_addr
624 && loc->addr.sin_port == port) {
636 * Return a pointer to the first instance of this user@realm in the
641 ulogin_find_user(user)
651 str = make_string(user, 0);
653 /* i is the current midpoint location, rlo is the lowest we will
654 * still check, and rhi is the highest we will still check. */
660 while ((compar = comp_string(locations[i].user, str)) != 0) {
672 /* Back up to the first location for this user. */
673 while (i > 0 && locations[i - 1].user == str)
676 return &locations[i];
683 if (l1->machine != l2->machine)
685 if (l1->tty != l2->tty)
691 * remove the user specified in notice from the internal table
695 ulogin_remove_user(notice, who, err_return)
697 struct sockaddr_in *who;
700 Location *new_locs, *loc;
705 loc = ulogin_find(notice->z_class_inst, &who->sin_addr, notice->z_port);
711 quiet = loc->exposure;
713 if (--num_locs == 0) { /* last one */
720 new_locs = (Location *) malloc(num_locs * sizeof(Location));
722 syslog(LOG_CRIT, "ul_rem alloc");
726 /* copy old entries */
727 while (i < num_locs && &locations[i] < loc) {
728 new_locs[i] = locations[i];
732 /* free up this one */
733 free_loc(&locations[i]);
734 i++; /* skip over this one */
737 while (i <= num_locs) {
738 new_locs[i - 1] = locations[i];
744 locations = new_locs;
751 * remove all locs of the user specified in notice from the internal table
755 ulogin_flush_user(notice)
758 Location *loc, *loc2;
759 int i, j, num_match, num_left;
761 i = num_match = num_left = 0;
763 if (!(loc2 = ulogin_find_user(notice->z_class_inst)))
766 /* compute # locations left in the list, after loc2 (inclusive) */
767 num_left = num_locs - (loc2 - locations);
770 !strcasecmp(loc2[num_match].user->string,
771 notice->z_class_inst)) {
772 /* as long as we keep matching, march up the list */
776 if (num_locs == num_match) { /* no other locations left */
777 for (j = 0; j < num_match; j++)
778 free_loc(&locations[j]); /* free storage */
785 loc = (Location *) malloc((num_locs - num_match) * sizeof(Location));
787 syslog(LOG_CRIT, "ul_rem alloc");
791 /* copy old entries */
792 while (i < num_locs && &locations[i] < loc2) {
793 loc[i] = locations[i];
797 for(j = 0; j < num_match; j++) {
798 free_loc(&locations[i]);
803 while (i < num_locs) {
804 loc[i - num_match] = locations[i];
811 num_locs -= num_match;
817 for (i = 0; i < num_locs; i++) {
818 syslog(LOG_DEBUG, "%s/%d", locations[i].user->string,
819 (int) locations[i].exposure);
827 ulogin_locate(notice, who, auth)
829 struct sockaddr_in *who;
835 struct sockaddr_in send_to_who;
837 answer = ulogin_marshal_locs(notice, &found, auth);
840 send_to_who.sin_port = notice->z_port;
842 retval = ZSetDestAddr(&send_to_who);
843 if (retval != ZERR_NONE) {
844 syslog(LOG_WARNING, "ulogin_locate set addr: %s",
845 error_message(retval));
851 notice->z_kind = ACKED;
853 /* use xmit_frag() to send each piece of the notice */
855 retval = ZSrvSendRawList(notice, answer, found * NUM_FIELDS, xmit_frag);
856 if (retval != ZERR_NONE)
857 syslog(LOG_WARNING, "ulog_locate xmit: %s", error_message(retval));
863 * Locate the user and collect the locations into an array. Return the # of
864 * locations in *found.
868 ulogin_marshal_locs(notice, found, auth)
873 Location **matches = (Location **) 0;
878 int local = (auth && realm_sender_in_realm(ZGetRealm(), notice->z_sender));
880 *found = 0; /* # of matches */
882 loc = ulogin_find_user(notice->z_class_inst);
888 inst = make_string(notice->z_class_inst,0);
889 while (i < num_locs && (inst == locations[i].user)) {
890 /* these locations match */
891 switch (locations[i].exposure) {
907 matches = (Location **) malloc(sizeof(Location *));
909 syslog(LOG_ERR, "ulog_loc: no mem");
910 break; /* from the while */
912 matches[0] = &locations[i];
915 matches = (Location **) realloc(matches,
916 ++(*found) * sizeof(Location *));
918 syslog(LOG_ERR, "ulog_loc: realloc no mem");
920 break; /* from the while */
922 matches[*found - 1] = &locations[i];
928 /* OK, now we have a list of user@host's to return to the client
934 for (i = 0; i < *found ; i++)
935 zdbug((LOG_DEBUG,"found %s",
936 matches[i]->user->string));
940 /* coalesce the location information into a list of char *'s */
941 answer = (char **) malloc((*found) * NUM_FIELDS * sizeof(char *));
943 syslog(LOG_ERR, "zloc no mem(answer)");
946 for (i = 0; i < *found ; i++) {
947 answer[i * NUM_FIELDS] = matches[i]->machine->string;
948 answer[i * NUM_FIELDS + 1] = matches[i]->time;
949 answer[i * NUM_FIELDS + 2] = matches[i]->tty->string;
963 for (i = 0; i < num_locs; i++) {
965 dump_quote(locations[i].user->string, fp);
967 dump_quote(locations[i].machine->string, fp);
969 dump_quote(locations[i].time, fp);
971 dump_quote(locations[i].tty->string, fp);
973 switch (locations[i].exposure) {
975 fputs("OPSTAFF", fp);
978 fputs("RLM_VIS", fp);
981 fputs("RLM_ANN", fp);
984 fputs("NET_VIS", fp);
987 fputs("NET_ANN", fp);
990 fprintf(fp, "? %d ?", locations[i].exposure);
993 fprintf(fp, " %s/%d\n", inet_ntoa(locations[i].addr.sin_addr),
994 ntohs(locations[i].addr.sin_port));
1002 free_string(loc->user);
1003 free_string(loc->machine);
1004 free_string(loc->tty);
1010 ulogin_locate_forward(notice, who, realm)
1012 struct sockaddr_in *who;
1018 lnotice.z_opcode = REALM_REQ_LOCATE;
1020 realm_handoff(&lnotice, 1, who, realm, 0);
1024 ulogin_realm_locate(notice, who, realm)
1026 struct sockaddr_in *who;
1038 zdbug((LOG_DEBUG, "ulogin_realm_locate"));
1041 answer = ulogin_marshal_locs(notice, &found, 0/*AUTH*/);
1044 lnotice.z_opcode = REALM_ANS_LOCATE;
1046 if ((retval = ZFormatRawNoticeList(&lnotice, answer, found * NUM_FIELDS, &pack, &packlen)) != ZERR_NONE) {
1047 syslog(LOG_WARNING, "ulog_rlm_loc format: %s",
1048 error_message(retval));
1057 if ((retval = ZParseNotice(pack, packlen, &lnotice)) != ZERR_NONE) {
1058 syslog(LOG_WARNING, "subscr_rlm_sendit parse: %s",
1059 error_message(retval));
1064 realm_handoff(&lnotice, 1, who, realm, 0);
1071 ulogin_relay_locate(notice, who)
1073 struct sockaddr_in *who;
1077 struct sockaddr_in newwho;
1081 newwho.sin_addr.s_addr = notice->z_sender_addr.s_addr;
1082 newwho.sin_port = notice->z_port;
1083 newwho.sin_family = AF_INET;
1085 if ((retval = ZSetDestAddr(&newwho)) != ZERR_NONE) {
1086 syslog(LOG_WARNING, "uloc_relay_loc set addr: %s",
1087 error_message(retval));
1092 lnotice.z_opcode = LOCATE_LOCATE;
1093 lnotice.z_kind = ACKED;
1095 lnotice.z_authent_len = 0;
1096 lnotice.z_ascii_authent = "";
1097 lnotice.z_checksum = 0;
1098 lnotice.z_ascii_checksum = "";
1100 if ((retval = ZFormatRawNotice(&lnotice, &pack, &packlen)) != ZERR_NONE) {
1101 syslog(LOG_WARNING, "ulog_relay_loc format: %s",
1102 error_message(retval));
1106 if ((retval = ZSendPacket(pack, packlen, 0)) != ZERR_NONE) {
1107 syslog(LOG_WARNING, "ulog_relay_loc xmit: %s",
1108 error_message(retval));