2008-11-26 11:18:10 +01:00
|
|
|
/* setlsapwd.cc: Set LSA private data password for current user.
|
|
|
|
|
2014-01-23 18:02:30 +01:00
|
|
|
Copyright 2008, 2009, 2011, 2014 Red Hat, Inc.
|
2008-11-26 11:18:10 +01:00
|
|
|
|
|
|
|
This file is part of Cygwin.
|
|
|
|
|
|
|
|
This software is a copyrighted work licensed under the terms of the
|
|
|
|
Cygwin license. Please consult the file "CYGWIN_LICENSE" for
|
|
|
|
details. */
|
|
|
|
|
|
|
|
#include "winsup.h"
|
|
|
|
#include "shared_info.h"
|
|
|
|
#include "cygerrno.h"
|
|
|
|
#include "path.h"
|
|
|
|
#include "fhandler.h"
|
|
|
|
#include "dtable.h"
|
|
|
|
#include "cygheap.h"
|
|
|
|
#include "security.h"
|
|
|
|
#include "cygserver_setpwd.h"
|
|
|
|
#include "ntdll.h"
|
|
|
|
#include <ntsecapi.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* client_request_setpwd Constructor
|
|
|
|
*/
|
|
|
|
|
|
|
|
client_request_setpwd::client_request_setpwd (PUNICODE_STRING passwd)
|
|
|
|
: client_request (CYGSERVER_REQUEST_SETPWD, &_parameters, sizeof (_parameters))
|
|
|
|
{
|
|
|
|
memset (_parameters.in.passwd, 0, sizeof _parameters.in.passwd);
|
|
|
|
if (passwd->Length > 0 && passwd->Length < 256 * sizeof (WCHAR))
|
|
|
|
wcpncpy (_parameters.in.passwd, passwd->Buffer, 255);
|
|
|
|
|
|
|
|
msglen (sizeof (_parameters.in));
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
2011-04-04 11:04:00 +02:00
|
|
|
setlsapwd (const char *passwd, const char *username)
|
2008-11-26 11:18:10 +01:00
|
|
|
{
|
|
|
|
unsigned long ret = (unsigned long) -1;
|
2014-03-28 21:34:04 +01:00
|
|
|
HANDLE lsa;
|
2008-11-26 11:18:10 +01:00
|
|
|
WCHAR sid[128];
|
|
|
|
WCHAR key_name[128 + wcslen (CYGWIN_LSA_KEY_PREFIX)];
|
|
|
|
PWCHAR data_buf = NULL;
|
|
|
|
UNICODE_STRING key;
|
|
|
|
UNICODE_STRING data;
|
|
|
|
|
2011-04-04 11:04:00 +02:00
|
|
|
if (username)
|
|
|
|
{
|
|
|
|
cygsid psid;
|
2014-02-09 20:44:56 +01:00
|
|
|
struct passwd *pw = internal_getpwnam (username);
|
2011-04-04 11:04:00 +02:00
|
|
|
|
|
|
|
if (!pw || !psid.getfrompw (pw))
|
|
|
|
{
|
|
|
|
set_errno (ENOENT);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
wcpcpy (wcpcpy (key_name, CYGWIN_LSA_KEY_PREFIX), psid.string (sid));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
wcpcpy (wcpcpy (key_name, CYGWIN_LSA_KEY_PREFIX),
|
|
|
|
cygheap->user.get_windows_id (sid));
|
2008-11-26 11:18:10 +01:00
|
|
|
RtlInitUnicodeString (&key, key_name);
|
|
|
|
if (!passwd || ! *passwd
|
|
|
|
|| sys_mbstowcs_alloc (&data_buf, HEAP_NOTHEAP, passwd))
|
|
|
|
{
|
|
|
|
memset (&data, 0, sizeof data);
|
|
|
|
if (data_buf)
|
|
|
|
RtlInitUnicodeString (&data, data_buf);
|
|
|
|
/* First try it locally. Works for admin accounts. */
|
2014-02-18 15:53:38 +01:00
|
|
|
if ((lsa = lsa_open_policy (NULL, POLICY_CREATE_SECRET)))
|
2008-11-26 11:18:10 +01:00
|
|
|
{
|
2008-12-15 19:07:35 +01:00
|
|
|
NTSTATUS status = LsaStorePrivateData (lsa, &key,
|
|
|
|
data.Length ? &data : NULL);
|
|
|
|
/* Success or we're trying to remove a password entry which doesn't
|
|
|
|
exist. */
|
|
|
|
if (NT_SUCCESS (status)
|
|
|
|
|| (data.Length == 0 && status == STATUS_OBJECT_NAME_NOT_FOUND))
|
2008-11-26 11:18:10 +01:00
|
|
|
ret = 0;
|
2008-12-15 19:07:35 +01:00
|
|
|
else
|
|
|
|
__seterrno_from_nt_status (status);
|
2014-01-23 18:02:30 +01:00
|
|
|
lsa_close_policy (lsa);
|
2008-11-26 11:18:10 +01:00
|
|
|
}
|
2011-04-04 11:04:00 +02:00
|
|
|
else if (ret && !username)
|
2008-11-26 11:18:10 +01:00
|
|
|
{
|
|
|
|
client_request_setpwd request (&data);
|
|
|
|
if (request.make_request () == -1 || request.error_code ())
|
|
|
|
set_errno (request.error_code ());
|
|
|
|
else
|
|
|
|
ret = 0;
|
|
|
|
}
|
|
|
|
if (data_buf)
|
2008-12-15 18:39:39 +01:00
|
|
|
{
|
2014-03-06 18:00:52 +01:00
|
|
|
RtlSecureZeroMemory (data.Buffer, data.Length);
|
2008-12-15 18:39:39 +01:00
|
|
|
free (data_buf);
|
|
|
|
}
|
2008-11-26 11:18:10 +01:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|