util.odin 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #+build windows
  2. package sys_windows
  3. import "base:runtime"
  4. import "base:intrinsics"
  5. L :: intrinsics.constant_utf16_cstring
  6. // https://learn.microsoft.com/en-us/windows/win32/winmsg/makeword
  7. MAKEWORD :: #force_inline proc "contextless" (#any_int a, b: int) -> WORD {
  8. return WORD(BYTE(DWORD_PTR(a) & 0xff)) | (WORD(BYTE(DWORD_PTR(b) & 0xff)) << 8)
  9. }
  10. // https://learn.microsoft.com/en-us/windows/win32/winmsg/makelong
  11. MAKELONG :: #force_inline proc "contextless" (#any_int a, b: int) -> LONG {
  12. return LONG(WORD(DWORD_PTR(a) & 0xffff)) | (LONG(WORD(DWORD_PTR(b) & 0xffff)) << 16)
  13. }
  14. // https://learn.microsoft.com/en-us/windows/win32/winmsg/loword
  15. LOWORD :: #force_inline proc "contextless" (#any_int x: int) -> WORD {
  16. return WORD(x & 0xffff)
  17. }
  18. // https://learn.microsoft.com/en-us/windows/win32/winmsg/hiword
  19. HIWORD :: #force_inline proc "contextless" (#any_int x: int) -> WORD {
  20. return WORD(x >> 16)
  21. }
  22. // https://learn.microsoft.com/en-us/windows/win32/winmsg/lobyte
  23. LOBYTE :: #force_inline proc "contextless" (w: WORD) -> BYTE {
  24. return BYTE((DWORD_PTR(w)) & 0xff)
  25. }
  26. // https://learn.microsoft.com/en-us/windows/win32/winmsg/hibyte
  27. HIBYTE :: #force_inline proc "contextless" (w: WORD) -> BYTE {
  28. return BYTE(((DWORD_PTR(w)) >> 8) & 0xff)
  29. }
  30. // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-makewparam
  31. MAKEWPARAM :: #force_inline proc "contextless" (#any_int l, h: int) -> WPARAM {
  32. return WPARAM(MAKELONG(l, h))
  33. }
  34. // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-makelparam
  35. MAKELPARAM :: #force_inline proc "contextless" (#any_int l, h: int) -> LPARAM {
  36. return LPARAM(MAKELONG(l, h))
  37. }
  38. // https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-makelresult
  39. MAKELRESULT :: #force_inline proc "contextless" (#any_int l, h: int) -> LRESULT {
  40. return LRESULT(MAKELONG(l, h))
  41. }
  42. // https://learn.microsoft.com/en-us/windows/win32/api/windowsx/nf-windowsx-get_x_lparam
  43. GET_X_LPARAM :: #force_inline proc "contextless" (lp: LPARAM) -> c_int {
  44. return cast(c_int)cast(c_short)LOWORD(cast(DWORD)lp)
  45. }
  46. // https://learn.microsoft.com/en-us/windows/win32/api/windowsx/nf-windowsx-get_y_lparam
  47. GET_Y_LPARAM :: #force_inline proc "contextless" (lp: LPARAM) -> c_int {
  48. return cast(c_int)cast(c_short)HIWORD(cast(DWORD)lp)
  49. }
  50. // https://learn.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-makelcid
  51. MAKELCID :: #force_inline proc "contextless" (lgid, srtid: WORD) -> LCID {
  52. return (DWORD(WORD(srtid)) << 16) | DWORD(WORD(lgid))
  53. }
  54. // https://learn.microsoft.com/en-us/windows/win32/api/winnt/nf-winnt-makelangid
  55. MAKELANGID :: #force_inline proc "contextless" (p, s: WORD) -> DWORD {
  56. return DWORD(WORD(s)) << 10 | DWORD(WORD(p))
  57. }
  58. LANGIDFROMLCID :: #force_inline proc "contextless" (lcid: LCID) -> LANGID {
  59. return LANGID(lcid)
  60. }
  61. utf8_to_utf16_alloc :: proc(s: string, allocator := context.temp_allocator) -> []u16 {
  62. if len(s) < 1 {
  63. return nil
  64. }
  65. b := transmute([]byte)s
  66. cstr := raw_data(b)
  67. n := MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, cstr, i32(len(s)), nil, 0)
  68. if n == 0 {
  69. return nil
  70. }
  71. text := make([]u16, n+1, allocator)
  72. n1 := MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, cstr, i32(len(s)), raw_data(text), n)
  73. if n1 == 0 {
  74. delete(text, allocator)
  75. return nil
  76. }
  77. text[n] = 0
  78. for n >= 1 && text[n-1] == 0 {
  79. n -= 1
  80. }
  81. return text[:n]
  82. }
  83. utf8_to_utf16_buf :: proc(buf: []u16, s: string) -> []u16 {
  84. n1 := MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, raw_data(s), i32(len(s)), nil, 0)
  85. if n1 == 0 {
  86. return nil
  87. } else if int(n1) > len(buf) {
  88. return nil
  89. }
  90. n1 = MultiByteToWideChar(CP_UTF8, MB_ERR_INVALID_CHARS, raw_data(s), i32(len(s)), raw_data(buf[:]), n1)
  91. if n1 == 0 {
  92. return nil
  93. } else if int(n1) > len(buf) {
  94. return nil
  95. }
  96. return buf[:n1]
  97. }
  98. utf8_to_utf16 :: proc{utf8_to_utf16_alloc, utf8_to_utf16_buf}
  99. utf8_to_wstring_alloc :: proc(s: string, allocator := context.temp_allocator) -> wstring {
  100. if res := utf8_to_utf16(s, allocator); len(res) > 0 {
  101. return wstring(raw_data(res))
  102. }
  103. return nil
  104. }
  105. utf8_to_wstring_buf :: proc(buf: []u16, s: string) -> wstring {
  106. if res := utf8_to_utf16(buf, s); len(res) > 0 {
  107. return wstring(raw_data(res))
  108. }
  109. return nil
  110. }
  111. utf8_to_wstring :: proc{utf8_to_wstring_alloc, utf8_to_wstring_buf}
  112. wstring_to_utf8_alloc :: proc(s: wstring, N: int, allocator := context.temp_allocator) -> (res: string, err: runtime.Allocator_Error) {
  113. context.allocator = allocator
  114. if N == 0 {
  115. return
  116. }
  117. n := WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, s, i32(N) if N > 0 else -1, nil, 0, nil, nil)
  118. if n == 0 {
  119. return
  120. }
  121. // If N < 0 the call to WideCharToMultiByte assume the wide string is null terminated
  122. // and will scan it to find the first null terminated character. The resulting string will
  123. // also be null terminated.
  124. // If N > 0 it assumes the wide string is not null terminated and the resulting string
  125. // will not be null terminated.
  126. text := make([]byte, n) or_return
  127. n1 := WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, s, i32(N), raw_data(text), n, nil, nil)
  128. if n1 == 0 {
  129. delete(text, allocator)
  130. return
  131. }
  132. for i in 0..<n {
  133. if text[i] == 0 {
  134. n = i
  135. break
  136. }
  137. }
  138. return string(text[:n]), nil
  139. }
  140. wstring_to_utf8_buf :: proc(buf: []u8, s: wstring, N := -1) -> (res: string) {
  141. n := WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, s, i32(N), nil, 0, nil, nil)
  142. if n == 0 {
  143. return
  144. } else if int(n) > len(buf) {
  145. return
  146. }
  147. n2 := WideCharToMultiByte(CP_UTF8, WC_ERR_INVALID_CHARS, s, i32(N), raw_data(buf), n, nil, nil)
  148. if n2 == 0 {
  149. return
  150. } else if int(n2) > len(buf) {
  151. return
  152. }
  153. for i in 0..<n2 {
  154. if buf[i] == 0 {
  155. n2 = i
  156. break
  157. }
  158. }
  159. return string(buf[:n2])
  160. }
  161. wstring_to_utf8 :: proc{wstring_to_utf8_alloc, wstring_to_utf8_buf}
  162. /*
  163. Converts a UTF-16 string into a regular UTF-8 `string` and allocates the result.
  164. If the input is null-terminated, only the part of the input string leading up
  165. to it will be converted.
  166. *Allocates Using Provided Allocator*
  167. Inputs:
  168. - s: The string to be converted
  169. - allocator: (default: context.allocator)
  170. Returns:
  171. - res: A cloned and converted string
  172. - err: An optional allocator error if one occured, `nil` otherwise
  173. */
  174. utf16_to_utf8_alloc :: proc(s: []u16, allocator := context.temp_allocator) -> (res: string, err: runtime.Allocator_Error) {
  175. if len(s) == 0 {
  176. return "", nil
  177. }
  178. return wstring_to_utf8(wstring(raw_data(s)), len(s), allocator)
  179. }
  180. /*
  181. Converts a UTF-16 string into a regular UTF-8 `string`, using `buf` as its backing.
  182. If the input is null-terminated, only the part of the input string leading up
  183. to it will be converted.
  184. *Uses `buf` for backing*
  185. Inputs:
  186. - s: The string to be converted
  187. - buf: Backing buffer for result string
  188. Returns:
  189. - res: A converted string, backed byu `buf`
  190. */
  191. utf16_to_utf8_buf :: proc(buf: []u8, s: []u16) -> (res: string) {
  192. if len(s) == 0 {
  193. return
  194. }
  195. return wstring_to_utf8(buf, wstring(raw_data(s)), len(s))
  196. }
  197. utf16_to_utf8 :: proc{utf16_to_utf8_alloc, utf16_to_utf8_buf}
  198. // AdvAPI32, NetAPI32 and UserENV helpers.
  199. allowed_username :: proc(username: string) -> bool {
  200. contains_any :: proc(s, chars: string) -> bool {
  201. if chars == "" {
  202. return false
  203. }
  204. for c in transmute([]byte)s {
  205. for b in transmute([]byte)chars {
  206. if c == b {
  207. return true
  208. }
  209. }
  210. }
  211. return false
  212. }
  213. /*
  214. User account names are limited to 20 characters and group names are limited to 256 characters.
  215. In addition, account names cannot be terminated by a period and they cannot include commas or any of the following printable characters:
  216. ", /, , [, ], :, |, <, >, +, =, ;, ?, *. Names also cannot include characters in the range 1-31, which are nonprintable.
  217. */
  218. _DISALLOWED :: "\"/ []:|<>+=;?*,"
  219. if len(username) > LM20_UNLEN || len(username) == 0 {
  220. return false
  221. }
  222. if username[len(username)-1] == '.' {
  223. return false
  224. }
  225. for r in username {
  226. if r > 0 && r < 32 {
  227. return false
  228. }
  229. }
  230. if contains_any(username, _DISALLOWED) {
  231. return false
  232. }
  233. return true
  234. }
  235. // Returns .Success on success.
  236. _add_user :: proc(servername: string, username: string, password: string) -> (ok: NET_API_STATUS) {
  237. servername_w: wstring
  238. username_w: []u16
  239. password_w: []u16
  240. if len(servername) == 0 {
  241. // Create account on this computer
  242. servername_w = nil
  243. } else {
  244. server := utf8_to_utf16(servername, context.temp_allocator)
  245. servername_w = wstring(&server[0])
  246. }
  247. if len(username) == 0 || len(username) > LM20_UNLEN {
  248. return .BadUsername
  249. }
  250. if !allowed_username(username) {
  251. return .BadUsername
  252. }
  253. if len(password) == 0 || len(password) > LM20_PWLEN {
  254. return .BadPassword
  255. }
  256. username_w = utf8_to_utf16(username, context.temp_allocator)
  257. password_w = utf8_to_utf16(password, context.temp_allocator)
  258. level := DWORD(1)
  259. parm_err: DWORD
  260. user_info := USER_INFO_1{
  261. name = &username_w[0],
  262. password = &password_w[0], // Max password length is defined in LM20_PWLEN.
  263. password_age = 0, // Ignored
  264. priv = .User,
  265. home_dir = nil, // We'll set it later
  266. comment = nil,
  267. flags = {.Script, .Normal_Account},
  268. script_path = nil,
  269. }
  270. ok = NetUserAdd(
  271. servername_w,
  272. level,
  273. &user_info,
  274. &parm_err,
  275. )
  276. return
  277. }
  278. get_computer_name_and_account_sid :: proc(username: string) -> (computer_name: string, sid := SID{}, ok: bool) {
  279. username_w := utf8_to_utf16(username, context.temp_allocator)
  280. cbsid: DWORD
  281. computer_name_size: DWORD
  282. pe_use := SID_NAME_USE.SidTypeUser
  283. res := LookupAccountNameW(
  284. nil, // Look on this computer first
  285. wstring(&username_w[0]),
  286. &sid,
  287. &cbsid,
  288. nil,
  289. &computer_name_size,
  290. &pe_use,
  291. )
  292. if computer_name_size == 0 {
  293. // User didn't exist, or we'd have a size here.
  294. return "", {}, false
  295. }
  296. cname_w := make([]u16, min(computer_name_size, 1), context.temp_allocator)
  297. res = LookupAccountNameW(
  298. nil,
  299. wstring(&username_w[0]),
  300. &sid,
  301. &cbsid,
  302. wstring(&cname_w[0]),
  303. &computer_name_size,
  304. &pe_use,
  305. )
  306. if !res {
  307. return "", {}, false
  308. }
  309. computer_name = utf16_to_utf8(cname_w, context.temp_allocator) or_else ""
  310. ok = true
  311. return
  312. }
  313. get_sid :: proc(username: string, sid: ^SID) -> (ok: bool) {
  314. username_w := utf8_to_utf16(username, context.temp_allocator)
  315. cbsid: DWORD
  316. computer_name_size: DWORD
  317. pe_use := SID_NAME_USE.SidTypeUser
  318. res := LookupAccountNameW(
  319. nil, // Look on this computer first
  320. wstring(&username_w[0]),
  321. sid,
  322. &cbsid,
  323. nil,
  324. &computer_name_size,
  325. &pe_use,
  326. )
  327. if computer_name_size == 0 {
  328. // User didn't exist, or we'd have a size here.
  329. return false
  330. }
  331. cname_w := make([]u16, min(computer_name_size, 1), context.temp_allocator)
  332. res = LookupAccountNameW(
  333. nil,
  334. wstring(&username_w[0]),
  335. sid,
  336. &cbsid,
  337. wstring(&cname_w[0]),
  338. &computer_name_size,
  339. &pe_use,
  340. )
  341. if !res {
  342. return false
  343. }
  344. ok = true
  345. return
  346. }
  347. add_user_to_group :: proc(sid: ^SID, group: string) -> (ok: NET_API_STATUS) {
  348. group_member := LOCALGROUP_MEMBERS_INFO_0{
  349. sid = sid,
  350. }
  351. group_name := utf8_to_utf16(group, context.temp_allocator)
  352. ok = NetLocalGroupAddMembers(
  353. nil,
  354. wstring(&group_name[0]),
  355. 0,
  356. &group_member,
  357. 1,
  358. )
  359. return
  360. }
  361. add_del_from_group :: proc(sid: ^SID, group: string) -> (ok: NET_API_STATUS) {
  362. group_member := LOCALGROUP_MEMBERS_INFO_0{
  363. sid = sid,
  364. }
  365. group_name := utf8_to_utf16(group, context.temp_allocator)
  366. ok = NetLocalGroupDelMembers(
  367. nil,
  368. cstring16(&group_name[0]),
  369. 0,
  370. &group_member,
  371. 1,
  372. )
  373. return
  374. }
  375. add_user_profile :: proc(username: string) -> (ok: bool, profile_path: string) {
  376. username_w := utf8_to_utf16(username, context.temp_allocator)
  377. sid := SID{}
  378. ok = get_sid(username, &sid)
  379. if ok == false {
  380. return false, ""
  381. }
  382. sb: wstring
  383. res := ConvertSidToStringSidW(&sid, &sb)
  384. if res == false {
  385. return false, ""
  386. }
  387. defer LocalFree(rawptr(sb))
  388. pszProfilePath := make([]u16, 257, context.temp_allocator)
  389. res2 := CreateProfile(
  390. sb,
  391. cstring16(&username_w[0]),
  392. cstring16(&pszProfilePath[0]),
  393. 257,
  394. )
  395. if res2 != 0 {
  396. return false, ""
  397. }
  398. profile_path = wstring_to_utf8(wstring(&pszProfilePath[0]), 257) or_else ""
  399. return true, profile_path
  400. }
  401. delete_user_profile :: proc(username: string) -> (ok: bool) {
  402. sid := SID{}
  403. ok = get_sid(username, &sid)
  404. if ok == false {
  405. return false
  406. }
  407. sb: wstring
  408. res := ConvertSidToStringSidW(&sid, &sb)
  409. if res == false {
  410. return false
  411. }
  412. defer LocalFree(rawptr(sb))
  413. res2 := DeleteProfileW(
  414. sb,
  415. nil,
  416. nil,
  417. )
  418. return bool(res2)
  419. }
  420. add_user :: proc(servername: string, username: string, password: string) -> (ok: bool) {
  421. /*
  422. Convenience function that creates a new user, adds it to the group Users and creates a profile directory for it.
  423. Requires elevated privileges (run as administrator).
  424. TODO: Add a bool that governs whether to delete the user if adding to group and/or creating profile fail?
  425. TODO: SecureZeroMemory the password after use.
  426. */
  427. res := _add_user(servername, username, password)
  428. if res != .Success {
  429. return false
  430. }
  431. // Grab the SID to add the user to the Users group.
  432. sid: SID
  433. ok2 := get_sid(username, &sid)
  434. if ok2 == false {
  435. return false
  436. }
  437. ok3 := add_user_to_group(&sid, "Users")
  438. if ok3 != .Success {
  439. return false
  440. }
  441. return true
  442. }
  443. delete_user :: proc(servername: string, username: string) -> (ok: bool) {
  444. /*
  445. Convenience function that deletes a user.
  446. Requires elevated privileges (run as administrator).
  447. TODO: Add a bool that governs whether to delete the profile from this wrapper?
  448. */
  449. servername_w: wstring
  450. if len(servername) == 0 {
  451. // Delete account on this computer
  452. servername_w = nil
  453. } else {
  454. server := utf8_to_utf16(servername, context.temp_allocator)
  455. servername_w = wstring(&server[0])
  456. }
  457. username_w := utf8_to_utf16(username)
  458. res := NetUserDel(
  459. servername_w,
  460. wstring(&username_w[0]),
  461. )
  462. if res != .Success {
  463. return false
  464. }
  465. return true
  466. }
  467. run_as_user :: proc(username, password, application, commandline: string, pi: ^PROCESS_INFORMATION, wait := true) -> (ok: bool) {
  468. /*
  469. Needs to be run as an account which has the "Replace a process level token" privilege.
  470. This can be added to an account from: Control Panel -> Administrative Tools -> Local Security Policy.
  471. The path to this policy is as follows: Local Policies -> User Rights Assignment -> Replace a process level token.
  472. A reboot may be required for this change to take effect and impersonating a user to work.
  473. TODO: SecureZeroMemory the password after use.
  474. */
  475. username_w := utf8_to_utf16(username)
  476. domain_w := utf8_to_utf16(".")
  477. password_w := utf8_to_utf16(password)
  478. app_w := utf8_to_utf16(application)
  479. commandline_w: []u16 = {0}
  480. if len(commandline) > 0 {
  481. commandline_w = utf8_to_utf16(commandline)
  482. }
  483. user_token: HANDLE
  484. ok = bool(LogonUserW(
  485. lpszUsername = wstring(&username_w[0]),
  486. lpszDomain = wstring(&domain_w[0]),
  487. lpszPassword = wstring(&password_w[0]),
  488. dwLogonType = .NEW_CREDENTIALS,
  489. dwLogonProvider = .WINNT50,
  490. phToken = &user_token,
  491. ))
  492. if !ok {
  493. return false
  494. // err := GetLastError();
  495. // fmt.printf("GetLastError: %v\n", err);
  496. }
  497. si := STARTUPINFOW{}
  498. si.cb = size_of(STARTUPINFOW)
  499. pi := pi
  500. ok = bool(CreateProcessAsUserW(
  501. user_token,
  502. wstring(&app_w[0]),
  503. wstring(&commandline_w[0]),
  504. nil, // lpProcessAttributes,
  505. nil, // lpThreadAttributes,
  506. false, // bInheritHandles,
  507. 0, // creation flags
  508. nil, // environment,
  509. nil, // current directory: inherit from parent if nil
  510. &si,
  511. pi,
  512. ))
  513. if ok {
  514. if wait {
  515. WaitForSingleObject(pi.hProcess, INFINITE)
  516. CloseHandle(pi.hProcess)
  517. CloseHandle(pi.hThread)
  518. }
  519. return true
  520. } else {
  521. return false
  522. }
  523. }
  524. ensure_winsock_initialized :: proc "contextless" () {
  525. @static gate := false
  526. @static initted := false
  527. if initted {
  528. return
  529. }
  530. for intrinsics.atomic_compare_exchange_strong(&gate, false, true) {
  531. intrinsics.cpu_relax()
  532. }
  533. defer intrinsics.atomic_store(&gate, false)
  534. unused_info: WSADATA
  535. version_requested := WORD(2) << 8 | 2
  536. res := WSAStartup(version_requested, &unused_info)
  537. assert_contextless(res == 0, "unable to initialized Winsock2")
  538. initted = true
  539. }