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.

394 lines
10 KiB

  1. package Win32::NetResource;
  2. require Exporter;
  3. require DynaLoader;
  4. require AutoLoader;
  5. $VERSION = '0.052';
  6. @ISA = qw(Exporter DynaLoader);
  7. # Items to export into callers namespace by default. Note: do not export
  8. # names by default without a very good reason. Use EXPORT_OK instead.
  9. # Do not simply export all your public functions/methods/constants.
  10. @EXPORT = qw(
  11. RESOURCEDISPLAYTYPE_DOMAIN
  12. RESOURCEDISPLAYTYPE_FILE
  13. RESOURCEDISPLAYTYPE_GENERIC
  14. RESOURCEDISPLAYTYPE_GROUP
  15. RESOURCEDISPLAYTYPE_SERVER
  16. RESOURCEDISPLAYTYPE_SHARE
  17. RESOURCEDISPLAYTYPE_TREE
  18. RESOURCETYPE_ANY
  19. RESOURCETYPE_DISK
  20. RESOURCETYPE_PRINT
  21. RESOURCETYPE_UNKNOWN
  22. RESOURCEUSAGE_CONNECTABLE
  23. RESOURCEUSAGE_CONTAINER
  24. RESOURCEUSAGE_RESERVED
  25. RESOURCE_CONNECTED
  26. RESOURCE_GLOBALNET
  27. RESOURCE_REMEMBERED
  28. );
  29. @EXPORT_OK = qw(
  30. GetSharedResources
  31. AddConnection
  32. CancelConnection
  33. WNetGetLastError
  34. GetError
  35. GetUNCName
  36. NetShareAdd
  37. NetShareCheck
  38. NetShareDel
  39. NetShareGetInfo
  40. NetShareSetInfo
  41. );
  42. =head1 NAME
  43. Win32::NetResource - manage network resources in perl
  44. =head1 SYNOPSIS
  45. use Win32::NetResource;
  46. $ShareInfo = {
  47. 'path' => "C:\\MyShareDir",
  48. 'netname' => "MyShare",
  49. 'remark' => "It is good to share",
  50. 'passwd' => "",
  51. 'current-users' =>0,
  52. 'permissions' => 0,
  53. 'maxusers' => -1,
  54. 'type' => 0,
  55. };
  56. Win32::NetResource::NetShareAdd( $ShareInfo,$parm )
  57. or die "unable to add share";
  58. =head1 DESCRIPTION
  59. This module offers control over the network resources of Win32.Disks,
  60. printers etc can be shared over a network.
  61. =head1 DATA TYPES
  62. There are two main data types required to control network resources.
  63. In Perl these are represented by hash types.
  64. =over 10
  65. =item %NETRESOURCE
  66. KEY VALUE
  67. 'Scope' => Scope of an Enumeration
  68. RESOURCE_CONNECTED,
  69. RESOURCE_GLOBALNET,
  70. RESOURCE_REMEMBERED.
  71. 'Type' => The type of resource to Enum
  72. RESOURCETYPE_ANY All resources
  73. RESOURCETYPE_DISK Disk resources
  74. RESOURCETYPE_PRINT Print resources
  75. 'DisplayType' => The way the resource should be displayed.
  76. RESOURCEDISPLAYTYPE_DOMAIN
  77. The object should be displayed as a domain.
  78. RESOURCEDISPLAYTYPE_GENERIC
  79. The method used to display the object does not matter.
  80. RESOURCEDISPLAYTYPE_SERVER
  81. The object should be displayed as a server.
  82. RESOURCEDISPLAYTYPE_SHARE
  83. The object should be displayed as a sharepoint.
  84. 'Usage' => Specifies the Resources usage:
  85. RESOURCEUSAGE_CONNECTABLE
  86. RESOURCEUSAGE_CONTAINER.
  87. 'LocalName' => Name of the local device the resource is
  88. connected to.
  89. 'RemoteName' => The network name of the resource.
  90. 'Comment' => A string comment.
  91. 'Provider' => Name of the provider of the resource.
  92. =back
  93. =item %SHARE_INFO
  94. This hash represents the SHARE_INFO_502 struct.
  95. =over 10
  96. KEY VALUE
  97. 'netname' => Name of the share.
  98. 'type' => type of share.
  99. 'remark' => A string comment.
  100. 'permissions' => Permissions value
  101. 'maxusers' => the max # of users.
  102. 'current-users' => the current # of users.
  103. 'path' => The path of the share.
  104. 'passwd' => A password if one is req'd
  105. =back
  106. =head1 FUNCTIONS
  107. =head2 NOTE
  108. All of the functions return false if they fail.
  109. =over 10
  110. =item GetSharedResources(\@Resources,dwType,\%NetResource = NULL)
  111. Creates a list in @Resources of %NETRESOURCE hash references.
  112. The return value indicates whether there was an error in accessing
  113. any of the shared resources. All the shared resources that were
  114. encountered (until the point of an error, if any) are pushed into
  115. @Resources as references to %NETRESOURCE hashes. See example
  116. below. The \%NetResource argument is optional. If it is not supplied,
  117. the root (that is, the topmost container) of the network is assumed,
  118. and all network resources available from the toplevel container will
  119. be enumerated.
  120. =item AddConnection(\%NETRESOURCE,$Password,$UserName,$Connection)
  121. Makes a connection to a network resource specified by %NETRESOURCE
  122. =item CancelConnection($Name,$Connection,$Force)
  123. Cancels a connection to a network resource connected to local device
  124. $name.$Connection is either 1 - persistent connection or 0, non-persistent.
  125. =item WNetGetLastError($ErrorCode,$Description,$Name)
  126. Gets the Extended Network Error.
  127. =item GetError( $ErrorCode )
  128. Gets the last Error for a Win32::NetResource call.
  129. =item GetUNCName( $UNCName, $LocalPath );
  130. Returns the UNC name of the disk share connected to $LocalPath in $UNCName.
  131. =head2 NOTE
  132. $servername is optional for all the calls below. (if not given the
  133. local machine is assumed.)
  134. =item NetShareAdd(\%SHARE,$parm_err,$servername = NULL )
  135. Add a share for sharing.
  136. =item NetShareCheck($device,$type,$servername = NULL )
  137. Check if a share is available for connection.
  138. =item NetShareDel( $netname, $servername = NULL )
  139. Remove a share from a machines list of shares.
  140. =item NetShareGetInfo( $netname, \%SHARE,$servername=NULL )
  141. Get the %SHARE_INFO information about the share $netname on the
  142. server $servername.
  143. =item NetShareSetInfo( $netname,\%SHARE,$parm_err,$servername=NULL)
  144. Set the information for share $netname.
  145. =back
  146. =head1 EXAMPLE
  147. #
  148. # This example displays all the share points in the entire
  149. # visible part of the network.
  150. #
  151. use strict;
  152. use Win32::NetResource qw(:DEFAULT GetSharedResources GetError);
  153. my $resources = [];
  154. unless(GetSharedResources($resources, RESOURCETYPE_ANY)) {
  155. my $err = undef;
  156. GetError($err);
  157. warn Win32::FormatMessage($err);
  158. }
  159. foreach my $href (@$resources) {
  160. next if ($$href{DisplayType} != RESOURCEDISPLAYTYPE_SHARE);
  161. print "-----\n";
  162. foreach( keys %$href){
  163. print "$_: $href->{$_}\n";
  164. }
  165. }
  166. =head1 AUTHOR
  167. Jesse Dougherty for Hip Communications.
  168. Additional general cleanups and bug fixes by Gurusamy Sarathy <[email protected]>.
  169. =cut
  170. sub AUTOLOAD {
  171. # This AUTOLOAD is used to 'autoload' constants from the constant()
  172. # XS function. If a constant is not found then control is passed
  173. # to the AUTOLOAD in AutoLoader.
  174. my($constname);
  175. ($constname = $AUTOLOAD) =~ s/.*:://;
  176. #reset $! to zero to reset any current errors.
  177. $!=0;
  178. my $val = constant($constname, @_ ? $_[0] : 0);
  179. if ($! != 0) {
  180. if ($! =~ /Invalid/) {
  181. $AutoLoader::AUTOLOAD = $AUTOLOAD;
  182. goto &AutoLoader::AUTOLOAD;
  183. }
  184. else {
  185. ($pack,$file,$line) = caller;
  186. die "Your vendor has not defined Win32::NetResource macro $constname, used at $file line $line.
  187. ";
  188. }
  189. }
  190. eval "sub $AUTOLOAD { $val }";
  191. goto &$AUTOLOAD;
  192. }
  193. sub AddConnection
  194. {
  195. my $h = $_[0];
  196. die "AddConnection: HASH reference required" unless ref($h) eq "HASH";
  197. #
  198. # The last four items *must* not be deallocated until the
  199. # _AddConnection() completes (since the packed structure is
  200. # pointing into these values.
  201. #
  202. my $netres = pack( 'i4 p4', $h->{Scope},
  203. $h->{Type},
  204. $h->{DisplayType},
  205. $h->{Usage},
  206. $h->{LocalName},
  207. $h->{RemoteName},
  208. $h->{Comment},
  209. $h->{Provider});
  210. _AddConnection($netres,$_[1],$_[2],$_[3]);
  211. }
  212. #use Data::Dumper;
  213. sub GetSharedResources
  214. {
  215. die "GetSharedResources: ARRAY reference required"
  216. unless ref($_[0]) eq "ARRAY";
  217. my $aref = [];
  218. # Get the shared resources.
  219. my $ret;
  220. if (@_ > 2 and $_[2]) {
  221. my $netres = pack('i4 p4', @{$_[2]}{qw(Scope
  222. Type
  223. DisplayType
  224. Usage
  225. LocalName
  226. RemoteName
  227. Comment
  228. Provider)});
  229. $ret = _GetSharedResources( $aref , $_[1], $netres );
  230. }
  231. else {
  232. $ret = _GetSharedResources( $aref , $_[1] );
  233. }
  234. # build the array of hashes in $_[0]
  235. # print Dumper($aref);
  236. foreach ( @$aref ) {
  237. my %hash;
  238. @hash{'Scope',
  239. 'Type',
  240. 'DisplayType',
  241. 'Usage',
  242. 'LocalName',
  243. 'RemoteName',
  244. 'Comment',
  245. 'Provider'} = split /\001/, $_;
  246. push @{$_[0]}, \%hash;
  247. }
  248. $ret;
  249. }
  250. sub NetShareAdd
  251. {
  252. my $shareinfo = _hash2SHARE( $_[0] );
  253. _NetShareAdd($shareinfo,$_[1], $_[2] || "");
  254. }
  255. sub NetShareGetInfo
  256. {
  257. my ($netinfo,$val);
  258. $val = _NetShareGetInfo( $_[0],$netinfo,$_[2] || "");
  259. %{$_[1]} = %{_SHARE2hash( $netinfo )};
  260. $val;
  261. }
  262. sub NetShareSetInfo
  263. {
  264. my $shareinfo = _hash2SHARE( $_[1] );
  265. _NetShareSetInfo( $_[0],$shareinfo,$_[2],$_[3] || "");
  266. }
  267. # These are private functions to work with the ShareInfo structure.
  268. # please note that the implementation of these calls requires the
  269. # SHARE_INFO_502 level of information.
  270. sub _SHARE2hash
  271. {
  272. my %hash = ();
  273. @hash{'type',
  274. 'permissions',
  275. 'maxusers',
  276. 'current-users',
  277. 'remark',
  278. 'netname',
  279. 'path',
  280. 'passwd'} = unpack('i4 A257 A81 A257 A257',$_[0]);
  281. return \%hash;
  282. }
  283. sub _hash2SHARE
  284. {
  285. my $h = $_[0];
  286. die "Argument must be a HASH reference" unless ref($h) eq "HASH";
  287. return pack 'i4 a257 a81 a257 a257',
  288. @$h{'type',
  289. 'permissions',
  290. 'maxusers',
  291. 'current-users',
  292. 'remark',
  293. 'netname',
  294. 'path',
  295. 'passwd'};
  296. }
  297. bootstrap Win32::NetResource;
  298. 1;
  299. __END__