41
41
#include "lib/events/events.h"
42
42
#include "auth/credentials/credentials.h"
43
43
#include "param/secrets.h"
44
#include "auth/auth.h"
45
char *samdb_relative_path(struct ldb_context *ldb,
47
make sure the static credentials are not freed
49
static int samdb_credentials_destructor(struct cli_credentials *creds)
49
const char *base_url =
50
(const char *)ldb_get_opaque(ldb, "ldb_url");
51
char *path, *p, *full_name;
55
if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
56
return talloc_strdup(mem_ctx, name);
58
path = talloc_strdup(mem_ctx, base_url);
62
if ( (p = strrchr(path, '/')) != NULL) {
64
full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
66
full_name = talloc_asprintf(mem_ctx, "./%s", name);
72
struct cli_credentials *samdb_credentials(TALLOC_CTX *mem_ctx,
73
struct tevent_context *event_ctx,
74
struct loadparm_context *lp_ctx)
55
this returns a static set of system credentials. It is static so
56
that we always get the same pointer in ldb_wrap_connect()
58
struct cli_credentials *samdb_credentials(struct loadparm_context *lp_ctx)
76
struct cli_credentials *cred = cli_credentials_init(mem_ctx);
60
static struct cli_credentials *static_credentials;
61
struct cli_credentials *cred;
64
if (static_credentials) {
65
return static_credentials;
68
cred = cli_credentials_init(talloc_autofree_context());
99
95
struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx,
100
96
struct tevent_context *ev_ctx,
101
97
struct loadparm_context *lp_ctx,
102
struct auth_session_info *session_info)
98
struct auth_session_info *session_info,
104
101
struct ldb_context *ldb;
105
ldb = ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx,
106
lp_sam_url(lp_ctx), session_info,
107
samdb_credentials(mem_ctx, ev_ctx, lp_ctx),
112
dsdb_make_schema_global(ldb);
102
struct dsdb_schema *schema;
104
struct cli_credentials *credentials;
107
url = lpcfg_sam_url(lp_ctx);
108
credentials = samdb_credentials(lp_ctx);
110
ldb = ldb_wrap_find(url, ev_ctx, lp_ctx, session_info, credentials, flags);
112
return talloc_reference(mem_ctx, ldb);
114
ldb = samba_ldb_init(mem_ctx, ev_ctx, lp_ctx, session_info, credentials);
119
dsdb_set_global_schema(ldb);
121
ret = samba_ldb_connect(ldb, lp_ctx, url, flags);
122
if (ret != LDB_SUCCESS) {
127
schema = dsdb_get_schema(ldb, NULL);
128
/* make the resulting schema global */
130
dsdb_make_schema_global(ldb, schema);
133
if (!ldb_wrap_add(url, ev_ctx, lp_ctx, session_info, credentials, flags, ldb)) {
118
143
Create the SID list for this user.
119
144
****************************************************************************/
120
145
NTSTATUS security_token_create(TALLOC_CTX *mem_ctx,
121
struct tevent_context *ev_ctx,
122
146
struct loadparm_context *lp_ctx,
123
struct dom_sid *user_sid,
124
struct dom_sid *group_sid,
126
struct dom_sid **groupSIDs,
127
bool is_authenticated,
147
unsigned int num_sids,
148
struct dom_sid *sids,
149
uint32_t session_info_flags,
128
150
struct security_token **token)
130
152
struct security_token *ptoken;
134
156
ptoken = security_token_initialise(mem_ctx);
135
157
NT_STATUS_HAVE_NO_MEMORY(ptoken);
137
ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
159
ptoken->sids = talloc_array(ptoken, struct dom_sid, num_sids + 6 /* over-allocate */);
138
160
NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
140
ptoken->user_sid = talloc_reference(ptoken, user_sid);
141
ptoken->group_sid = talloc_reference(ptoken, group_sid);
142
ptoken->privilege_mask = 0;
144
ptoken->sids[0] = ptoken->user_sid;
145
ptoken->sids[1] = ptoken->group_sid;
148
* Finally add the "standard" SIDs.
149
* The only difference between guest and "anonymous"
150
* is the addition of Authenticated_Users.
152
ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
153
NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
154
ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
155
NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
156
ptoken->num_sids = 4;
158
if (is_authenticated) {
159
ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
160
NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
164
for (i = 0; i < n_groupSIDs; i++) {
162
ptoken->num_sids = 0;
164
for (i = 0; i < num_sids; i++) {
165
165
size_t check_sid_idx;
166
for (check_sid_idx = 1;
167
check_sid_idx < ptoken->num_sids;
166
for (check_sid_idx = 0;
167
check_sid_idx < ptoken->num_sids;
168
168
check_sid_idx++) {
169
if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
169
if (dom_sid_equal(&ptoken->sids[check_sid_idx], &sids[i])) {
174
174
if (check_sid_idx == ptoken->num_sids) {
175
ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
179
/* setup the privilege mask for this token */
180
status = samdb_privilege_setup(ev_ctx, lp_ctx, ptoken);
181
if (!NT_STATUS_IS_OK(status)) {
186
security_token_debug(10, ptoken);
175
ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
176
NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
178
ptoken->sids[ptoken->num_sids] = sids[i];
184
* Finally add the "standard" sids.
185
* The only difference between guest and "anonymous"
186
* is the addition of Authenticated_Users.
189
if (session_info_flags & AUTH_SESSION_INFO_DEFAULT_GROUPS) {
190
ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 2);
191
NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
193
if (!dom_sid_parse(SID_WORLD, &ptoken->sids[ptoken->num_sids])) {
194
return NT_STATUS_INTERNAL_ERROR;
198
if (!dom_sid_parse(SID_NT_NETWORK, &ptoken->sids[ptoken->num_sids])) {
199
return NT_STATUS_INTERNAL_ERROR;
204
if (session_info_flags & AUTH_SESSION_INFO_AUTHENTICATED) {
205
ptoken->sids = talloc_realloc(ptoken, ptoken->sids, struct dom_sid, ptoken->num_sids + 1);
206
NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
208
if (!dom_sid_parse(SID_NT_AUTHENTICATED_USERS, &ptoken->sids[ptoken->num_sids])) {
209
return NT_STATUS_INTERNAL_ERROR;
214
/* The caller may have requested simple privilages, for example if there isn't a local DB */
215
if (session_info_flags & AUTH_SESSION_INFO_SIMPLE_PRIVILEGES) {
216
/* Shortcuts to prevent recursion and avoid lookups */
217
if (ptoken->sids == NULL) {
218
ptoken->privilege_mask = 0;
219
} else if (security_token_is_system(ptoken)) {
220
ptoken->privilege_mask = ~0;
221
} else if (security_token_is_anonymous(ptoken)) {
222
ptoken->privilege_mask = 0;
223
} else if (security_token_has_builtin_administrators(ptoken)) {
224
ptoken->privilege_mask = ~0;
226
/* All other 'users' get a empty priv set so far */
227
ptoken->privilege_mask = 0;
230
/* setup the privilege mask for this token */
231
status = samdb_privilege_setup(lp_ctx, ptoken);
232
if (!NT_STATUS_IS_OK(status)) {
234
DEBUG(1,("Unable to access privileges database\n"));
239
security_token_debug(0, 10, ptoken);