wmi-1.3.16 from opsview.com

This commit is contained in:
Are Casilla
2019-02-16 00:16:52 +01:00
parent 163fdd3d1b
commit 17b3af2911
2146 changed files with 678824 additions and 0 deletions
+152
View File
@@ -0,0 +1,152 @@
/*
Unix SMB/CIFS implementation.
security access checking routines
Copyright (C) Andrew Tridgell 2004
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "libcli/security/security.h"
/*
perform a SEC_FLAG_MAXIMUM_ALLOWED access check
*/
static uint32_t access_check_max_allowed(const struct security_descriptor *sd,
const struct security_token *token)
{
uint32_t denied = 0, granted = 0;
unsigned i;
if (security_token_has_sid(token, sd->owner_sid)) {
granted |= SEC_STD_WRITE_DAC | SEC_STD_READ_CONTROL | SEC_STD_DELETE;
} else if (security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
granted |= SEC_STD_DELETE;
}
for (i = 0;i<sd->dacl->num_aces; i++) {
struct security_ace *ace = &sd->dacl->aces[i];
if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
continue;
}
if (!security_token_has_sid(token, &ace->trustee)) {
continue;
}
switch (ace->type) {
case SEC_ACE_TYPE_ACCESS_ALLOWED:
granted |= ace->access_mask;
break;
case SEC_ACE_TYPE_ACCESS_DENIED:
case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
denied |= ace->access_mask;
break;
default: /* Other ACE types not handled/supported */
break;
}
}
return granted & ~denied;
}
/*
the main entry point for access checking.
*/
NTSTATUS sec_access_check(const struct security_descriptor *sd,
const struct security_token *token,
uint32_t access_desired,
uint32_t *access_granted)
{
int i;
uint32_t bits_remaining;
*access_granted = access_desired;
bits_remaining = access_desired;
/* handle the maximum allowed flag */
if (access_desired & SEC_FLAG_MAXIMUM_ALLOWED) {
access_desired |= access_check_max_allowed(sd, token);
access_desired &= ~SEC_FLAG_MAXIMUM_ALLOWED;
*access_granted = access_desired;
bits_remaining = access_desired & ~SEC_STD_DELETE;
}
if (access_desired & SEC_FLAG_SYSTEM_SECURITY) {
if (security_token_has_privilege(token, SEC_PRIV_SECURITY)) {
bits_remaining &= ~SEC_FLAG_SYSTEM_SECURITY;
} else {
return NT_STATUS_PRIVILEGE_NOT_HELD;
}
}
/* dacl not present allows access */
if (!(sd->type & SEC_DESC_DACL_PRESENT)) {
*access_granted = access_desired;
return NT_STATUS_OK;
}
/* empty dacl denies access */
if (sd->dacl == NULL || sd->dacl->num_aces == 0) {
return NT_STATUS_ACCESS_DENIED;
}
/* the owner always gets SEC_STD_WRITE_DAC, SEC_STD_READ_CONTROL and SEC_STD_DELETE */
if ((bits_remaining & (SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL|SEC_STD_DELETE)) &&
security_token_has_sid(token, sd->owner_sid)) {
bits_remaining &= ~(SEC_STD_WRITE_DAC|SEC_STD_READ_CONTROL|SEC_STD_DELETE);
}
if ((bits_remaining & SEC_STD_DELETE) &&
security_token_has_privilege(token, SEC_PRIV_RESTORE)) {
bits_remaining &= ~SEC_STD_DELETE;
}
/* check each ace in turn. */
for (i=0; bits_remaining && i < sd->dacl->num_aces; i++) {
struct security_ace *ace = &sd->dacl->aces[i];
if (ace->flags & SEC_ACE_FLAG_INHERIT_ONLY) {
continue;
}
if (!security_token_has_sid(token, &ace->trustee)) {
continue;
}
switch (ace->type) {
case SEC_ACE_TYPE_ACCESS_ALLOWED:
bits_remaining &= ~ace->access_mask;
break;
case SEC_ACE_TYPE_ACCESS_DENIED:
case SEC_ACE_TYPE_ACCESS_DENIED_OBJECT:
if (bits_remaining & ace->access_mask) {
return NT_STATUS_ACCESS_DENIED;
}
break;
default: /* Other ACE types not handled/supported */
break;
}
}
if (bits_remaining != 0) {
return NT_STATUS_ACCESS_DENIED;
}
return NT_STATUS_OK;
}
+13
View File
@@ -0,0 +1,13 @@
#################################
# Start SUBSYSTEM LIBSECURITY
[SUBSYSTEM::LIBSECURITY]
PRIVATE_PROTO_HEADER = proto.h
OBJ_FILES = security_token.o \
security_descriptor.o \
dom_sid.o \
access_check.o \
privilege.o \
sddl.o
PUBLIC_DEPENDENCIES = NDR_MISC
# End SUBSYSTEM LIBSECURITY
#################################
+292
View File
@@ -0,0 +1,292 @@
/*
Unix SMB/CIFS implementation.
Samba utility functions
Copyright (C) Stefan (metze) Metzmacher 2002-2004
Copyright (C) Andrew Tridgell 1992-2004
Copyright (C) Jeremy Allison 1999
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "librpc/gen_ndr/security.h"
/*****************************************************************
Compare the auth portion of two sids.
*****************************************************************/
static int dom_sid_compare_auth(const struct dom_sid *sid1, const struct dom_sid *sid2)
{
int i;
if (sid1 == sid2)
return 0;
if (!sid1)
return -1;
if (!sid2)
return 1;
if (sid1->sid_rev_num != sid2->sid_rev_num)
return sid1->sid_rev_num - sid2->sid_rev_num;
for (i = 0; i < 6; i++)
if (sid1->id_auth[i] != sid2->id_auth[i])
return sid1->id_auth[i] - sid2->id_auth[i];
return 0;
}
/*****************************************************************
Compare two sids.
*****************************************************************/
static int dom_sid_compare(const struct dom_sid *sid1, const struct dom_sid *sid2)
{
int i;
if (sid1 == sid2)
return 0;
if (!sid1)
return -1;
if (!sid2)
return 1;
/* Compare most likely different rids, first: i.e start at end */
if (sid1->num_auths != sid2->num_auths)
return sid1->num_auths - sid2->num_auths;
for (i = sid1->num_auths-1; i >= 0; --i)
if (sid1->sub_auths[i] != sid2->sub_auths[i])
return sid1->sub_auths[i] - sid2->sub_auths[i];
return dom_sid_compare_auth(sid1, sid2);
}
/*****************************************************************
Compare two sids.
*****************************************************************/
BOOL dom_sid_equal(const struct dom_sid *sid1, const struct dom_sid *sid2)
{
return dom_sid_compare(sid1, sid2) == 0;
}
/*
convert a string to a dom_sid, returning a talloc'd dom_sid
*/
struct dom_sid *dom_sid_parse_talloc(TALLOC_CTX *mem_ctx, const char *sidstr)
{
struct dom_sid *ret;
uint_t rev, ia, num_sub_auths, i;
char *p;
if (strncasecmp(sidstr, "S-", 2)) {
return NULL;
}
sidstr += 2;
rev = strtol(sidstr, &p, 10);
if (*p != '-') {
return NULL;
}
sidstr = p+1;
ia = strtol(sidstr, &p, 10);
if (p == sidstr) {
return NULL;
}
sidstr = p;
num_sub_auths = 0;
for (i=0;sidstr[i];i++) {
if (sidstr[i] == '-') num_sub_auths++;
}
ret = talloc(mem_ctx, struct dom_sid);
if (!ret) {
return NULL;
}
ret->sub_auths = talloc_array(ret, uint32_t, num_sub_auths);
if (!ret->sub_auths) {
return NULL;
}
ret->sid_rev_num = rev;
ret->id_auth[0] = 0;
ret->id_auth[1] = 0;
ret->id_auth[2] = ia >> 24;
ret->id_auth[3] = ia >> 16;
ret->id_auth[4] = ia >> 8;
ret->id_auth[5] = ia;
ret->num_auths = num_sub_auths;
for (i=0;i<num_sub_auths;i++) {
if (sidstr[0] != '-') {
return NULL;
}
sidstr++;
ret->sub_auths[i] = strtoul(sidstr, &p, 10);
if (p == sidstr) {
return NULL;
}
sidstr = p;
}
return ret;
}
/*
copy a dom_sid structure
*/
struct dom_sid *dom_sid_dup(TALLOC_CTX *mem_ctx, const struct dom_sid *dom_sid)
{
struct dom_sid *ret;
int i;
if (!dom_sid) {
return NULL;
}
ret = talloc(mem_ctx, struct dom_sid);
if (!ret) {
return NULL;
}
ret->sub_auths = talloc_array(ret, uint32_t, dom_sid->num_auths);
if (!ret->sub_auths) {
return NULL;
}
ret->sid_rev_num = dom_sid->sid_rev_num;
ret->id_auth[0] = dom_sid->id_auth[0];
ret->id_auth[1] = dom_sid->id_auth[1];
ret->id_auth[2] = dom_sid->id_auth[2];
ret->id_auth[3] = dom_sid->id_auth[3];
ret->id_auth[4] = dom_sid->id_auth[4];
ret->id_auth[5] = dom_sid->id_auth[5];
ret->num_auths = dom_sid->num_auths;
for (i=0;i<dom_sid->num_auths;i++) {
ret->sub_auths[i] = dom_sid->sub_auths[i];
}
return ret;
}
/*
add a rid to a domain dom_sid to make a full dom_sid. This function
returns a new sid in the suppplied memory context
*/
struct dom_sid *dom_sid_add_rid(TALLOC_CTX *mem_ctx,
const struct dom_sid *domain_sid,
uint32_t rid)
{
struct dom_sid *sid;
sid = talloc(mem_ctx, struct dom_sid);
if (!sid) return NULL;
*sid = *domain_sid;
sid->sub_auths = talloc_array(sid, uint32_t, sid->num_auths+1);
if (!sid->sub_auths) {
return NULL;
}
memcpy(sid->sub_auths, domain_sid->sub_auths, sid->num_auths*sizeof(uint32_t));
sid->sub_auths[sid->num_auths] = rid;
sid->num_auths++;
return sid;
}
/*
Split up a SID into its domain and RID part
*/
NTSTATUS dom_sid_split_rid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
struct dom_sid **domain, uint32_t *rid)
{
if (sid->num_auths == 0) {
return NT_STATUS_INVALID_PARAMETER;
}
if (!(*domain = dom_sid_dup(mem_ctx, sid))) {
return NT_STATUS_NO_MEMORY;
}
(*domain)->num_auths -= 1;
*rid = (*domain)->sub_auths[(*domain)->num_auths];
return NT_STATUS_OK;
}
/*
return True if the 2nd sid is in the domain given by the first sid
*/
BOOL dom_sid_in_domain(const struct dom_sid *domain_sid,
const struct dom_sid *sid)
{
int i;
if (!domain_sid || !sid) {
return False;
}
if (domain_sid->num_auths > sid->num_auths) {
return False;
}
for (i = domain_sid->num_auths-1; i >= 0; --i) {
if (domain_sid->sub_auths[i] != sid->sub_auths[i]) {
return False;
}
}
return dom_sid_compare_auth(domain_sid, sid) == 0;
}
/*
convert a dom_sid to a string
*/
char *dom_sid_string(TALLOC_CTX *mem_ctx, const struct dom_sid *sid)
{
int i, ofs, maxlen;
uint32_t ia;
char *ret;
if (!sid) {
return talloc_strdup(mem_ctx, "(NULL SID)");
}
maxlen = sid->num_auths * 11 + 25;
ret = talloc_size(mem_ctx, maxlen);
if (!ret) return talloc_strdup(mem_ctx, "(SID ERR)");
ia = (sid->id_auth[5]) +
(sid->id_auth[4] << 8 ) +
(sid->id_auth[3] << 16) +
(sid->id_auth[2] << 24);
ofs = snprintf(ret, maxlen, "S-%u-%lu",
(unsigned int)sid->sid_rev_num, (unsigned long)ia);
for (i = 0; i < sid->num_auths; i++) {
ofs += snprintf(ret + ofs, maxlen - ofs, "-%lu", (unsigned long)sid->sub_auths[i]);
}
return ret;
}
+241
View File
@@ -0,0 +1,241 @@
/*
Unix SMB/CIFS implementation.
manipulate privileges
Copyright (C) Andrew Tridgell 2004
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "librpc/gen_ndr/security.h"
static const struct {
enum sec_privilege privilege;
const char *name;
const char *display_name;
} privilege_names[] = {
{SEC_PRIV_SECURITY,
"SeSecurityPrivilege",
"System security"},
{SEC_PRIV_BACKUP,
"SeBackupPrivilege",
"Backup files and directories"},
{SEC_PRIV_RESTORE,
"SeRestorePrivilege",
"Restore files and directories"},
{SEC_PRIV_SYSTEMTIME,
"SeSystemtimePrivilege",
"Set the system clock"},
{SEC_PRIV_SHUTDOWN,
"SeShutdownPrivilege",
"Shutdown the system"},
{SEC_PRIV_REMOTE_SHUTDOWN,
"SeRemoteShutdownPrivilege",
"Shutdown the system remotely"},
{SEC_PRIV_TAKE_OWNERSHIP,
"SeTakeOwnershipPrivilege",
"Take ownership of files and directories"},
{SEC_PRIV_DEBUG,
"SeDebugPrivilege",
"Debug processes"},
{SEC_PRIV_SYSTEM_ENVIRONMENT,
"SeSystemEnvironmentPrivilege",
"Modify system environment"},
{SEC_PRIV_SYSTEM_PROFILE,
"SeSystemProfilePrivilege",
"Profile the system"},
{SEC_PRIV_PROFILE_SINGLE_PROCESS,
"SeProfileSingleProcessPrivilege",
"Profile one process"},
{SEC_PRIV_INCREASE_BASE_PRIORITY,
"SeIncreaseBasePriorityPrivilege",
"Increase base priority"},
{SEC_PRIV_LOAD_DRIVER,
"SeLoadDriverPrivilege",
"Load drivers"},
{SEC_PRIV_CREATE_PAGEFILE,
"SeCreatePagefilePrivilege",
"Create page files"},
{SEC_PRIV_INCREASE_QUOTA,
"SeIncreaseQuotaPrivilege",
"Increase quota"},
{SEC_PRIV_CHANGE_NOTIFY,
"SeChangeNotifyPrivilege",
"Register for change notify"},
{SEC_PRIV_UNDOCK,
"SeUndockPrivilege",
"Undock devices"},
{SEC_PRIV_MANAGE_VOLUME,
"SeManageVolumePrivilege",
"Manage system volumes"},
{SEC_PRIV_IMPERSONATE,
"SeImpersonatePrivilege",
"Impersonate users"},
{SEC_PRIV_CREATE_GLOBAL,
"SeCreateGlobalPrivilege",
"Create global"},
{SEC_PRIV_ENABLE_DELEGATION,
"SeEnableDelegationPrivilege",
"Enable Delegation"},
{SEC_PRIV_INTERACTIVE_LOGON,
"SeInteractiveLogonRight",
"Interactive logon"},
{SEC_PRIV_NETWORK_LOGON,
"SeNetworkLogonRight",
"Network logon"},
{SEC_PRIV_REMOTE_INTERACTIVE_LOGON,
"SeRemoteInteractiveLogonRight",
"Remote Interactive logon"}
};
/*
map a privilege id to the wire string constant
*/
const char *sec_privilege_name(enum sec_privilege privilege)
{
int i;
for (i=0;i<ARRAY_SIZE(privilege_names);i++) {
if (privilege_names[i].privilege == privilege) {
return privilege_names[i].name;
}
}
return NULL;
}
/*
map a privilege id to a privilege display name. Return NULL if not found
TODO: this should use language mappings
*/
const char *sec_privilege_display_name(enum sec_privilege privilege, uint16_t *language)
{
int i;
if (privilege < 1 || privilege > 64) {
return NULL;
}
for (i=0;i<ARRAY_SIZE(privilege_names);i++) {
if (privilege_names[i].privilege == privilege) {
return privilege_names[i].display_name;
}
}
return NULL;
}
/*
map a privilege name to a privilege id. Return -1 if not found
*/
enum sec_privilege sec_privilege_id(const char *name)
{
int i;
for (i=0;i<ARRAY_SIZE(privilege_names);i++) {
if (strcasecmp(privilege_names[i].name, name) == 0) {
return privilege_names[i].privilege;
}
}
return -1;
}
/*
return a privilege mask given a privilege id
*/
static uint64_t sec_privilege_mask(enum sec_privilege privilege)
{
uint64_t mask = 1;
if (privilege < 1 || privilege > 64) {
return 0;
}
mask <<= (privilege-1);
return mask;
}
/*
return True if a security_token has a particular privilege bit set
*/
BOOL security_token_has_privilege(const struct security_token *token, enum sec_privilege privilege)
{
uint64_t mask;
if (privilege < 1 || privilege > 64) {
return False;
}
mask = sec_privilege_mask(privilege);
if (token->privilege_mask & mask) {
return True;
}
return False;
}
/*
set a bit in the privilege mask
*/
void security_token_set_privilege(struct security_token *token, enum sec_privilege privilege)
{
if (privilege < 1 || privilege > 64) {
return;
}
token->privilege_mask |= sec_privilege_mask(privilege);
}
void security_token_debug_privileges(int dbg_lev, const struct security_token *token)
{
DEBUGADD(dbg_lev, (" Privileges (0x%16llX):\n",
(unsigned long long) token->privilege_mask));
if (token->privilege_mask) {
int i = 0;
uint_t privilege;
for (privilege = 1; privilege <= 64; privilege++) {
uint64_t mask = sec_privilege_mask(privilege);
if (token->privilege_mask & mask) {
DEBUGADD(dbg_lev, (" Privilege[%3lu]: %s\n", (unsigned long)i++,
sec_privilege_name(privilege)));
}
}
}
}
+582
View File
@@ -0,0 +1,582 @@
/*
Unix SMB/CIFS implementation.
security descriptor description language functions
Copyright (C) Andrew Tridgell 2005
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "libcli/security/security.h"
#include "librpc/gen_ndr/ndr_misc.h"
#include "system/locale.h"
struct flag_map {
const char *name;
uint32_t flag;
};
/*
map a series of letter codes into a uint32_t
*/
static BOOL sddl_map_flags(const struct flag_map *map, const char *str,
uint32_t *flags, size_t *len)
{
const char *str0 = str;
if (len) *len = 0;
*flags = 0;
while (str[0] && isupper(str[0])) {
int i;
for (i=0;map[i].name;i++) {
size_t l = strlen(map[i].name);
if (strncmp(map[i].name, str, l) == 0) {
*flags |= map[i].flag;
str += l;
if (len) *len += l;
break;
}
}
if (map[i].name == NULL) {
DEBUG(1, ("Unknown flag - %s in %s\n", str, str0));
return False;
}
}
return True;
}
/*
a mapping between the 2 letter SID codes and sid strings
*/
static const struct {
const char *code;
const char *sid;
uint32_t rid;
} sid_codes[] = {
{ "AO", SID_BUILTIN_ACCOUNT_OPERATORS },
{ "BA", SID_BUILTIN_ADMINISTRATORS },
{ "RU", SID_BUILTIN_PREW2K },
{ "PO", SID_BUILTIN_PRINT_OPERATORS },
{ "RS", SID_BUILTIN_RAS_SERVERS },
{ "AU", SID_NT_AUTHENTICATED_USERS },
{ "SY", SID_NT_SYSTEM },
{ "PS", SID_NT_SELF },
{ "WD", SID_WORLD },
{ "ED", SID_NT_ENTERPRISE_DCS },
{ "CO", SID_CREATOR_OWNER },
{ "CG", SID_CREATOR_GROUP },
{ "DA", NULL, DOMAIN_RID_ADMINS },
{ "EA", NULL, DOMAIN_RID_ENTERPRISE_ADMINS },
{ "DD", NULL, DOMAIN_RID_DCS },
{ "DU", NULL, DOMAIN_RID_USERS },
{ "CA", NULL, DOMAIN_RID_CERT_ADMINS },
};
/*
decode a SID
It can either be a special 2 letter code, or in S-* format
*/
static struct dom_sid *sddl_decode_sid(TALLOC_CTX *mem_ctx, const char **sddlp,
const struct dom_sid *domain_sid)
{
const char *sddl = (*sddlp);
int i;
/* see if its in the numeric format */
if (strncmp(sddl, "S-", 2) == 0) {
size_t len = strspn(sddl+2, "-0123456789");
(*sddlp) += len+2;
return dom_sid_parse_talloc(mem_ctx, sddl);
}
/* now check for one of the special codes */
for (i=0;i<ARRAY_SIZE(sid_codes);i++) {
if (strncmp(sid_codes[i].code, sddl, 2) == 0) break;
}
if (i == ARRAY_SIZE(sid_codes)) {
DEBUG(1,("Unknown sddl sid code '%2.2s'\n", sddl));
return NULL;
}
(*sddlp) += 2;
if (sid_codes[i].sid == NULL) {
return dom_sid_add_rid(mem_ctx, domain_sid, sid_codes[i].rid);
}
return dom_sid_parse_talloc(mem_ctx, sid_codes[i].sid);
}
static const struct flag_map ace_types[] = {
{ "AU", SEC_ACE_TYPE_SYSTEM_AUDIT },
{ "AL", SEC_ACE_TYPE_SYSTEM_ALARM },
{ "OA", SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT },
{ "OD", SEC_ACE_TYPE_ACCESS_DENIED_OBJECT },
{ "OU", SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT },
{ "OL", SEC_ACE_TYPE_SYSTEM_ALARM_OBJECT },
{ "A", SEC_ACE_TYPE_ACCESS_ALLOWED },
{ "D", SEC_ACE_TYPE_ACCESS_DENIED },
{ NULL, 0 }
};
static const struct flag_map ace_flags[] = {
{ "OI", SEC_ACE_FLAG_OBJECT_INHERIT },
{ "CI", SEC_ACE_FLAG_CONTAINER_INHERIT },
{ "NP", SEC_ACE_FLAG_NO_PROPAGATE_INHERIT },
{ "IO", SEC_ACE_FLAG_INHERIT_ONLY },
{ "ID", SEC_ACE_FLAG_INHERITED_ACE },
{ "SA", SEC_ACE_FLAG_SUCCESSFUL_ACCESS },
{ "FA", SEC_ACE_FLAG_FAILED_ACCESS },
{ NULL, 0 },
};
static const struct flag_map ace_access_mask[] = {
{ "RP", SEC_ADS_READ_PROP },
{ "WP", SEC_ADS_WRITE_PROP },
{ "CR", SEC_ADS_CONTROL_ACCESS },
{ "CC", SEC_ADS_CREATE_CHILD },
{ "DC", SEC_ADS_DELETE_CHILD },
{ "LC", SEC_ADS_LIST },
{ "LO", SEC_ADS_LIST_OBJECT },
{ "RC", SEC_STD_READ_CONTROL },
{ "WO", SEC_STD_WRITE_OWNER },
{ "WD", SEC_STD_WRITE_DAC },
{ "SD", SEC_STD_DELETE },
{ "DT", SEC_ADS_DELETE_TREE },
{ "SW", SEC_ADS_SELF_WRITE },
{ "GA", SEC_GENERIC_ALL },
{ "GR", SEC_GENERIC_READ },
{ "GW", SEC_GENERIC_WRITE },
{ "GX", SEC_GENERIC_EXECUTE },
{ NULL, 0 }
};
/*
decode an ACE
return True on success, False on failure
note that this routine modifies the string
*/
static BOOL sddl_decode_ace(TALLOC_CTX *mem_ctx, struct security_ace *ace, char *str,
const struct dom_sid *domain_sid)
{
const char *tok[6];
const char *s;
int i;
uint32_t v;
struct dom_sid *sid;
ZERO_STRUCTP(ace);
/* parse out the 6 tokens */
tok[0] = str;
for (i=0;i<5;i++) {
char *ptr = strchr(str, ';');
if (ptr == NULL) return False;
*ptr = 0;
str = ptr+1;
tok[i+1] = str;
}
/* parse ace type */
if (!sddl_map_flags(ace_types, tok[0], &v, NULL)) {
return False;
}
ace->type = v;
/* ace flags */
if (!sddl_map_flags(ace_flags, tok[1], &v, NULL)) {
return False;
}
ace->flags = v;
/* access mask */
if (strncmp(tok[2], "0x", 2) == 0) {
ace->access_mask = strtol(tok[2], NULL, 16);
} else {
if (!sddl_map_flags(ace_access_mask, tok[2], &v, NULL)) {
return False;
}
ace->access_mask = v;
}
/* object */
if (tok[3][0] != 0) {
NTSTATUS status = GUID_from_string(tok[3],
&ace->object.object.type.type);
if (!NT_STATUS_IS_OK(status)) {
return False;
}
ace->object.object.flags |= SEC_ACE_OBJECT_TYPE_PRESENT;
}
/* inherit object */
if (tok[4][0] != 0) {
NTSTATUS status = GUID_from_string(tok[4],
&ace->object.object.inherited_type.inherited_type);
if (!NT_STATUS_IS_OK(status)) {
return False;
}
ace->object.object.flags |= SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT;
}
/* trustee */
s = tok[5];
sid = sddl_decode_sid(mem_ctx, &s, domain_sid);
if (sid == NULL) {
return False;
}
ace->trustee = *sid;
talloc_steal(mem_ctx, sid->sub_auths);
talloc_free(sid);
return True;
}
static const struct flag_map acl_flags[] = {
{ "P", SEC_DESC_DACL_PROTECTED },
{ "AR", SEC_DESC_DACL_AUTO_INHERIT_REQ },
{ "AI", SEC_DESC_DACL_AUTO_INHERITED },
{ NULL, 0 }
};
/*
decode an ACL
*/
static struct security_acl *sddl_decode_acl(struct security_descriptor *sd,
const char **sddlp, uint32_t *flags,
const struct dom_sid *domain_sid)
{
const char *sddl = *sddlp;
struct security_acl *acl;
size_t len;
*flags = 0;
acl = talloc_zero(sd, struct security_acl);
if (acl == NULL) return NULL;
acl->revision = SECURITY_ACL_REVISION_NT4;
if (isupper(sddl[0]) && sddl[1] == ':') {
/* its an empty ACL */
return acl;
}
/* work out the ACL flags */
if (!sddl_map_flags(acl_flags, sddl, flags, &len)) {
talloc_free(acl);
return NULL;
}
sddl += len;
/* now the ACEs */
while (*sddl == '(') {
char *astr;
len = strcspn(sddl+1, ")");
astr = talloc_strndup(acl, sddl+1, len);
if (astr == NULL || sddl[len+1] != ')') {
talloc_free(acl);
return NULL;
}
acl->aces = talloc_realloc(acl, acl->aces, struct security_ace,
acl->num_aces+1);
if (acl->aces == NULL) {
talloc_free(acl);
return NULL;
}
if (!sddl_decode_ace(acl->aces, &acl->aces[acl->num_aces],
astr, domain_sid)) {
talloc_free(acl);
return NULL;
}
talloc_free(astr);
sddl += len+2;
acl->num_aces++;
}
(*sddlp) = sddl;
return acl;
}
/*
decode a security descriptor in SDDL format
*/
struct security_descriptor *sddl_decode(TALLOC_CTX *mem_ctx, const char *sddl,
const struct dom_sid *domain_sid)
{
struct security_descriptor *sd;
sd = talloc_zero(mem_ctx, struct security_descriptor);
sd->revision = SECURITY_DESCRIPTOR_REVISION_1;
sd->type = SEC_DESC_SELF_RELATIVE;
while (*sddl) {
uint32_t flags;
char c = sddl[0];
if (sddl[1] != ':') goto failed;
sddl += 2;
switch (c) {
case 'D':
if (sd->dacl != NULL) goto failed;
sd->dacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
if (sd->dacl == NULL) goto failed;
sd->type |= flags | SEC_DESC_DACL_PRESENT;
break;
case 'S':
if (sd->sacl != NULL) goto failed;
sd->sacl = sddl_decode_acl(sd, &sddl, &flags, domain_sid);
if (sd->sacl == NULL) goto failed;
/* this relies on the SEC_DESC_SACL_* flags being
1 bit shifted from the SEC_DESC_DACL_* flags */
sd->type |= (flags<<1) | SEC_DESC_SACL_PRESENT;
break;
case 'O':
if (sd->owner_sid != NULL) goto failed;
sd->owner_sid = sddl_decode_sid(sd, &sddl, domain_sid);
if (sd->owner_sid == NULL) goto failed;
break;
case 'G':
if (sd->group_sid != NULL) goto failed;
sd->group_sid = sddl_decode_sid(sd, &sddl, domain_sid);
if (sd->group_sid == NULL) goto failed;
break;
}
}
return sd;
failed:
DEBUG(2,("Badly formatted SDDL '%s'\n", sddl));
talloc_free(sd);
return NULL;
}
/*
turn a set of flags into a string
*/
static char *sddl_flags_to_string(TALLOC_CTX *mem_ctx, const struct flag_map *map,
uint32_t flags, BOOL check_all)
{
int i;
char *s;
/* try to find an exact match */
for (i=0;map[i].name;i++) {
if (map[i].flag == flags) {
return talloc_strdup(mem_ctx, map[i].name);
}
}
s = talloc_strdup(mem_ctx, "");
/* now by bits */
for (i=0;map[i].name;i++) {
if ((flags & map[i].flag) != 0) {
s = talloc_asprintf_append(s, "%s", map[i].name);
if (s == NULL) goto failed;
flags &= ~map[i].flag;
}
}
if (check_all && flags != 0) {
goto failed;
}
return s;
failed:
talloc_free(s);
return NULL;
}
/*
encode a sid in SDDL format
*/
static char *sddl_encode_sid(TALLOC_CTX *mem_ctx, const struct dom_sid *sid,
const struct dom_sid *domain_sid)
{
int i;
char *sidstr;
sidstr = dom_sid_string(mem_ctx, sid);
if (sidstr == NULL) return NULL;
/* seen if its a well known sid */
for (i=0;sid_codes[i].sid;i++) {
if (strcmp(sidstr, sid_codes[i].sid) == 0) {
talloc_free(sidstr);
return talloc_strdup(mem_ctx, sid_codes[i].code);
}
}
/* or a well known rid in our domain */
if (dom_sid_in_domain(domain_sid, sid)) {
uint32_t rid = sid->sub_auths[sid->num_auths-1];
for (;i<ARRAY_SIZE(sid_codes);i++) {
if (rid == sid_codes[i].rid) {
talloc_free(sidstr);
return talloc_strdup(mem_ctx, sid_codes[i].code);
}
}
}
talloc_free(sidstr);
/* TODO: encode well known sids as two letter codes */
return dom_sid_string(mem_ctx, sid);
}
/*
encode an ACE in SDDL format
*/
static char *sddl_encode_ace(TALLOC_CTX *mem_ctx, const struct security_ace *ace,
const struct dom_sid *domain_sid)
{
char *sddl = NULL;
TALLOC_CTX *tmp_ctx;
const char *s_type="", *s_flags="", *s_mask="",
*s_object="", *s_iobject="", *s_trustee="";
tmp_ctx = talloc_new(mem_ctx);
if (tmp_ctx == NULL) {
DEBUG(0, ("talloc_new failed\n"));
return NULL;
}
s_type = sddl_flags_to_string(tmp_ctx, ace_types, ace->type, True);
if (s_type == NULL) goto failed;
s_flags = sddl_flags_to_string(tmp_ctx, ace_flags, ace->flags, True);
if (s_flags == NULL) goto failed;
s_mask = sddl_flags_to_string(tmp_ctx, ace_access_mask, ace->access_mask, True);
if (s_mask == NULL) {
s_mask = talloc_asprintf(tmp_ctx, "0x%08x", ace->access_mask);
if (s_mask == NULL) goto failed;
}
if (ace->type == SEC_ACE_TYPE_ACCESS_ALLOWED_OBJECT ||
ace->type == SEC_ACE_TYPE_ACCESS_DENIED_OBJECT ||
ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT ||
ace->type == SEC_ACE_TYPE_SYSTEM_AUDIT_OBJECT) {
if (ace->object.object.flags & SEC_ACE_OBJECT_TYPE_PRESENT) {
s_object = GUID_string(tmp_ctx, &ace->object.object.type.type);
if (s_object == NULL) goto failed;
}
if (ace->object.object.flags & SEC_ACE_INHERITED_OBJECT_TYPE_PRESENT) {
s_iobject = GUID_string(tmp_ctx, &ace->object.object.inherited_type.inherited_type);
if (s_iobject == NULL) goto failed;
}
}
s_trustee = sddl_encode_sid(tmp_ctx, &ace->trustee, domain_sid);
if (s_trustee == NULL) goto failed;
sddl = talloc_asprintf(mem_ctx, "%s;%s;%s;%s;%s;%s",
s_type, s_flags, s_mask, s_object, s_iobject, s_trustee);
failed:
talloc_free(tmp_ctx);
return sddl;
}
/*
encode an ACL in SDDL format
*/
static char *sddl_encode_acl(TALLOC_CTX *mem_ctx, const struct security_acl *acl,
uint32_t flags, const struct dom_sid *domain_sid)
{
char *sddl;
int i;
/* add any ACL flags */
sddl = sddl_flags_to_string(mem_ctx, acl_flags, flags, False);
if (sddl == NULL) goto failed;
/* now the ACEs, encoded in braces */
for (i=0;i<acl->num_aces;i++) {
char *ace = sddl_encode_ace(sddl, &acl->aces[i], domain_sid);
if (ace == NULL) goto failed;
sddl = talloc_asprintf_append(sddl, "(%s)", ace);
if (sddl == NULL) goto failed;
talloc_free(ace);
}
return sddl;
failed:
talloc_free(sddl);
return NULL;
}
/*
encode a security descriptor to SDDL format
*/
char *sddl_encode(TALLOC_CTX *mem_ctx, const struct security_descriptor *sd,
const struct dom_sid *domain_sid)
{
char *sddl;
TALLOC_CTX *tmp_ctx;
/* start with a blank string */
sddl = talloc_strdup(mem_ctx, "");
if (sddl == NULL) goto failed;
tmp_ctx = talloc_new(mem_ctx);
if (sd->owner_sid != NULL) {
char *sid = sddl_encode_sid(tmp_ctx, sd->owner_sid, domain_sid);
if (sid == NULL) goto failed;
sddl = talloc_asprintf_append(sddl, "O:%s", sid);
if (sddl == NULL) goto failed;
}
if (sd->group_sid != NULL) {
char *sid = sddl_encode_sid(tmp_ctx, sd->group_sid, domain_sid);
if (sid == NULL) goto failed;
sddl = talloc_asprintf_append(sddl, "G:%s", sid);
if (sddl == NULL) goto failed;
}
if ((sd->type & SEC_DESC_DACL_PRESENT) && sd->dacl != NULL) {
char *acl = sddl_encode_acl(tmp_ctx, sd->dacl, sd->type, domain_sid);
if (acl == NULL) goto failed;
sddl = talloc_asprintf_append(sddl, "D:%s", acl);
if (sddl == NULL) goto failed;
}
if ((sd->type & SEC_DESC_SACL_PRESENT) && sd->sacl != NULL) {
char *acl = sddl_encode_acl(tmp_ctx, sd->sacl, sd->type>>1, domain_sid);
if (acl == NULL) goto failed;
sddl = talloc_asprintf_append(sddl, "S:%s", acl);
if (sddl == NULL) goto failed;
}
talloc_free(tmp_ctx);
return sddl;
failed:
talloc_free(sddl);
return NULL;
}
+22
View File
@@ -0,0 +1,22 @@
/*
Unix SMB/CIFS implementation.
Copyright (C) Stefan Metzmacher 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "librpc/gen_ndr/security.h"
#include "libcli/security/proto.h"
@@ -0,0 +1,366 @@
/*
Unix SMB/CIFS implementation.
security descriptror utility functions
Copyright (C) Andrew Tridgell 2004
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "libcli/security/security.h"
/*
return a blank security descriptor (no owners, dacl or sacl)
*/
struct security_descriptor *security_descriptor_initialise(TALLOC_CTX *mem_ctx)
{
struct security_descriptor *sd;
sd = talloc(mem_ctx, struct security_descriptor);
if (!sd) {
return NULL;
}
sd->revision = SD_REVISION;
/* we mark as self relative, even though it isn't while it remains
a pointer in memory because this simplifies the ndr code later.
All SDs that we store/emit are in fact SELF_RELATIVE
*/
sd->type = SEC_DESC_SELF_RELATIVE;
sd->owner_sid = NULL;
sd->group_sid = NULL;
sd->sacl = NULL;
sd->dacl = NULL;
return sd;
}
static struct security_acl *security_acl_dup(TALLOC_CTX *mem_ctx,
const struct security_acl *oacl)
{
struct security_acl *nacl;
int i;
nacl = talloc (mem_ctx, struct security_acl);
if (nacl == NULL) {
return NULL;
}
nacl->aces = talloc_memdup (nacl, oacl->aces, sizeof(struct security_ace) * oacl->num_aces);
if ((nacl->aces == NULL) && (oacl->num_aces > 0)) {
goto failed;
}
/* remapping array in trustee dom_sid from old acl to new acl */
for (i = 0; i < oacl->num_aces; i++) {
nacl->aces[i].trustee.sub_auths =
talloc_memdup(nacl->aces, nacl->aces[i].trustee.sub_auths,
sizeof(uint32_t) * nacl->aces[i].trustee.num_auths);
if ((nacl->aces[i].trustee.sub_auths == NULL) && (nacl->aces[i].trustee.num_auths > 0)) {
goto failed;
}
}
nacl->revision = oacl->revision;
nacl->size = oacl->size;
nacl->num_aces = oacl->num_aces;
return nacl;
failed:
talloc_free (nacl);
return NULL;
}
/*
talloc and copy a security descriptor
*/
struct security_descriptor *security_descriptor_copy(TALLOC_CTX *mem_ctx,
const struct security_descriptor *osd)
{
struct security_descriptor *nsd;
nsd = talloc_zero(mem_ctx, struct security_descriptor);
if (!nsd) {
return NULL;
}
if (osd->owner_sid) {
nsd->owner_sid = dom_sid_dup(nsd, osd->owner_sid);
if (nsd->owner_sid == NULL) {
goto failed;
}
}
if (osd->group_sid) {
nsd->group_sid = dom_sid_dup(nsd, osd->group_sid);
if (nsd->group_sid == NULL) {
goto failed;
}
}
if (osd->sacl) {
nsd->sacl = security_acl_dup(nsd, osd->sacl);
if (nsd->sacl == NULL) {
goto failed;
}
}
if (osd->dacl) {
nsd->dacl = security_acl_dup(nsd, osd->dacl);
if (nsd->dacl == NULL) {
goto failed;
}
}
return nsd;
failed:
talloc_free(nsd);
return NULL;
}
/*
add an ACE to the DACL of a security_descriptor
*/
NTSTATUS security_descriptor_dacl_add(struct security_descriptor *sd,
const struct security_ace *ace)
{
if (sd->dacl == NULL) {
sd->dacl = talloc(sd, struct security_acl);
if (sd->dacl == NULL) {
return NT_STATUS_NO_MEMORY;
}
sd->dacl->revision = NT4_ACL_REVISION;
sd->dacl->size = 0;
sd->dacl->num_aces = 0;
sd->dacl->aces = NULL;
}
sd->dacl->aces = talloc_realloc(sd->dacl, sd->dacl->aces,
struct security_ace, sd->dacl->num_aces+1);
if (sd->dacl->aces == NULL) {
return NT_STATUS_NO_MEMORY;
}
sd->dacl->aces[sd->dacl->num_aces] = *ace;
sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths =
talloc_memdup(sd->dacl->aces,
sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths,
sizeof(uint32_t) *
sd->dacl->aces[sd->dacl->num_aces].trustee.num_auths);
if (sd->dacl->aces[sd->dacl->num_aces].trustee.sub_auths == NULL) {
return NT_STATUS_NO_MEMORY;
}
sd->dacl->num_aces++;
sd->type |= SEC_DESC_DACL_PRESENT;
return NT_STATUS_OK;
}
/*
delete the ACE corresponding to the given trustee in the DACL of a security_descriptor
*/
NTSTATUS security_descriptor_dacl_del(struct security_descriptor *sd,
struct dom_sid *trustee)
{
int i;
if (sd->dacl == NULL) {
return NT_STATUS_OBJECT_NAME_NOT_FOUND;
}
for (i=0;i<sd->dacl->num_aces;i++) {
if (dom_sid_equal(trustee, &sd->dacl->aces[i].trustee)) {
memmove(&sd->dacl->aces[i], &sd->dacl->aces[i+1],
sizeof(sd->dacl->aces[i]) * (sd->dacl->num_aces - (i+1)));
sd->dacl->num_aces--;
if (sd->dacl->num_aces == 0) {
sd->dacl->aces = NULL;
}
return NT_STATUS_OK;
}
}
return NT_STATUS_OBJECT_NAME_NOT_FOUND;
}
/*
compare two security ace structures
*/
BOOL security_ace_equal(const struct security_ace *ace1,
const struct security_ace *ace2)
{
if (ace1 == ace2) return True;
if (!ace1 || !ace2) return False;
if (ace1->type != ace2->type) return False;
if (ace1->flags != ace2->flags) return False;
if (ace1->access_mask != ace2->access_mask) return False;
if (!dom_sid_equal(&ace1->trustee, &ace2->trustee)) return False;
return True;
}
/*
compare two security acl structures
*/
BOOL security_acl_equal(const struct security_acl *acl1,
const struct security_acl *acl2)
{
int i;
if (acl1 == acl2) return True;
if (!acl1 || !acl2) return False;
if (acl1->revision != acl2->revision) return False;
if (acl1->num_aces != acl2->num_aces) return False;
for (i=0;i<acl1->num_aces;i++) {
if (!security_ace_equal(&acl1->aces[i], &acl2->aces[i])) return False;
}
return True;
}
/*
compare two security descriptors.
*/
BOOL security_descriptor_equal(const struct security_descriptor *sd1,
const struct security_descriptor *sd2)
{
if (sd1 == sd2) return True;
if (!sd1 || !sd2) return False;
if (sd1->revision != sd2->revision) return False;
if (sd1->type != sd2->type) return False;
if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return False;
if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return False;
if (!security_acl_equal(sd1->sacl, sd2->sacl)) return False;
if (!security_acl_equal(sd1->dacl, sd2->dacl)) return False;
return True;
}
/*
compare two security descriptors, but allow certain (missing) parts
to be masked out of the comparison
*/
BOOL security_descriptor_mask_equal(const struct security_descriptor *sd1,
const struct security_descriptor *sd2,
uint32_t mask)
{
if (sd1 == sd2) return True;
if (!sd1 || !sd2) return False;
if (sd1->revision != sd2->revision) return False;
if ((sd1->type & mask) != (sd2->type & mask)) return False;
if (!dom_sid_equal(sd1->owner_sid, sd2->owner_sid)) return False;
if (!dom_sid_equal(sd1->group_sid, sd2->group_sid)) return False;
if ((mask & SEC_DESC_DACL_PRESENT) && !security_acl_equal(sd1->dacl, sd2->dacl)) return False;
if ((mask & SEC_DESC_SACL_PRESENT) && !security_acl_equal(sd1->sacl, sd2->sacl)) return False;
return True;
}
/*
create a security descriptor using string SIDs. This is used by the
torture code to allow the easy creation of complex ACLs
This is a varargs function. The list of DACL ACEs ends with a NULL sid.
Each ACE contains a set of 4 parameters:
SID, ACCESS_TYPE, MASK, FLAGS
a typical call would be:
sd = security_descriptor_create(mem_ctx,
mysid,
mygroup,
SID_NT_AUTHENTICATED_USERS,
SEC_ACE_TYPE_ACCESS_ALLOWED,
SEC_FILE_ALL,
SEC_ACE_FLAG_OBJECT_INHERIT,
NULL);
that would create a sd with one DACL ACE
*/
struct security_descriptor *security_descriptor_create(TALLOC_CTX *mem_ctx,
const char *owner_sid,
const char *group_sid,
...)
{
va_list ap;
struct security_descriptor *sd;
const char *sidstr;
sd = security_descriptor_initialise(mem_ctx);
if (sd == NULL) return NULL;
if (owner_sid) {
sd->owner_sid = dom_sid_parse_talloc(sd, owner_sid);
if (sd->owner_sid == NULL) {
talloc_free(sd);
return NULL;
}
}
if (group_sid) {
sd->group_sid = dom_sid_parse_talloc(sd, group_sid);
if (sd->group_sid == NULL) {
talloc_free(sd);
return NULL;
}
}
va_start(ap, group_sid);
while ((sidstr = va_arg(ap, const char *))) {
struct dom_sid *sid;
struct security_ace *ace = talloc(sd, struct security_ace);
NTSTATUS status;
if (ace == NULL) {
talloc_free(sd);
va_end(ap);
return NULL;
}
ace->type = va_arg(ap, unsigned int);
ace->access_mask = va_arg(ap, unsigned int);
ace->flags = va_arg(ap, unsigned int);
sid = dom_sid_parse_talloc(ace, sidstr);
if (sid == NULL) {
va_end(ap);
talloc_free(sd);
return NULL;
}
ace->trustee = *sid;
status = security_descriptor_dacl_add(sd, ace);
/* TODO: check: would talloc_free(ace) here be correct? */
if (!NT_STATUS_IS_OK(status)) {
va_end(ap);
talloc_free(sd);
return NULL;
}
}
va_end(ap);
return sd;
}
@@ -0,0 +1,144 @@
/*
Unix SMB/CIFS implementation.
security descriptror utility functions
Copyright (C) Andrew Tridgell 2004
Copyright (C) Stefan Metzmacher 2005
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "includes.h"
#include "dsdb/samdb/samdb.h"
#include "libcli/security/security.h"
/*
return a blank security token
*/
struct security_token *security_token_initialise(TALLOC_CTX *mem_ctx)
{
struct security_token *st;
st = talloc(mem_ctx, struct security_token);
if (!st) {
return NULL;
}
st->user_sid = NULL;
st->group_sid = NULL;
st->num_sids = 0;
st->sids = NULL;
st->privilege_mask = 0;
return st;
}
/****************************************************************************
prints a struct security_token to debug output.
****************************************************************************/
void security_token_debug(int dbg_lev, const struct security_token *token)
{
TALLOC_CTX *mem_ctx;
int i;
if (!token) {
DEBUG(dbg_lev, ("Security token: (NULL)\n"));
return;
}
mem_ctx = talloc_init("security_token_debug()");
if (!mem_ctx) {
return;
}
DEBUG(dbg_lev, ("Security token of user %s\n",
dom_sid_string(mem_ctx, token->user_sid) ));
DEBUGADD(dbg_lev, (" SIDs (%lu):\n",
(unsigned long)token->num_sids));
for (i = 0; i < token->num_sids; i++) {
DEBUGADD(dbg_lev, (" SID[%3lu]: %s\n", (unsigned long)i,
dom_sid_string(mem_ctx, token->sids[i])));
}
security_token_debug_privileges(dbg_lev, token);
talloc_free(mem_ctx);
}
/* These really should be cheaper... */
BOOL security_token_is_sid(const struct security_token *token, const struct dom_sid *sid)
{
if (dom_sid_equal(token->user_sid, sid)) {
return True;
}
return False;
}
BOOL security_token_is_sid_string(const struct security_token *token, const char *sid_string)
{
BOOL ret;
struct dom_sid *sid = dom_sid_parse_talloc(NULL, sid_string);
if (!sid) return False;
ret = security_token_is_sid(token, sid);
talloc_free(sid);
return ret;
}
BOOL security_token_is_system(const struct security_token *token)
{
return security_token_is_sid_string(token, SID_NT_SYSTEM);
}
BOOL security_token_is_anonymous(const struct security_token *token)
{
return security_token_is_sid_string(token, SID_NT_ANONYMOUS);
}
BOOL security_token_has_sid(const struct security_token *token, const struct dom_sid *sid)
{
int i;
for (i = 0; i < token->num_sids; i++) {
if (dom_sid_equal(token->sids[i], sid)) {
return True;
}
}
return False;
}
BOOL security_token_has_sid_string(const struct security_token *token, const char *sid_string)
{
BOOL ret;
struct dom_sid *sid = dom_sid_parse_talloc(NULL, sid_string);
if (!sid) return False;
ret = security_token_has_sid(token, sid);
talloc_free(sid);
return ret;
}
BOOL security_token_has_builtin_administrators(const struct security_token *token)
{
return security_token_has_sid_string(token, SID_BUILTIN_ADMINISTRATORS);
}
BOOL security_token_has_nt_authenticated_users(const struct security_token *token)
{
return security_token_has_sid_string(token, SID_NT_AUTHENTICATED_USERS);
}