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.

371 lines
10 KiB

  1. /****************************** Module Header ******************************\
  2. * Module Name: net.c
  3. *
  4. * PURPOSE: Contains routines network support
  5. *
  6. * Created: Feb 1991
  7. *
  8. * Copyright (c) 1991 Microsoft Corporation
  9. *
  10. * History:
  11. * Srinik 02\12\1190 Orginal
  12. *
  13. \***************************************************************************/
  14. #include <windows.h>
  15. #include <winnet.h>
  16. #include "dll.h"
  17. #define MAX_DRIVE 26
  18. char szNULL[] = "";
  19. char szSS[] = "SS";
  20. char szOffset[] = "OFFSET";
  21. BOOL FAR PASCAL GetTaskVisibleWindow (HWND, DWORD);
  22. void INTERNAL RemoveNetName (LPOBJECT_LE);
  23. // Gets the drive letter from topic (if one exists) and then gets the remote
  24. // name for that drive and then saves it in the object.
  25. OLESTATUS FARINTERNAL SetNetName (lpobj)
  26. LPOBJECT_LE lpobj;
  27. {
  28. char buf[MAX_STR];
  29. WORD cbBuf = sizeof(buf);
  30. WORD driveType;
  31. char szDrive[3];
  32. if (lpobj->head.ctype == CT_EMBEDDED)
  33. return OLE_OK;
  34. if (!GlobalGetAtomName (lpobj->topic, buf, cbBuf))
  35. return OLE_ERROR_BLANK;
  36. if (buf[1] != ':') {
  37. RemoveNetName (lpobj);
  38. return OLE_OK;
  39. }
  40. szDrive[2] = NULL;
  41. szDrive[1] = ':';
  42. szDrive[0] = buf[0];
  43. AnsiUpperBuff ((LPSTR) szDrive, 1);
  44. if (!(driveType = GetDriveType (szDrive[0] - 'A'))) {
  45. // drive is non existent
  46. return OLE_ERROR_DRIVE;
  47. }
  48. if (driveType == DRIVE_REMOTE) {
  49. if (WNetGetConnection (szDrive, buf, (WORD FAR *) &cbBuf)
  50. != WN_SUCCESS)
  51. return OLE_ERROR_DRIVE;
  52. lpobj->cDrive = szDrive[0];
  53. if (lpobj->aNetName)
  54. GlobalDeleteAtom (lpobj->aNetName);
  55. lpobj->aNetName = GlobalAddAtom(buf);
  56. lpobj->dwNetInfo = MAKELONG((WNetGetCaps (WNNC_NET_TYPE)),
  57. (WNetGetCaps (WNNC_DRIVER_VERSION)));
  58. }
  59. else {
  60. RemoveNetName (lpobj);
  61. }
  62. return OLE_OK;
  63. }
  64. // If netname exists for the given object, then it makes sure that drive
  65. // in topic corresponds to the netname. If it's not the drive letter will
  66. // be fixed by calling FixNet()
  67. OLESTATUS FARINTERNAL CheckNetDrive (lpobj, fNetDlg)
  68. LPOBJECT_LE lpobj;
  69. BOOL fNetDlg;
  70. {
  71. char buf[MAX_NET_NAME];
  72. char netName[MAX_NET_NAME];
  73. WORD cbBuf = sizeof(buf);
  74. char szDrive[3];
  75. if (lpobj->head.ctype == CT_EMBEDDED)
  76. return OLE_OK;
  77. if (!lpobj->aNetName)
  78. return OLE_OK;
  79. if (!GlobalGetAtomName (lpobj->aNetName, netName, sizeof(netName)))
  80. return OLE_ERROR_MEMORY;
  81. szDrive[2] = NULL;
  82. szDrive[1] = ':';
  83. if (!(szDrive[0] = lpobj->cDrive)) {
  84. if (GlobalGetAtomName (lpobj->topic, buf, sizeof(buf)))
  85. szDrive[0] = lpobj->cDrive = buf[0];
  86. }
  87. if ((WNetGetConnection (szDrive, buf, (WORD FAR *) &cbBuf)
  88. == WN_SUCCESS) && (!lstrcmp(netName, buf)))
  89. return OLE_OK;
  90. return FixNet (lpobj, netName, fNetDlg);
  91. }
  92. // Find if there is a drive connected to the given server. If so, get the
  93. // drive letter and set it in topic. If not try to make connection, and if
  94. // that attempt is successful the set the drive letter in topic.
  95. OLESTATUS INTERNAL FixNet (lpobj, lpNetName, fNetDlg)
  96. LPOBJECT_LE lpobj;
  97. LPSTR lpNetName;
  98. BOOL fNetDlg;
  99. {
  100. int nDrive = 2; // drive 'C'
  101. OLESTATUS retVal;
  102. if (SetNextNetDrive(lpobj, &nDrive, lpNetName))
  103. return OLE_OK;
  104. if (fNetDlg != POPUP_NETDLG)
  105. return OLE_ERROR_NETWORK;
  106. if ((retVal = ConnectNet (lpobj, lpNetName)) == OLE_OK) {
  107. if (!ChangeTopic (lpobj))
  108. return OLE_ERROR_BLANK;
  109. }
  110. return retVal;
  111. }
  112. BOOL FARINTERNAL SetNextNetDrive (lpobj, lpnDrive, lpNetName)
  113. LPOBJECT_LE lpobj;
  114. int FAR * lpnDrive;
  115. LPSTR lpNetName;
  116. {
  117. char buf[MAX_STR];
  118. WORD cbBuf = sizeof(buf);
  119. char szDrive[3];
  120. if (!lpNetName[0]) {
  121. if (!GlobalGetAtomName(lpobj->aNetName, lpNetName, MAX_STR))
  122. return FALSE;
  123. }
  124. szDrive[2] = NULL;
  125. szDrive[1] = ':';
  126. while (*lpnDrive < MAX_DRIVE) {
  127. if (GetDriveType (++*lpnDrive) == DRIVE_REMOTE) {
  128. szDrive[0] = (char) ('A' + *lpnDrive);
  129. cbBuf = sizeof(buf);
  130. if ((WNetGetConnection (szDrive, buf, (WORD FAR *) &cbBuf)
  131. == WN_SUCCESS) && (!lstrcmp(lpNetName, buf))) {
  132. lpobj->cDrive = szDrive[0];
  133. return ChangeTopic (lpobj);
  134. }
  135. }
  136. }
  137. return FALSE;
  138. }
  139. BOOL FARINTERNAL ChangeTopic (lpobj)
  140. LPOBJECT_LE lpobj;
  141. {
  142. char buf[MAX_STR];
  143. if (!GlobalGetAtomName(lpobj->topic, buf, sizeof(buf)))
  144. return FALSE;
  145. if (lpobj->topic)
  146. GlobalDeleteAtom(lpobj->topic);
  147. buf[0] = lpobj->cDrive;
  148. lpobj->topic = GlobalAddAtom (buf);
  149. if (lpobj->hLink) {
  150. GlobalFree (lpobj->hLink);
  151. lpobj->hLink = NULL;
  152. }
  153. return TRUE;
  154. }
  155. OLESTATUS INTERNAL ConnectNet (lpobj, lpNetName)
  156. LPOBJECT_LE lpobj;
  157. LPSTR lpNetName;
  158. {
  159. FARPROC lpConnectDlg;
  160. FARPROC lpGetTaskVisWnd;
  161. OLESTATUS retVal = OLE_ERROR_MEMORY;
  162. HWND hCurTask;
  163. HWND hwndParent = NULL;
  164. if (!(lpConnectDlg = MakeProcInstance ((FARPROC) ConnectDlgProc,
  165. hInstDLL)))
  166. return OLE_ERROR_MEMORY;
  167. hCurTask = GetCurrentTask();
  168. ASSERT (hCurTask, "Current task handle in NULL");
  169. if (!(lpGetTaskVisWnd = MakeProcInstance (GetTaskVisibleWindow,hInstDLL)))
  170. goto errRtn;
  171. // Get the container task's main window, and use that as parent for
  172. // the dlg box.
  173. EnumTaskWindows (hCurTask, lpGetTaskVisWnd,
  174. (DWORD) ((WORD FAR *) &hwndParent));
  175. if (lpobj->cDrive = (char) DialogBoxParam (hInstDLL, "CONNECTDLG",
  176. hwndParent, lpConnectDlg,
  177. (DWORD) lpNetName))
  178. retVal = OLE_OK;
  179. else
  180. retVal = OLE_ERROR_NETWORK;
  181. FreeProcInstance (lpGetTaskVisWnd);
  182. errRtn:
  183. FreeProcInstance (lpConnectDlg);
  184. return retVal;
  185. }
  186. int FAR PASCAL ConnectDlgProc(HWND hDlg, WORD wMsg, WORD wParam, DWORD lParam)
  187. {
  188. char szPassword[32];
  189. char szTitle[64];
  190. switch (wMsg) {
  191. case WM_INITDIALOG:
  192. SetProp (hDlg, szSS, HIWORD (lParam));
  193. SetProp (hDlg, szOffset, LOWORD (lParam));
  194. FillDrives (hDlg);
  195. SetDlgItemText (hDlg, IDD_PATH, (LPSTR) lParam);
  196. break;
  197. case WM_COMMAND:
  198. switch (wParam) {
  199. case IDOK:
  200. {
  201. WORD cch = 128;
  202. char szMessage[128];
  203. char szDrive[3];
  204. LPSTR lpNetName;
  205. GetDlgItemText(hDlg, IDD_DRIVE, szDrive, sizeof(szDrive));
  206. GetDlgItemText(hDlg, IDD_PASSWORD, szPassword,
  207. sizeof(szPassword));
  208. lpNetName = (LPSTR) MAKELONG(((WORD) GetProp (hDlg, szOffset)), ((WORD) GetProp (hDlg, szSS)));
  209. wParam = WNetAddConnection (lpNetName,
  210. (LPSTR) szPassword, szDrive);
  211. if (wParam == WN_SUCCESS) {
  212. RemoveProp (hDlg, szSS);
  213. RemoveProp (hDlg, szOffset);
  214. EndDialog (hDlg, szDrive[0]);
  215. return TRUE;
  216. }
  217. LoadString (hInstDLL, IDS_NETERR, szTitle,
  218. sizeof(szTitle));
  219. if (WNetGetErrorText (wParam, szMessage, &cch)
  220. != WN_SUCCESS)
  221. LoadString (hInstDLL, IDS_NETCONERRMSG,
  222. szMessage, sizeof(szMessage));
  223. if (MessageBox (hDlg, szMessage, szTitle,
  224. MB_RETRYCANCEL | MB_SYSTEMMODAL) == IDCANCEL)
  225. goto error;
  226. if (wParam == WN_ALREADY_CONNECTED)
  227. FillDrives (hDlg);
  228. SetDlgItemText (hDlg, IDD_PASSWORD, szNULL);
  229. break;
  230. }
  231. case IDCANCEL:
  232. error:
  233. RemoveProp (hDlg, szSS);
  234. RemoveProp (hDlg, szOffset);
  235. EndDialog(hDlg, NULL);
  236. return TRUE;
  237. case IDD_DRIVE:
  238. break;
  239. case IDD_PATH:
  240. if (HIWORD(lParam) == EN_KILLFOCUS) {
  241. LPSTR lpNetName;
  242. lpNetName = (LPSTR) MAKELONG(((WORD)GetProp (hDlg, szOffset)), ((WORD) GetProp (hDlg, szSS)));
  243. SendDlgItemMessage (hDlg, IDD_PATH, WM_SETTEXT, 0,
  244. (DWORD) lpNetName);
  245. }
  246. break;
  247. default:
  248. break;
  249. }
  250. break;
  251. default:
  252. break;
  253. }
  254. return FALSE;
  255. }
  256. VOID INTERNAL FillDrives (hDlg)
  257. HWND hDlg;
  258. {
  259. HWND hwndCB;
  260. int nDrive = 3;
  261. char szDrive[3];
  262. hwndCB = GetDlgItem(hDlg, IDD_DRIVE);
  263. SendMessage(hwndCB, CB_RESETCONTENT, 0, 0L);
  264. szDrive[2] = NULL;
  265. szDrive[1] = ':';
  266. while (nDrive < MAX_DRIVE) {
  267. szDrive[0] = (char) ('A' + nDrive);
  268. if (!GetDriveType (nDrive))
  269. SendMessage(hwndCB, CB_ADDSTRING, 0, (DWORD)(LPSTR)szDrive);
  270. nDrive++;
  271. }
  272. SendMessage(hwndCB, CB_SETCURSEL, 0, 0L);
  273. }
  274. BOOL FAR PASCAL GetTaskVisibleWindow (hWnd, lpTaskVisWnd)
  275. HWND hWnd;
  276. DWORD lpTaskVisWnd;
  277. {
  278. if (IsWindowVisible (hWnd)) {
  279. *(WORD FAR *) lpTaskVisWnd = hWnd;
  280. return FALSE;
  281. }
  282. return TRUE;
  283. }
  284. void INTERNAL RemoveNetName (LPOBJECT_LE lpobj)
  285. {
  286. if (lpobj->aNetName) {
  287. GlobalDeleteAtom (lpobj->aNetName);
  288. lpobj->aNetName = NULL;
  289. }
  290. lpobj->cDrive = NULL;
  291. }