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.

719 lines
16 KiB

  1. #include "procs.hxx"
  2. #pragma hdrstop
  3. //----------------------------------------------------------------------------
  4. //
  5. // Function: NWApiGetBinderyHandle
  6. //
  7. // Synopsis:
  8. //
  9. //----------------------------------------------------------------------------
  10. HRESULT
  11. NWApiGetBinderyHandle(
  12. NWCONN_HANDLE *phConnReturned,
  13. BSTR bstrBinderyName
  14. )
  15. {
  16. NWLOCAL_SCOPE ScopeFlag = 0;
  17. NWCONN_HANDLE hConn;
  18. NWCCODE usRet = SUCCESSFUL;
  19. HRESULT hr = S_OK;
  20. usRet = NWCAttachToFileServerW(
  21. bstrBinderyName,
  22. ScopeFlag,
  23. &hConn
  24. );
  25. hr = HRESULT_FROM_NWCCODE(usRet);
  26. BAIL_ON_FAILURE(hr);
  27. //
  28. // Return.
  29. //
  30. *phConnReturned = hConn;
  31. RRETURN(hr);
  32. error:
  33. *phConnReturned = NULL;
  34. RRETURN(hr);
  35. }
  36. //----------------------------------------------------------------------------
  37. //
  38. // Function: NWApiReleaseBinderyHandle
  39. //
  40. // Synopsis:
  41. //
  42. //----------------------------------------------------------------------------
  43. HRESULT
  44. NWApiReleaseBinderyHandle(
  45. NWCONN_HANDLE hConn
  46. )
  47. {
  48. HRESULT hr = S_OK;
  49. NWCCODE usRet = SUCCESSFUL;
  50. if (hConn) {
  51. usRet = NWCDetachFromFileServer(hConn);
  52. hr = HRESULT_FROM_NWCCODE(usRet);
  53. }
  54. RRETURN(hr);
  55. }
  56. //----------------------------------------------------------------------------
  57. //
  58. // Function: NWApiWriteProperty
  59. //
  60. // Synopsis: This function modifies values of a bindery property. For now, it
  61. // only accept one buffer segment. However, one segment is enough
  62. // for most practical purpose. If the bindery property does not
  63. // exist, the function will attempt to create the property.
  64. //
  65. //----------------------------------------------------------------------------
  66. HRESULT
  67. NWApiWriteProperty(
  68. NWCONN_HANDLE hConn,
  69. BSTR bstrObjectName,
  70. NWOBJ_TYPE wObjType,
  71. LPSTR lpszPropertyName,
  72. NWSEGMENT_DATA *SegmentData
  73. )
  74. {
  75. CHAR szObjectName[(OBJ_NAME_SIZE + 1)*2];
  76. HRESULT hr = S_OK;
  77. NWCCODE usRet;
  78. //
  79. // Convert BSTR into an ANSI representation required by NWC APIs. "0" is
  80. // passed to UnicodeToAnsiString when the length of the string is unknown.
  81. //
  82. if (wcslen(bstrObjectName) > OBJ_NAME_SIZE) {
  83. hr = E_INVALIDARG;
  84. BAIL_ON_FAILURE(hr);
  85. }
  86. UnicodeToAnsiString(
  87. bstrObjectName,
  88. szObjectName,
  89. 0
  90. );
  91. usRet = NWCWritePropertyValue(
  92. hConn,
  93. szObjectName,
  94. wObjType,
  95. lpszPropertyName,
  96. 1, // "1" for one segment.
  97. SegmentData,
  98. 0 // "0" for no more segment.
  99. );
  100. //
  101. // Create the property if it doesn't exist and attempt to write again.
  102. //
  103. // If the property doesn't exist, NWCWritePropertyValue will return
  104. // UNSUCCESSFUL, not NO_SUCH_PROPERTY (bug #34833 --- by design).
  105. // So if the call doesn't succeed, try to create the property and
  106. // see if that succeeds.
  107. if (usRet == 0xffff) {
  108. hr = NWApiCreateProperty(
  109. hConn,
  110. bstrObjectName,
  111. wObjType,
  112. lpszPropertyName,
  113. BF_ITEM
  114. );
  115. BAIL_ON_FAILURE(hr);
  116. usRet = NWCWritePropertyValue(
  117. hConn,
  118. szObjectName,
  119. wObjType,
  120. lpszPropertyName,
  121. 1, // "1" for one segment.
  122. SegmentData,
  123. 0 // "0" for no more segment.
  124. );
  125. }
  126. hr = HRESULT_FROM_NWCCODE(usRet);
  127. error:
  128. RRETURN(hr);
  129. }
  130. //----------------------------------------------------------------------------
  131. //
  132. // Function: NWApiObjectEnum
  133. //
  134. // Synopsis:
  135. //
  136. //----------------------------------------------------------------------------
  137. HRESULT
  138. NWApiObjectEnum(
  139. NWCONN_HANDLE hConn,
  140. NWOBJ_TYPE dwObjType,
  141. LPWSTR *lppszObjectName,
  142. DWORD *pdwResumeObjectID
  143. )
  144. {
  145. HRESULT hr = S_OK;
  146. LPWSTR lpszTemp = NULL;
  147. NWCCODE usRet = SUCCESSFUL;
  148. NWOBJ_TYPE pdwObjType = 0xFFFF;
  149. NWFLAGS pucHasProperties;
  150. NWFLAGS pucObjectFlags;
  151. NWFLAGS pucObjSecurity;
  152. CHAR szObjectName[(OBJ_NAME_SIZE + 1)*2];
  153. //
  154. // This call will fail and return 0xffff if the user is not authenticated
  155. // on the server to which the hConn handle is attached to.
  156. //
  157. usRet = NWCScanObject(
  158. hConn,
  159. "*",
  160. dwObjType,
  161. pdwResumeObjectID,
  162. szObjectName,
  163. &pdwObjType,
  164. &pucHasProperties,
  165. &pucObjectFlags,
  166. &pucObjSecurity
  167. );
  168. hr = HRESULT_FROM_NWCCODE(usRet);
  169. BAIL_ON_FAILURE(hr);
  170. lpszTemp = AllocateUnicodeString(szObjectName);
  171. if (!lpszTemp) {
  172. RRETURN(E_OUTOFMEMORY);
  173. }
  174. *lppszObjectName = AllocADsStr(lpszTemp);
  175. if (!(*lppszObjectName)) {
  176. RRETURN(E_OUTOFMEMORY);
  177. }
  178. if(lpszTemp){
  179. FreeUnicodeString(lpszTemp);
  180. }
  181. RRETURN(hr);
  182. error:
  183. *lppszObjectName = NULL;
  184. pdwResumeObjectID = NULL;
  185. RRETURN(hr);
  186. }
  187. //----------------------------------------------------------------------------
  188. //
  189. // Function: NWApiValidateObject
  190. //
  191. // Synopsis:
  192. //
  193. //----------------------------------------------------------------------------
  194. HRESULT
  195. NWApiValidateObject(
  196. NWCONN_HANDLE hConn,
  197. NWOBJ_TYPE dwObjType,
  198. LPWSTR lpszObjectName,
  199. DWORD *pdwResumeObjectID
  200. )
  201. {
  202. HRESULT hr = S_OK;
  203. NWCCODE usRet = SUCCESSFUL;
  204. CHAR szAnsiObjectName[(OBJ_NAME_SIZE + 1)*2];
  205. CHAR szObjectName[(OBJ_NAME_SIZE + 1)*2];
  206. NWOBJ_TYPE pdwObjType = 0xFFFF;
  207. NWFLAGS pucHasProperties;
  208. NWFLAGS pucObjectFlags;
  209. NWFLAGS pucObjSecurity;
  210. //
  211. // Convert BSTR into an ANSI representation required by NWC APIs. "0" is
  212. // passed to UnicodeToAnsiString when the length of the string is unknown.
  213. //
  214. if (wcslen(lpszObjectName) > OBJ_NAME_SIZE) {
  215. RRETURN(E_INVALIDARG);
  216. }
  217. UnicodeToAnsiString(
  218. lpszObjectName,
  219. szAnsiObjectName,
  220. 0
  221. );
  222. //
  223. // This call will fail and return 0xffff if the user is not authenticated
  224. // on the server to which the hConn handle is attached to.
  225. //
  226. usRet = NWCScanObject(
  227. hConn,
  228. szAnsiObjectName,
  229. dwObjType,
  230. pdwResumeObjectID,
  231. szObjectName,
  232. &pdwObjType,
  233. &pucHasProperties,
  234. &pucObjectFlags,
  235. &pucObjSecurity
  236. );
  237. hr = HRESULT_FROM_NWCCODE(usRet);
  238. RRETURN(hr);
  239. }
  240. //----------------------------------------------------------------------------
  241. //
  242. // Function: NWApiGetAnyBinderyHandle
  243. //
  244. // Synopsis:
  245. //
  246. //----------------------------------------------------------------------------
  247. HRESULT
  248. NWApiGetAnyBinderyHandle(
  249. NWCONN_HANDLE *phConn
  250. )
  251. {
  252. HRESULT hr = S_OK;
  253. //
  254. // Get Bindery handle.
  255. //
  256. hr = NWApiGetBinderyHandle(
  257. phConn,
  258. L"*"
  259. );
  260. RRETURN(hr);
  261. }
  262. //----------------------------------------------------------------------------
  263. //
  264. // Function: NWApiGetObjectName
  265. //
  266. // Synopsis:
  267. //
  268. //----------------------------------------------------------------------------
  269. HRESULT
  270. NWApiGetObjectName(
  271. NWCONN_HANDLE hConn,
  272. DWORD dwObjectID,
  273. LPWSTR *lppszObjectName
  274. )
  275. {
  276. CHAR szObjectName[(OBJ_NAME_SIZE + 1)*2];
  277. HRESULT hr = S_OK;
  278. LPWSTR lpszTemp = NULL;
  279. NWCCODE usRet = SUCCESSFUL;
  280. NWOBJ_TYPE dwObjType;
  281. usRet = NWCGetObjectName(
  282. hConn,
  283. dwObjectID,
  284. szObjectName,
  285. &dwObjType
  286. );
  287. hr = HRESULT_FROM_NWCCODE(usRet);
  288. BAIL_ON_FAILURE(hr);
  289. lpszTemp = AllocateUnicodeString(szObjectName);
  290. if (!lpszTemp) {
  291. RRETURN(E_OUTOFMEMORY);
  292. }
  293. *lppszObjectName = AllocADsStr(lpszTemp);
  294. if (!(*lppszObjectName)) {
  295. RRETURN(E_OUTOFMEMORY);
  296. }
  297. FreeUnicodeString(lpszTemp);
  298. RRETURN(hr);
  299. error:
  300. *lppszObjectName = NULL;
  301. RRETURN(hr);
  302. }
  303. //----------------------------------------------------------------------------
  304. //
  305. // Function: HRESULT_FROM_NWCCODE
  306. //
  307. // Synopsis:
  308. //
  309. //----------------------------------------------------------------------------
  310. HRESULT
  311. HRESULT_FROM_NWCCODE(
  312. NWCCODE usRet
  313. )
  314. {
  315. HRESULT hr = S_OK;
  316. if (usRet != SUCCESSFUL) {
  317. hr = HRESULT_FROM_WIN32(GetLastError());
  318. if (hr == S_OK) {
  319. //
  320. // In case CSNW didn't SetLastError,
  321. // make sure we don't return a false S_OK,
  322. // since we know _some_ error occurred
  323. //
  324. hr = HRESULT_FROM_WIN32(ERROR_EXTENDED_ERROR);
  325. }
  326. if (hr == HRESULT_FROM_WIN32(ERROR_EXTENDED_ERROR)) {
  327. ADsSetLastError((DWORD) usRet,
  328. L"",
  329. L"NDS Provider"
  330. );
  331. }
  332. }
  333. RRETURN(hr);
  334. }
  335. //----------------------------------------------------------------------------
  336. //
  337. // Function: NWApiOpenPrinter
  338. //
  339. // Synopsis:
  340. //
  341. //----------------------------------------------------------------------------
  342. HRESULT
  343. NWApiOpenPrinter(
  344. LPWSTR lpszUncPrinterName,
  345. HANDLE *phPrinter,
  346. DWORD dwAccess
  347. )
  348. {
  349. BOOL fStatus = TRUE;
  350. HANDLE hPrinter;
  351. HRESULT hr = S_OK;
  352. PRINTER_DEFAULTS PrinterDefault = {0, 0, dwAccess};
  353. //
  354. // Set desired access right.
  355. //
  356. PrinterDefault.DesiredAccess = dwAccess;
  357. //
  358. // Get a handle to the speccified printer using Win32 API.
  359. //
  360. fStatus = OpenPrinter(
  361. lpszUncPrinterName,
  362. &hPrinter,
  363. &PrinterDefault
  364. );
  365. //
  366. // Convert error code into HRESULT.
  367. //
  368. if (fStatus == FALSE) {
  369. hr = HRESULT_FROM_WIN32(GetLastError());
  370. }
  371. //
  372. // Return.
  373. //
  374. else {
  375. *phPrinter = hPrinter;
  376. }
  377. RRETURN(hr);
  378. }
  379. //----------------------------------------------------------------------------
  380. //
  381. // Function: NWApiClosePrinter
  382. //
  383. // Synopsis:
  384. //
  385. //----------------------------------------------------------------------------
  386. HRESULT
  387. NWApiClosePrinter(
  388. HANDLE hPrinter
  389. )
  390. {
  391. BOOL fStatus = TRUE;
  392. HRESULT hr = S_OK;
  393. //
  394. // Close a printer using Win32 API.
  395. //
  396. fStatus = ClosePrinter(hPrinter);
  397. //
  398. // Convert error code into HRESULT.
  399. //
  400. if (fStatus == FALSE) {
  401. hr = HRESULT_FROM_WIN32(GetLastError());
  402. }
  403. //
  404. // Return.
  405. //
  406. RRETURN(hr);
  407. }
  408. //----------------------------------------------------------------------------
  409. //
  410. // Function: NWApiSetPrinter
  411. //
  412. // Synopsis:
  413. //
  414. //----------------------------------------------------------------------------
  415. HRESULT
  416. NWApiSetPrinter(
  417. HANDLE hPrinter,
  418. DWORD dwLevel,
  419. LPBYTE lpbPrinters,
  420. DWORD dwAccess
  421. )
  422. {
  423. BOOL fStatus = FALSE;
  424. HRESULT hr = S_OK;
  425. fStatus = SetPrinter(
  426. hPrinter,
  427. dwLevel,
  428. lpbPrinters,
  429. dwAccess
  430. );
  431. if (!fStatus) {
  432. goto error;
  433. }
  434. RRETURN(S_OK);
  435. error:
  436. hr = HRESULT_FROM_WIN32(GetLastError());
  437. RRETURN(hr);
  438. }
  439. //----------------------------------------------------------------------------
  440. //
  441. // Function: NWApiGetJob
  442. //
  443. // Synopsis:
  444. //
  445. //----------------------------------------------------------------------------
  446. HRESULT
  447. NWApiGetJob(
  448. HANDLE hPrinter,
  449. DWORD dwJobId,
  450. DWORD dwLevel,
  451. LPBYTE *lplpbJobs
  452. )
  453. {
  454. BOOL fStatus = FALSE;
  455. DWORD dwError = 0;
  456. DWORD dwNeeded = 0;
  457. DWORD dwPassed = 1024;
  458. LPBYTE pMem = NULL;
  459. //
  460. // Allocate memory for return buffer.
  461. //
  462. pMem = (LPBYTE)AllocADsMem(dwPassed);
  463. if (!pMem) {
  464. RRETURN(E_OUTOFMEMORY);
  465. }
  466. //
  467. // Get Job's information.
  468. //
  469. fStatus = GetJob(
  470. hPrinter,
  471. dwJobId,
  472. dwLevel,
  473. pMem,
  474. dwPassed,
  475. &dwNeeded
  476. );
  477. //
  478. // Get job's information again with a bigger buffer if a bigger buffer is
  479. // needed for the result.
  480. //
  481. if (!fStatus) {
  482. if (pMem){
  483. FreeADsMem(pMem);
  484. }
  485. if ((dwError = GetLastError()) != ERROR_INSUFFICIENT_BUFFER) {
  486. RRETURN(HRESULT_FROM_WIN32(dwError));
  487. }
  488. pMem = (LPBYTE)AllocADsMem(
  489. dwNeeded
  490. );
  491. if (!pMem) {
  492. RRETURN(E_OUTOFMEMORY);
  493. }
  494. dwPassed = dwNeeded;
  495. fStatus = GetJob(
  496. hPrinter,
  497. dwJobId,
  498. dwLevel,
  499. pMem,
  500. dwPassed,
  501. &dwNeeded
  502. );
  503. if (!fStatus) {
  504. FreeADsMem(pMem);
  505. RRETURN(HRESULT_FROM_WIN32(GetLastError()));
  506. }
  507. }
  508. //
  509. // Return.
  510. //
  511. *lplpbJobs = pMem;
  512. RRETURN(S_OK);
  513. }
  514. //----------------------------------------------------------------------------
  515. //
  516. // Function: NWApiSetJob
  517. //
  518. // Synopsis:
  519. //
  520. //----------------------------------------------------------------------------
  521. HRESULT
  522. NWApiSetJob(
  523. HANDLE hPrinter,
  524. DWORD dwJobId,
  525. DWORD dwLevel,
  526. LPBYTE lpbJobs,
  527. DWORD dwCommand
  528. )
  529. {
  530. BOOL fStatus = FALSE;
  531. HRESULT hr = S_OK;
  532. fStatus = SetJob(
  533. hPrinter,
  534. dwJobId,
  535. dwLevel,
  536. lpbJobs,
  537. dwCommand
  538. );
  539. if (!fStatus) {
  540. goto error;
  541. }
  542. RRETURN(S_OK);
  543. error:
  544. hr = HRESULT_FROM_WIN32(GetLastError());
  545. RRETURN(hr);
  546. }
  547. //----------------------------------------------------------------------------
  548. //
  549. // Function: NWApiCreateProperty
  550. //
  551. // Synopsis: This function creates a bindery property. Access is logged read,
  552. // supervisor write.
  553. //
  554. //----------------------------------------------------------------------------
  555. HRESULT
  556. NWApiCreateProperty(
  557. NWCONN_HANDLE hConn,
  558. LPWSTR lpszObjectName,
  559. NWOBJ_TYPE wObjType,
  560. LPSTR lpszPropertyName,
  561. NWFLAGS ucObjectFlags
  562. )
  563. {
  564. CHAR szAnsiObjectName[(OBJ_NAME_SIZE + 1)*2];
  565. HRESULT hr = S_OK;
  566. NWCCODE usRet = SUCCESSFUL;
  567. //
  568. // Convert BSTR into an ANSI representation required by NWC APIs. "0" is
  569. // passed to UnicodeToAnsiString when the length of the string is unknown.
  570. //
  571. if (wcslen(lpszObjectName) > OBJ_NAME_SIZE) {
  572. RRETURN(E_INVALIDARG);
  573. }
  574. UnicodeToAnsiString(
  575. lpszObjectName,
  576. szAnsiObjectName,
  577. 0
  578. );
  579. //
  580. // Create property.
  581. //
  582. usRet = NWCCreateProperty(
  583. hConn,
  584. szAnsiObjectName,
  585. wObjType,
  586. lpszPropertyName,
  587. ucObjectFlags,
  588. BS_LOGGED_READ | BS_SUPER_WRITE
  589. );
  590. //
  591. // Return.
  592. //
  593. hr = HRESULT_FROM_NWCCODE(usRet);
  594. RRETURN(hr);
  595. }