#include <assert.h>
#include <string.h>
#include <libuutil.h>
#include "idmapd.h"
static wksids_table_t wksids[] = {
{"S-1-0", 0, "", "Nobody", 1, IDMAP_SENTINEL_PID, -1, 1},
{"S-1-1", 0, "", "Everyone", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-2", 0, "", "Local", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-2", 1, "", "Console Logon", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-3", 0, "", "Creator Owner", 1, IDMAP_WK_CREATOR_OWNER_UID, 1, 0},
{"S-1-3", 1, "", "Creator Group", 0, IDMAP_WK_CREATOR_GROUP_GID, 0, 0},
{"S-1-3", 2, "", "Creator Owner Server", 1, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-3", 3, "", "Creator Group Server", 0, IDMAP_SENTINEL_PID, -1, 1},
{"S-1-3", 4, "", "Owner Rights", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 1, "", "Dialup", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 2, "", "Network", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 3, "", "Batch", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 4, "", "Interactive", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 6, "", "Service", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 7, "", "Anonymous Logon", 0, GID_NOBODY, 0, 0},
{"S-1-5", 7, "", "Anonymous Logon", 0, UID_NOBODY, 1, 0},
{"S-1-5", 8, "", "Proxy", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 9, "", "Enterprise Domain Controllers", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 10, "", "Self", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 11, "", "Authenticated Users", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 12, "", "Restricted", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 13, "", "Terminal Server Users", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 14, "", "Remote Interactive Logon", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 15, "", "This Organization", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 17, "", "IUSR", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 18, "", "Local System", 0, IDMAP_WK_LOCAL_SYSTEM_GID, 0, 0},
{"S-1-5", 19, "", "Local Service", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 20, "", "Network Service", 0, IDMAP_SENTINEL_PID, -1, -1},
{NULL, 498, NULL, "Enterprise Read-only Domain Controllers", 0,
IDMAP_SENTINEL_PID, -1, -1},
{NULL, 500, NULL, "Administrator", 1, IDMAP_SENTINEL_PID, 1, -1},
{NULL, 501, NULL, "Guest", 1, IDMAP_SENTINEL_PID, 1, -1},
{NULL, 502, NULL, "KRBTGT", 1, IDMAP_SENTINEL_PID, 1, -1},
{NULL, 512, NULL, "Domain Admins", 0, IDMAP_SENTINEL_PID, -1, -1},
{NULL, 513, NULL, "Domain Users", 0, IDMAP_SENTINEL_PID, -1, -1},
{NULL, 514, NULL, "Domain Guests", 0, IDMAP_SENTINEL_PID, -1, -1},
{NULL, 515, NULL, "Domain Computers", 0, IDMAP_SENTINEL_PID, -1, -1},
{NULL, 516, NULL, "Domain Controllers", 0, IDMAP_SENTINEL_PID, -1, -1},
{NULL, 517, NULL, "Cert Publishers", 0, IDMAP_SENTINEL_PID, -1, -1},
{NULL, 518, NULL, "Schema Admins", 0, IDMAP_SENTINEL_PID, -1, -1},
{NULL, 519, NULL, "Enterprise Admins", 0, IDMAP_SENTINEL_PID, -1, -1},
{NULL, 520, NULL, "Global Policy Creator Owners", 0,
IDMAP_SENTINEL_PID, -1, -1},
{NULL, 533, NULL, "RAS and IAS Servers", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 544, "BUILTIN", "Administrators", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 545, "BUILTIN", "Users", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 546, "BUILTIN", "Guests", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 547, "BUILTIN", "Power Users", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 548, "BUILTIN", "Account Operators", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 549, "BUILTIN", "Server Operators", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 550, "BUILTIN", "Print Operators", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 551, "BUILTIN", "Backup Operators", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 552, "BUILTIN", "Replicator", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 554, "BUILTIN", "Pre-Windows 2000 Compatible Access", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 555, "BUILTIN", "Remote Desktop Users", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 556, "BUILTIN", "Network Configuration Operators", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 557, "BUILTIN", "Incoming Forest Trust Builders", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 558, "BUILTIN", "Performance Monitor Users", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 559, "BUILTIN", "Performance Log Users", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 560, "BUILTIN", "Windows Authorization Access Group", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 561, "BUILTIN", "Terminal Server License Servers", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 562, "BUILTIN", "Distributed COM Users", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 568, "BUILTIN", "IIS_IUSRS", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 569, "BUILTIN", "Cryptographic Operators", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 573, "BUILTIN", "Event Log Readers", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-32", 574, "BUILTIN", "Certificate Service DCOM Access", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 33, "", "Write Restricted", 0, IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-64", 10, "", "NTLM Authentication", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-64", 14, "", "SChannel Authentication", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5-64", 21, "", "Digest Authentication", 0,
IDMAP_SENTINEL_PID, -1, -1},
{"S-1-5", 1000, "", "Other Organization", 0,
IDMAP_SENTINEL_PID, -1, -1},
};
const
wksids_table_t *
find_wksid_by_name(const char *name, const char *domain, idmap_id_type type)
{
int i;
RDLOCK_CONFIG();
int len = strlen(_idmapdstate.hostname);
char my_host_name[len + 1];
(void) strcpy(my_host_name, _idmapdstate.hostname);
UNLOCK_CONFIG();
for (i = 0; i < UU_NELEM(wksids); i++) {
switch (type) {
case IDMAP_UID:
if (wksids[i].is_user == 0)
continue;
break;
case IDMAP_GID:
if (wksids[i].is_user == 1)
continue;
break;
case IDMAP_POSIXID:
break;
default:
assert(FALSE);
}
if (strcasecmp(wksids[i].winname, name) != 0)
continue;
if (!EMPTY_STRING(domain)) {
const char *dom;
if (wksids[i].domain != NULL) {
dom = wksids[i].domain;
} else {
dom = my_host_name;
}
if (strcasecmp(dom, domain) != 0)
continue;
}
if (wksids[i].direction == IDMAP_DIRECTION_U2W)
continue;
return (&wksids[i]);
}
return (NULL);
}
const
wksids_table_t *
find_wksid_by_sid(const char *sid, int rid, idmap_id_type type)
{
int i;
RDLOCK_CONFIG();
int len = strlen(_idmapdstate.cfg->pgcfg.machine_sid);
char my_machine_sid[len + 1];
(void) strcpy(my_machine_sid, _idmapdstate.cfg->pgcfg.machine_sid);
UNLOCK_CONFIG();
for (i = 0; i < UU_NELEM(wksids); i++) {
int sidcmp;
switch (type) {
case IDMAP_UID:
if (wksids[i].is_user == 0)
continue;
break;
case IDMAP_GID:
if (wksids[i].is_user == 1)
continue;
break;
case IDMAP_POSIXID:
break;
default:
assert(FALSE);
}
if (wksids[i].sidprefix != NULL) {
sidcmp = strcasecmp(wksids[i].sidprefix, sid);
} else {
sidcmp = strcasecmp(my_machine_sid, sid);
}
if (sidcmp != 0)
continue;
if (wksids[i].rid != rid)
continue;
if (wksids[i].direction == IDMAP_DIRECTION_U2W)
continue;
return (&wksids[i]);
}
return (NULL);
}
const
wksids_table_t *
find_wksid_by_pid(uid_t pid, int is_user)
{
int i;
if (pid == IDMAP_SENTINEL_PID)
return (NULL);
for (i = 0; i < UU_NELEM(wksids); i++) {
if (wksids[i].pid == pid &&
wksids[i].is_user == is_user &&
(wksids[i].direction == IDMAP_DIRECTION_BI ||
wksids[i].direction == IDMAP_DIRECTION_U2W)) {
return (&wksids[i]);
}
}
return (NULL);
}
const
wksids_table_t *
find_wk_by_sid(char *sid)
{
int i;
RDLOCK_CONFIG();
int len = strlen(_idmapdstate.cfg->pgcfg.machine_sid);
char my_machine_sid[len + 1];
(void) strcpy(my_machine_sid, _idmapdstate.cfg->pgcfg.machine_sid);
UNLOCK_CONFIG();
for (i = 0; i < UU_NELEM(wksids); i++) {
int len;
const char *prefix;
char *p;
unsigned long rid;
if (wksids[i].sidprefix == NULL)
prefix = my_machine_sid;
else
prefix = wksids[i].sidprefix;
len = strlen(prefix);
if (strncasecmp(sid, prefix, len) != 0)
continue;
if (sid[len] != '-')
continue;
rid = strtoul(sid + len + 1, &p, 10);
if (*p != '\0')
continue;
if (rid != wksids[i].rid)
continue;
return (&wksids[i]);
}
return (NULL);
}