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 #include <afs/param.h>
17 #include <sys/socket.h>
24 #include "cm_config.h"
26 char AFSConfigKeyName[] =
27 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
29 #define AFS_THISCELL "ThisCell"
30 #define AFS_CELLSERVDB_UNIX "CellServDB"
31 #define AFS_CELLSERVDB_NT "afsdcell.ini"
32 #define AFSDIR_CLIENT_ETC_DIRPATH "c:/afs"
33 #if defined(DJGPP) || defined(AFS_WIN95_ENV)
34 #define AFS_CELLSERVDB AFS_CELLSERVDB_UNIX
36 extern char cm_confDir[];
40 #define AFS_CELLSERVDB AFS_CELLSERVDB_NT
41 #endif /* DJGPP || WIN95 */
44 static long cm_ParsePair(char *lineBufferp, char *leftp, char *rightp)
53 for(tp = lineBufferp; *tp; tp++) {
62 /* comment or line end */
63 if (tc == '#' || tc == '\r' || tc == '\n') break;
65 /* square bracket comment -- look for closing delim
66 if (tc == '[') {sawBracket = 1; continue;}
69 if (tc == ' ' || tc == '\t') continue;
76 /* now we have a real character, put it in the appropriate bucket */
85 /* null terminate the strings */
89 return 0; /* and return success */
92 /* search for a cell, and either return an error code if we don't find it,
93 * or return 0 if we do, in which case we also fill in the addresses in
96 * new feature: we can handle abbreviations and are insensitive to case.
97 * If the caller wants the "real" cell name, it puts a non-null pointer in
98 * newCellNamep. Anomaly: if cellNamep is ambiguous, we may modify
99 * newCellNamep but return an error code.
101 long cm_SearchCellFile(char *cellNamep, char *newCellNamep,
102 cm_configProc_t *procp, void *rockp)
106 FILE *tfilep, *bestp, *tempp;
108 char lineBuffer[256];
111 struct sockaddr_in vlSockAddr;
115 int tracking = 1, partial = 0;
117 #if defined(DJGPP) || defined(AFS_WIN95_ENV)
126 code = GetWindowsDirectory(wdir, sizeof(wdir));
127 if (code == 0 || code > sizeof(wdir)) return -1;
129 /* add trailing backslash, if required */
131 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
133 strcpy(wdir, cm_confDir);
137 strcat(wdir, AFS_CELLSERVDB);
139 tfilep = fopen(wdir, "r");
142 /* If we are using DJGPP client, cellservdb will be in afsconf dir. */
143 /* If we are in Win95 here, we are linking with klog etc. and are
144 using DJGPP client even though DJGPP is not defined. So we still
145 need to check AFSCONF for location. */
146 afsconf_path = getenv("AFSCONF");
148 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
150 strcpy(wdir, afsconf_path);
152 strcat(wdir, AFS_CELLSERVDB_UNIX);
153 /*fprintf(stderr, "opening cellservdb file %s\n", wdir);*/
154 tfilep = fopen(wdir, "r");
155 if (!tfilep) return -2;
158 bestp = fopen(wdir, "r");
160 /* have we seen the cell line for the guy we're looking for? */
163 tp = fgets(lineBuffer, sizeof(lineBuffer), tfilep);
165 (void) fgets(lineBuffer, sizeof(lineBuffer), bestp);
171 * found partial match earlier;
184 return (foundCell? 0 : -3);
189 /* turn trailing cr or lf into null */
190 tp = strchr(lineBuffer, '\r');
192 tp = strchr(lineBuffer, '\n');
195 /* skip blank lines */
196 if (lineBuffer[0] == 0) continue;
198 if (lineBuffer[0] == '>') {
199 /* trim off at white space or '#' chars */
200 tp = strchr(lineBuffer, ' ');
202 tp = strchr(lineBuffer, '\t');
204 tp = strchr(lineBuffer, '#');
207 /* now see if this is the right cell */
208 if (stricmp(lineBuffer+1, cellNamep) == 0) {
209 /* found the cell we're looking for */
211 strcpy(newCellNamep, lineBuffer+1);
215 else if (strnicmp(lineBuffer+1, cellNamep,
216 strlen(cellNamep)) == 0) {
218 if (partial) { /* ambiguous */
224 strcpy(newCellNamep, lineBuffer+1);
229 else inRightCell = 0;
232 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
233 valuep = strchr(lineBuffer, '#');
234 if (valuep == NULL) {
239 valuep++; /* skip the "#" */
241 valuep += strspn(valuep, " "); /* skip SP & TAB */
244 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
245 /* add the server to the VLDB list */
246 thp = gethostbyname(valuep);
248 memcpy(&vlSockAddr.sin_addr.s_addr, thp->h_addr,
250 vlSockAddr.sin_family = AF_INET;
251 /* sin_port supplied by connection code */
253 (*procp)(rockp, &vlSockAddr, valuep);
257 /* For DJGPP, we will read IP address instead
258 of name/comment field */
259 code = sscanf(lineBuffer, "%d.%d.%d.%d #%s",
260 &c1, &c2, &c3, &c4, aname);
261 tp = (char *) &ip_addr;
266 memcpy(&vlSockAddr.sin_addr.s_addr, &ip_addr,
268 vlSockAddr.sin_family = AF_INET;
269 /* sin_port supplied by connection code */
271 (*procp)(rockp, &vlSockAddr, valuep);
276 } /* while loop processing all lines */
279 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
280 /* look up the root cell's name in the Registry */
281 long cm_GetRootCellName(char *cellNamep)
283 DWORD code, dummyLen;
286 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
287 0, KEY_QUERY_VALUE, &parmKey);
288 if (code != ERROR_SUCCESS)
292 code = RegQueryValueEx(parmKey, "Cell", NULL, NULL,
293 cellNamep, &dummyLen);
294 RegCloseKey (parmKey);
295 if (code != ERROR_SUCCESS)
301 /* look up the root cell's name in the THISCELL file */
302 long cm_GetRootCellName(char *cellNamep)
305 char thisCellPath[256];
310 strcpy(thisCellPath, cm_confDir);
313 afsconf_path = getenv("AFSCONF");
315 strcpy(thisCellPath, AFSDIR_CLIENT_ETC_DIRPATH);
317 strcpy(thisCellPath, afsconf_path);
319 strcat(thisCellPath,"/");
321 strcat(thisCellPath, AFS_THISCELL);
322 thisCell = fopen(thisCellPath, "r");
323 if (thisCell == NULL)
326 fgets(cellNamep, 256, thisCell);
329 newline = strrchr(cellNamep,'\n');
330 if (newline) *newline = '\0';
331 newline = strrchr(cellNamep,'\r');
332 if (newline) *newline = '\0';
338 cm_configFile_t *cm_CommonOpen(char *namep, char *rwp)
346 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
347 code = GetWindowsDirectory(wdir, sizeof(wdir));
348 if (code == 0 || code > sizeof(wdir)) return NULL;
350 /* add trailing backslash, if required */
352 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
355 strcpy(wdir,cm_confDir);
357 afsconf_path = getenv("AFSCONF");
359 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
361 strcpy(wdir, afsconf_path);
364 #endif /* DJGPP || WIN95 */
368 tfilep = fopen(wdir, rwp);
370 return ((cm_configFile_t *) tfilep);
374 long cm_WriteConfigString(char *labelp, char *valuep)
376 DWORD code, dummyDisp;
379 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
380 0, "container", 0, KEY_SET_VALUE, NULL,
381 &parmKey, &dummyDisp);
382 if (code != ERROR_SUCCESS)
385 code = RegSetValueEx(parmKey, labelp, 0, REG_SZ,
386 valuep, strlen(valuep) + 1);
387 RegCloseKey (parmKey);
388 if (code != ERROR_SUCCESS)
396 long cm_WriteConfigInt(char *labelp, long value)
398 DWORD code, dummyDisp;
401 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
402 0, "container", 0, KEY_SET_VALUE, NULL,
403 &parmKey, &dummyDisp);
404 if (code != ERROR_SUCCESS)
407 code = RegSetValueEx(parmKey, labelp, 0, REG_DWORD,
408 &value, sizeof(value));
409 RegCloseKey (parmKey);
410 if (code != ERROR_SUCCESS)
417 cm_configFile_t *cm_OpenCellFile(void)
419 cm_configFile_t *cfp;
421 cfp = cm_CommonOpen("afsdcel2.ini", "w");
425 long cm_AppendPrunedCellList(cm_configFile_t *ofp, char *cellNamep)
427 cm_configFile_t *tfilep; /* input file */
429 char lineBuffer[256];
434 tfilep = cm_CommonOpen(AFS_CELLSERVDB, "r");
435 if (!tfilep) return -1;
439 /* have we seen the cell line for the guy we're looking for? */
442 tp = fgets(lineBuffer, sizeof(lineBuffer), (FILE *)tfilep);
444 if (feof((FILE *)tfilep)) {
446 fclose((FILE *)tfilep);
451 /* turn trailing cr or lf into null */
452 tp = strchr(lineBuffer, '\r');
454 tp = strchr(lineBuffer, '\n');
457 /* skip blank lines */
458 if (lineBuffer[0] == 0) {
459 fprintf((FILE *)ofp, "%s\n", lineBuffer);
463 if (lineBuffer[0] == '>') {
464 /* trim off at white space or '#' chars */
465 tp = strchr(lineBuffer, ' ');
467 tp = strchr(lineBuffer, '\t');
469 tp = strchr(lineBuffer, '#');
472 /* now see if this is the right cell */
473 if (strcmp(lineBuffer+1, cellNamep) == 0) {
474 /* found the cell we're looking for */
479 fprintf((FILE *)ofp, "%s\n", lineBuffer);
483 valuep = strchr(lineBuffer, '#');
484 if (valuep == NULL) return -2;
485 valuep++; /* skip the "#" */
487 fprintf((FILE *)ofp, "%s\n", lineBuffer);
490 } /* while loop processing all lines */
493 long cm_AppendNewCell(cm_configFile_t *filep, char *cellNamep)
495 fprintf((FILE *)filep, ">%s\n", cellNamep);
499 long cm_AppendNewCellLine(cm_configFile_t *filep, char *linep)
501 fprintf((FILE *)filep, "%s\n", linep);
505 extern long cm_CloseCellFile(cm_configFile_t *filep)
514 closeCode = fclose((FILE *)filep);
516 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
517 code = GetWindowsDirectory(wdir, sizeof(wdir));
518 if (code == 0 || code > sizeof(wdir)) return NULL;
520 /* add trailing backslash, if required */
522 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
525 strcpy(wdir,cm_confDir);
527 afsconf_path = getenv("AFSCONF");
529 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
531 strcpy(wdir, afsconf_path);
534 #endif /* DJGPP || WIN95 */
538 if (closeCode != 0) {
539 /* something went wrong, preserve original database */
540 strcat(wdir, "afsdcel2.ini");
545 strcat(wdir, AFS_CELLSERVDB);
546 strcat(sdir, "afsdcel2.ini"); /* new file */
548 unlink(wdir); /* delete old file */
550 code = rename(sdir, wdir); /* do the rename */
552 if (code) code = errno;