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
17 #include <WINNT\afssw.h>
18 #include <WINNT\afsreg.h>
20 #include <afs/param.h>
22 #include <afs/cellconfig.h>
26 #include <afs/afsint.h>
29 static long cm_ParsePair(char *lineBufferp, char *leftp, char *rightp)
38 for(tp = lineBufferp; *tp; tp++) {
47 /* comment or line end */
48 if (tc == '#' || tc == '\r' || tc == '\n')
51 /* square bracket comment -- look for closing delim */
58 if (tc == ' ' || tc == '\t')
66 /* now we have a real character, put it in the appropriate bucket */
75 /* null terminate the strings */
79 return 0; /* and return success */
83 IsWindowsModule(const char * name)
88 /* Do not perform searches for probable Windows modules */
89 for (p = name, i=0; *p; p++) {
93 p = strrchr(name, '.');
96 (!cm_stricmp_utf8N(p,".dll") ||
97 !cm_stricmp_utf8N(p,".exe") ||
98 !cm_stricmp_utf8N(p,".ini") ||
99 !cm_stricmp_utf8N(p,".db") ||
100 !cm_stricmp_utf8N(p,".drv")))
106 /* search for a cell, and either return an error code if we don't find it,
107 * or return 0 if we do, in which case we also fill in the addresses in
110 * new feature: we can handle abbreviations and are insensitive to case.
111 * If the caller wants the "real" cell name, it puts a non-null pointer in
112 * newCellNamep. Anomaly: if cellNamep is ambiguous, we may modify
113 * newCellNamep but return an error code.
115 * newCellNamep is required to be CELL_MAXNAMELEN in size.
117 long cm_SearchCellFile(char *cellNamep, char *newCellNamep,
118 cm_configProc_t *procp, void *rockp)
120 char wdir[MAX_PATH]="";
121 FILE *tfilep = NULL, *bestp, *tempp;
123 char lineBuffer[257];
126 struct sockaddr_in vlSockAddr;
130 int tracking = 1, partial = 0;
132 if ( IsWindowsModule(cellNamep) )
135 cm_GetCellServDB(wdir, sizeof(wdir));
137 tfilep = fopen(wdir, "r");
142 bestp = fopen(wdir, "r");
144 #ifdef CELLSERV_DEBUG
145 osi_Log2(afsd_logp,"cm_searchfile fopen handle[%p], wdir[%s]", bestp,
146 osi_LogSaveString(afsd_logp,wdir));
148 /* have we seen the cell line for the guy we're looking for? */
150 tp = fgets(lineBuffer, sizeof(lineBuffer), tfilep);
152 (void) fgets(lineBuffer, sizeof(lineBuffer), bestp);
158 * found partial match earlier;
171 return (foundCell? 0 : -3);
176 /* turn trailing cr or lf into null */
177 tp = strrchr(lineBuffer, '\r');
179 tp = strrchr(lineBuffer, '\n');
182 /* skip blank lines */
183 if (lineBuffer[0] == 0) continue;
185 if (lineBuffer[0] == '>') {
189 return(foundCell ? 0 : -6);
192 /* trim off at white space or '#' chars */
193 tp = strchr(lineBuffer, ' ');
195 tp = strchr(lineBuffer, '\t');
197 tp = strchr(lineBuffer, '#');
200 /* now see if this is the right cell */
201 if (stricmp(lineBuffer+1, cellNamep) == 0) {
202 /* found the cell we're looking for */
204 strncpy(newCellNamep, lineBuffer+1,CELL_MAXNAMELEN);
205 newCellNamep[CELL_MAXNAMELEN-1] = '\0';
206 strlwr(newCellNamep);
210 #ifdef CELLSERV_DEBUG
211 osi_Log2(afsd_logp, "cm_searchfile is cell inRightCell[%p], linebuffer[%s]",
212 inRightCell, osi_LogSaveString(afsd_logp,lineBuffer));
215 else if (cm_stricmp_utf8(lineBuffer+1, cellNamep) == 0) {
217 if (partial) { /* ambiguous */
223 strncpy(newCellNamep, lineBuffer+1,CELL_MAXNAMELEN);
224 newCellNamep[CELL_MAXNAMELEN-1] = '\0';
225 strlwr(newCellNamep);
231 else inRightCell = 0;
234 valuep = strchr(lineBuffer, '#');
235 if (valuep == NULL) {
240 valuep++; /* skip the "#" */
242 valuep += strspn(valuep, " \t"); /* skip SP & TAB */
243 /* strip spaces and tabs in the end. They should not be there according to CellServDB format
244 * so do this just in case
246 while (valuep[strlen(valuep) - 1] == ' ' || valuep[strlen(valuep) - 1] == '\t')
247 valuep[strlen(valuep) - 1] = '\0';
250 /* add the server to the VLDB list */
252 thp = gethostbyname(valuep);
253 #ifdef CELLSERV_DEBUG
254 osi_Log3(afsd_logp,"cm_searchfile inRightCell thp[%p], valuep[%s], WSAGetLastError[%d]",
255 thp, osi_LogSaveString(afsd_logp,valuep), WSAGetLastError());
258 memcpy(&vlSockAddr.sin_addr.s_addr, thp->h_addr,
260 vlSockAddr.sin_family = AF_INET;
261 /* sin_port supplied by connection code */
263 (*procp)(rockp, &vlSockAddr, valuep);
270 /* Since there is no gethostbyname() data
271 * available we will read the IP address
272 * stored in the CellServDB file
274 code = sscanf(lineBuffer, " %d.%d.%d.%d",
277 tp = (char *) &ip_addr;
282 memcpy(&vlSockAddr.sin_addr.s_addr, &ip_addr,
284 vlSockAddr.sin_family = AF_INET;
285 /* sin_port supplied by connection code */
287 (*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 /* newCellNamep is required to be CELL_MAXNAMELEN in size */
300 long cm_SearchCellByDNS(char *cellNamep, char *newCellNamep, int *ttl,
301 cm_configProc_t *procp, void *rockp)
305 int cellHostAddrs[AFSMAXCELLHOSTS];
306 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
309 struct sockaddr_in vlSockAddr;
310 #ifdef CELLSERV_DEBUG
311 osi_Log1(afsd_logp,"SearchCellDNS-Doing search for [%s]", osi_LogSaveString(afsd_logp,cellNamep));
313 if ( IsWindowsModule(cellNamep) )
315 rc = getAFSServer(cellNamep, cellHostAddrs, cellHostNames, &numServers, ttl);
316 if (rc == 0 && numServers > 0) { /* found the cell */
317 for (i = 0; i < numServers; i++) {
318 memcpy(&vlSockAddr.sin_addr.s_addr, &cellHostAddrs[i],
320 vlSockAddr.sin_family = AF_INET;
321 /* sin_port supplied by connection code */
323 (*procp)(rockp, &vlSockAddr, cellHostNames[i]);
325 strncpy(newCellNamep,cellNamep,CELL_MAXNAMELEN);
326 newCellNamep[CELL_MAXNAMELEN-1] = '\0';
327 strlwr(newCellNamep);
330 return 0; /* found cell */
333 return -1; /* not found */
335 return -1; /* not found */
336 #endif /* AFS_AFSDB_ENV */
339 /* use cm_GetConfigDir() plus AFS_CELLSERVDB to
340 * generate the fully qualified name of the CellServDB
343 long cm_GetCellServDB(char *cellNamep, afs_uint32 len)
347 cm_GetConfigDir(cellNamep, len);
349 /* add trailing backslash, if required */
350 tlen = (int)strlen(cellNamep);
352 if (cellNamep[tlen-1] != '\\') {
353 strncat(cellNamep, "\\", len);
354 cellNamep[len-1] = '\0';
357 strncat(cellNamep, AFS_CELLSERVDB, len);
358 cellNamep[len-1] = '\0';
363 /* look up the root cell's name in the Registry */
364 long cm_GetRootCellName(char *cellNamep)
366 DWORD code, dummyLen;
369 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
370 0, KEY_QUERY_VALUE, &parmKey);
371 if (code != ERROR_SUCCESS)
375 code = RegQueryValueEx(parmKey, "Cell", NULL, NULL,
376 cellNamep, &dummyLen);
377 RegCloseKey (parmKey);
378 if (code != ERROR_SUCCESS || cellNamep[0] == 0)
384 cm_configFile_t *cm_CommonOpen(char *namep, char *rwp)
386 char wdir[MAX_PATH]="";
389 cm_GetConfigDir(wdir, sizeof(wdir));
391 strncat(wdir, namep, sizeof(wdir));
392 wdir[sizeof(wdir)-1] = '\0';
394 tfilep = fopen(wdir, rwp);
396 return ((cm_configFile_t *) tfilep);
399 long cm_WriteConfigString(char *labelp, char *valuep)
401 DWORD code, dummyDisp;
404 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
405 0, "container", 0, KEY_SET_VALUE, NULL,
406 &parmKey, &dummyDisp);
407 if (code != ERROR_SUCCESS)
410 code = RegSetValueEx(parmKey, labelp, 0, REG_SZ,
411 valuep, (DWORD)strlen(valuep) + 1);
412 RegCloseKey (parmKey);
413 if (code != ERROR_SUCCESS)
419 long cm_WriteConfigInt(char *labelp, long value)
421 DWORD code, dummyDisp;
424 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
425 0, "container", 0, KEY_SET_VALUE, NULL,
426 &parmKey, &dummyDisp);
427 if (code != ERROR_SUCCESS)
430 code = RegSetValueEx(parmKey, labelp, 0, REG_DWORD,
431 (LPBYTE)&value, sizeof(value));
432 RegCloseKey (parmKey);
433 if (code != ERROR_SUCCESS)
439 cm_configFile_t *cm_OpenCellFile(void)
441 cm_configFile_t *cfp;
443 cfp = cm_CommonOpen(AFS_CELLSERVDB ".new", "w");
447 long cm_AppendPrunedCellList(cm_configFile_t *ofp, char *cellNamep)
449 cm_configFile_t *tfilep; /* input file */
451 char lineBuffer[256];
456 tfilep = cm_CommonOpen(AFS_CELLSERVDB, "r");
462 /* have we seen the cell line for the guy we're looking for? */
465 tp = fgets(lineBuffer, sizeof(lineBuffer), (FILE *)tfilep);
467 if (feof((FILE *)tfilep)) {
469 fclose((FILE *)tfilep);
474 /* turn trailing cr or lf into null */
475 tp = strchr(lineBuffer, '\r');
477 tp = strchr(lineBuffer, '\n');
480 /* skip blank lines */
481 if (lineBuffer[0] == 0) {
482 fprintf((FILE *)ofp, "%s\n", lineBuffer);
486 if (lineBuffer[0] == '>') {
487 /* trim off at white space or '#' chars */
488 tp = strchr(lineBuffer, ' ');
490 tp = strchr(lineBuffer, '\t');
492 tp = strchr(lineBuffer, '#');
495 /* now see if this is the right cell */
496 if (strcmp(lineBuffer+1, cellNamep) == 0) {
497 /* found the cell we're looking for */
502 fprintf((FILE *)ofp, "%s\n", lineBuffer);
506 valuep = strchr(lineBuffer, '#');
507 if (valuep == NULL) return -2;
508 valuep++; /* skip the "#" */
510 fprintf((FILE *)ofp, "%s\n", lineBuffer);
513 } /* while loop processing all lines */
516 long cm_AppendNewCell(cm_configFile_t *filep, char *cellNamep)
518 fprintf((FILE *)filep, ">%s\n", cellNamep);
522 long cm_AppendNewCellLine(cm_configFile_t *filep, char *linep)
524 fprintf((FILE *)filep, "%s\n", linep);
528 long cm_CloseCellFile(cm_configFile_t *filep)
534 closeCode = fclose((FILE *)filep);
536 cm_GetConfigDir(wdir, sizeof(wdir));
539 if (closeCode != 0) {
540 /* something went wrong, preserve original database */
541 strncat(wdir, AFS_CELLSERVDB ".new", sizeof(wdir));
542 wdir[sizeof(wdir)-1] = '\0';
547 strncat(wdir, AFS_CELLSERVDB, sizeof(wdir));
548 wdir[sizeof(wdir)-1] = '\0';
549 strncat(sdir, AFS_CELLSERVDB ".new", sizeof(sdir));/* new file */
550 sdir[sizeof(sdir)-1] = '\0';
552 unlink(sdir); /* delete old file */
554 code = rename(sdir, wdir); /* do the rename */
562 void cm_GetConfigDir(char *dir, afs_uint32 len)
566 if (!afssw_GetClientCellServDBDir(&dirp)) {
567 strncpy(dir, dirp, len);