Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

559 lines
14 KiB

  1. /***************************************************************************
  2. Name : REGISTRY.C
  3. Comment : INIfile handling
  4. Revision Log
  5. Date Name Description
  6. -------- ----- ---------------------------------------------------------
  7. ***************************************************************************/
  8. #define USE_DEBUG_CONTEXT DEBUG_CONTEXT_T30_MAIN
  9. #include "prep.h"
  10. #include "efaxcb.h"
  11. #include "t30.h"
  12. #include "hdlc.h"
  13. #include "debug.h"
  14. #include "glbproto.h"
  15. #include "faxreg.h"
  16. // These are NOT localizable items.
  17. #define szKEYPREFIX REGKEY_TAPIDEVICES
  18. #define szKEYCLASS "DATA"
  19. DWORD my_atoul(LPSTR lpsz);
  20. ULONG_PTR ProfileOpen(DWORD dwProfileID, LPSTR lpszSection, DWORD dwFlags)
  21. {
  22. ULONG_PTR dwRet = 0;
  23. char rgchKey[128];
  24. HKEY hKey=0;
  25. LONG l;
  26. LPSTR lpszPrefix;
  27. DWORD sam=0;
  28. if (dwProfileID==OEM_BASEKEY)
  29. {
  30. lpszPrefix= ""; // we don't prepend szKEYPREFIX
  31. if (!lpszSection) goto failure;
  32. }
  33. else if (lpszSection)
  34. {
  35. lpszPrefix= szKEYPREFIX "\\";
  36. }
  37. else
  38. {
  39. lpszPrefix= szKEYPREFIX;
  40. lpszSection="";
  41. }
  42. if ((lstrlen(lpszPrefix)+lstrlen(lpszSection))>=sizeof(rgchKey))
  43. goto failure;
  44. lstrcpy(rgchKey, lpszPrefix);
  45. lstrcat(rgchKey, lpszSection);
  46. sam = 0;
  47. if (dwFlags &fREG_READ) sam |= KEY_READ;
  48. if (dwFlags &fREG_WRITE) sam |= KEY_WRITE;
  49. if (dwFlags & fREG_CREATE)
  50. {
  51. DWORD dwDisposition=0;
  52. DWORD dwOptions = (dwFlags & fREG_VOLATILE)
  53. ?REG_OPTION_VOLATILE
  54. :REG_OPTION_NON_VOLATILE;
  55. sam = KEY_READ | KEY_WRITE; // we force sam to this when creating.
  56. l = RegCreateKeyEx(
  57. HKEY_LOCAL_MACHINE, // handle of open key
  58. rgchKey, // address of name of subkey to open
  59. 0, // reserved
  60. szKEYCLASS, // address of class string
  61. dwOptions, // special options flag
  62. sam, // desired security access
  63. NULL, // address of key security structure
  64. &hKey, // address of buffer for opened handle
  65. &dwDisposition // address of dispostion value buffer
  66. );
  67. }
  68. else
  69. {
  70. l = RegOpenKeyEx(
  71. HKEY_LOCAL_MACHINE, // handle of open key
  72. rgchKey, // address of name of subkey to open
  73. 0, // reserved
  74. sam , // desired security access
  75. &hKey // address of buffer for opened handle
  76. );
  77. }
  78. if (l!=ERROR_SUCCESS)
  79. {
  80. //LOG((_ERR, "RegCreateKeyEx returns error %ld\n", (long) l));
  81. goto failure;
  82. }
  83. dwRet = (ULONG_PTR) hKey;
  84. return dwRet;
  85. failure:
  86. return 0;
  87. }
  88. UINT
  89. ProfileListGetInt
  90. (
  91. ULONG_PTR KeyList[10],
  92. LPSTR lpszValueName,
  93. UINT uDefault
  94. )
  95. {
  96. int i;
  97. int Num=0;
  98. UINT uRet = uDefault;
  99. BOOL fExist = 0;
  100. for (i=0; i<10; i++)
  101. {
  102. if (KeyList[i] == 0)
  103. {
  104. Num = i-1;
  105. break;
  106. }
  107. }
  108. for (i=Num; i>=0; i--)
  109. {
  110. uRet = ProfileGetInt (KeyList[i], lpszValueName, uDefault, &fExist);
  111. if (fExist)
  112. {
  113. return uRet;
  114. }
  115. }
  116. return uRet;
  117. }
  118. UINT ProfileGetInt(ULONG_PTR dwKey, LPSTR lpszValueName, UINT uDefault, BOOL *fExist)
  119. {
  120. UINT uRet = uDefault;
  121. char rgchBuf[128];
  122. DWORD dwType;
  123. DWORD dwcbSize=sizeof(rgchBuf);
  124. LONG l = RegQueryValueEx(
  125. (HKEY) dwKey,
  126. lpszValueName,
  127. 0,
  128. &dwType,
  129. rgchBuf,
  130. &dwcbSize);
  131. if (fExist)
  132. {
  133. *fExist = 0;
  134. }
  135. if (l!=ERROR_SUCCESS)
  136. {
  137. //LOG((_ERR, "RegQueryValueEx returned error %ld\n", (long) l));
  138. goto end;
  139. }
  140. if (fExist)
  141. {
  142. *fExist = 1;
  143. }
  144. if (dwType != REG_SZ)
  145. {
  146. //LOG((_ERR, "RegQueryValueEx value type not string:0x%lx\n",
  147. // (unsigned long) dwType));
  148. goto end;
  149. }
  150. uRet = (UINT) my_atoul(rgchBuf);
  151. end:
  152. return uRet;
  153. }
  154. DWORD ProfileGetString
  155. (
  156. ULONG_PTR dwKey,
  157. LPSTR lpszValueName,
  158. LPSTR lpszDefault,
  159. LPSTR lpszBuf ,
  160. DWORD dwcbMax
  161. )
  162. {
  163. DWORD dwRet = 0;
  164. DWORD dwType;
  165. LONG l = RegQueryValueEx(
  166. (HKEY) dwKey,
  167. lpszValueName,
  168. 0,
  169. &dwType,
  170. lpszBuf,
  171. &dwcbMax);
  172. if (l!=ERROR_SUCCESS)
  173. {
  174. //LOG((_ERR, "RegQueryValueEx returned error %ld\n", (long) l));
  175. goto copy_default;
  176. }
  177. if (dwType != REG_SZ)
  178. {
  179. //LOG((_ERR, "RegQueryValueEx value type not string:0x%lx\n",
  180. // (unsigned long) dwType));
  181. goto copy_default;
  182. }
  183. // Make sure we null-terminate the string and return the true string
  184. // length..
  185. if (dwcbMax)
  186. {
  187. lpszBuf[dwcbMax-1]=0;
  188. dwcbMax = (DWORD) lstrlen(lpszBuf);
  189. }
  190. dwRet = dwcbMax;
  191. goto end;
  192. copy_default:
  193. dwRet = 0;
  194. if (!lpszDefault || !*lpszDefault)
  195. {
  196. if (dwcbMax) *lpszBuf=0;
  197. }
  198. else
  199. {
  200. UINT cb = _fstrlen(lpszDefault)+1;
  201. if (cb>(UINT)dwcbMax) cb=dwcbMax;
  202. if (cb)
  203. {
  204. _fmemcpy(lpszBuf, lpszDefault, cb);
  205. lpszBuf[cb-1]=0;
  206. dwRet = cb-1;
  207. }
  208. }
  209. // fall through...
  210. end:
  211. return dwRet;
  212. }
  213. BOOL
  214. ProfileWriteString
  215. (
  216. ULONG_PTR dwKey,
  217. LPSTR lpszValueName,
  218. LPSTR lpszBuf,
  219. BOOL fRemoveCR
  220. )
  221. {
  222. // NOTE: If lpszValueName is null, delete the key. (can't do this in,
  223. // the registry, unfortunately).
  224. // If lpszBuf is null pointer -- "delete" this value.
  225. BOOL fRet=FALSE;
  226. LONG l;
  227. if (!lpszValueName)
  228. goto end;
  229. if (!lpszBuf)
  230. {
  231. // delete value...
  232. l = RegDeleteValue((HKEY) dwKey, lpszValueName);
  233. if (l!=ERROR_SUCCESS) goto end;
  234. }
  235. else
  236. {
  237. if (fRemoveCR)
  238. {
  239. RemoveCR (lpszBuf);
  240. }
  241. l = RegSetValueEx((HKEY) dwKey, lpszValueName, 0, REG_SZ,
  242. lpszBuf, lstrlen(lpszBuf)+1);
  243. if (l!=ERROR_SUCCESS)
  244. {
  245. //LOG((_ERR,
  246. // "RegSetValueEx(\"%s\", \"%s\") returned error %ld\n",
  247. // (LPSTR) lpszValueName,
  248. // (LPSTR) lpszBuf,
  249. // (long) l));
  250. goto end;
  251. }
  252. }
  253. fRet = TRUE;
  254. goto end;
  255. end:
  256. return fRet;
  257. }
  258. void ProfileClose(ULONG_PTR dwKey)
  259. {
  260. if (RegCloseKey((HKEY)dwKey)!=ERROR_SUCCESS)
  261. {
  262. //LOG((_WRN, "Couldn't close registry key:%lu\n\r",
  263. // (unsigned long) dwKey));
  264. }
  265. }
  266. BOOL ProfileDeleteSection(DWORD dwProfileID, LPSTR lpszSection)
  267. {
  268. char rgchKey[128];
  269. LPSTR lpszPrefix= szKEYPREFIX "\\";
  270. if (dwProfileID==OEM_BASEKEY) goto failure; // Can't delete this
  271. if ((lstrlen(lpszPrefix)+lstrlen(lpszSection))>=sizeof(rgchKey))
  272. goto failure;
  273. lstrcpy(rgchKey, lpszPrefix);
  274. lstrcat(rgchKey, lpszSection);
  275. return (RegDeleteKey(HKEY_LOCAL_MACHINE, rgchKey)==ERROR_SUCCESS);
  276. failure:
  277. return FALSE;
  278. }
  279. BOOL
  280. ProfileCopyTree
  281. (
  282. DWORD dwProfileIDTo,
  283. LPSTR lpszSectionTo,
  284. DWORD dwProfileIDFr,
  285. LPSTR lpszSectionFr
  286. )
  287. {
  288. BOOL fRet=TRUE;
  289. char SecTo[200];
  290. char SecFr[200];
  291. //
  292. // Since there is no CopyKeyWithAllSubkeys API, it is difficult to write generic tree-walking algorithm.
  293. // We will hard-code the keys here.
  294. //
  295. // copy Fax key always
  296. ProfileCopySection(dwProfileIDTo,
  297. lpszSectionTo,
  298. dwProfileIDFr,
  299. lpszSectionFr,
  300. TRUE);
  301. // copy Fax/Class1 key if exists
  302. sprintf(SecTo, "%s\\Class1", lpszSectionTo);
  303. sprintf(SecFr, "%s\\Class1", lpszSectionFr);
  304. ProfileCopySection(dwProfileIDTo,
  305. SecTo,
  306. dwProfileIDFr,
  307. SecFr,
  308. FALSE);
  309. // copy Fax/Class1/AdaptiveAnswer key if exists
  310. sprintf(SecTo, "%s\\Class1\\AdaptiveAnswer", lpszSectionTo);
  311. sprintf(SecFr, "%s\\Class1\\AdaptiveAnswer", lpszSectionFr);
  312. ProfileCopySection(dwProfileIDTo,
  313. SecTo,
  314. dwProfileIDFr,
  315. SecFr,
  316. FALSE);
  317. // copy Fax/Class1/AdaptiveAnswer/Answer key if exists
  318. sprintf(SecTo, "%s\\Class1\\AdaptiveAnswer\\AnswerCommand", lpszSectionTo);
  319. sprintf(SecFr, "%s\\Class1\\AdaptiveAnswer\\AnswerCommand", lpszSectionFr);
  320. ProfileCopySection(dwProfileIDTo,
  321. SecTo,
  322. dwProfileIDFr,
  323. SecFr,
  324. FALSE);
  325. // copy Fax/Class2 key if exists
  326. sprintf(SecTo, "%s\\Class2", lpszSectionTo);
  327. sprintf(SecFr, "%s\\Class2", lpszSectionFr);
  328. ProfileCopySection(dwProfileIDTo,
  329. SecTo,
  330. dwProfileIDFr,
  331. SecFr,
  332. FALSE);
  333. // copy Fax/Class2/AdaptiveAnswer key if exists
  334. sprintf(SecTo, "%s\\Class2\\AdaptiveAnswer", lpszSectionTo);
  335. sprintf(SecFr, "%s\\Class2\\AdaptiveAnswer", lpszSectionFr);
  336. ProfileCopySection(dwProfileIDTo,
  337. SecTo,
  338. dwProfileIDFr,
  339. SecFr,
  340. FALSE);
  341. // copy Fax/Class2/AdaptiveAnswer/Answer key if exists
  342. sprintf(SecTo, "%s\\Class2\\AdaptiveAnswer\\AnswerCommand", lpszSectionTo);
  343. sprintf(SecFr, "%s\\Class2\\AdaptiveAnswer\\AnswerCommand", lpszSectionFr);
  344. ProfileCopySection(dwProfileIDTo,
  345. SecTo,
  346. dwProfileIDFr,
  347. SecFr,
  348. FALSE);
  349. // copy Fax/Class2_0 key if exists
  350. sprintf(SecTo, "%s\\Class2_0", lpszSectionTo);
  351. sprintf(SecFr, "%s\\Class2_0", lpszSectionFr);
  352. ProfileCopySection(dwProfileIDTo,
  353. SecTo,
  354. dwProfileIDFr,
  355. SecFr,
  356. FALSE);
  357. // copy Fax/Class2_0/AdaptiveAnswer key if exists
  358. sprintf(SecTo, "%s\\Class2_0\\AdaptiveAnswer", lpszSectionTo);
  359. sprintf(SecFr, "%s\\Class2_0\\AdaptiveAnswer", lpszSectionFr);
  360. ProfileCopySection(dwProfileIDTo,
  361. SecTo,
  362. dwProfileIDFr,
  363. SecFr,
  364. FALSE);
  365. // copy Fax/Class2/AdaptiveAnswer/Answer key if exists
  366. sprintf(SecTo, "%s\\Class2_0\\AdaptiveAnswer\\AnswerCommand", lpszSectionTo);
  367. sprintf(SecFr, "%s\\Class2_0\\AdaptiveAnswer\\AnswerCommand", lpszSectionFr);
  368. ProfileCopySection(dwProfileIDTo,
  369. SecTo,
  370. dwProfileIDFr,
  371. SecFr,
  372. FALSE);
  373. return fRet;
  374. }
  375. BOOL
  376. ProfileCopySection
  377. (
  378. DWORD dwProfileIDTo,
  379. LPSTR lpszSectionTo,
  380. DWORD dwProfileIDFr,
  381. LPSTR lpszSectionFr,
  382. BOOL fCreateAlways
  383. )
  384. {
  385. BOOL fRet=FALSE;
  386. DWORD iValue=0;
  387. DWORD cbValue, cbData, dwType;
  388. char rgchValue[60], rgchData[256];
  389. HKEY hkFr;
  390. HKEY hkTo;
  391. hkFr = (HKEY) ProfileOpen(dwProfileIDFr, lpszSectionFr, fREG_READ);
  392. if ( (!hkFr) && (!fCreateAlways) )
  393. {
  394. return fRet;
  395. }
  396. hkTo = (HKEY) ProfileOpen( dwProfileIDTo,
  397. lpszSectionTo,
  398. fREG_CREATE |fREG_READ|fREG_WRITE);
  399. if (!hkTo || !hkFr)
  400. goto end;
  401. iValue=0;
  402. dwType=0;
  403. cbValue=sizeof(rgchValue);
  404. cbData=sizeof(rgchData);
  405. while( RegEnumValue( hkFr,
  406. iValue,
  407. rgchValue,
  408. &cbValue,
  409. NULL,
  410. &dwType,
  411. rgchData,
  412. &cbData)==ERROR_SUCCESS)
  413. {
  414. if (RegQueryValueEx( hkFr,
  415. rgchValue,
  416. NULL,
  417. &dwType,
  418. rgchData,
  419. &cbData)==ERROR_SUCCESS)
  420. {
  421. if (RegSetValueEx( hkTo,
  422. rgchValue,
  423. 0,
  424. dwType,
  425. rgchData,
  426. cbData)== ERROR_SUCCESS)
  427. {
  428. fRet=TRUE;
  429. }
  430. }
  431. iValue++;dwType=0;cbValue=sizeof(rgchValue);cbData=sizeof(rgchData);
  432. }
  433. end:
  434. if (hkTo) RegCloseKey(hkTo);
  435. if (hkFr) RegCloseKey(hkFr);
  436. return fRet;
  437. }
  438. DWORD my_atoul(LPSTR lpsz)
  439. {
  440. unsigned i=8, c;
  441. unsigned long ul=0;
  442. while(i-- && (c=*lpsz++)) {
  443. ul*=10;
  444. switch(c) {
  445. case '0': break;
  446. case '1':ul+=1; break;
  447. case '2':ul+=2; break;
  448. case '3':ul+=3; break;
  449. case '4':ul+=4; break;
  450. case '5':ul+=5; break;
  451. case '6':ul+=6; break;
  452. case '7':ul+=7; break;
  453. case '8':ul+=8; break;
  454. case '9':ul+=9; break;
  455. default: goto end;
  456. }
  457. }
  458. end:
  459. return ul;
  460. }