2 Unix SMB/CIFS implementation.
4 interface functions for the sam database
6 Copyright (C) Andrew Tridgell 2004
7 Copyright (C) Volker Lendecke 2004
8 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27 #include "lib/events/events.h"
28 #include "lib/ldb-samba/ldb_wrap.h"
30 #include <ldb_errors.h>
31 #include "libcli/security/security.h"
32 #include "libcli/auth/libcli_auth.h"
33 #include "libcli/ldap/ldap_ndr.h"
34 #include "system/time.h"
35 #include "system/filesys.h"
37 #include "../lib/util/util_ldb.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "../libds/common/flags.h"
40 #include "param/param.h"
41 #include "lib/events/events.h"
42 #include "auth/credentials/credentials.h"
43 #include "param/secrets.h"
44 #include "auth/auth.h"
47 connect to the SAM database specified by URL
48 return an opaque context pointer on success, or NULL on failure
50 struct ldb_context
*samdb_connect_url(TALLOC_CTX
*mem_ctx
,
51 struct tevent_context
*ev_ctx
,
52 struct loadparm_context
*lp_ctx
,
53 struct auth_session_info
*session_info
,
54 unsigned int flags
, const char *url
)
56 struct ldb_context
*ldb
;
59 ldb
= ldb_wrap_find(url
, ev_ctx
, lp_ctx
, session_info
, NULL
, flags
);
61 return talloc_reference(mem_ctx
, ldb
);
63 ldb
= samba_ldb_init(mem_ctx
, ev_ctx
, lp_ctx
, session_info
, NULL
);
68 dsdb_set_global_schema(ldb
);
70 ret
= samba_ldb_connect(ldb
, lp_ctx
, url
, flags
);
71 if (ret
!= LDB_SUCCESS
) {
76 if (!ldb_wrap_add(url
, ev_ctx
, lp_ctx
, session_info
, NULL
, flags
, ldb
)) {
86 connect to the SAM database
87 return an opaque context pointer on success, or NULL on failure
89 struct ldb_context
*samdb_connect(TALLOC_CTX
*mem_ctx
,
90 struct tevent_context
*ev_ctx
,
91 struct loadparm_context
*lp_ctx
,
92 struct auth_session_info
*session_info
,
95 return samdb_connect_url(mem_ctx
, ev_ctx
, lp_ctx
, session_info
, flags
, "sam.ldb");
98 /****************************************************************************
99 Create the SID list for this user.
100 ****************************************************************************/
101 NTSTATUS
security_token_create(TALLOC_CTX
*mem_ctx
,
102 struct loadparm_context
*lp_ctx
,
103 unsigned int num_sids
,
104 struct dom_sid
*sids
,
105 uint32_t session_info_flags
,
106 struct security_token
**token
)
108 struct security_token
*ptoken
;
112 ptoken
= security_token_initialise(mem_ctx
);
113 NT_STATUS_HAVE_NO_MEMORY(ptoken
);
115 ptoken
->sids
= talloc_array(ptoken
, struct dom_sid
, num_sids
+ 6 /* over-allocate */);
116 NT_STATUS_HAVE_NO_MEMORY(ptoken
->sids
);
118 ptoken
->num_sids
= 0;
120 for (i
= 0; i
< num_sids
; i
++) {
121 size_t check_sid_idx
;
122 for (check_sid_idx
= 0;
123 check_sid_idx
< ptoken
->num_sids
;
125 if (dom_sid_equal(&ptoken
->sids
[check_sid_idx
], &sids
[i
])) {
130 if (check_sid_idx
== ptoken
->num_sids
) {
131 ptoken
->sids
= talloc_realloc(ptoken
, ptoken
->sids
, struct dom_sid
, ptoken
->num_sids
+ 1);
132 NT_STATUS_HAVE_NO_MEMORY(ptoken
->sids
);
134 ptoken
->sids
[ptoken
->num_sids
] = sids
[i
];
139 /* The caller may have requested simple privilages, for example if there isn't a local DB */
140 if (session_info_flags
& AUTH_SESSION_INFO_SIMPLE_PRIVILEGES
) {
141 /* Shortcuts to prevent recursion and avoid lookups */
142 if (ptoken
->sids
== NULL
) {
143 ptoken
->privilege_mask
= 0;
144 } else if (security_token_is_system(ptoken
)) {
145 ptoken
->privilege_mask
= ~0;
146 } else if (security_token_is_anonymous(ptoken
)) {
147 ptoken
->privilege_mask
= 0;
148 } else if (security_token_has_builtin_administrators(ptoken
)) {
149 ptoken
->privilege_mask
= ~0;
151 /* All other 'users' get a empty priv set so far */
152 ptoken
->privilege_mask
= 0;
155 /* setup the privilege mask for this token */
156 status
= samdb_privilege_setup(lp_ctx
, ptoken
);
157 if (!NT_STATUS_IS_OK(status
)) {
159 DEBUG(1,("Unable to access privileges database\n"));
164 security_token_debug(0, 10, ptoken
);