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"
27 #include <afs/afsint.h>
30 char AFSConfigKeyName[] =
31 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
33 #define AFS_THISCELL "ThisCell"
34 #define AFS_CELLSERVDB_UNIX "CellServDB"
35 #define AFS_CELLSERVDB_NT "afsdcell.ini"
36 #define AFSDIR_CLIENT_ETC_DIRPATH "c:/afs"
37 #if defined(DJGPP) || defined(AFS_WIN95_ENV)
38 #define AFS_CELLSERVDB AFS_CELLSERVDB_UNIX
40 extern char cm_confDir[];
44 #define AFS_CELLSERVDB AFS_CELLSERVDB_NT
45 #endif /* DJGPP || WIN95 */
48 static long cm_ParsePair(char *lineBufferp, char *leftp, char *rightp)
57 for(tp = lineBufferp; *tp; tp++) {
66 /* comment or line end */
67 if (tc == '#' || tc == '\r' || tc == '\n') break;
69 /* square bracket comment -- look for closing delim
70 if (tc == '[') {sawBracket = 1; continue;}
73 if (tc == ' ' || tc == '\t') continue;
80 /* now we have a real character, put it in the appropriate bucket */
89 /* null terminate the strings */
93 return 0; /* and return success */
96 /* search for a cell, and either return an error code if we don't find it,
97 * or return 0 if we do, in which case we also fill in the addresses in
100 * new feature: we can handle abbreviations and are insensitive to case.
101 * If the caller wants the "real" cell name, it puts a non-null pointer in
102 * newCellNamep. Anomaly: if cellNamep is ambiguous, we may modify
103 * newCellNamep but return an error code.
105 long cm_SearchCellFile(char *cellNamep, char *newCellNamep,
106 cm_configProc_t *procp, void *rockp)
110 FILE *tfilep, *bestp, *tempp;
112 char lineBuffer[256];
115 struct sockaddr_in vlSockAddr;
119 int tracking = 1, partial = 0;
120 #if defined(DJGPP) || defined(AFS_WIN95_ENV)
130 code = GetWindowsDirectory(wdir, sizeof(wdir));
131 if (code == 0 || code > sizeof(wdir)) return -1;
133 /* add trailing backslash, if required */
135 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
137 strcpy(wdir, cm_confDir);
141 strcat(wdir, AFS_CELLSERVDB);
143 tfilep = fopen(wdir, "r");
146 /* If we are using DJGPP client, cellservdb will be in afsconf dir. */
147 /* If we are in Win95 here, we are linking with klog etc. and are
148 using DJGPP client even though DJGPP is not defined. So we still
149 need to check AFSCONF for location. */
150 afsconf_path = getenv("AFSCONF");
152 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
154 strcpy(wdir, afsconf_path);
156 strcat(wdir, AFS_CELLSERVDB_UNIX);
157 /*fprintf(stderr, "opening cellservdb file %s\n", wdir);*/
158 tfilep = fopen(wdir, "r");
159 if (!tfilep) return -2;
162 bestp = fopen(wdir, "r");
164 /* have we seen the cell line for the guy we're looking for? */
167 tp = fgets(lineBuffer, sizeof(lineBuffer), tfilep);
169 (void) fgets(lineBuffer, sizeof(lineBuffer), bestp);
175 * found partial match earlier;
188 return (foundCell? 0 : -3);
193 /* turn trailing cr or lf into null */
194 tp = strchr(lineBuffer, '\r');
196 tp = strchr(lineBuffer, '\n');
199 /* skip blank lines */
200 if (lineBuffer[0] == 0) continue;
202 if (lineBuffer[0] == '>') {
203 /* trim off at white space or '#' chars */
204 tp = strchr(lineBuffer, ' ');
206 tp = strchr(lineBuffer, '\t');
208 tp = strchr(lineBuffer, '#');
211 /* now see if this is the right cell */
212 if (stricmp(lineBuffer+1, cellNamep) == 0) {
213 /* found the cell we're looking for */
215 strcpy(newCellNamep, lineBuffer+1);
219 else if (strnicmp(lineBuffer+1, cellNamep,
220 strlen(cellNamep)) == 0) {
222 if (partial) { /* ambiguous */
228 strcpy(newCellNamep, lineBuffer+1);
233 else inRightCell = 0;
236 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
237 valuep = strchr(lineBuffer, '#');
238 if (valuep == NULL) {
243 valuep++; /* skip the "#" */
245 valuep += strspn(valuep, " "); /* skip SP & TAB */
248 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
249 /* add the server to the VLDB list */
250 thp = gethostbyname(valuep);
252 memcpy(&vlSockAddr.sin_addr.s_addr, thp->h_addr,
254 vlSockAddr.sin_family = AF_INET;
255 /* sin_port supplied by connection code */
257 (*procp)(rockp, &vlSockAddr, valuep);
261 /* For DJGPP, we will read IP address instead
262 of name/comment field */
263 code = sscanf(lineBuffer, "%d.%d.%d.%d #%s",
264 &c1, &c2, &c3, &c4, aname);
265 tp = (char *) &ip_addr;
270 memcpy(&vlSockAddr.sin_addr.s_addr, &ip_addr,
272 vlSockAddr.sin_family = AF_INET;
273 /* sin_port supplied by connection code */
275 (*procp)(rockp, &vlSockAddr, valuep);
280 } /* while loop processing all lines */
283 long cm_SearchCellByDNS(char *cellNamep, char *newCellNamep, int *ttl,
284 cm_configProc_t *procp, void *rockp)
288 int cellHosts[AFSMAXCELLHOSTS];
291 struct sockaddr_in vlSockAddr;
293 rc = getAFSServer(cellNamep, cellHosts, &numServers, ttl);
294 if (rc == 0 && numServers > 0) { /* found the cell */
295 for (i = 0; i < numServers; i++) {
296 memcpy(&vlSockAddr.sin_addr.s_addr, &cellHosts[i],
298 vlSockAddr.sin_family = AF_INET;
299 /* sin_port supplied by connection code */
301 (*procp)(rockp, &vlSockAddr, NULL);
303 strcpy(newCellNamep,cellNamep);
305 return 0; /* found cell */
308 #endif /* AFS_AFSDB_ENV */
309 return -1; /* not found */
312 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
313 /* look up the root cell's name in the Registry */
314 long cm_GetRootCellName(char *cellNamep)
316 DWORD code, dummyLen;
319 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
320 0, KEY_QUERY_VALUE, &parmKey);
321 if (code != ERROR_SUCCESS)
325 code = RegQueryValueEx(parmKey, "Cell", NULL, NULL,
326 cellNamep, &dummyLen);
327 RegCloseKey (parmKey);
328 if (code != ERROR_SUCCESS)
334 /* look up the root cell's name in the THISCELL file */
335 long cm_GetRootCellName(char *cellNamep)
338 char thisCellPath[256];
343 strcpy(thisCellPath, cm_confDir);
346 afsconf_path = getenv("AFSCONF");
348 strcpy(thisCellPath, AFSDIR_CLIENT_ETC_DIRPATH);
350 strcpy(thisCellPath, afsconf_path);
352 strcat(thisCellPath,"/");
354 strcat(thisCellPath, AFS_THISCELL);
355 thisCell = fopen(thisCellPath, "r");
356 if (thisCell == NULL)
359 fgets(cellNamep, 256, thisCell);
362 newline = strrchr(cellNamep,'\n');
363 if (newline) *newline = '\0';
364 newline = strrchr(cellNamep,'\r');
365 if (newline) *newline = '\0';
371 cm_configFile_t *cm_CommonOpen(char *namep, char *rwp)
379 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
380 code = GetWindowsDirectory(wdir, sizeof(wdir));
381 if (code == 0 || code > sizeof(wdir)) return NULL;
383 /* add trailing backslash, if required */
385 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
388 strcpy(wdir,cm_confDir);
390 afsconf_path = getenv("AFSCONF");
392 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
394 strcpy(wdir, afsconf_path);
397 #endif /* DJGPP || WIN95 */
401 tfilep = fopen(wdir, rwp);
403 return ((cm_configFile_t *) tfilep);
407 long cm_WriteConfigString(char *labelp, char *valuep)
409 DWORD code, dummyDisp;
412 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
413 0, "container", 0, KEY_SET_VALUE, NULL,
414 &parmKey, &dummyDisp);
415 if (code != ERROR_SUCCESS)
418 code = RegSetValueEx(parmKey, labelp, 0, REG_SZ,
419 valuep, strlen(valuep) + 1);
420 RegCloseKey (parmKey);
421 if (code != ERROR_SUCCESS)
429 long cm_WriteConfigInt(char *labelp, long value)
431 DWORD code, dummyDisp;
434 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
435 0, "container", 0, KEY_SET_VALUE, NULL,
436 &parmKey, &dummyDisp);
437 if (code != ERROR_SUCCESS)
440 code = RegSetValueEx(parmKey, labelp, 0, REG_DWORD,
441 &value, sizeof(value));
442 RegCloseKey (parmKey);
443 if (code != ERROR_SUCCESS)
450 cm_configFile_t *cm_OpenCellFile(void)
452 cm_configFile_t *cfp;
454 cfp = cm_CommonOpen("afsdcel2.ini", "w");
458 long cm_AppendPrunedCellList(cm_configFile_t *ofp, char *cellNamep)
460 cm_configFile_t *tfilep; /* input file */
462 char lineBuffer[256];
467 tfilep = cm_CommonOpen(AFS_CELLSERVDB, "r");
468 if (!tfilep) return -1;
472 /* have we seen the cell line for the guy we're looking for? */
475 tp = fgets(lineBuffer, sizeof(lineBuffer), (FILE *)tfilep);
477 if (feof((FILE *)tfilep)) {
479 fclose((FILE *)tfilep);
484 /* turn trailing cr or lf into null */
485 tp = strchr(lineBuffer, '\r');
487 tp = strchr(lineBuffer, '\n');
490 /* skip blank lines */
491 if (lineBuffer[0] == 0) {
492 fprintf((FILE *)ofp, "%s\n", lineBuffer);
496 if (lineBuffer[0] == '>') {
497 /* trim off at white space or '#' chars */
498 tp = strchr(lineBuffer, ' ');
500 tp = strchr(lineBuffer, '\t');
502 tp = strchr(lineBuffer, '#');
505 /* now see if this is the right cell */
506 if (strcmp(lineBuffer+1, cellNamep) == 0) {
507 /* found the cell we're looking for */
512 fprintf((FILE *)ofp, "%s\n", lineBuffer);
516 valuep = strchr(lineBuffer, '#');
517 if (valuep == NULL) return -2;
518 valuep++; /* skip the "#" */
520 fprintf((FILE *)ofp, "%s\n", lineBuffer);
523 } /* while loop processing all lines */
526 long cm_AppendNewCell(cm_configFile_t *filep, char *cellNamep)
528 fprintf((FILE *)filep, ">%s\n", cellNamep);
532 long cm_AppendNewCellLine(cm_configFile_t *filep, char *linep)
534 fprintf((FILE *)filep, "%s\n", linep);
538 extern long cm_CloseCellFile(cm_configFile_t *filep)
547 closeCode = fclose((FILE *)filep);
549 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
550 code = GetWindowsDirectory(wdir, sizeof(wdir));
551 if (code == 0 || code > sizeof(wdir)) return NULL;
553 /* add trailing backslash, if required */
555 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
558 strcpy(wdir,cm_confDir);
560 afsconf_path = getenv("AFSCONF");
562 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
564 strcpy(wdir, afsconf_path);
567 #endif /* DJGPP || WIN95 */
571 if (closeCode != 0) {
572 /* something went wrong, preserve original database */
573 strcat(wdir, "afsdcel2.ini");
578 strcat(wdir, AFS_CELLSERVDB);
579 strcat(sdir, "afsdcel2.ini"); /* new file */
581 unlink(wdir); /* delete old file */
583 code = rename(sdir, wdir); /* do the rename */
585 if (code) code = errno;
590 void cm_GetConfigDir(char *dir)
597 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
598 code = GetWindowsDirectory(wdir, sizeof(wdir));
599 if (code == 0 || code > sizeof(wdir)) wdir[0] = 0;
601 /* add trailing backslash, if required */
603 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
606 strcpy(wdir,cm_confDir);
608 afsconf_path = getenv("AFSCONF");
610 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
612 strcpy(wdir, afsconf_path);
615 #endif /* DJGPP || WIN95 */