Remove special case logic in 'samdb_relative_path'.
[ira/wip.git] / source4 / dsdb / samdb / samdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    interface functions for the sam database
5
6    Copyright (C) Andrew Tridgell 2004
7    Copyright (C) Volker Lendecke 2004
8    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2006
9
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.
14    
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.
19    
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/>.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_netlogon.h"
26 #include "librpc/gen_ndr/ndr_misc.h"
27 #include "librpc/gen_ndr/ndr_security.h"
28 #include "lib/events/events.h"
29 #include "lib/ldb/include/ldb.h"
30 #include "lib/ldb/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"
36 #include "ldb_wrap.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
45 char *samdb_relative_path(struct ldb_context *ldb,
46                                  TALLOC_CTX *mem_ctx, 
47                                  const char *name) 
48 {
49         const char *base_url = 
50                 (const char *)ldb_get_opaque(ldb, "ldb_url");
51         char *path, *p, *full_name;
52         if (name == NULL) {
53                 return NULL;
54         }
55         if (strncmp("tdb://", base_url, 6) == 0) {
56                 base_url = base_url+6;
57         }
58         path = talloc_strdup(mem_ctx, base_url);
59         if (path == NULL) {
60                 return NULL;
61         }
62         if ( (p = strrchr(path, '/')) != NULL) {
63                 p[0] = '\0';
64                 full_name = talloc_asprintf(mem_ctx, "%s/%s", path, name);
65         } else {
66                 full_name = talloc_asprintf(mem_ctx, "./%s", name);
67         }
68         talloc_free(path);
69         return full_name;
70 }
71
72 /*
73   make sure the static credentials are not freed
74  */
75 static int samdb_credentials_destructor(struct cli_credentials *creds)
76 {
77         return -1;
78 }
79
80 /*
81   this returns a static set of system credentials. It is static so
82   that we always get the same pointer in ldb_wrap_connect()
83  */
84 struct cli_credentials *samdb_credentials(struct tevent_context *event_ctx, 
85                                           struct loadparm_context *lp_ctx) 
86 {
87         static struct cli_credentials *static_credentials;
88         struct cli_credentials *cred;
89
90         if (static_credentials) {
91                 return static_credentials;
92         }
93
94         cred = cli_credentials_init(talloc_autofree_context());
95         if (!cred) {
96                 return NULL;
97         }
98         cli_credentials_set_conf(cred, lp_ctx);
99
100         /* We don't want to use krb5 to talk to our samdb - recursion
101          * here would be bad, and this account isn't in the KDC
102          * anyway */
103         cli_credentials_set_kerberos_state(cred, CRED_DONT_USE_KERBEROS);
104
105         if (!NT_STATUS_IS_OK(cli_credentials_set_secrets(cred, event_ctx, lp_ctx, NULL, NULL,
106                                                          SECRETS_LDAP_FILTER))) {
107                 /* Perfectly OK - if not against an LDAP backend */
108                 talloc_free(cred);
109                 return NULL;
110         }
111         static_credentials = cred;
112         talloc_set_destructor(cred, samdb_credentials_destructor);
113         return cred;
114 }
115
116 /*
117   connect to the SAM database
118   return an opaque context pointer on success, or NULL on failure
119  */
120 struct ldb_context *samdb_connect(TALLOC_CTX *mem_ctx, 
121                                   struct tevent_context *ev_ctx,
122                                   struct loadparm_context *lp_ctx,
123                                   struct auth_session_info *session_info)
124 {
125         struct ldb_context *ldb;
126         ldb = ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx, 
127                                lp_sam_url(lp_ctx), session_info,
128                                samdb_credentials(ev_ctx, lp_ctx), 
129                                0);
130         if (!ldb) {
131                 return NULL;
132         }
133         return ldb;
134 }
135
136
137 /****************************************************************************
138  Create the SID list for this user.
139 ****************************************************************************/
140 NTSTATUS security_token_create(TALLOC_CTX *mem_ctx, 
141                                struct tevent_context *ev_ctx, 
142                                struct loadparm_context *lp_ctx,
143                                struct dom_sid *user_sid,
144                                struct dom_sid *group_sid, 
145                                int n_groupSIDs,
146                                struct dom_sid **groupSIDs, 
147                                bool is_authenticated,
148                                struct security_token **token)
149 {
150         struct security_token *ptoken;
151         int i;
152         NTSTATUS status;
153
154         ptoken = security_token_initialise(mem_ctx);
155         NT_STATUS_HAVE_NO_MEMORY(ptoken);
156
157         ptoken->sids = talloc_array(ptoken, struct dom_sid *, n_groupSIDs + 5);
158         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids);
159
160         ptoken->user_sid = talloc_reference(ptoken, user_sid);
161         ptoken->group_sid = talloc_reference(ptoken, group_sid);
162         ptoken->privilege_mask = 0;
163
164         ptoken->sids[0] = ptoken->user_sid;
165         ptoken->sids[1] = ptoken->group_sid;
166
167         /*
168          * Finally add the "standard" SIDs.
169          * The only difference between guest and "anonymous"
170          * is the addition of Authenticated_Users.
171          */
172         ptoken->sids[2] = dom_sid_parse_talloc(ptoken->sids, SID_WORLD);
173         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[2]);
174         ptoken->sids[3] = dom_sid_parse_talloc(ptoken->sids, SID_NT_NETWORK);
175         NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[3]);
176         ptoken->num_sids = 4;
177
178         if (is_authenticated) {
179                 ptoken->sids[4] = dom_sid_parse_talloc(ptoken->sids, SID_NT_AUTHENTICATED_USERS);
180                 NT_STATUS_HAVE_NO_MEMORY(ptoken->sids[4]);
181                 ptoken->num_sids++;
182         }
183
184         for (i = 0; i < n_groupSIDs; i++) {
185                 size_t check_sid_idx;
186                 for (check_sid_idx = 1; 
187                      check_sid_idx < ptoken->num_sids; 
188                      check_sid_idx++) {
189                         if (dom_sid_equal(ptoken->sids[check_sid_idx], groupSIDs[i])) {
190                                 break;
191                         }
192                 }
193
194                 if (check_sid_idx == ptoken->num_sids) {
195                         ptoken->sids[ptoken->num_sids++] = talloc_reference(ptoken->sids, groupSIDs[i]);
196                 }
197         }
198
199         /* setup the privilege mask for this token */
200         status = samdb_privilege_setup(ev_ctx, lp_ctx, ptoken);
201         if (!NT_STATUS_IS_OK(status)) {
202                 talloc_free(ptoken);
203                 return status;
204         }
205
206         security_token_debug(10, ptoken);
207
208         *token = ptoken;
209
210         return NT_STATUS_OK;
211 }