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 #ifndef AFSDIR_CLIENT_ETC_DIRPATH
37 #define AFSDIR_CLIENT_ETC_DIRPATH "c:/afs"
39 #if defined(DJGPP) || defined(AFS_WIN95_ENV)
40 #define AFS_CELLSERVDB AFS_CELLSERVDB_UNIX
42 extern char cm_confDir[];
46 #define AFS_CELLSERVDB AFS_CELLSERVDB_NT
47 #endif /* DJGPP || WIN95 */
50 static long cm_ParsePair(char *lineBufferp, char *leftp, char *rightp)
59 for(tp = lineBufferp; *tp; tp++) {
68 /* comment or line end */
69 if (tc == '#' || tc == '\r' || tc == '\n')
72 /* square bracket comment -- look for closing delim */
79 if (tc == ' ' || tc == '\t')
87 /* now we have a real character, put it in the appropriate bucket */
96 /* null terminate the strings */
100 return 0; /* and return success */
103 /* search for a cell, and either return an error code if we don't find it,
104 * or return 0 if we do, in which case we also fill in the addresses in
107 * new feature: we can handle abbreviations and are insensitive to case.
108 * If the caller wants the "real" cell name, it puts a non-null pointer in
109 * newCellNamep. Anomaly: if cellNamep is ambiguous, we may modify
110 * newCellNamep but return an error code.
112 long cm_SearchCellFile(char *cellNamep, char *newCellNamep,
113 cm_configProc_t *procp, void *rockp)
117 FILE *tfilep, *bestp, *tempp;
119 char lineBuffer[256];
122 struct sockaddr_in vlSockAddr;
126 int tracking = 1, partial = 0;
127 #if defined(DJGPP) || defined(AFS_WIN95_ENV)
137 code = GetWindowsDirectory(wdir, sizeof(wdir));
138 if (code == 0 || code > sizeof(wdir))
141 /* add trailing backslash, if required */
143 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
145 strcpy(wdir, cm_confDir);
149 strcat(wdir, AFS_CELLSERVDB);
151 tfilep = fopen(wdir, "r");
154 /* If we are using DJGPP client, cellservdb will be in afsconf dir. */
155 /* If we are in Win95 here, we are linking with klog etc. and are
156 using DJGPP client even though DJGPP is not defined. So we still
157 need to check AFSCONF for location. */
158 afsconf_path = getenv("AFSCONF");
160 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
162 strcpy(wdir, afsconf_path);
164 strcat(wdir, AFS_CELLSERVDB_UNIX);
165 /*fprintf(stderr, "opening cellservdb file %s\n", wdir);*/
166 tfilep = fopen(wdir, "r");
167 if (!tfilep) return -2;
170 bestp = fopen(wdir, "r");
172 /* have we seen the cell line for the guy we're looking for? */
175 tp = fgets(lineBuffer, sizeof(lineBuffer), tfilep);
177 (void) fgets(lineBuffer, sizeof(lineBuffer), bestp);
183 * found partial match earlier;
196 return (foundCell? 0 : -3);
201 /* turn trailing cr or lf into null */
202 tp = strchr(lineBuffer, '\r');
204 tp = strchr(lineBuffer, '\n');
207 /* skip blank lines */
208 if (lineBuffer[0] == 0) continue;
210 if (lineBuffer[0] == '>') {
211 /* trim off at white space or '#' chars */
212 tp = strchr(lineBuffer, ' ');
214 tp = strchr(lineBuffer, '\t');
216 tp = strchr(lineBuffer, '#');
219 /* now see if this is the right cell */
220 if (stricmp(lineBuffer+1, cellNamep) == 0) {
221 /* found the cell we're looking for */
223 strcpy(newCellNamep, lineBuffer+1);
227 else if (strnicmp(lineBuffer+1, cellNamep,
228 strlen(cellNamep)) == 0) {
230 if (partial) { /* ambiguous */
236 strcpy(newCellNamep, lineBuffer+1);
241 else inRightCell = 0;
244 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
245 valuep = strchr(lineBuffer, '#');
246 if (valuep == NULL) {
251 valuep++; /* skip the "#" */
253 valuep += strspn(valuep, " \t"); /* skip SP & TAB */
254 /* strip spaces and tabs in the end. They should not be there according to CellServDB format
255 so do this just in case */
256 while (valuep[strlen(valuep) - 1] == ' ' || valuep[strlen(valuep) - 1] == '\t')
257 valuep[strlen(valuep) - 1] = '\0';
261 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
262 /* add the server to the VLDB list */
263 thp = gethostbyname(valuep);
265 memcpy(&vlSockAddr.sin_addr.s_addr, thp->h_addr,
267 vlSockAddr.sin_family = AF_INET;
268 /* sin_port supplied by connection code */
270 (*procp)(rockp, &vlSockAddr, valuep);
274 /* For DJGPP, we will read IP address instead
275 of name/comment field */
276 code = sscanf(lineBuffer, "%d.%d.%d.%d #%s",
277 &c1, &c2, &c3, &c4, aname);
278 tp = (char *) &ip_addr;
283 memcpy(&vlSockAddr.sin_addr.s_addr, &ip_addr,
285 vlSockAddr.sin_family = AF_INET;
286 /* sin_port supplied by connection code */
288 (*procp)(rockp, &vlSockAddr, valuep);
293 } /* while loop processing all lines */
295 /* if for some unknown reason cell is not found, return negative code (-11) ??? */
296 return (foundCell) ? 0 : -11;
299 long cm_SearchCellByDNS(char *cellNamep, char *newCellNamep, int *ttl,
300 cm_configProc_t *procp, void *rockp)
304 int cellHosts[AFSMAXCELLHOSTS];
307 struct sockaddr_in vlSockAddr;
309 rc = getAFSServer(cellNamep, cellHosts, &numServers, ttl);
310 if (rc == 0 && numServers > 0) { /* found the cell */
311 for (i = 0; i < numServers; i++) {
312 memcpy(&vlSockAddr.sin_addr.s_addr, &cellHosts[i],
314 vlSockAddr.sin_family = AF_INET;
315 /* sin_port supplied by connection code */
317 (*procp)(rockp, &vlSockAddr, NULL);
319 strcpy(newCellNamep,cellNamep);
321 return 0; /* found cell */
324 #endif /* AFS_AFSDB_ENV */
325 return -1; /* not found */
328 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
329 /* look up the root cell's name in the Registry */
330 long cm_GetRootCellName(char *cellNamep)
332 DWORD code, dummyLen;
335 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
336 0, KEY_QUERY_VALUE, &parmKey);
337 if (code != ERROR_SUCCESS)
341 code = RegQueryValueEx(parmKey, "Cell", NULL, NULL,
342 cellNamep, &dummyLen);
343 RegCloseKey (parmKey);
344 if (code != ERROR_SUCCESS)
350 /* look up the root cell's name in the THISCELL file */
351 long cm_GetRootCellName(char *cellNamep)
354 char thisCellPath[256];
358 strcpy(thisCellPath, cm_confDir);
362 afsconf_path = getenv("AFSCONF");
364 strcpy(thisCellPath, AFSDIR_CLIENT_ETC_DIRPATH);
366 strcpy(thisCellPath, afsconf_path);
368 strcat(thisCellPath,"/");
370 strcat(thisCellPath, AFS_THISCELL);
371 thisCell = fopen(thisCellPath, "r");
372 if (thisCell == NULL)
375 fgets(cellNamep, 256, thisCell);
378 newline = strrchr(cellNamep,'\n');
379 if (newline) *newline = '\0';
380 newline = strrchr(cellNamep,'\r');
381 if (newline) *newline = '\0';
387 cm_configFile_t *cm_CommonOpen(char *namep, char *rwp)
394 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
395 code = GetWindowsDirectory(wdir, sizeof(wdir));
396 if (code == 0 || code > sizeof(wdir))
399 /* add trailing backslash, if required */
401 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
404 strcpy(wdir,cm_confDir);
406 char *afsconf_path = getenv("AFSCONF");
408 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
410 strcpy(wdir, afsconf_path);
413 #endif /* DJGPP || WIN95 */
417 tfilep = fopen(wdir, rwp);
419 return ((cm_configFile_t *) tfilep);
423 long cm_WriteConfigString(char *labelp, char *valuep)
425 DWORD code, dummyDisp;
428 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
429 0, "container", 0, KEY_SET_VALUE, NULL,
430 &parmKey, &dummyDisp);
431 if (code != ERROR_SUCCESS)
434 code = RegSetValueEx(parmKey, labelp, 0, REG_SZ,
435 valuep, strlen(valuep) + 1);
436 RegCloseKey (parmKey);
437 if (code != ERROR_SUCCESS)
445 long cm_WriteConfigInt(char *labelp, long value)
447 DWORD code, dummyDisp;
450 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
451 0, "container", 0, KEY_SET_VALUE, NULL,
452 &parmKey, &dummyDisp);
453 if (code != ERROR_SUCCESS)
456 code = RegSetValueEx(parmKey, labelp, 0, REG_DWORD,
457 (LPBYTE)&value, sizeof(value));
458 RegCloseKey (parmKey);
459 if (code != ERROR_SUCCESS)
466 cm_configFile_t *cm_OpenCellFile(void)
468 cm_configFile_t *cfp;
470 cfp = cm_CommonOpen("afsdcel2.ini", "w");
474 long cm_AppendPrunedCellList(cm_configFile_t *ofp, char *cellNamep)
476 cm_configFile_t *tfilep; /* input file */
478 char lineBuffer[256];
483 tfilep = cm_CommonOpen(AFS_CELLSERVDB, "r");
484 if (!tfilep) return -1;
488 /* have we seen the cell line for the guy we're looking for? */
491 tp = fgets(lineBuffer, sizeof(lineBuffer), (FILE *)tfilep);
493 if (feof((FILE *)tfilep)) {
495 fclose((FILE *)tfilep);
500 /* turn trailing cr or lf into null */
501 tp = strchr(lineBuffer, '\r');
503 tp = strchr(lineBuffer, '\n');
506 /* skip blank lines */
507 if (lineBuffer[0] == 0) {
508 fprintf((FILE *)ofp, "%s\n", lineBuffer);
512 if (lineBuffer[0] == '>') {
513 /* trim off at white space or '#' chars */
514 tp = strchr(lineBuffer, ' ');
516 tp = strchr(lineBuffer, '\t');
518 tp = strchr(lineBuffer, '#');
521 /* now see if this is the right cell */
522 if (strcmp(lineBuffer+1, cellNamep) == 0) {
523 /* found the cell we're looking for */
528 fprintf((FILE *)ofp, "%s\n", lineBuffer);
532 valuep = strchr(lineBuffer, '#');
533 if (valuep == NULL) return -2;
534 valuep++; /* skip the "#" */
536 fprintf((FILE *)ofp, "%s\n", lineBuffer);
539 } /* while loop processing all lines */
542 long cm_AppendNewCell(cm_configFile_t *filep, char *cellNamep)
544 fprintf((FILE *)filep, ">%s\n", cellNamep);
548 long cm_AppendNewCellLine(cm_configFile_t *filep, char *linep)
550 fprintf((FILE *)filep, "%s\n", linep);
554 long cm_CloseCellFile(cm_configFile_t *filep)
564 closeCode = fclose((FILE *)filep);
566 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
567 code = GetWindowsDirectory(wdir, sizeof(wdir));
568 if (code == 0 || code > sizeof(wdir))
571 /* add trailing backslash, if required */
573 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
576 strcpy(wdir,cm_confDir);
578 afsconf_path = getenv("AFSCONF");
580 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
582 strcpy(wdir, afsconf_path);
585 #endif /* DJGPP || WIN95 */
589 if (closeCode != 0) {
590 /* something went wrong, preserve original database */
591 strcat(wdir, "afsdcel2.ini");
596 strcat(wdir, AFS_CELLSERVDB);
597 strcat(sdir, "afsdcel2.ini"); /* new file */
599 unlink(wdir); /* delete old file */
601 code = rename(sdir, wdir); /* do the rename */
609 void cm_GetConfigDir(char *dir)
618 #if !defined(DJGPP) && !defined(AFS_WIN95_ENV)
619 code = GetWindowsDirectory(wdir, sizeof(wdir));
620 if (code == 0 || code > sizeof(wdir)) wdir[0] = 0;
622 /* add trailing backslash, if required */
624 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
627 strcpy(wdir,cm_confDir);
629 afsconf_path = getenv("AFSCONF");
631 strcpy(wdir, AFSDIR_CLIENT_ETC_DIRPATH);
633 strcpy(wdir, afsconf_path);
636 #endif /* DJGPP || WIN95 */