Fix backslashes in SHIBSP_PREFIX variable by manually creating it during the script...
[shibboleth/sp.git] / isapi_shib / directive_class.h
1 #include <string>
2 #include <tchar.h>
3 #define STR_PENDING_DELETION "(Pending Removal)"
4
5 using namespace std;
6
7 class Directive
8 {
9 public:
10         
11         Directive() {
12         }
13         
14         ~Directive() {
15         }
16         
17         Directive(unsigned int which_directive) {
18                 Init_Directive(which_directive);
19         }
20         
21         string name;
22         unsigned short type; 
23         string value;
24         string new_value;
25         string defined_in;
26         string description;
27         string bound_val[NUM_BOUND_VAL];
28         string MachineName;
29         unsigned int d_index;
30         
31         bool Set_Path(string new_path)
32         {
33                 if (new_path.length() && new_path.at(0) == '/') {
34                         path = new_path;
35                 } else {
36                         path = "/";
37                         path += new_path;
38                 }
39                 Get_Value();
40                 return true;
41         }
42         
43         bool WriteValue(string RegPath)
44         {
45                 if (type == D_BOUND_INT || type == D_FREE_INT) {
46                         return WriteRegInt(RegPath.c_str(), name.c_str(), new_value.c_str());
47                 } else {
48                         return WriteRegString(RegPath.c_str(), name.c_str(), new_value.c_str());
49                 }
50         }
51
52         void Directive::Init_Directive(unsigned int w) {
53                 
54                 if (w >= NUM_DIRECTIVES) {
55                         return;
56                 }
57                 d_index = w;
58                 name  = directives[w].name;
59                 type  = directives[w].type;
60                 value = directives[w].value;
61                 defined_in = directives[w].defined_in;
62                 description = directives[w].description;
63                 for (int i=0;i<NUM_BOUND_VAL;i++) {
64                         bound_val[i] = directives[w].bound_val[i];
65                 }
66         }
67
68         void DeleteValue()
69         {
70                 new_value = STR_PENDING_DELETION;
71         }
72
73         bool Directive::DeleteRegVal(string Key) 
74         {
75                 HKEY hKey;
76                 
77                 if ((hKey=OpenKey(Key.c_str(),KEY_ALL_ACCESS))) {
78                         if (RegDeleteValue(hKey,name.c_str()) == ERROR_SUCCESS) {
79                                 return true;
80                         }
81                 }
82                 return false;
83         }
84
85 private:
86         
87         string path;
88                 
89         HKEY Directive::OpenKey(LPCTSTR szKey, REGSAM samDesired) {
90                 
91                 HKEY hKey,rhKey;
92                 _TCHAR localname[MAX_PATH];
93                 DWORD lsize = MAX_PATH;
94                 
95                 //Support for Remote Registries 
96                 GetComputerName(localname,&lsize);
97                 if (!_tcsicmp(localname,MachineName.c_str())) {
98                         rhKey = HKEY_LOCAL_MACHINE;
99                 }else {
100                         if (RegConnectRegistry(MachineName.c_str(),HKEY_LOCAL_MACHINE, &rhKey) != ERROR_SUCCESS) {
101                                 //MessageBox(hwndDlg,L"Error opening remote registry.  Values displayed may not be accurate.",L"Error",MB_ICONERROR);
102                         }
103                 }
104                 
105                 if (samDesired == KEY_READ) {
106                         // Open existing key.
107                         if( RegOpenKeyEx(rhKey,
108                                 szKey,
109                                 0,
110                                 samDesired,
111                                 &hKey) != ERROR_SUCCESS) 
112                         {
113                                 return NULL ;
114                         }
115                         
116                 } else {
117                         // Create and open key and subkey.
118                         if( RegCreateKeyEx(rhKey,
119                                 szKey,
120                                 0, NULL, REG_OPTION_NON_VOLATILE,
121                                 samDesired, NULL,
122                                 &hKey, NULL) != ERROR_SUCCESS) 
123                         {
124                                 return NULL ;
125                         }
126                 }
127                 
128                 return hKey;
129         
130         }
131         
132         void Directive::ReadValAsString(string key, LPCTSTR defined_in_val) {
133                 HKEY hKey;
134                 char RegBuff[MAX_REG_BUFF];
135                 long debug;
136                 DWORD dwRead=MAX_REG_BUFF*sizeof(char);
137                 
138                 if (hKey = OpenKey(key.c_str(),KEY_READ)) {
139                         if ((debug = RegQueryValueEx (hKey,name.c_str(), NULL, NULL, (LPBYTE)RegBuff, &dwRead)) == ERROR_SUCCESS) {
140                                 if (type == D_FREE_INT || type == D_BOUND_INT) {
141                                         char tmpw[22];
142                                         value = itoa(*(DWORD *)RegBuff,tmpw,10);
143                                 } else {
144                                         value = RegBuff;
145                                 }
146                                 defined_in = defined_in_val;
147                         } 
148                         RegCloseKey (hKey); 
149                 } 
150         }
151         
152         bool Directive::WriteRegInt(const _TCHAR* szKey,
153                 const _TCHAR* szValueName,
154                 const _TCHAR* szValue)
155         {
156                 HKEY hKey;
157                 DWORD value;
158                 
159                 if (!(hKey=OpenKey(szKey,KEY_ALL_ACCESS)))
160                         return FALSE;
161                 
162                 // Set the Value.
163                 if (szValue != NULL)
164                 {
165                         value = _ttoi(szValue);
166                         RegSetValueEx(hKey, szValueName, 0, REG_DWORD,
167                                 (BYTE *)&value,
168                                 sizeof(DWORD)) ;
169                 }
170                 
171                 RegCloseKey(hKey) ;
172                 return TRUE ;
173         }
174         
175
176         bool Directive::WriteRegString(const _TCHAR* szKey,
177                 const _TCHAR* szValueName,
178                 const _TCHAR* szValue)
179         {
180                 HKEY hKey;
181                 
182                 if (!(hKey=OpenKey(szKey,KEY_ALL_ACCESS)))
183                         return FALSE;
184                 
185                 // Set the Value.
186                 if (szValue != NULL)
187                 {
188                         RegSetValueEx(hKey, szValueName, 0, REG_SZ,
189                                 (BYTE *)szValue,
190                                 ((DWORD)_tcslen(szValue)+1)*sizeof(_TCHAR)) ;
191                 }
192                 
193                 RegCloseKey(hKey);
194                 return TRUE ;
195         }
196
197         void Directive::Get_Value() {
198                 
199                 string key, node, defined_in_val;
200                 size_t pos = 0;
201                 size_t opos = 0;
202                 char done = 0;
203                 
204                 value = directives[d_index].value;
205                 defined_in = directives[d_index].defined_in;
206
207                 key = SHIB_DEFAULT_WEB_KEY;
208
209                 do {
210
211                         pos = path.find('/',opos);  // while we still have a '/' left to deal with
212                         
213                         if (pos != string::npos) {
214                                 node = path.substr(opos,pos-opos);
215                         }
216                         else {
217                                 node = path.substr(opos);
218                                 done = 1;
219                         }
220                         if (defined_in_val[defined_in_val.length()-1] != '/') {
221                                 defined_in_val += "/";
222                                 key += "\\";
223                         }
224
225                         defined_in_val += node;
226                         key += node;
227                         
228                         ReadValAsString(key,defined_in_val.c_str());
229                         opos = pos+1;
230                         
231                 } while (!done);
232         }
233 };