summaryrefslogtreecommitdiff
path: root/lib/chef/win32/api/net.rb
blob: fdbe55579f739b1cdf88452db0b885eb6ff31c09 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#
# Author:: Serdar Sutay (<serdar@chef.io>)
# Copyright:: Copyright 2014-2016, Chef Software, Inc.
# License:: Apache License, Version 2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

require "chef/win32/api"
require "chef/win32/unicode"

class Chef
  module ReservedNames::Win32
    module API
      module Net
        extend Chef::ReservedNames::Win32::API

        FILTER_TEMP_DUPLICATE_ACCOUNT       = 0x0001
        FILTER_NORMAL_ACCOUNT               = 0x0002
        FILTER_INTERDOMAIN_TRUST_ACCOUNT    = 0x0008
        FILTER_WORKSTATION_TRUST_ACCOUNT    = 0x0010
        FILTER_SERVER_TRUST_ACCOUNT         = 0x0020

        MAX_PREFERRED_LENGTH                = 0xFFFF

        DOMAIN_GROUP_RID_USERS = 0x00000201

        UF_SCRIPT              = 0x000001
        UF_ACCOUNTDISABLE      = 0x000002
        UF_PASSWD_CANT_CHANGE  = 0x000040
        UF_NORMAL_ACCOUNT      = 0x000200
        UF_DONT_EXPIRE_PASSWD  = 0x010000

        USE_NOFORCE = 0
        USE_FORCE = 1
        USE_LOTS_OF_FORCE = 2 #every windows API should support this flag

        NERR_Success = 0
        ERROR_MORE_DATA = 234

        ffi_lib "netapi32"

        module StructHelpers
          def set(key, val)
            val = if val.is_a? String
                    encoded = if val.encoding == Encoding::UTF_16LE
                                val
                              else
                                val.to_wstring
                              end
                    FFI::MemoryPointer.from_string(encoded)
                  else
                    val
                  end
            self[key] = val
          end

          def get(key)
            if respond_to? key
              send(key)
            else
              val = self[key]
              if val.is_a? FFI::Pointer
                if val.null?
                  nil
                else
                  val.read_wstring
                end
              else
                val
              end
            end
          end

          def as_ruby
            members.inject({}) do |memo, key|
              memo[key] = get(key)
              memo
            end
          end
        end


        class USER_INFO_3 < FFI::Struct
          include StructHelpers
          layout :usri3_name, :LPWSTR,
            :usri3_password, :LPWSTR,
            :usri3_password_age, :DWORD,
            :usri3_priv, :DWORD,
            :usri3_home_dir, :LPWSTR,
            :usri3_comment, :LPWSTR,
            :usri3_flags, :DWORD,
            :usri3_script_path, :LPWSTR,
            :usri3_auth_flags, :DWORD,
            :usri3_full_name, :LPWSTR,
            :usri3_usr_comment, :LPWSTR,
            :usri3_parms, :LPWSTR,
            :usri3_workstations, :LPWSTR,
            :usri3_last_logon, :DWORD,
            :usri3_last_logoff, :DWORD,
            :usri3_acct_expires, :DWORD,
            :usri3_max_storage, :DWORD,
            :usri3_units_per_week, :DWORD,
            :usri3_logon_hours, :PBYTE,
            :usri3_bad_pw_count, :DWORD,
            :usri3_num_logons, :DWORD,
            :usri3_logon_server, :LPWSTR,
            :usri3_country_code, :DWORD,
            :usri3_code_page, :DWORD,
            :usri3_user_id, :DWORD,
            :usri3_primary_group_id, :DWORD,
            :usri3_profile, :LPWSTR,
            :usri3_home_dir_drive, :LPWSTR,
            :usri3_password_expired, :DWORD

          def usri3_logon_hours
            val = self[:usri3_logon_hours]
            if !val.nil? && !val.null?
              val.read_bytes(21)
            else
              nil
            end
          end
        end

        class LOCALGROUP_MEMBERS_INFO_0 < FFI::Struct
          layout :lgrmi0_sid, :PSID
        end

        class LOCALGROUP_MEMBERS_INFO_3 < FFI::Struct
          layout :lgrmi3_domainandname, :LPWSTR
        end

        class LOCALGROUP_INFO_0 < FFI::Struct
          layout :lgrpi0_name, :LPWSTR
        end

        class USE_INFO_2 < FFI::Struct
          include StructHelpers

          layout :ui2_local, :LMSTR,
            :ui2_remote, :LMSTR,
            :ui2_password, :LMSTR,
            :ui2_status, :DWORD,
            :ui2_asg_type, :DWORD,
            :ui2_refcount, :DWORD,
            :ui2_usecount, :DWORD,
            :ui2_username, :LPWSTR,
            :ui2_domainname, :LMSTR
        end


        #NET_API_STATUS NetLocalGroupAdd(
        #_In_  LPCWSTR servername,
        #_In_  DWORD   level,
        #_In_  LPBYTE  buf,
        #_Out_ LPDWORD parm_err
        #);
        safe_attach_function :NetLocalGroupAdd, [
          :LPCWSTR, :DWORD, :LPBYTE, :LPDWORD
        ], :DWORD

        #NET_API_STATUS NetLocalGroupDel(
        #_In_ LPCWSTR servername,
        #_In_ LPCWSTR groupname
        #);
        safe_attach_function :NetLocalGroupDel, [:LPCWSTR, :LPCWSTR], :DWORD

        #NET_API_STATUS NetLocalGroupGetMembers(
        #_In_    LPCWSTR    servername,
        #_In_    LPCWSTR    localgroupname,
        #_In_    DWORD      level,
        #_Out_   LPBYTE     *bufptr,
        #_In_    DWORD      prefmaxlen,
        #_Out_   LPDWORD    entriesread,
        #_Out_   LPDWORD    totalentries,
        #_Inout_ PDWORD_PTR resumehandle
        #);
        safe_attach_function :NetLocalGroupGetMembers, [
          :LPCWSTR, :LPCWSTR, :DWORD, :LPBYTE, :DWORD,
          :LPDWORD, :LPDWORD, :PDWORD_PTR
        ], :DWORD

        # NET_API_STATUS NetUserEnum(
        #   _In_     LPCWSTR servername,
        #   _In_     DWORD level,
        #   _In_     DWORD filter,
        #   _Out_    LPBYTE *bufptr,
        #   _In_     DWORD prefmaxlen,
        #   _Out_    LPDWORD entriesread,
        #   _Out_    LPDWORD totalentries,
        #   _Inout_  LPDWORD resume_handle
        # );
        safe_attach_function :NetUserEnum, [
          :LPCWSTR, :DWORD, :DWORD, :LPBYTE,
          :DWORD, :LPDWORD, :LPDWORD, :LPDWORD
        ], :DWORD

        # NET_API_STATUS NetApiBufferFree(
        #   _In_  LPVOID Buffer
        # );
        safe_attach_function :NetApiBufferFree, [:LPVOID], :DWORD

        #NET_API_STATUS NetUserAdd(
        #_In_  LMSTR   servername,
        #_In_  DWORD   level,
        #_In_  LPBYTE  buf,
        #_Out_ LPDWORD parm_err
        #);
        safe_attach_function :NetUserAdd, [
          :LMSTR, :DWORD, :LPBYTE, :LPDWORD
        ], :DWORD

        #NET_API_STATUS NetLocalGroupAddMembers(
        #  _In_ LPCWSTR servername,
        #  _In_ LPCWSTR groupname,
        #  _In_ DWORD   level,
        #  _In_ LPBYTE  buf,
        #  _In_ DWORD   totalentries
        #);
        safe_attach_function :NetLocalGroupAddMembers, [
          :LPCWSTR, :LPCWSTR, :DWORD, :LPBYTE, :DWORD
        ], :DWORD

        #NET_API_STATUS NetLocalGroupSetMembers(
        #  _In_ LPCWSTR servername,
        #  _In_ LPCWSTR groupname,
        #  _In_ DWORD   level,
        #  _In_ LPBYTE  buf,
        #  _In_ DWORD   totalentries
        #);
        safe_attach_function :NetLocalGroupSetMembers, [
          :LPCWSTR, :LPCWSTR, :DWORD, :LPBYTE, :DWORD
        ], :DWORD

        #NET_API_STATUS NetLocalGroupDelMembers(
        #  _In_ LPCWSTR servername,
        #  _In_ LPCWSTR groupname,
        #  _In_ DWORD   level,
        #  _In_ LPBYTE  buf,
        #  _In_ DWORD   totalentries
        #);
        safe_attach_function :NetLocalGroupDelMembers, [
          :LPCWSTR, :LPCWSTR, :DWORD, :LPBYTE, :DWORD
        ], :DWORD

        #NET_API_STATUS NetUserGetInfo(
        #  _In_  LPCWSTR servername,
        #  _In_  LPCWSTR username,
        #  _In_  DWORD   level,
        #  _Out_ LPBYTE  *bufptr
        #);
        safe_attach_function :NetUserGetInfo, [
          :LPCWSTR, :LPCWSTR, :DWORD, :LPBYTE
        ], :DWORD

        #NET_API_STATUS NetApiBufferFree(
        #  _In_ LPVOID Buffer
        #);
        safe_attach_function :NetApiBufferFree, [:LPVOID], :DWORD

        #NET_API_STATUS NetUserSetInfo(
        #  _In_  LPCWSTR servername,
        #  _In_  LPCWSTR username,
        #  _In_  DWORD   level,
        #  _In_  LPBYTE  buf,
        #  _Out_ LPDWORD parm_err
        #);
        safe_attach_function :NetUserSetInfo, [
          :LPCWSTR, :LPCWSTR, :DWORD, :LPBYTE, :LPDWORD
        ], :DWORD

        #NET_API_STATUS NetUserDel(
        #  _In_ LPCWSTR servername,
        #  _In_ LPCWSTR username
        #);
        safe_attach_function :NetUserDel, [:LPCWSTR, :LPCWSTR], :DWORD

        #NET_API_STATUS NetUseDel(
        #_In_ LMSTR UncServerName,
        #_In_ LMSTR UseName,
        #_In_ DWORD ForceCond
        #);
        safe_attach_function :NetUseDel, [:LMSTR, :LMSTR, :DWORD], :DWORD

        #NET_API_STATUS NetUseGetInfo(
        #_In_  LMSTR  UncServerName,
        #_In_  LMSTR  UseName,
        #_In_  DWORD  Level,
        #_Out_ LPBYTE *BufPtr
        #);
        safe_attach_function :NetUseGetInfo, [:LMSTR, :LMSTR, :DWORD, :pointer], :DWORD

        #NET_API_STATUS NetUseAdd(
        #_In_  LMSTR   UncServerName,
        #_In_  DWORD   Level,
        #_In_  LPBYTE  Buf,
        #_Out_ LPDWORD ParmError
        #);
        safe_attach_function :NetUseAdd, [:LMSTR, :DWORD, :LPBYTE, :LPDWORD], :DWORD
      end
    end
  end
end