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>
19 #include "cm_config.h"
21 char AFSConfigKeyName[] =
22 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
24 static long cm_ParsePair(char *lineBufferp, char *leftp, char *rightp)
33 for(tp = lineBufferp; *tp; tp++) {
42 /* comment or line end */
43 if (tc == '#' || tc == '\r' || tc == '\n') break;
45 /* square bracket comment -- look for closing delim
46 if (tc == '[') {sawBracket = 1; continue;}
49 if (tc == ' ' || tc == '\t') continue;
56 /* now we have a real character, put it in the appropriate bucket */
65 /* null terminate the strings */
69 return 0; /* and return success */
72 /* search for a cell, and either return an error code if we don't find it,
73 * or return 0 if we do, in which case we also fill in the addresses in
76 * new feature: we can handle abbreviations and are insensitive to case.
77 * If the caller wants the "real" cell name, it puts a non-null pointer in
78 * newCellNamep. Anomaly: if cellNamep is ambiguous, we may modify
79 * newCellNamep but return an error code.
81 long cm_SearchCellFile(char *cellNamep, char *newCellNamep,
82 cm_configProc_t *procp, void *rockp)
86 FILE *tfilep, *bestp, *tempp;
91 struct sockaddr_in vlSockAddr;
95 int tracking = 1, partial = 0;
99 code = GetWindowsDirectory(wdir, sizeof(wdir));
100 if (code == 0 || code > sizeof(wdir)) return -1;
102 /* add trailing backslash, if required */
104 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
106 strcat(wdir, "afsdcell.ini");
108 tfilep = fopen(wdir, "r");
109 if (!tfilep) return -2;
110 bestp = fopen(wdir, "r");
112 /* have we seen the cell line for the guy we're looking for? */
115 tp = fgets(lineBuffer, sizeof(lineBuffer), tfilep);
117 (void) fgets(lineBuffer, sizeof(lineBuffer), bestp);
123 * found partial match earlier;
136 return (foundCell? 0 : -3);
141 /* turn trailing cr or lf into null */
142 tp = strchr(lineBuffer, '\r');
144 tp = strchr(lineBuffer, '\n');
147 /* skip blank lines */
148 if (lineBuffer[0] == 0) continue;
150 if (lineBuffer[0] == '>') {
151 /* trim off at white space or '#' chars */
152 tp = strchr(lineBuffer, ' ');
154 tp = strchr(lineBuffer, '\t');
156 tp = strchr(lineBuffer, '#');
159 /* now see if this is the right cell */
160 if (stricmp(lineBuffer+1, cellNamep) == 0) {
161 /* found the cell we're looking for */
163 strcpy(newCellNamep, lineBuffer+1);
167 else if (strnicmp(lineBuffer+1, cellNamep,
168 strlen(cellNamep)) == 0) {
170 if (partial) { /* ambiguous */
176 strcpy(newCellNamep, lineBuffer+1);
181 else inRightCell = 0;
184 valuep = strchr(lineBuffer, '#');
185 if (valuep == NULL) {
190 valuep++; /* skip the "#" */
191 valuep += strspn(valuep, " "); /* skip SP & TAB */
193 /* add the server to the VLDB list */
194 thp = gethostbyname(valuep);
196 memcpy(&vlSockAddr.sin_addr.s_addr, thp->h_addr,
198 vlSockAddr.sin_family = AF_INET;
199 /* sin_port supplied by connection code */
201 (*procp)(rockp, &vlSockAddr, valuep);
206 } /* while loop processing all lines */
209 /* look up the root cell's name in the Registry */
210 long cm_GetRootCellName(char *cellNamep)
212 DWORD code, dummyLen;
215 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
216 0, KEY_QUERY_VALUE, &parmKey);
217 if (code != ERROR_SUCCESS)
221 code = RegQueryValueEx(parmKey, "Cell", NULL, NULL,
222 cellNamep, &dummyLen);
223 RegCloseKey (parmKey);
224 if (code != ERROR_SUCCESS)
230 cm_configFile_t *cm_CommonOpen(char *namep, char *rwp)
237 code = GetWindowsDirectory(wdir, sizeof(wdir));
238 if (code == 0 || code > sizeof(wdir)) return NULL;
240 /* add trailing backslash, if required */
242 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
246 tfilep = fopen(wdir, rwp);
248 return ((cm_configFile_t *) tfilep);
251 long cm_WriteConfigString(char *labelp, char *valuep)
253 DWORD code, dummyDisp;
256 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
257 0, "container", 0, KEY_SET_VALUE, NULL,
258 &parmKey, &dummyDisp);
259 if (code != ERROR_SUCCESS)
262 code = RegSetValueEx(parmKey, labelp, 0, REG_SZ,
263 valuep, strlen(valuep) + 1);
264 RegCloseKey (parmKey);
265 if (code != ERROR_SUCCESS)
271 long cm_WriteConfigInt(char *labelp, long value)
273 DWORD code, dummyDisp;
276 code = RegCreateKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
277 0, "container", 0, KEY_SET_VALUE, NULL,
278 &parmKey, &dummyDisp);
279 if (code != ERROR_SUCCESS)
282 code = RegSetValueEx(parmKey, labelp, 0, REG_DWORD,
283 &value, sizeof(value));
284 RegCloseKey (parmKey);
285 if (code != ERROR_SUCCESS)
291 cm_configFile_t *cm_OpenCellFile(void)
293 cm_configFile_t *cfp;
295 cfp = cm_CommonOpen("afsdcel2.ini", "w");
299 long cm_AppendPrunedCellList(cm_configFile_t *ofp, char *cellNamep)
301 cm_configFile_t *tfilep; /* input file */
303 char lineBuffer[256];
308 tfilep = cm_CommonOpen("afsdcell.ini", "r");
309 if (!tfilep) return -1;
313 /* have we seen the cell line for the guy we're looking for? */
316 tp = fgets(lineBuffer, sizeof(lineBuffer), (FILE *)tfilep);
318 if (feof((FILE *)tfilep)) {
320 fclose((FILE *)tfilep);
325 /* turn trailing cr or lf into null */
326 tp = strchr(lineBuffer, '\r');
328 tp = strchr(lineBuffer, '\n');
331 /* skip blank lines */
332 if (lineBuffer[0] == 0) {
333 fprintf((FILE *)ofp, "%s\n", lineBuffer);
337 if (lineBuffer[0] == '>') {
338 /* trim off at white space or '#' chars */
339 tp = strchr(lineBuffer, ' ');
341 tp = strchr(lineBuffer, '\t');
343 tp = strchr(lineBuffer, '#');
346 /* now see if this is the right cell */
347 if (strcmp(lineBuffer+1, cellNamep) == 0) {
348 /* found the cell we're looking for */
353 fprintf((FILE *)ofp, "%s\n", lineBuffer);
357 valuep = strchr(lineBuffer, '#');
358 if (valuep == NULL) return -2;
359 valuep++; /* skip the "#" */
361 fprintf((FILE *)ofp, "%s\n", lineBuffer);
364 } /* while loop processing all lines */
367 long cm_AppendNewCell(cm_configFile_t *filep, char *cellNamep)
369 fprintf((FILE *)filep, ">%s\n", cellNamep);
373 long cm_AppendNewCellLine(cm_configFile_t *filep, char *linep)
375 fprintf((FILE *)filep, "%s\n", linep);
379 extern long cm_CloseCellFile(cm_configFile_t *filep)
387 closeCode = fclose((FILE *)filep);
389 code = GetWindowsDirectory(wdir, sizeof(wdir));
390 if (code == 0 || code > sizeof(wdir)) return -1;
392 /* add trailing backslash, if required */
394 if (wdir[tlen-1] != '\\') strcat(wdir, "\\");
397 if (closeCode != 0) {
398 /* something went wrong, preserve original database */
399 strcat(wdir, "afsdcel2.ini");
404 strcat(wdir, "afsdcell.ini");
405 strcat(sdir, "afsdcel2.ini"); /* new file */
407 unlink(wdir); /* delete old file */
409 code = rename(sdir, wdir); /* do the rename */
411 if (code) code = errno;