2 * Copyright 2000, International Business Machines Corporation and others.
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
10 /* Interface and supporting routines for the backup system's ubik database */
12 #include <afsconfig.h>
18 #include <sys/types.h>
22 #elif defined(AFS_SUN5_ENV)
25 #include <sys/param.h> /* for hostnames etc */
28 #include <afs/cellconfig.h>
30 #include <afs/volser.h>
31 #include <afs/afsutil.h>
32 #include <afs/bubasics.h>
33 #include <afs/budb_client.h>
38 #include "error_macros.h"
41 afs_int32 bcdb_AddVolume(register struct budb_volumeEntry *);
42 afs_int32 bcdb_AddVolumes(register struct budb_volumeEntry *, afs_int32 );
43 afs_int32 bcdb_CreateDump(register struct budb_dumpEntry *) ;
44 afs_int32 bcdb_deleteDump(afs_int32, afs_int32, afs_int32, budb_dumpsList *);
45 /*note the pinter to the function comes from ubik/ubikclient ubik_Call function.*/
46 afs_int32 bcdb_listDumps (int (), afs_int32,afs_int32,afs_int32, budb_dumpsList *,
48 afs_int32 bcdb_DeleteVDP(char *, char *, afs_int32 );
49 afs_int32 bcdb_FindClone(afs_int32, char *, afs_int32 *);
53 /* -------------------------------------
55 * -------------------------------------
58 struct udbHandleS udbHandle;
60 /* -------------------------------------
61 * interface routines (alphabetic)
62 * -------------------------------------
65 afs_int32 bcdb_AddVolume(register struct budb_volumeEntry *veptr)
69 code = ubik_Call(BUDB_AddVolume, udbHandle.uh_client, 0, veptr);
73 afs_int32 bcdb_AddVolumes(register struct budb_volumeEntry *veptr, afs_int32 count)
75 struct budb_volumeList volumeList;
78 volumeList.budb_volumeList_len = count;
79 volumeList.budb_volumeList_val = veptr;
80 code = ubik_Call(BUDB_AddVolumes, udbHandle.uh_client, 0, &volumeList);
85 afs_int32 bcdb_CreateDump(register struct budb_dumpEntry *deptr)
89 code = ubik_Call(BUDB_CreateDump, udbHandle.uh_client, 0, deptr);
93 afs_int32 bcdb_deleteDump(afs_int32 dumpID, afs_int32 fromTime, afs_int32 toTime,
94 budb_dumpsList *dumps)
97 budb_dumpsList dumpsList, *dumpsPtr;
99 dumpsList.budb_dumpsList_len = 0;
100 dumpsList.budb_dumpsList_val = 0;
101 dumpsPtr = (dumps ? dumps : &dumpsList);
104 ubik_Call(BUDB_DeleteDump, udbHandle.uh_client, 0, dumpID, fromTime,
106 if (dumpsList.budb_dumpsList_val)
107 free(dumpsList.budb_dumpsList_val);
111 afs_int32 bcdb_listDumps (int (*sflags) (), afs_int32 groupId,afs_int32 fromTime, afs_int32 toTime,budb_dumpsList *dumps, budb_dumpsList *flags)
113 afs_int32 code, sflag = 0;
114 budb_dumpsList dumpsList, *dumpsPtr;
115 budb_dumpsList flagsList, *flagsPtr;
117 dumpsList.budb_dumpsList_len = 0;
118 dumpsList.budb_dumpsList_val = 0;
119 dumpsPtr = (dumps ? dumps : &dumpsList);
121 flagsList.budb_dumpsList_len = 0;
122 flagsList.budb_dumpsList_val = 0;
123 flagsPtr = (flags ? flags : &flagsList);
126 ubik_Call(BUDB_ListDumps, udbHandle.uh_client, 0, sflags, "", groupId,
127 fromTime, toTime, dumpsPtr, flagsPtr);
129 if (dumpsList.budb_dumpsList_val)
130 free(dumpsList.budb_dumpsList_val);
131 if (flagsList.budb_dumpsList_val)
132 free(flagsList.budb_dumpsList_val);
137 afs_int32 bcdb_DeleteVDP(char *dumpSetName, char *dumpPath, afs_int32 dumpID)
142 ubik_Call(BUDB_DeleteVDP, udbHandle.uh_client, 0, dumpSetName,
148 * Returns the clone time of a volume by going up the parent chain.
149 * If no clone time is found, a clone time of 0 is returned, forcing
152 * dumpID - of the first dump to examine.
153 * volName - name of the volume for whom a clone time is required
154 * clonetime - ptr to vbl for returning result
156 * 0 - clonetime set appropriately
157 * -1 - error occured in traversing chain, clone time set to 0.
158 * -2 - no clone times found, clone time set to 0
161 afs_int32 bcdb_FindClone(afs_int32 dumpID, char *volName, afs_int32 *clonetime)
165 ubik_Call(BUDB_FindClone, udbHandle.uh_client, 0, dumpID, volName,
171 * scan entire database for latest volume dump before adate. Optimize
172 * further by reading only the first line of the dump and if it is older
173 * than the oldest acceptable dump we've found so far, we don't bother
174 * scanning the dump file we've just opened
176 * Used by restore code when restoring a user requested volume(s)
178 * volumeName - name of volume to match on
179 * beforeDate - look for dumps older than this date
181 * deptr - desciptor of most recent dump
183 * should be able to implement this in a single call rather than
184 * the current multiple bcdb_ call algorithm.
187 bcdb_FindDump(volumeName, beforeDate, deptr)
189 afs_int32 beforeDate;
190 struct budb_dumpEntry *deptr;
194 ubik_Call(BUDB_FindDump, udbHandle.uh_client, 0, volumeName,
200 * find a dump by id. Currently insists on a single return value.
202 * dumpID - id to lookup
205 bcdb_FindDumpByID(dumpID, deptr)
207 register struct budb_dumpEntry *deptr;
209 register afs_int32 code;
214 /* initialize the dump list */
215 dl.budb_dumpList_len = 0;
216 dl.budb_dumpList_val = 0;
218 /* outline algorithm */
219 code = ubik_Call(BUDB_GetDumps, udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_DUMPID, "", /* no name */
223 &nextindex, &dbTime, &dl);
226 || (dl.budb_dumpList_len != 1) /* single retn val expected */
228 /* printf("bcdb_FindDumpByID: code %d, nvalues %d\n",
229 code, dl.budb_dumpList_len); */
231 code = 1; /* multiple id's */
235 memcpy(deptr, dl.budb_dumpList_val, sizeof(*deptr));
238 if (dl.budb_dumpList_val) {
239 /* free any allocated structures */
240 free(dl.budb_dumpList_val);
245 memset(deptr, 0, sizeof(*deptr));
249 /* bcdb_FindLastVolClone
250 * Returns the clone time, from the most recent dump of volName, when
251 * dumped in the volume set volSetName, with dump schedule dumpName.
252 * The clone time can be used to check if the volume has been correctly
253 * re-cloned, and also is used as the time from which to do the current
256 * volSetName - name of volume set
257 * dumpName - full path of dump node
258 * volName - name of volume for whom a clone time is required
259 * clonetime - ptr to vbl for result
261 * 0 - clonetime set appropriately
263 * used only for warning generation. Suggest that this be omitted.
267 bcdb_FindLastVolClone(volSetName, dumpName, volName, clonetime)
271 afs_int32 *clonetime;
275 * match on volumeset and dump path
276 * search for the volume name
281 /* bcdb_FindLatestDump
282 * find the latest dump with volume set component avname and the
283 * specified dump pathname. Used to find a dump, relative to which an
284 * incremental dump can be done. Defines the parent <-> child relations
287 * avname: volume set name
288 * dumpPath: full path of dump node
290 * 0: adentry: dump entry structure filled in.
291 * -1: probably an internal error
294 * Need to store volumeset name in dump in order to implement this.
295 * Need new routine since params are two strings
298 bcdb_FindLatestDump(volSetName, dumpPath, deptr)
301 struct budb_dumpEntry *deptr;
305 ubik_Call(BUDB_FindLatestDump, udbHandle.uh_client, 0, volSetName,
314 * dumpid: dump id to which tape beint32s
315 * tapeName: name of tape
318 bcdb_FindTape(dumpid, tapeName, teptr)
321 struct budb_tapeEntry *teptr;
328 memset(teptr, 0, sizeof(*teptr));
329 tl.budb_tapeList_len = 0;
330 tl.budb_tapeList_val = 0;
333 ubik_Call(BUDB_GetTapes, udbHandle.uh_client, 0, BUDB_MAJORVERSION,
334 BUDB_OP_TAPENAME | BUDB_OP_DUMPID, tapeName, dumpid, 0, 0,
335 &next, &dbTime, &tl);
340 if (tl.budb_tapeList_len != 1)
341 ERROR(BC_NOTUNIQUE); /* expecting a single descriptor */
343 memcpy(teptr, tl.budb_tapeList_val, sizeof(*teptr));
346 if (tl.budb_tapeList_val)
347 free(tl.budb_tapeList_val);
351 bcdb_FindTapeSeq(dumpid, tapeSeq, teptr)
354 struct budb_tapeEntry *teptr;
361 memset(teptr, 0, sizeof(*teptr));
362 tl.budb_tapeList_len = 0;
363 tl.budb_tapeList_val = 0;
366 ubik_Call(BUDB_GetTapes, udbHandle.uh_client, 0, BUDB_MAJORVERSION,
367 BUDB_OP_TAPESEQ | BUDB_OP_DUMPID, "", dumpid, tapeSeq, 0,
368 &next, &dbTime, &tl);
372 if (tl.budb_tapeList_len != 1)
373 ERROR(BC_NOTUNIQUE); /* expecting a single descriptor */
375 memcpy(teptr, tl.budb_tapeList_val, sizeof(*teptr));
378 if (tl.budb_tapeList_val)
379 free(tl.budb_tapeList_val);
385 * - this is part of dblookup. The existing semantics will not work since
386 * they do lookups based on dump id.
387 * - in the restore code, it uses this to extract information about
388 * the volume. Need current semantics. Could filter the output, selecting
390 * - Suggest that the lookup be based on volume name only, with optional
391 * match on backup, and readonly volumes.
392 * - Further, need to check if the volume structure returns enough
397 bcdb_FindVolumes(dumpID, volumeName, returnArray, last, next, maxa, nEntries)
400 struct budb_volumeEntry *returnArray;
410 vl.budb_volumeList_len = maxa;
411 vl.budb_volumeList_val = returnArray;
413 /* outline algorithm */
414 code = ubik_Call(BUDB_GetVolumes, udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_VOLUMENAME | BUDB_OP_DUMPID, volumeName, /* name */
418 next, /* nextindex */
421 *nEntries = vl.budb_volumeList_len;
426 bcdb_FinishDump(deptr)
427 register struct budb_dumpEntry *deptr;
430 code = ubik_Call(BUDB_FinishDump, udbHandle.uh_client, 0, deptr);
434 bcdb_FinishTape(teptr)
435 register struct budb_tapeEntry *teptr;
438 code = ubik_Call(BUDB_FinishTape, udbHandle.uh_client, 0, teptr);
443 /* bcdb_LookupVolumes
447 bcdb_LookupVolume(volumeName, returnArray, last, next, maxa, nEntries)
449 struct budb_volumeEntry *returnArray;
459 vl.budb_volumeList_len = maxa;
460 vl.budb_volumeList_val = returnArray;
462 /* outline algorithm */
463 code = ubik_Call(BUDB_GetVolumes, udbHandle.uh_client, 0, BUDB_MAJORVERSION, BUDB_OP_VOLUMENAME, volumeName, /* name */
467 next, /* nextindex */
473 *nEntries = vl.budb_volumeList_len;
478 bcdb_UseTape(teptr, newFlag)
479 register struct budb_tapeEntry *teptr;
483 code = ubik_Call(BUDB_UseTape, udbHandle.uh_client, 0, teptr, newFlag);
488 /* ---- text configuration handling routines ----
491 * The caller should pass in/out a fid for an unlinked, open file to prevent
492 * tampering with the files contents;
496 * extract the specified textType and put it in a temporary, local
499 * ctPtr - ptr to client structure with all the required information
502 bcdb_GetTextFile(register udbClientTextP ctPtr)
504 afs_int32 bufferSize;
505 afs_int32 offset, nextOffset;
509 /* Initialize charlistT_val. We try to deallocate this structure based on
511 memset((void *)&charList, 0, sizeof(charList));
513 /* check params and cleanup any previous state */
514 if (ctPtr->lockHandle == 0)
515 ERROR(BUDB_INTERNALERROR);
517 if (ctPtr->textStream == NULL) /* Should have an open stream */
518 ERROR(BUDB_INTERNALERROR);
520 /* allocate a buffer */
522 charList.charListT_val = (char *)malloc(bufferSize);
523 if (charList.charListT_val == 0)
524 ERROR(BUDB_INTERNALERROR);
525 charList.charListT_len = bufferSize;
530 while (nextOffset != -1) {
532 charList.charListT_len = bufferSize;
534 ubik_Call(BUDB_GetText, udbHandle.uh_client, 0, ctPtr->lockHandle,
535 ctPtr->textType, bufferSize, offset, &nextOffset,
542 fwrite(charList.charListT_val, sizeof(char),
543 charList.charListT_len, ctPtr->textStream);
544 if (ferror(ctPtr->textStream))
545 ERROR(BUDB_INTERNALERROR);
547 ctPtr->textSize += charList.charListT_len;
550 /* get text version */
552 ubik_Call(BUDB_GetTextVersion, udbHandle.uh_client, 0,
553 ctPtr->textType, &ctPtr->textVersion);
558 fflush(ctPtr->textStream); /* debug */
560 /* exit, leaving the configuration text file open */
561 if (charList.charListT_val)
562 free(charList.charListT_val);
566 if (ctPtr->textStream != NULL) {
567 fclose(ctPtr->textStream);
568 ctPtr->textStream = NULL;
575 * save the text file in ubik database
577 * textType - identifies type of configuration file
578 * filename - where to get the text from
581 bcdb_SaveTextFile(ctPtr)
582 udbClientTextP ctPtr;
584 afs_int32 bufferSize;
585 afs_int32 offset, chunkSize, fileSize;
588 afs_int32 filesize();
590 /* allocate a buffer */
592 charList.charListT_val = (char *)malloc(bufferSize);
593 if (charList.charListT_val == 0)
594 ERROR(BUDB_INTERNALERROR);
595 charList.charListT_len = bufferSize;
597 if (ctPtr->textStream == NULL)
598 ERROR(BUDB_INTERNALERROR);
599 rewind(ctPtr->textStream);
601 fileSize = (afs_int32) filesize(ctPtr->textStream);
603 dprintf(("filesize is %d\n", fileSize));
605 rewind(ctPtr->textStream);
607 /* special case empty files */
609 charList.charListT_len = 0;
611 ubik_Call(BUDB_SaveText, udbHandle.uh_client, 0,
612 ctPtr->lockHandle, ctPtr->textType, 0,
613 BUDB_TEXT_COMPLETE, &charList);
618 while (fileSize != 0) {
619 chunkSize = MIN(fileSize, bufferSize);
621 fread(charList.charListT_val, sizeof(char), chunkSize,
624 if (code != chunkSize)
625 printf("code = %d\n", code);
626 if (ferror(ctPtr->textStream))
627 ERROR(BUDB_INTERNALERROR);
629 charList.charListT_len = chunkSize;
631 ubik_Call(BUDB_SaveText, udbHandle.uh_client, 0,
632 ctPtr->lockHandle, ctPtr->textType, offset,
633 (chunkSize == fileSize) ? BUDB_TEXT_COMPLETE : 0,
638 fileSize -= chunkSize;
643 /* if ( ctPtr->textStream >= 0 )
644 * close(ctPtr->textStream); */
645 if (charList.charListT_val)
646 free(charList.charListT_val);
650 bcdb_FindLastTape(dumpID, dumpEntry, tapeEntry, volEntry)
652 struct budb_dumpEntry *dumpEntry;
653 struct budb_tapeEntry *tapeEntry;
654 struct budb_volumeEntry *volEntry;
657 (BUDB_FindLastTape, udbHandle.uh_client, 0, dumpID, dumpEntry,
658 tapeEntry, volEntry));
661 bcdb_MakeDumpAppended(appendedDumpID, initialDumpID, startTapeSeq)
662 afs_int32 appendedDumpID;
663 afs_int32 initialDumpID;
664 afs_int32 startTapeSeq;
667 (BUDB_MakeDumpAppended, udbHandle.uh_client, 0, appendedDumpID,
668 initialDumpID, startTapeSeq));
672 /* -------------------------------------
673 * misc. support routines
674 * -------------------------------------
684 offset = ftell(stream);
685 fseek(stream, (afs_int32) 0, 2); /* end of file */
686 size = ftell(stream);
687 fseek(stream, offset, 0);
692 /* ------------------------------------
693 * misc. support routines - general text management
694 * ------------------------------------
699 * locks the text described by the ctPtr
701 * ctptr - client text ptr
708 register udbClientTextP ctPtr;
711 afs_int32 timeout, j = 0;
713 if (ctPtr->lockHandle != 0)
714 return (1); /* already locked */
717 ((ctPtr->textSize == 0) ? 30 : ((ctPtr->textSize / 50000) + 10));
721 ubik_Call(BUDB_GetLock, udbHandle.uh_client, 0,
722 udbHandle.uh_instanceId, ctPtr->textType, timeout,
724 if ((code != BUDB_LOCKED) && (code != BUDB_SELFLOCKED)) {
728 /* Mention something every 30 seconds */
730 com_err(whoami, code,
731 "; Waiting for db configuration text unlock");
734 #ifdef AFS_PTHREAD_ENV
743 ctPtr->lockHandle = 0;
748 * unlocks the text described by the ctPtr
750 * ctptr - client text ptr
757 register udbClientTextP ctPtr;
761 if (ctPtr->lockHandle == 0)
765 ubik_Call(BUDB_FreeLock, udbHandle.uh_client, 0, ctPtr->lockHandle);
766 ctPtr->lockHandle = 0;
768 /* Don't try to analyse the error. Let the lock timeout */
772 /* bc_CheckTextVersion
775 * n - out of date or error
778 bc_CheckTextVersion(ctPtr)
779 register udbClientTextP ctPtr;
784 if (ctPtr->textVersion == -1)
785 return (BC_VERSIONMISMATCH);
788 ubik_Call(BUDB_GetTextVersion, udbHandle.uh_client, 0,
789 ctPtr->textType, &tversion);
792 if (tversion != ctPtr->textVersion)
793 return (BC_VERSIONMISMATCH);
797 /* -------------------------------------
798 * initialization routines
799 * -------------------------------------
803 * Initialize a client for the vl ubik database.
805 vldbClientInit(noAuthFlag, localauth, cellName, cstruct, ttoken)
809 struct ubik_client **cstruct;
810 struct ktc_token *ttoken;
813 struct afsconf_dir *acdir;
814 struct rc_securityClass *sc;
815 afs_int32 i, scIndex = 0; /* Index of Rx security object - noauth */
816 struct afsconf_cell info;
817 struct ktc_principal sname;
818 struct rx_connection *serverconns[VLDB_MAXSERVERS];
821 /* Find out about the given cell */
823 afsconf_Open((localauth ? AFSDIR_SERVER_ETC_DIRPATH :
824 AFSDIR_CLIENT_ETC_DIRPATH));
826 com_err(whoami, 0, "Can't open configuration directory '%s'",
827 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
828 AFSDIR_CLIENT_ETC_DIRPATH));
829 ERROR(BC_NOCELLCONFIG);
835 code = afsconf_GetLocalCell(acdir, cname, sizeof(cname));
837 com_err(whoami, code,
838 "; Can't get the local cell name - check %s/%s",
839 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
840 AFSDIR_CLIENT_ETC_DIRPATH), AFSDIR_THISCELL_FILE);
843 strcpy(cellName, cname);
846 code = afsconf_GetCellInfo(acdir, cellName, AFSCONF_VLDBSERVICE, &info);
848 com_err(whoami, code, "; Can't find cell %s's hosts in %s/%s",
850 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
851 AFSDIR_CLIENT_ETC_DIRPATH), AFSDIR_CELLSERVDB_FILE);
852 ERROR(BC_NOCELLCONFIG);
856 * Grab tickets if we care about authentication.
860 code = afsconf_GetLatestKey(acdir, 0, 0);
862 com_err(whoami, code, "; Can't get key from local key file");
865 code = afsconf_ClientAuth(acdir, &sc, &scIndex);
867 com_err(whoami, code, "; Calling ClientAuth");
871 ttoken->endTime = NEVERDATE;
875 strcpy(sname.cell, info.name);
876 sname.instance[0] = 0;
877 strcpy(sname.name, "afs");
880 ktc_GetToken(&sname, ttoken, sizeof(struct ktc_token), NULL);
882 com_err(whoami, code, 0,
883 "; Can't get AFS tokens - running unauthenticated");
885 if ((ttoken->kvno < 0) || (ttoken->kvno > 255))
887 "Funny kvno (%d) in ticket, proceeding",
896 sc = rxnull_NewClientSecurityObject();
899 sc = (struct rx_securityClass *)
900 rxkad_NewClientSecurityObject(rxkad_clear,
902 ttoken->kvno, ttoken->ticketLen,
906 com_err(whoami, 0, "Unsupported authentication type %d", scIndex);
914 "Can't create a security object with security index %d",
919 /* tell UV module about default authentication */
920 UV_SetSecurity(sc, scIndex);
922 if (info.numServers > VLDB_MAXSERVERS) {
924 "Warning: %d VLDB servers exist for cell '%s', can only remember the first %d",
925 info.numServers, cellName, VLDB_MAXSERVERS);
926 info.numServers = VLDB_MAXSERVERS;
929 for (i = 0; i < info.numServers; i++)
931 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
932 info.hostAddr[i].sin_port, USER_SERVICE_ID, sc,
937 code = ubik_ClientInit(serverconns, cstruct);
939 com_err(whoami, code, "; Can't initialize ubik connection to vldb");
945 afsconf_Close(acdir);
950 * initialize a client for the backup systems ubik database.
954 udbClientInit(noAuthFlag, localauth, cellName)
959 struct ktc_principal principal;
960 struct ktc_token token;
961 struct afsconf_cell info;
962 struct afsconf_dir *acdir;
967 afsconf_Open((localauth ? AFSDIR_SERVER_ETC_DIRPATH :
968 AFSDIR_CLIENT_ETC_DIRPATH));
970 com_err(whoami, 0, "Can't open configuration directory '%s'",
971 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
972 AFSDIR_CLIENT_ETC_DIRPATH));
973 ERROR(BC_NOCELLCONFIG);
979 code = afsconf_GetLocalCell(acdir, cname, sizeof(cname));
981 com_err(whoami, code,
982 "; Can't get the local cell name - check %s/%s",
983 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
984 AFSDIR_CLIENT_ETC_DIRPATH), AFSDIR_THISCELL_FILE);
987 strcpy(cellName, cname);
990 code = afsconf_GetCellInfo(acdir, cellName, 0, &info);
992 com_err(whoami, code, "; Can't find cell %s's hosts in %s/%s",
994 (localauth ? AFSDIR_SERVER_ETC_DIRPATH :
995 AFSDIR_CLIENT_ETC_DIRPATH), AFSDIR_CELLSERVDB_FILE);
996 ERROR(BC_NOCELLCONFIG);
999 udbHandle.uh_scIndex = RX_SCINDEX_NULL;
1002 code = afsconf_GetLatestKey(acdir, 0, 0);
1004 com_err(whoami, code, "; Can't get key from local key file");
1008 afsconf_ClientAuth(acdir, &udbHandle.uh_secobj,
1009 &udbHandle.uh_scIndex);
1011 com_err(whoami, code, "; Calling ClientAuth");
1017 /* setup principal */
1018 strcpy(principal.cell, info.name);
1019 principal.instance[0] = 0;
1020 strcpy(principal.name, "afs");
1023 code = ktc_GetToken(&principal, &token, sizeof(token), NULL);
1025 com_err(whoami, code,
1026 "; Can't get tokens - running unauthenticated");
1028 if ((token.kvno < 0) || (token.kvno > 255))
1030 "Unexpected kvno (%d) in ticket - proceeding",
1032 udbHandle.uh_scIndex = RX_SCINDEX_KAD; /* Kerberos */
1036 switch (udbHandle.uh_scIndex) {
1038 udbHandle.uh_secobj = rxnull_NewClientSecurityObject();
1042 udbHandle.uh_secobj = (struct rx_securityClass *)
1043 rxkad_NewClientSecurityObject(rxkad_clear, &token.sessionKey,
1044 token.kvno, token.ticketLen,
1049 com_err(whoami, 0, "Unsupported authentication type %d",
1050 udbHandle.uh_scIndex);
1056 if (!udbHandle.uh_secobj) {
1058 "Can't create a security object with security index %d",
1059 udbHandle.uh_secobj);
1063 if (info.numServers > MAXSERVERS) {
1065 "Warning: %d BDB servers exist for cell '%s', can only remember the first %d",
1066 info.numServers, cellName, MAXSERVERS);
1067 info.numServers = MAXSERVERS;
1070 /* establish connections to the servers. Check for failed connections? */
1071 for (i = 0; i < info.numServers; i++) {
1072 udbHandle.uh_serverConn[i] =
1073 rx_NewConnection(info.hostAddr[i].sin_addr.s_addr,
1074 htons(AFSCONF_BUDBPORT), BUDB_SERVICE,
1075 udbHandle.uh_secobj, udbHandle.uh_scIndex);
1077 udbHandle.uh_serverConn[i] = 0;
1079 code = ubik_ClientInit(udbHandle.uh_serverConn, &udbHandle.uh_client);
1081 com_err(whoami, code,
1082 "; Can't initialize ubik connection to backup database");
1086 /* Try to quickly find a good site by setting deadtime low */
1087 for (i = 0; i < info.numServers; i++)
1088 rx_SetConnDeadTime(udbHandle.uh_client->conns[i], 1);
1090 ubik_Call(BUDB_GetInstanceId, udbHandle.uh_client, 0,
1091 &udbHandle.uh_instanceId);
1093 /* Reset dead time back up to default */
1094 for (i = 0; i < info.numServers; i++)
1095 rx_SetConnDeadTime(udbHandle.uh_client->conns[i], 60);
1097 /* If did not find a site on first quick pass, try again */
1100 ubik_Call(BUDB_GetInstanceId, udbHandle.uh_client, 0,
1101 &udbHandle.uh_instanceId);
1103 com_err(whoami, code, "; Can't access backup database");
1109 afsconf_Close(acdir);
1113 /* -------------------------------------
1114 * specialized ubik support
1115 * -------------------------------------
1123 * 1) first call with SINGLESERVER set, record the server to be used.
1124 * 2) subsequent calls use that server. If a failure is encountered,
1125 * the state is cleaned up and the error returned back to the caller.
1126 * 3) upon completion, the user must make a dummy call with
1127 * END_SINGLESERVER set, to clean up state.
1128 * 4) if the vanilla ubik_Call is ever modified, the END_SINGLESERVER
1129 * flag can be discarded. The first call without SINGLESERVER set
1130 * can clean up the state.
1133 struct ubikCallState {
1134 afs_int32 ucs_flags; /* state flags */
1135 afs_int32 ucs_selectedServer; /* which server selected */
1138 static struct ubikCallState uServer;
1140 /* ubik_Call_SingleServer
1141 * variant of ubik_Call. This is used by the backup system to initiate
1142 * a series of calls to a single ubik server. The first call sets up
1143 * process state (not recorded in ubik) that requires all further calls
1144 * of that group to be made to the same server process.
1146 * call this instead of stub and we'll guarantee to find a host that's up.
1147 * in the future, we should also put in a protocol to find the sync site
1151 ubik_Call_SingleServer(aproc, aclient, aflags, p1, p2, p3, p4, p5, p6, p7, p8,
1152 p9, p10, p11, p12, p13, p14, p15, p16)
1153 register struct ubik_client *aclient;
1156 char *p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9, *p10, *p11, *p12, *p13,
1159 register afs_int32 code;
1160 afs_int32 someCode, newHost, thisHost;
1161 register afs_int32 i;
1162 register afs_int32 count;
1165 struct rx_connection *tc;
1166 struct rx_peer *rxp;
1168 if ((aflags & (UF_SINGLESERVER | UF_END_SINGLESERVER)) != 0) {
1169 if (((aflags & UF_SINGLESERVER) != 0)
1170 && ((uServer.ucs_flags & UF_SINGLESERVER) != 0)
1173 /* have a selected server */
1174 tc = aclient->conns[uServer.ucs_selectedServer];
1177 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11,
1178 p12, p13, p14, p15, p16);
1180 /* error. Clean up single server state */
1181 memset(&uServer, 0, sizeof(uServer));
1184 } else if ((aflags & UF_END_SINGLESERVER) != 0) {
1185 memset(&uServer, 0, sizeof(uServer));
1190 someCode = UNOSERVERS;
1196 /* tc is the next conn to try */
1197 tc = aclient->conns[count];
1200 pass = 1; /* in pass 1, we look at down hosts, too */
1204 break; /* nothing left to try */
1206 if (pass == 0 && (aclient->states[count] & CFLastFailed)) {
1208 continue; /* this guy's down, try someone else first */
1212 (*aproc) (tc, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12,
1213 p13, p14, p15, p16);
1215 /* note that getting a UNOTSYNC error code back does *not* guarantee
1216 * that there is a sync site yet elected. However, if there is a
1217 * sync site out there somewhere, and you're trying an operation that
1218 * requires a sync site, ubik will return UNOTSYNC, indicating the
1219 * operation won't work until you find a sync site
1221 if (code == UNOTSYNC) { /*ns */
1222 /* means that this requires a sync site to work */
1223 someCode = code; /* remember an error, if this fails */
1225 /* now see if we can find the sync site host */
1226 code = VOTE_GetSyncSite(tc, &newHost);
1227 if (code == 0 && newHost != 0) {
1228 newHost = htonl(newHost); /* convert back to network order */
1230 /* position count at the appropriate slot in the client
1231 * structure and retry. If we can't find in slot, we'll just
1232 * continue through the whole list
1234 for (i = 0; i < MAXSERVERS; i++) { /*f */
1235 rxp = rx_PeerOf(aclient->conns[i]);
1236 if (!(thisHost = rx_HostOf(rxp))) {
1237 count++; /* host not found, try the next dude */
1240 if (thisHost == newHost) {
1241 /* avoid asking in a loop */
1242 if (chaseCount++ > 2)
1244 count = i; /* we were told to use this one */
1249 count++; /* not directed, keep looking for a sync site */
1252 else if (code == UNOQUORUM) { /* this guy is still recovering */
1256 } else if (code < 0) { /* network errors */
1258 aclient->states[count] |= CFLastFailed;
1262 /* ok, operation worked */
1263 aclient->states[count] &= ~CFLastFailed;
1264 /* either misc ubik code, or misc application code (incl success)
1267 /* if the call succeeded, setup connection state for subsequent
1271 && ((aflags & UF_SINGLESERVER) != 0)
1273 /* need to save state */
1274 uServer.ucs_flags = UF_SINGLESERVER;
1275 uServer.ucs_selectedServer = count;
1285 /* -------------------------------------
1286 * debug and test routines
1287 * -------------------------------------
1291 * For testing only. Open a connect to the database server running on
1294 * 0 - ubik connection established in the global udbHandle structure
1300 afs_int32 serverList[MAXSERVERS];
1306 /* get our host name */
1307 gethostname(hostname, sizeof(hostname));
1308 /* strcpy(hostname, "hops"); */
1311 args[1] = "-servers";
1314 code = ubik_ParseClientList(3, args, serverList);
1316 com_err(whoami, code, "; udbLocalInit: parsing ubik server list");
1320 udbHandle.uh_scIndex = RX_SCINDEX_NULL;
1321 udbHandle.uh_secobj = (struct rx_securityClass *)
1322 rxnull_NewClientSecurityObject();
1324 for (i = 0; serverList[i] != 0; i++) {
1325 udbHandle.uh_serverConn[i] =
1326 rx_NewConnection(serverList[i], htons(AFSCONF_BUDBPORT),
1327 BUDB_SERVICE, udbHandle.uh_secobj,
1328 udbHandle.uh_scIndex);
1329 if (udbHandle.uh_serverConn[i] == 0) {
1330 com_err(whoami, 0, "connection %d failed", i);
1334 udbHandle.uh_serverConn[i] = 0;
1335 code = ubik_ClientInit(udbHandle.uh_serverConn, &udbHandle.uh_client);
1337 com_err(whoami, code, "; in ubik_ClientInit");
1342 ubik_Call(BUDB_GetInstanceId, udbHandle.uh_client, 0,
1343 &udbHandle.uh_instanceId);
1345 com_err(whoami, code, "; Can't estblish instance Id");
1353 /* bc_openTextFile - This function opens a temp file to read in the
1354 * config text recd from the bu server. On Unix, an unlink() is done on
1355 * the file as soon as it is opened, so when the program exits, the file will
1356 * be removed automatically, while being invisible while in use.
1357 * On NT, however, the file must be explicitly deleted after use with an unlink()
1359 * Pointer to a udhClientTextP struct. The open stream ptr is stored in
1360 * the udbClientTextP.textStream member.
1361 * Output: The temp file name is returned in tmpFileName. This should be used
1362 * to delete the file when done with it.
1368 bc_openTextFile(udbClientTextP ctPtr, char *tmpFileName)
1372 if (ctPtr->textStream != NULL)
1373 fclose(ctPtr->textStream);
1375 sprintf(tmpFileName, "%s/bu_XXXXXX", gettmpdir());
1376 #ifdef AFS_LINUX20_ENV
1377 mkstemp(tmpFileName);
1379 mktemp(tmpFileName);
1381 ctPtr->textStream = fopen(tmpFileName, "w+");
1382 if (ctPtr->textStream == NULL)
1383 ERROR(BUDB_INTERNALERROR);
1385 #ifndef AFS_NT40_ENV /* This can't be done on NT */
1386 /* make the file invisible to others */
1387 code = unlink(tmpFileName);
1392 dprintf(("file is %s\n", tmpFileName));
1398 if (ctPtr->textStream != NULL) {
1399 fclose(ctPtr->textStream);
1400 ctPtr->textStream = NULL;
1406 /* bc_closeTextFile: This function closes any actual temp files associated with
1407 * a udbClientText structure.
1408 * Input: ctPtr->textStream - stream to close
1409 * tmpFileName - temp file name to delete
1415 bc_closeTextFile(udbClientTextP ctPtr, char *tmpFileName)
1419 if (ctPtr->textStream)
1420 fclose(ctPtr->textStream);
1422 if (*tmpFileName) { /* we have a valid name */
1423 code = unlink(tmpFileName);