auth: Relocate NetRestrict support functions
[openafs.git] / src / ubik / beacon.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afsconfig.h>
11 #include <afs/param.h>
12
13 #include <roken.h>
14
15 #include <lock.h>
16 #include <rx/xdr.h>
17 #include <rx/rx.h>
18 #include <rx/rxkad.h>
19 #include <rx/rx_multi.h>
20 #include <afs/cellconfig.h>
21 #ifndef AFS_NT40_ENV
22 #include <afs/afsutil.h>
23 #endif
24
25 #define UBIK_INTERNALS
26 #include "ubik.h"
27 #include "ubik_int.h"
28
29 /* These global variables were used to set the function to use to initialise
30  * the client security layer. They are retained for backwards compatiblity with
31  * legacy callers - the ubik_SetClientSecurityProcs() interface should be used
32  * instead
33  */
34 int (*ubik_CRXSecurityProc) (void *rock, struct rx_securityClass **,
35                              afs_int32 *);
36 void *ubik_CRXSecurityRock;
37
38 /*! \name statics used to determine if we're the sync site */
39 static int nServers;            /*!< total number of servers */
40 static char amIMagic = 0;       /*!< is this host the magic host */
41 char amIClone = 0;              /*!< is this a clone which doesn't vote */
42 static char ubik_singleServer = 0;
43 /*\}*/
44 static int (*secLayerProc) (void *rock, struct rx_securityClass **,
45                             afs_int32 *) = NULL;
46 static int (*tokenCheckProc) (void *rock) = NULL;
47 static void * securityRock = NULL;
48
49 afs_int32 ubikSecIndex;
50 struct rx_securityClass *ubikSecClass;
51
52 /* Values protected by the address lock */
53 struct addr_data addr_globals;
54
55 /* Values protected by the beacon lock */
56 struct beacon_data beacon_globals;
57
58 static int ubeacon_InitServerListCommon(afs_uint32 ame,
59                                         struct afsconf_cell *info,
60                                         char clones[],
61                                         afs_uint32 aservers[]);
62 static int verifyInterfaceAddress(afs_uint32 *ame, struct afsconf_cell *info,
63                                   afs_uint32 aservers[]);
64
65 /*! \file
66  * Module responsible for both deciding if we're currently the sync site,
67  * and keeping collecting votes so as to stay sync site.
68  *
69  * The basic module contacts all of the servers it can, trying to get them to vote
70  * for this server for sync site.  The vote request message (called a beacon message)
71  * also specifies until which time this site claims to be the sync site, if at all, thus enabling
72  * receiving sites to know how long the sync site guarantee is made for.
73  *
74  * Each  of these beacon messages is thus both a declaration of how long this site will
75  * remain sync site, and an attempt to extend that time by collecting votes for a later
76  * sync site extension.
77  *
78  * The voting module is responsible for choosing a reasonable time until which it promises
79  * not to vote for someone else.  This parameter (BIG seconds) is not actually passed in
80  * the interface (perhaps it should be?) but is instead a compile time constant that both
81  * sides know about.
82
83  * The beacon and vote modules work intimately together; the vote module decides how long
84  * it should promise the beacon module its vote, and the beacon module takes all of these
85  * votes and decides for how long it is the synchronization site.
86  */
87
88 /*! \brief procedure called from debug rpc call to get this module's state for debugging */
89 void
90 ubeacon_Debug(struct ubik_debug *aparm)
91 {
92     /* fill in beacon's state fields in the ubik_debug structure */
93     aparm->syncSiteUntil = beacon_globals.syncSiteUntil;
94     aparm->nServers = nServers;
95 }
96
97 static int
98 amSyncSite(void) {
99     afs_int32 now;
100     afs_int32 rcode;
101
102     /* special case for fast startup */
103     if (nServers == 1 && !amIClone)
104         return 1;               /* one guy is always the sync site */
105
106     UBIK_BEACON_LOCK;
107     if (beacon_globals.ubik_amSyncSite == 0 || amIClone)
108         rcode = 0;              /* if I don't think I'm the sync site, say so */
109     else {
110         now = FT_ApproxTime();
111         if (beacon_globals.syncSiteUntil <= now) {      /* if my votes have expired, say so */
112             if (beacon_globals.ubik_amSyncSite)
113                 ubik_dprint("Ubik: I am no longer the sync site\n");
114             beacon_globals.ubik_amSyncSite = 0;
115             rcode = 0;
116         } else {
117             rcode = 1;          /* otherwise still have the required votes */
118         }
119     }
120     UBIK_BEACON_UNLOCK;
121     ubik_dprint("beacon: amSyncSite is %d\n", rcode);
122     return rcode;
123 }
124
125 /*!
126  * \brief Procedure that determines whether this site has enough current votes to remain sync site.
127  *
128  * Called from higher-level modules (everything but the vote module).
129  *
130  * If we're the sync site, check that our guarantees, obtained by the ubeacon_Interact()
131  * light-weight process, haven't expired.  We're sync site as long as a majority of the
132  * servers in existence have promised us unexpired guarantees.  The variable #ubik_syncSiteUntil
133  * contains the time at which the latest of the majority of the sync site guarantees expires
134  * (if the variable #ubik_amSyncSite is true)
135  * This module also calls up to the recovery module if it thinks that the recovery module
136  * may have to pick up a new database (which offucr sif [sic] we lose the sync site votes).
137  *
138  * \return 1 if local site is the sync site
139  * \return 0 if sync site is elsewhere
140  */
141 int
142 ubeacon_AmSyncSite(void)
143 {
144     afs_int32 rcode;
145
146     rcode = amSyncSite();
147
148     if (!rcode)
149         urecovery_ResetState();
150
151     return rcode;
152 }
153
154 /*!
155  * \see ubeacon_InitServerListCommon()
156  */
157 int
158 ubeacon_InitServerListByInfo(afs_uint32 ame, struct afsconf_cell *info,
159                              char clones[])
160 {
161     afs_int32 code;
162
163     code = ubeacon_InitServerListCommon(ame, info, clones, 0);
164     return code;
165 }
166
167 /*!
168  * \param ame "address of me"
169  * \param aservers list of other servers
170  *
171  * \see ubeacon_InitServerListCommon()
172  */
173 int
174 ubeacon_InitServerList(afs_uint32 ame, afs_uint32 aservers[])
175 {
176     afs_int32 code;
177
178     code =
179         ubeacon_InitServerListCommon(ame, (struct afsconf_cell *)0, 0,
180                                      aservers);
181     return code;
182 }
183
184 /* Must be called with address lock held */
185 void
186 ubeacon_InitSecurityClass(void)
187 {
188     int i;
189     /* get the security index to use, if we can */
190     if (secLayerProc) {
191         i = (*secLayerProc) (securityRock, &addr_globals.ubikSecClass, &addr_globals.ubikSecIndex);
192     } else if (ubik_CRXSecurityProc) {
193         i = (*ubik_CRXSecurityProc) (ubik_CRXSecurityRock, &addr_globals.ubikSecClass,
194                                      &addr_globals.ubikSecIndex);
195     } else
196         i = 1;
197     if (i) {
198         /* don't have sec module yet */
199         addr_globals.ubikSecIndex = 0;
200         addr_globals.ubikSecClass = rxnull_NewClientSecurityObject();
201     }
202 }
203
204 void
205 ubeacon_ReinitServer(struct ubik_server *ts)
206 {
207     if (tokenCheckProc && !(*tokenCheckProc) (securityRock)) {
208         struct rx_connection *disk_rxcid;
209         struct rx_connection *vote_rxcid;
210         struct rx_connection *tmp;
211         UBIK_ADDR_LOCK;
212         ubeacon_InitSecurityClass();
213         disk_rxcid =
214             rx_NewConnection(rx_HostOf(rx_PeerOf(ts->disk_rxcid)),
215                              ubik_callPortal, DISK_SERVICE_ID,
216                              addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
217         if (disk_rxcid) {
218             tmp = ts->disk_rxcid;
219             ts->disk_rxcid = disk_rxcid;
220             rx_PutConnection(tmp);
221         }
222         vote_rxcid =
223             rx_NewConnection(rx_HostOf(rx_PeerOf(ts->vote_rxcid)),
224                              ubik_callPortal, VOTE_SERVICE_ID,
225                              addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
226         if (vote_rxcid) {
227             tmp = ts->vote_rxcid;
228             ts->vote_rxcid = vote_rxcid;
229             rx_PutConnection(tmp);
230         }
231         UBIK_ADDR_UNLOCK;
232     }
233 }
234
235 /*!
236  * \brief setup server list
237  *
238  * \param ame "address of me"
239  * \param aservers list of other servers
240  *
241  * called only at initialization to set up the list of servers to
242  * contact for votes.  Just creates the server structure.
243  *
244  * The "magic" host is the one with the lowest internet address.  It is
245  * magic because its vote counts epsilon more than the others.  This acts
246  * as a tie-breaker when we have an even number of hosts in the system.
247  * For example, if the "magic" host is up in a 2 site system, then it
248  * is sync site.  Without the magic host hack, if anyone crashed in a 2
249  * site system, we'd be out of business.
250  *
251  * \note There are two connections in every server structure, one for
252  * vote calls (which must always go through quickly) and one for database
253  * operations, which are subject to waiting for locks.  If we used only
254  * one, the votes would sometimes get held up behind database operations,
255  * and the sync site guarantees would timeout even though the host would be
256  * up for communication.
257  *
258  * \see ubeacon_InitServerList(), ubeacon_InitServerListByInfo()
259  */
260 int
261 ubeacon_InitServerListCommon(afs_uint32 ame, struct afsconf_cell *info,
262                              char clones[], afs_uint32 aservers[])
263 {
264     struct ubik_server *ts;
265     afs_int32 me = -1;
266     afs_int32 servAddr;
267     afs_int32 i, code;
268     afs_int32 magicHost;
269     struct ubik_server *magicServer;
270
271     /* verify that the addresses passed in are correct */
272     if ((code = verifyInterfaceAddress(&ame, info, aservers)))
273         return code;
274
275     ubeacon_InitSecurityClass();
276
277     magicHost = ntohl(ame);     /* do comparisons in host order */
278     magicServer = (struct ubik_server *)0;
279
280     if (info) {
281         for (i = 0; i < info->numServers; i++) {
282             if (ntohl((afs_uint32) info->hostAddr[i].sin_addr.s_addr) ==
283                 ntohl((afs_uint32) ame)) {
284                 me = i;
285                 if (clones[i]) {
286                     amIClone = 1;
287                     magicHost = 0;
288                 }
289             }
290         }
291         nServers = 0;
292         for (i = 0; i < info->numServers; i++) {
293             if (i == me)
294                 continue;
295             ts = calloc(1, sizeof(struct ubik_server));
296             ts->next = ubik_servers;
297             ubik_servers = ts;
298             ts->addr[0] = info->hostAddr[i].sin_addr.s_addr;
299             if (clones[i]) {
300                 ts->isClone = 1;
301             } else {
302                 if (!magicHost
303                     || ntohl((afs_uint32) ts->addr[0]) <
304                     (afs_uint32) magicHost) {
305                     magicHost = ntohl(ts->addr[0]);
306                     magicServer = ts;
307                 }
308                 ++nServers;
309             }
310             /* for vote reqs */
311             ts->vote_rxcid =
312                 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
313                                  ubik_callPortal, VOTE_SERVICE_ID,
314                                  addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
315             /* for disk reqs */
316             ts->disk_rxcid =
317                 rx_NewConnection(info->hostAddr[i].sin_addr.s_addr,
318                                  ubik_callPortal, DISK_SERVICE_ID,
319                                  addr_globals.ubikSecClass, addr_globals.ubikSecIndex);
320             ts->up = 1;
321         }
322     } else {
323         i = 0;
324         while ((servAddr = *aservers++)) {
325             if (i >= MAXSERVERS)
326                 return UNHOSTS; /* too many hosts */
327             ts = calloc(1, sizeof(struct ubik_server));
328             ts->next = ubik_servers;
329             ubik_servers = ts;
330             ts->addr[0] = servAddr;     /* primary address in  net byte order */
331             ts->vote_rxcid = rx_NewConnection(servAddr, ubik_callPortal, VOTE_SERVICE_ID,
332                         addr_globals.ubikSecClass, addr_globals.ubikSecIndex);  /* for vote reqs */
333             ts->disk_rxcid = rx_NewConnection(servAddr, ubik_callPortal, DISK_SERVICE_ID,
334                         addr_globals.ubikSecClass, addr_globals.ubikSecIndex);  /* for disk reqs */
335             ts->isClone = 0;    /* don't know about clones */
336             ts->up = 1;
337             if (ntohl((afs_uint32) servAddr) < (afs_uint32) magicHost) {
338                 magicHost = ntohl(servAddr);
339                 magicServer = ts;
340             }
341             i++;
342         }
343     }
344     if (magicServer)
345         magicServer->magic = 1; /* remember for when counting votes */
346
347     if (!amIClone && !magicServer)
348         amIMagic = 1;
349     if (info) {
350         if (!amIClone)
351             ++nServers;         /* count this server as well as the remotes */
352     } else
353         nServers = i + 1;       /* count this server as well as the remotes */
354
355     ubik_quorum = (nServers >> 1) + 1;  /* compute the majority figure */
356
357 /* Shoud we set some defaults for RX??
358     r_retryInterval = 2;
359     r_nRetries = (RPCTIMEOUT/r_retryInterval);
360 */
361     if (info) {
362         if (!ubik_servers)      /* special case 1 server */
363             ubik_singleServer = 1;
364         if (nServers == 1 && !amIClone) {
365             beacon_globals.ubik_amSyncSite = 1; /* let's start as sync site */
366             beacon_globals.syncSiteUntil = 0x7fffffff;  /* and be it quite a while */
367         }
368     } else {
369         if (nServers == 1)      /* special case 1 server */
370             ubik_singleServer = 1;
371     }
372
373     if (ubik_singleServer) {
374         if (!beacon_globals.ubik_amSyncSite)
375             ubik_dprint("Ubik: I am the sync site - 1 server\n");
376         beacon_globals.ubik_amSyncSite = 1;
377         beacon_globals.syncSiteUntil = 0x7fffffff;      /* quite a while */
378     }
379     return 0;
380 }
381
382 /*!
383  * \brief main lwp loop for code that sends out beacons.
384  *
385  * This code only runs while we're sync site or we want to be the sync site.
386  * It runs in its very own light-weight process.
387  */
388 void *
389 ubeacon_Interact(void *dummy)
390 {
391     afs_int32 code;
392     struct timeval tt;
393     struct rx_connection *connections[MAXSERVERS];
394     struct ubik_server *servers[MAXSERVERS];
395     afs_int32 i;
396     struct ubik_server *ts;
397     afs_int32 temp, yesVotes, lastWakeupTime, oldestYesVote, syncsite;
398     struct ubik_tid ttid;
399     struct ubik_version tversion;
400     afs_int32 startTime;
401
402     afs_pthread_setname_self("beacon");
403
404     /* loop forever getting votes */
405     lastWakeupTime = 0;         /* keep track of time we last started a vote collection */
406     while (1) {
407
408         /* don't wakeup more than every POLLTIME seconds */
409         temp = (lastWakeupTime + POLLTIME) - FT_ApproxTime();
410         /* don't sleep if last collection phase took too long (probably timed someone out ) */
411         if (temp > 0) {
412             if (temp > POLLTIME)
413                 temp = POLLTIME;
414             tt.tv_sec = temp;
415             tt.tv_usec = 0;
416 #ifdef AFS_PTHREAD_ENV
417             code = select(0, 0, 0, 0, &tt);
418 #else
419             code = IOMGR_Select(0, 0, 0, 0, &tt);
420 #endif
421         } else
422             code = 0;
423
424         lastWakeupTime = FT_ApproxTime();       /* started a new collection phase */
425
426         if (ubik_singleServer)
427             continue;           /* special-case 1 server for speedy startup */
428
429         if (!uvote_ShouldIRun())
430             continue;           /* if voter has heard from a better candidate than us, don't bother running */
431
432         /* otherwise we should run for election, or we're the sync site (and have already won);
433          * send out the beacon packets */
434         /* build list of all up hosts (noticing dead hosts are running again
435          * is a task for the recovery module, not the beacon module), and
436          * prepare to send them an r multi-call containing the beacon message */
437         i = 0;                  /* collect connections */
438         UBIK_BEACON_LOCK;
439         UBIK_ADDR_LOCK;
440         for (ts = ubik_servers; ts; ts = ts->next) {
441             if (ts->up && ts->addr[0] != ubik_host[0]) {
442                 servers[i] = ts;
443                 connections[i++] = ts->vote_rxcid;
444             }
445         }
446         UBIK_ADDR_UNLOCK;
447         UBIK_BEACON_UNLOCK;
448         servers[i] = (struct ubik_server *)0;   /* end of list */
449         /* note that we assume in the vote module that we'll always get at least BIGTIME
450          * seconds of vote from anyone who votes for us, which means we can conservatively
451          * assume we'll be fine until SMALLTIME seconds after we start collecting votes */
452         /* this next is essentially an expansion of rgen's ServBeacon routine */
453
454         UBIK_VERSION_LOCK;
455         ttid.epoch = version_globals.ubik_epochTime;
456         if (ubik_dbase->flags & DBWRITING) {
457             /*
458              * if a write is in progress, we have to send the writeTidCounter
459              * which holds the tid counter of the write transaction , and not
460              * send the tidCounter value which holds the tid counter of the
461              * last transaction.
462              */
463             ttid.counter = ubik_dbase->writeTidCounter;
464         } else
465             ttid.counter = ubik_dbase->tidCounter + 1;
466         tversion.epoch = ubik_dbase->version.epoch;
467         tversion.counter = ubik_dbase->version.counter;
468         UBIK_VERSION_UNLOCK;
469
470         /* now analyze return codes, counting up our votes */
471         yesVotes = 0;           /* count how many to ensure we have quorum */
472         oldestYesVote = 0x7fffffff;     /* time quorum expires */
473         syncsite = amSyncSite();
474         if (!syncsite) {
475             /* Ok to use the DB lock here since we aren't sync site */
476             DBHOLD(ubik_dbase);
477             urecovery_ResetState();
478             DBRELE(ubik_dbase);
479         }
480         startTime = FT_ApproxTime();
481         /*
482          * Don't waste time using mult Rx calls if there are no connections out there
483          */
484         if (i > 0) {
485             char hoststr[16];
486             multi_Rx(connections, i) {
487                 multi_VOTE_Beacon(syncsite, startTime, &tversion,
488                                   &ttid);
489                 temp = FT_ApproxTime(); /* now, more or less */
490                 ts = servers[multi_i];
491                 UBIK_BEACON_LOCK;
492                 ts->lastBeaconSent = temp;
493                 code = multi_error;
494                 /* note that the vote time (the return code) represents the time
495                  * the vote was computed, *not* the time the vote expires.  We compute
496                  * the latter down below if we got enough votes to go with */
497                 if (code > 0) {
498                     if ((code & ~0xff) == ERROR_TABLE_BASE_RXK) {
499                         ubik_dprint("token error %d from host %s\n",
500                                     code, afs_inet_ntoa_r(ts->addr[0], hoststr));
501                         ts->up = 0;
502                         ts->beaconSinceDown = 0;
503                         urecovery_LostServer(ts);
504                     } else {
505                         ts->lastVoteTime = code;
506                         if (code < oldestYesVote)
507                             oldestYesVote = code;
508                         ts->lastVote = 1;
509                         if (!ts->isClone)
510                             yesVotes += 2;
511                         if (ts->magic)
512                             yesVotes++; /* the extra epsilon */
513                         ts->up = 1;     /* server is up (not really necessary: recovery does this for real) */
514                         ts->beaconSinceDown = 1;
515                         ubik_dprint("yes vote from host %s\n",
516                                     afs_inet_ntoa_r(ts->addr[0], hoststr));
517                     }
518                 } else if (code == 0) {
519                     ts->lastVoteTime = temp;
520                     ts->lastVote = 0;
521                     ts->beaconSinceDown = 1;
522                     ubik_dprint("no vote from %s\n",
523                                 afs_inet_ntoa_r(ts->addr[0], hoststr));
524                 } else if (code < 0) {
525                     ts->up = 0;
526                     ts->beaconSinceDown = 0;
527                     urecovery_LostServer(ts);
528                     ubik_dprint("time out from %s\n",
529                                 afs_inet_ntoa_r(ts->addr[0], hoststr));
530                 }
531                 UBIK_BEACON_UNLOCK;
532             }
533             multi_End;
534         }
535         /* now call our own voter module to see if we'll vote for ourself.  Note that
536          * the same restrictions apply for our voting for ourself as for our voting
537          * for anyone else. */
538         i = SVOTE_Beacon((struct rx_call *)0, ubeacon_AmSyncSite(), startTime,
539                          &tversion, &ttid);
540         if (i) {
541             yesVotes += 2;
542             if (amIMagic)
543                 yesVotes++;     /* extra epsilon */
544             if (i < oldestYesVote)
545                 oldestYesVote = i;
546         }
547
548         /* now decide if we have enough votes to become sync site.
549          * Note that we can still get enough votes even if we didn't for ourself. */
550         if (yesVotes > nServers) {      /* yesVotes is bumped by 2 or 3 for each site */
551             UBIK_BEACON_LOCK;
552             if (!beacon_globals.ubik_amSyncSite)
553                 ubik_dprint("Ubik: I am the sync site\n");
554             beacon_globals.ubik_amSyncSite = 1;
555             beacon_globals.syncSiteUntil = oldestYesVote + SMALLTIME;
556 #ifndef AFS_PTHREAD_ENV
557                 /* I did not find a corresponding LWP_WaitProcess(&ubik_amSyncSite) --
558                    this may be a spurious signal call -- sjenkins */
559                 LWP_NoYieldSignal(&beacon_globals.ubik_amSyncSite);
560 #endif
561             UBIK_BEACON_UNLOCK;
562         } else {
563             UBIK_BEACON_LOCK;
564             if (beacon_globals.ubik_amSyncSite)
565                 ubik_dprint("Ubik: I am no longer the sync site\n");
566             beacon_globals.ubik_amSyncSite = 0;
567             UBIK_BEACON_UNLOCK;
568             DBHOLD(ubik_dbase);
569             urecovery_ResetState();     /* tell recovery we're no longer the sync site */
570             DBRELE(ubik_dbase);
571         }
572
573     }                           /* while loop */
574     return NULL;
575 }
576
577 /*!
578  * \brief Verify that a given IP addresses does actually exist on this machine.
579  *
580  * \param ame      the pointer to my IP address specified in the
581  *                 CellServDB file.
582  * \param aservers an array containing IP
583  *                 addresses of remote ubik servers. The array is
584  *                 terminated by a zero address.
585  *
586  * Algorithm     : Verify that my IP addresses \p ame does actually exist
587  *                 on this machine.  If any of my IP addresses are there
588  *                 in the remote server list \p aserver, remove them from
589  *                 this list.  Update global variable \p ubik_host[] with
590  *                 my IP addresses.
591  *
592  * \return 0 on success, non-zero on failure
593  */
594 static int
595 verifyInterfaceAddress(afs_uint32 *ame, struct afsconf_cell *info,
596                        afs_uint32 aservers[]) {
597     afs_uint32 myAddr[UBIK_MAX_INTERFACE_ADDR], *servList, tmpAddr;
598     afs_uint32 myAddr2[UBIK_MAX_INTERFACE_ADDR];
599     char hoststr[16];
600     int tcount, count, found, i, j, totalServers, start, end, usednetfiles =
601         0;
602
603     if (info)
604         totalServers = info->numServers;
605     else {                      /* count the number of servers */
606         for (totalServers = 0, servList = aservers; *servList; servList++)
607             totalServers++;
608     }
609
610     if (AFSDIR_SERVER_NETRESTRICT_FILEPATH || AFSDIR_SERVER_NETINFO_FILEPATH) {
611         /*
612          * Find addresses we are supposed to register as per the netrestrict file
613          * if it exists, else just register all the addresses we find on this
614          * host as returned by rx_getAllAddr (in NBO)
615          */
616         char reason[1024];
617         count = afsconf_ParseNetFiles(myAddr, NULL, NULL,
618                                       UBIK_MAX_INTERFACE_ADDR, reason,
619                                       AFSDIR_SERVER_NETINFO_FILEPATH,
620                                       AFSDIR_SERVER_NETRESTRICT_FILEPATH);
621         if (count < 0) {
622             ubik_print("ubik: Can't register any valid addresses:%s\n",
623                        reason);
624             ubik_print("Aborting..\n");
625             return UBADHOST;
626         }
627         usednetfiles++;
628     } else {
629         /* get all my interface addresses in net byte order */
630         count = rx_getAllAddr(myAddr, UBIK_MAX_INTERFACE_ADDR);
631     }
632
633     if (count <= 0) {           /* no address found */
634         ubik_print("ubik: No network addresses found, aborting..");
635         return UBADHOST;
636     }
637
638     /* verify that the My-address passed in by ubik is correct */
639     for (j = 0, found = 0; j < count; j++) {
640         if (*ame == myAddr[j]) {        /* both in net byte order */
641             found = 1;
642             break;
643         }
644     }
645
646     if (!found) {
647         ubik_print("ubik: primary address %s does not exist\n",
648                    afs_inet_ntoa_r(*ame, hoststr));
649         /* if we had the result of rx_getAllAddr already, avoid subverting
650          * the "is gethostbyname(gethostname()) us" check. If we're
651          * using NetInfo/NetRestrict, we assume they have enough clue
652          * to avoid that big hole in their foot from the loaded gun. */
653         if (usednetfiles) {
654             /* take the address we did get, then see if ame was masked */
655             *ame = myAddr[0];
656             tcount = rx_getAllAddr(myAddr2, UBIK_MAX_INTERFACE_ADDR);
657             if (tcount <= 0) {  /* no address found */
658                 ubik_print("ubik: No network addresses found, aborting..");
659                 return UBADHOST;
660             }
661
662             /* verify that the My-address passed in by ubik is correct */
663             for (j = 0, found = 0; j < tcount; j++) {
664                 if (*ame == myAddr2[j]) {       /* both in net byte order */
665                     found = 1;
666                     break;
667                 }
668             }
669         }
670         if (!found)
671             return UBADHOST;
672     }
673
674     /* if any of my addresses are there in serverList, then
675      ** use that as my primary addresses : the higher level
676      ** application screwed up in dealing with multihomed concepts
677      */
678     for (j = 0, found = 0; j < count; j++) {
679         for (i = 0; i < totalServers; i++) {
680             if (info)
681                 tmpAddr = (afs_uint32) info->hostAddr[i].sin_addr.s_addr;
682             else
683                 tmpAddr = aservers[i];
684             if (myAddr[j] == tmpAddr) {
685                 *ame = tmpAddr;
686                 if (!info)
687                     aservers[i] = 0;
688                 found = 1;
689             }
690         }
691     }
692     if (found)
693         ubik_print("Using %s as my primary address\n", afs_inet_ntoa_r(*ame, hoststr));
694
695     if (!info) {
696         /* get rid of servers which were purged because all
697          ** those interface addresses are myself
698          */
699         for (start = 0, end = totalServers - 1; (start < end); start++, end--) {
700             /* find the first zero entry from the beginning */
701             for (; (start < end) && (aservers[start]); start++);
702
703             /* find the last non-zero entry from the end */
704             for (; (end >= 0) && (!aservers[end]); end--);
705
706             /* if there is nothing more to purge, exit from loop */
707             if (start >= end)
708                 break;
709
710             /* move the entry */
711             aservers[start] = aservers[end];
712             aservers[end] = 0;  /* this entry was moved */
713         }
714     }
715
716     /* update all my addresses in ubik_host in such a way
717      ** that ubik_host[0] has the primary address
718      */
719     ubik_host[0] = *ame;
720     for (j = 0, i = 1; j < count; j++)
721         if (*ame != myAddr[j])
722             ubik_host[i++] = myAddr[j];
723
724     return 0;                   /* return success */
725 }
726
727
728 /*!
729  * \brief Exchange IP address information with remote servers.
730  *
731  * \param ubik_host an array containing all my IP addresses.
732  *
733  * Algorithm     : Do an RPC to all remote ubik servers informing them
734  *                 about my IP addresses. Get their IP addresses and
735  *                 update my linked list of ubik servers \p ubik_servers
736  *
737  * \return 0 on success, non-zero on failure
738  */
739 int
740 ubeacon_updateUbikNetworkAddress(afs_uint32 ubik_host[UBIK_MAX_INTERFACE_ADDR])
741 {
742     int j, count, code = 0;
743     UbikInterfaceAddr inAddr, outAddr;
744     struct rx_connection *conns[MAXSERVERS];
745     struct ubik_server *ts, *server[MAXSERVERS];
746     char buffer[32];
747     char hoststr[16];
748
749     UBIK_ADDR_LOCK;
750     for (count = 0, ts = ubik_servers; ts; count++, ts = ts->next) {
751         conns[count] = ts->disk_rxcid;
752         server[count] = ts;
753     }
754     UBIK_ADDR_UNLOCK;
755
756
757     /* inform all other servers only if there are more than one
758      * database servers in the cell */
759
760     if (count > 0) {
761
762         for (j = 0; j < UBIK_MAX_INTERFACE_ADDR; j++)
763             inAddr.hostAddr[j] = ntohl(ubik_host[j]);
764
765
766         /* do the multi-RX RPC to all other servers */
767         multi_Rx(conns, count) {
768             multi_DISK_UpdateInterfaceAddr(&inAddr, &outAddr);
769             ts = server[multi_i];       /* reply received from this server */
770             if (!multi_error) {
771                 UBIK_ADDR_LOCK;
772                 if (ts->addr[0] != htonl(outAddr.hostAddr[0])) {
773                     code = UBADHOST;
774                     strcpy(buffer, afs_inet_ntoa_r(ts->addr[0], hoststr));
775                     ubik_print("ubik:Two primary addresses for same server \
776                     %s %s\n", buffer,
777                     afs_inet_ntoa_r(htonl(outAddr.hostAddr[0]), hoststr));
778                 } else {
779                     for (j = 1; j < UBIK_MAX_INTERFACE_ADDR; j++)
780                         ts->addr[j] = htonl(outAddr.hostAddr[j]);
781                 }
782                 UBIK_ADDR_UNLOCK;
783             } else if (multi_error == RXGEN_OPCODE) {   /* pre 3.5 remote server */
784                 UBIK_ADDR_LOCK;
785                 ubik_print
786                     ("ubik server %s does not support UpdateInterfaceAddr RPC\n",
787                      afs_inet_ntoa_r(ts->addr[0], hoststr));
788                 UBIK_ADDR_UNLOCK;
789             } else if (multi_error == UBADHOST) {
790                 code = UBADHOST;        /* remote CellServDB inconsistency */
791                 ubik_print("Inconsistent Cell Info on server: ");
792                 UBIK_ADDR_LOCK;
793                 for (j = 0; j < UBIK_MAX_INTERFACE_ADDR && ts->addr[j]; j++)
794                     ubik_print("%s ", afs_inet_ntoa_r(ts->addr[j], hoststr));
795                 UBIK_ADDR_UNLOCK;
796                 ubik_print("\n");
797             } else {
798                 UBIK_BEACON_LOCK;
799                 ts->up = 0;     /* mark the remote server as down */
800                 UBIK_BEACON_UNLOCK;
801             }
802         }
803         multi_End;
804     }
805     return code;
806 }
807
808 void
809 ubik_SetClientSecurityProcs(int (*secproc) (void *,
810                                             struct rx_securityClass **,
811                                             afs_int32 *),
812                             int (*checkproc) (void *),
813                             void *rock)
814 {
815     secLayerProc = secproc;
816     tokenCheckProc = checkproc;
817     securityRock = rock;
818 }