windows-more-cellservdb-consolidation-20080201
[openafs.git] / src / WINNT / afsd / cm_config.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 #include <windows.h>
11 #include <winsock2.h>
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15
16 #include "afsd.h"
17 #include <WINNT\afssw.h>
18 #include <WINNT\afsreg.h>
19
20 #include <afs/param.h>
21 #include <afs/stds.h>
22 #include <afs/cellconfig.h>
23
24 #ifdef AFS_AFSDB_ENV
25 #include "cm_dns.h"
26 #include <afs/afsint.h>
27 #endif
28
29 static long cm_ParsePair(char *lineBufferp, char *leftp, char *rightp)
30 {
31     char *tp;
32     char tc;
33     int sawEquals;
34     int sawBracket;
35         
36     sawEquals = 0;
37     sawBracket = 0;
38     for(tp = lineBufferp; *tp; tp++) {
39         tc = *tp;
40
41         if (sawBracket) {
42             if (tc == ']')
43                 sawBracket = 0;
44             continue;
45         }
46
47         /* comment or line end */
48         if (tc == '#' || tc == '\r' || tc == '\n') 
49             break;
50
51         /* square bracket comment -- look for closing delim */
52         if (tc == '[') {
53             sawBracket = 1; 
54             continue;
55         }       
56
57         /* space or tab */
58         if (tc == ' ' || tc == '\t') 
59             continue;
60
61         if (tc == '=') {
62             sawEquals = 1;
63             continue;
64         }
65
66         /* now we have a real character, put it in the appropriate bucket */
67         if (sawEquals == 0) {
68             *leftp++ = tc;
69         }       
70         else {  
71             *rightp++ = tc;
72         }
73     }
74
75     /* null terminate the strings */
76     *leftp = 0;
77     *rightp = 0;
78
79     return 0;   /* and return success */
80 }
81
82 static int
83 IsWindowsModule(const char * name)
84 {
85     const char * p;
86     int i;
87
88     /* Do not perform searches for probable Windows modules */
89     for (p = name, i=0; *p; p++) {
90         if ( *p == '.' )
91             i++;
92     }
93     p = strrchr(name, '.');
94     if (p) {
95         if (i == 1 && 
96             (!stricmp(p,".dll") ||
97              !stricmp(p,".exe") ||
98              !stricmp(p,".ini") ||
99              !stricmp(p,".db") ||
100              !stricmp(p,".drv")))
101             return 1;
102     }
103     return 0;
104 }
105
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
108  * the cellp field.
109  *
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.
114  */
115 long cm_SearchCellFile(char *cellNamep, char *newCellNamep,
116                        cm_configProc_t *procp, void *rockp)
117 {
118     char wdir[257];
119     FILE *tfilep = NULL, *bestp, *tempp;
120     char *tp;
121     char lineBuffer[257];
122     struct hostent *thp;
123     char *valuep;
124     struct sockaddr_in vlSockAddr;
125     int inRightCell;
126     int foundCell = 0;
127     long code;
128     int tracking = 1, partial = 0;
129
130     if ( IsWindowsModule(cellNamep) )
131         return -3;
132
133     cm_GetCellServDB(wdir, sizeof(wdir));
134     tfilep = fopen(wdir, "r");
135
136     if (!tfilep) 
137         return -2;
138
139     bestp = fopen(wdir, "r");
140     
141 #ifdef CELLSERV_DEBUG
142     osi_Log2(afsd_logp,"cm_searchfile fopen handle[%p], wdir[%s]", bestp, 
143              osi_LogSaveString(afsd_logp,wdir));
144 #endif
145     /* have we seen the cell line for the guy we're looking for? */
146     inRightCell = 0;
147     while (1) {
148         tp = fgets(lineBuffer, sizeof(lineBuffer), tfilep);
149         if (tracking)
150             (void) fgets(lineBuffer, sizeof(lineBuffer), bestp);
151         if (    tp == NULL) {
152             if (feof(tfilep)) {
153                 /* hit EOF */
154                 if (partial) {
155                     /*
156                      * found partial match earlier;
157                      * now go back to it
158                      */
159                     tempp = bestp;
160                     bestp = tfilep;
161                     tfilep = tempp;
162                     inRightCell = 1;
163                     partial = 0;
164                     continue;
165                 }
166                 else {
167                     fclose(tfilep);
168                     fclose(bestp);
169                     return (foundCell? 0 : -3);
170                 }
171             }
172         }       
173
174         /* turn trailing cr or lf into null */
175         tp = strchr(lineBuffer, '\r');
176         if (tp) *tp = 0;
177         tp = strchr(lineBuffer, '\n');
178         if (tp) *tp = 0;
179
180         /* skip blank lines */
181         if (lineBuffer[0] == 0) continue;
182
183         if (lineBuffer[0] == '>') {
184             /* trim off at white space or '#' chars */
185             tp = strchr(lineBuffer, ' ');
186             if (tp) *tp = 0;
187             tp = strchr(lineBuffer, '\t');
188             if (tp) *tp = 0;
189             tp = strchr(lineBuffer, '#');
190             if (tp) *tp = 0;
191
192             /* now see if this is the right cell */
193             if (stricmp(lineBuffer+1, cellNamep) == 0) {
194                 /* found the cell we're looking for */
195                 if (newCellNamep)
196                     strcpy(newCellNamep, lineBuffer+1);
197                 inRightCell = 1;
198                 tracking = 0;
199 #ifdef CELLSERV_DEBUG                
200                 osi_Log2(afsd_logp, "cm_searchfile is cell inRightCell[%p], linebuffer[%s]",
201                          inRightCell, osi_LogSaveString(afsd_logp,lineBuffer));
202 #endif
203             }
204             else if (strnicmp(lineBuffer+1, cellNamep,
205                                strlen(cellNamep)) == 0) {
206                 /* partial match */
207                 if (partial) {  /* ambiguous */
208                     fclose(tfilep);
209                     fclose(bestp);
210                     return -5;
211                 }
212                 if (newCellNamep)
213                     strcpy(newCellNamep, lineBuffer+1);
214                 inRightCell = 0;
215                 tracking = 0;
216                 partial = 1;
217             }
218             else inRightCell = 0;
219         }
220         else {
221             valuep = strchr(lineBuffer, '#');
222             if (valuep == NULL) {
223                 fclose(tfilep);
224                 fclose(bestp);
225                 return -4;
226             }
227             valuep++;   /* skip the "#" */
228
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                        
232              */
233             while (valuep[strlen(valuep) - 1] == ' ' || valuep[strlen(valuep) - 1] == '\t') 
234                 valuep[strlen(valuep) - 1] = '\0';
235
236             if (inRightCell) {
237                 /* add the server to the VLDB list */
238                 WSASetLastError(0);
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());
243 #endif
244                 if (thp) {
245                     memcpy(&vlSockAddr.sin_addr.s_addr, thp->h_addr,
246                             sizeof(long));
247                     vlSockAddr.sin_family = AF_INET;
248                     /* sin_port supplied by connection code */
249                     if (procp)
250                         (*procp)(rockp, &vlSockAddr, valuep);
251                     foundCell = 1;
252                 }
253                 if (!thp) {
254                     long ip_addr;
255                     int c1, c2, c3, c4;
256                     char aname[241] = "";                    
257                     
258                     /* Since there is no gethostbyname() data 
259                      * available we will read the IP address
260                      * stored in the CellServDB file
261                      */
262                     code = sscanf(lineBuffer, "%d.%d.%d.%d #%s",
263                                    &c1, &c2, &c3, &c4, aname);
264                     tp = (char *) &ip_addr;
265                     *tp++ = c1;
266                     *tp++ = c2;
267                     *tp++ = c3;
268                     *tp++ = c4;
269                     memcpy(&vlSockAddr.sin_addr.s_addr, &ip_addr,
270                             sizeof(long));
271                     vlSockAddr.sin_family = AF_INET;
272                     /* sin_port supplied by connection code */
273                     if (procp)
274                         (*procp)(rockp, &vlSockAddr, valuep);
275                     foundCell = 1;
276                 }
277             }
278         }       /* a vldb line */
279     }           /* while loop processing all lines */
280
281     /* if for some unknown reason cell is not found, return negative code (-11) ??? */
282     return (foundCell) ? 0 : -11;
283 }
284
285 long cm_SearchCellByDNS(char *cellNamep, char *newCellNamep, int *ttl,
286                cm_configProc_t *procp, void *rockp)
287 {
288 #ifdef AFS_AFSDB_ENV
289     int rc;
290     int  cellHostAddrs[AFSMAXCELLHOSTS];
291     char cellHostNames[AFSMAXCELLHOSTS][MAXHOSTCHARS];
292     int numServers;
293     int i;
294     struct sockaddr_in vlSockAddr;
295 #ifdef CELLSERV_DEBUG
296     osi_Log1(afsd_logp,"SearchCellDNS-Doing search for [%s]", osi_LogSaveString(afsd_logp,cellNamep));
297 #endif
298     if ( IsWindowsModule(cellNamep) )
299         return -1;
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],
304                    sizeof(long));
305            vlSockAddr.sin_family = AF_INET;
306            /* sin_port supplied by connection code */
307            if (procp)
308           (*procp)(rockp, &vlSockAddr, cellHostNames[i]);
309            if(newCellNamep)
310           strcpy(newCellNamep,cellNamep);
311         }
312         return 0;   /* found cell */
313     }
314     else
315        return -1;  /* not found */
316 #else
317     return -1;  /* not found */
318 #endif /* AFS_AFSDB_ENV */
319 }
320
321 /* use cm_GetConfigDir() plus AFS_CELLSERVDB to 
322  * generate the fully qualified name of the CellServDB 
323  * file.
324  */
325 long cm_GetCellServDB(char *cellNamep, afs_uint32 len)
326 {
327     int tlen;
328     
329     cm_GetConfigDir(cellNamep, len);
330
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';
336     }
337         
338     strncat(cellNamep, AFS_CELLSERVDB, len);
339     cellNamep[len-1] = '\0';
340     return 0;
341 }
342
343 /* look up the root cell's name in the Registry */
344 long cm_GetRootCellName(char *cellNamep)
345 {
346     DWORD code, dummyLen;
347     HKEY parmKey;
348
349     code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSREG_CLT_SVC_PARAM_SUBKEY,
350                         0, KEY_QUERY_VALUE, &parmKey);
351     if (code != ERROR_SUCCESS)
352         return -1;
353
354     dummyLen = 256;
355     code = RegQueryValueEx(parmKey, "Cell", NULL, NULL,
356                                 cellNamep, &dummyLen);
357     RegCloseKey (parmKey);
358     if (code != ERROR_SUCCESS || cellNamep[0] == 0)
359         return -1;
360
361     return 0;
362 }
363
364 cm_configFile_t *cm_CommonOpen(char *namep, char *rwp)
365 {
366     char wdir[256];
367     FILE *tfilep;
368
369     cm_GetConfigDir(wdir, sizeof(wdir));
370
371     strncat(wdir, namep, sizeof(wdir));
372     wdir[sizeof(wdir)-1] = '\0';
373         
374     tfilep = fopen(wdir, rwp);
375
376     return ((cm_configFile_t *) tfilep);        
377 }       
378
379 long cm_WriteConfigString(char *labelp, char *valuep)
380 {
381     DWORD code, dummyDisp;
382     HKEY parmKey;
383
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)
388         return -1;
389
390     code = RegSetValueEx(parmKey, labelp, 0, REG_SZ,
391                           valuep, (DWORD)strlen(valuep) + 1);
392     RegCloseKey (parmKey);
393     if (code != ERROR_SUCCESS)
394         return (long)-1;
395
396     return (long)0;
397 }
398
399 long cm_WriteConfigInt(char *labelp, long value)
400 {
401     DWORD code, dummyDisp;
402     HKEY parmKey;
403
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)
408         return -1;
409
410     code = RegSetValueEx(parmKey, labelp, 0, REG_DWORD,
411                           (LPBYTE)&value, sizeof(value));
412     RegCloseKey (parmKey);
413     if (code != ERROR_SUCCESS)
414         return -1;
415
416     return 0;
417 }
418
419 cm_configFile_t *cm_OpenCellFile(void)
420 {
421     cm_configFile_t *cfp;
422
423     cfp = cm_CommonOpen(AFS_CELLSERVDB ".new", "w");
424     return cfp;
425 }
426
427 long cm_AppendPrunedCellList(cm_configFile_t *ofp, char *cellNamep)
428 {
429     cm_configFile_t *tfilep;    /* input file */
430     char *tp;
431     char lineBuffer[256];
432     char *valuep;
433     int inRightCell;
434     int foundCell;
435
436     tfilep = cm_CommonOpen(AFS_CELLSERVDB, "r");
437     if (!tfilep) 
438         return -1;
439
440     foundCell = 0;
441
442     /* have we seen the cell line for the guy we're looking for? */
443     inRightCell = 0;
444     while (1) {
445         tp = fgets(lineBuffer, sizeof(lineBuffer), (FILE *)tfilep);
446         if (tp == NULL) {
447             if (feof((FILE *)tfilep)) {
448                 /* hit EOF */
449                 fclose((FILE *)tfilep);
450                 return 0;
451             }
452         }
453
454         /* turn trailing cr or lf into null */
455         tp = strchr(lineBuffer, '\r');
456         if (tp) *tp = 0;
457         tp = strchr(lineBuffer, '\n');
458         if (tp) *tp = 0;
459                 
460         /* skip blank lines */
461         if (lineBuffer[0] == 0) {
462             fprintf((FILE *)ofp, "%s\n", lineBuffer);
463             continue;
464         }
465
466         if (lineBuffer[0] == '>') {
467             /* trim off at white space or '#' chars */
468             tp = strchr(lineBuffer, ' ');
469             if (tp) *tp = 0;
470             tp = strchr(lineBuffer, '\t');
471             if (tp) *tp = 0;
472             tp = strchr(lineBuffer, '#');
473             if (tp) *tp = 0;
474
475             /* now see if this is the right cell */
476             if (strcmp(lineBuffer+1, cellNamep) == 0) {
477                 /* found the cell we're looking for */
478                 inRightCell = 1;
479             }
480             else {
481                 inRightCell = 0;
482                 fprintf((FILE *)ofp, "%s\n", lineBuffer);
483             }
484         }
485         else {
486             valuep = strchr(lineBuffer, '#');
487             if (valuep == NULL) return -2;
488             valuep++;   /* skip the "#" */
489             if (!inRightCell) {
490                 fprintf((FILE *)ofp, "%s\n", lineBuffer);
491             }
492         }       /* a vldb line */
493     }           /* while loop processing all lines */
494 }       
495
496 long cm_AppendNewCell(cm_configFile_t *filep, char *cellNamep)
497 {
498     fprintf((FILE *)filep, ">%s\n", cellNamep);
499     return 0;
500 }
501
502 long cm_AppendNewCellLine(cm_configFile_t *filep, char *linep)
503 {
504     fprintf((FILE *)filep, "%s\n", linep);
505     return 0;
506 }
507
508 long cm_CloseCellFile(cm_configFile_t *filep)
509 {
510     char wdir[260];
511     char sdir[260];
512     long code;
513     long closeCode;
514     closeCode = fclose((FILE *)filep);
515
516     cm_GetConfigDir(wdir, sizeof(wdir));
517     strcpy(sdir, wdir);
518
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';
523         unlink(wdir);
524         return closeCode;
525     }
526
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';
531
532     unlink(sdir);                       /* delete old file */
533
534     code = rename(sdir, wdir);  /* do the rename */
535
536     if (code) 
537         code = errno;
538
539     return code;
540 }   
541
542 void cm_GetConfigDir(char *dir, afs_uint32 len)
543 {
544     char * dirp = NULL;
545
546     if (!afssw_GetClientCellServDBDir(&dirp)) {
547         strncpy(dir, dirp, len);
548         dir[len-1] = '\0';
549         free(dirp);
550     } else {
551         dir[0] = '\0';
552     }
553 }