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 (!stricmp(p,".dll") ||
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 long cm_SearchCellFile(char *cellNamep, char *newCellNamep,
116 cm_configProc_t *procp, void *rockp)
119 FILE *tfilep = NULL, *bestp, *tempp;
121 char lineBuffer[257];
124 struct sockaddr_in vlSockAddr;
128 int tracking = 1, partial = 0;
130 if ( IsWindowsModule(cellNamep) )
133 cm_GetCellServDB(wdir, sizeof(wdir));
134 tfilep = fopen(wdir, "r");
139 bestp = fopen(wdir, "r");
141 #ifdef CELLSERV_DEBUG
142 osi_Log2(afsd_logp,"cm_searchfile fopen handle[%p], wdir[%s]", bestp,
143 osi_LogSaveString(afsd_logp,wdir));
145 /* have we seen the cell line for the guy we're looking for? */
148 tp = fgets(lineBuffer, sizeof(lineBuffer), tfilep);
150 (void) fgets(lineBuffer, sizeof(lineBuffer), bestp);
156 * found partial match earlier;
169 return (foundCell? 0 : -3);
174 /* turn trailing cr or lf into null */
175 tp = strchr(lineBuffer, '\r');
177 tp = strchr(lineBuffer, '\n');
180 /* skip blank lines */
181 if (lineBuffer[0] == 0) continue;
183 if (lineBuffer[0] == '>') {
184 /* trim off at white space or '#' chars */
185 tp = strchr(lineBuffer, ' ');
187 tp = strchr(lineBuffer, '\t');
189 tp = strchr(lineBuffer, '#');
192 /* now see if this is the right cell */
193 if (stricmp(lineBuffer+1, cellNamep) == 0) {
194 /* found the cell we're looking for */
196 strcpy(newCellNamep, lineBuffer+1);
199 #ifdef CELLSERV_DEBUG
200 osi_Log2(afsd_logp, "cm_searchfile is cell inRightCell[%p], linebuffer[%s]",
201 inRightCell, osi_LogSaveString(afsd_logp,lineBuffer));
204 else if (strnicmp(lineBuffer+1, cellNamep,
205 strlen(cellNamep)) == 0) {
207 if (partial) { /* ambiguous */
213 strcpy(newCellNamep, lineBuffer+1);
218 else inRightCell = 0;
221 valuep = strchr(lineBuffer, '#');
222 if (valuep == NULL) {
227 valuep++; /* skip the "#" */
229 valuep += strspn(valuep, " \t"); /* skip SP & TAB */
230 /* strip spaces and tabs in the end. They should not be there according to CellServDB format
231 * so do this just in case
233 while (valuep[strlen(valuep) - 1] == ' ' || valuep[strlen(valuep) - 1] == '\t')
234 valuep[strlen(valuep) - 1] = '\0';
237 /* add the server to the VLDB list */
239 thp = gethostbyname(valuep);
240 #ifdef CELLSERV_DEBUG
241 osi_Log3(afsd_logp,"cm_searchfile inRightCell thp[%p], valuep[%s], WSAGetLastError[%d]",
242 thp, osi_LogSaveString(afsd_logp,valuep), WSAGetLastError());
245 memcpy(&vlSockAddr.sin_addr.s_addr, thp->h_addr,
247 vlSockAddr.sin_family = AF_INET;
248 /* sin_port supplied by connection code */
250 (*procp)(rockp, &vlSockAddr, valuep);
256 char aname[241] = "";
258 /* Since there is no gethostbyname() data
259 * available we will read the IP address
260 * stored in the CellServDB file
262 code = sscanf(lineBuffer, "%d.%d.%d.%d #%s",
263 &c1, &c2, &c3, &c4, aname);
264 tp = (char *) &ip_addr;
269 memcpy(&vlSockAddr.sin_addr.s_addr, &ip_addr,
271 vlSockAddr.sin_family = AF_INET;
272 /* sin_port supplied by connection code */
274 (*procp)(rockp, &vlSockAddr, valuep);
279 } /* while loop processing all lines */
281 /* if for some unknown reason cell is not found, return negative code (-11) ??? */
282 return (foundCell) ? 0 : -11;
285 long cm_SearchCellByDNS(char *cellNamep, char *newCellNamep, int *ttl,
286 cm_configProc_t *procp, void *rockp)
290 int cellHostAddrs[AFSMAXCELLHOSTS];
291 char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
294 struct sockaddr_in vlSockAddr;
295 #ifdef CELLSERV_DEBUG
296 osi_Log1(afsd_logp,"SearchCellDNS-Doing search for [%s]", osi_LogSaveString(afsd_logp,cellNamep));
298 if ( IsWindowsModule(cellNamep) )
300 rc = getAFSServer(cellNamep, cellHostAddrs, cellHostNames, &numServers, ttl);
301 if (rc == 0 && numServers > 0) { /* found the cell */
302 for (i = 0; i < numServers; i++) {
303 memcpy(&vlSockAddr.sin_addr.s_addr, &cellHostAddrs[i],
305 vlSockAddr.sin_family = AF_INET;
306 /* sin_port supplied by connection code */
308 (*procp)(rockp, &vlSockAddr, cellHostNames[i]);
310 strcpy(newCellNamep,cellNamep);
312 return 0; /* found cell */
315 return -1; /* not found */
317 return -1; /* not found */
318 #endif /* AFS_AFSDB_ENV */
321 /* use cm_GetConfigDir() plus AFS_CELLSERVDB to
322 * generate the fully qualified name of the CellServDB
325 long cm_GetCellServDB(char *cellNamep, afs_uint32 len)
329 cm_GetConfigDir(cellNamep, len);
331 /* add trailing backslash, if required */
332 tlen = (int)strlen(cellNamep);
333 if (cellNamep[tlen-1] != '\\') {
334 strncat(cellNamep, "\\", len);
335 cellNamep[len-1] = '\0';
338 strncat(cellNamep, AFS_CELLSERVDB, len);
339 cellNamep[len-1] = '\0';
343 /* look up the root cell's name in the Registry */
344 long cm_GetRootCellName(char *cellNamep)
346 DWORD code, dummyLen;
349 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
350 0, KEY_QUERY_VALUE, &parmKey);
351 if (code != ERROR_SUCCESS)
355 code = RegQueryValueEx(parmKey, "Cell", NULL, NULL,
356 cellNamep, &dummyLen);
357 RegCloseKey (parmKey);
358 if (code != ERROR_SUCCESS || cellNamep[0] == 0)
364 cm_configFile_t *cm_CommonOpen(char *namep, char *rwp)
369 cm_GetConfigDir(wdir, sizeof(wdir));
371 strncat(wdir, namep, sizeof(wdir));
372 wdir[sizeof(wdir)-1] = '\0';
374 tfilep = fopen(wdir, rwp);
376 return ((cm_configFile_t *) tfilep);
379 long cm_WriteConfigString(char *labelp, char *valuep)
381 DWORD code, dummyDisp;
384 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
385 0, "container", 0, KEY_SET_VALUE, NULL,
386 &parmKey, &dummyDisp);
387 if (code != ERROR_SUCCESS)
390 code = RegSetValueEx(parmKey, labelp, 0, REG_SZ,
391 valuep, (DWORD)strlen(valuep) + 1);
392 RegCloseKey (parmKey);
393 if (code != ERROR_SUCCESS)
399 long cm_WriteConfigInt(char *labelp, long value)
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_DWORD,
411 (LPBYTE)&value, sizeof(value));
412 RegCloseKey (parmKey);
413 if (code != ERROR_SUCCESS)
419 cm_configFile_t *cm_OpenCellFile(void)
421 cm_configFile_t *cfp;
423 cfp = cm_CommonOpen(AFS_CELLSERVDB ".new", "w");
427 long cm_AppendPrunedCellList(cm_configFile_t *ofp, char *cellNamep)
429 cm_configFile_t *tfilep; /* input file */
431 char lineBuffer[256];
436 tfilep = cm_CommonOpen(AFS_CELLSERVDB, "r");
442 /* have we seen the cell line for the guy we're looking for? */
445 tp = fgets(lineBuffer, sizeof(lineBuffer), (FILE *)tfilep);
447 if (feof((FILE *)tfilep)) {
449 fclose((FILE *)tfilep);
454 /* turn trailing cr or lf into null */
455 tp = strchr(lineBuffer, '\r');
457 tp = strchr(lineBuffer, '\n');
460 /* skip blank lines */
461 if (lineBuffer[0] == 0) {
462 fprintf((FILE *)ofp, "%s\n", lineBuffer);
466 if (lineBuffer[0] == '>') {
467 /* trim off at white space or '#' chars */
468 tp = strchr(lineBuffer, ' ');
470 tp = strchr(lineBuffer, '\t');
472 tp = strchr(lineBuffer, '#');
475 /* now see if this is the right cell */
476 if (strcmp(lineBuffer+1, cellNamep) == 0) {
477 /* found the cell we're looking for */
482 fprintf((FILE *)ofp, "%s\n", lineBuffer);
486 valuep = strchr(lineBuffer, '#');
487 if (valuep == NULL) return -2;
488 valuep++; /* skip the "#" */
490 fprintf((FILE *)ofp, "%s\n", lineBuffer);
493 } /* while loop processing all lines */
496 long cm_AppendNewCell(cm_configFile_t *filep, char *cellNamep)
498 fprintf((FILE *)filep, ">%s\n", cellNamep);
502 long cm_AppendNewCellLine(cm_configFile_t *filep, char *linep)
504 fprintf((FILE *)filep, "%s\n", linep);
508 long cm_CloseCellFile(cm_configFile_t *filep)
514 closeCode = fclose((FILE *)filep);
516 cm_GetConfigDir(wdir, sizeof(wdir));
519 if (closeCode != 0) {
520 /* something went wrong, preserve original database */
521 strncat(wdir, AFS_CELLSERVDB ".new", sizeof(wdir));
522 wdir[sizeof(wdir)-1] = '\0';
527 strncat(wdir, AFS_CELLSERVDB, sizeof(wdir));
528 wdir[sizeof(wdir)-1] = '\0';
529 strncat(sdir, AFS_CELLSERVDB ".new", sizeof(sdir));/* new file */
530 sdir[sizeof(sdir)-1] = '\0';
532 unlink(sdir); /* delete old file */
534 code = rename(sdir, wdir); /* do the rename */
542 void cm_GetConfigDir(char *dir, afs_uint32 len)
546 if (!afssw_GetClientCellServDBDir(&dirp)) {
547 strncpy(dir, dirp, len);