1 /* setlsapwd.cc: Set LSA private data password for current user.
3 This file is part of Cygwin.
5 This software is a copyrighted work licensed under the terms of the
6 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
10 #include "shared_info.h"
17 #include "cygserver_setpwd.h"
24 * client_request_setpwd Constructor
27 client_request_setpwd::client_request_setpwd (PUNICODE_STRING passwd
)
28 : client_request (CYGSERVER_REQUEST_SETPWD
, &_parameters
, sizeof (_parameters
))
30 memset (_parameters
.in
.passwd
, 0, sizeof _parameters
.in
.passwd
);
31 if (passwd
->Length
> 0 && passwd
->Length
< 256 * sizeof (WCHAR
))
32 wcpncpy (_parameters
.in
.passwd
, passwd
->Buffer
, 255);
34 msglen (sizeof (_parameters
.in
));
38 setlsapwd (const char *passwd
, const char *username
)
40 unsigned long ret
= (unsigned long) -1;
43 WCHAR key_name
[128 + wcslen (CYGWIN_LSA_KEY_PREFIX
)];
44 PWCHAR data_buf
= NULL
;
51 struct passwd
*pw
= internal_getpwnam (username
);
53 if (!pw
|| !psid
.getfrompw (pw
))
58 wcpcpy (wcpcpy (key_name
, CYGWIN_LSA_KEY_PREFIX
), psid
.string (sid
));
61 wcpcpy (wcpcpy (key_name
, CYGWIN_LSA_KEY_PREFIX
),
62 cygheap
->user
.get_windows_id (sid
));
63 RtlInitUnicodeString (&key
, key_name
);
64 if (!passwd
|| ! *passwd
65 || sys_mbstowcs_alloc (&data_buf
, HEAP_NOTHEAP
, passwd
))
67 memset (&data
, 0, sizeof data
);
69 RtlInitUnicodeString (&data
, data_buf
);
70 /* First try it locally. Works for admin accounts. */
71 if ((lsa
= lsa_open_policy (NULL
, POLICY_CREATE_SECRET
)))
73 NTSTATUS status
= LsaStorePrivateData (lsa
, &key
,
74 data
.Length
? &data
: NULL
);
75 /* Success or we're trying to remove a password entry which doesn't
77 if (NT_SUCCESS (status
)
78 || (data
.Length
== 0 && status
== STATUS_OBJECT_NAME_NOT_FOUND
))
81 __seterrno_from_nt_status (status
);
82 lsa_close_policy (lsa
);
84 else if (ret
&& !username
)
86 client_request_setpwd
request (&data
);
87 if (request
.make_request () == -1 || request
.error_code ())
88 set_errno (request
.error_code ());
94 RtlSecureZeroMemory (data
.Buffer
, data
.Length
);