Finish removal of iconv_convenience in public API's.
[kai/samba.git] / source4 / param / secrets.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Copyright (C) Andrew Tridgell 1992-2001
4    Copyright (C) Andrew Bartlett      2002
5    Copyright (C) Rafal Szczesniak     2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /* the Samba secrets database stores any generated, private information
22    such as the local SID and machine trust password */
23
24 #include "includes.h"
25 #include "secrets.h"
26 #include "param/param.h"
27 #include "system/filesys.h"
28 #include "tdb_wrap.h"
29 #include "lib/ldb/include/ldb.h"
30 #include "../tdb/include/tdb.h"
31 #include "../lib/util/util_tdb.h"
32 #include "../lib/util/util_ldb.h"
33 #include "librpc/gen_ndr/ndr_security.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "dsdb/common/util.h"
36 #include "dsdb/common/proto.h"
37
38 /**
39  * Use a TDB to store an incrementing random seed.
40  *
41  * Initialised to the current pid, the very first time Samba starts,
42  * and incremented by one each time it is needed.  
43  * 
44  * @note Not called by systems with a working /dev/urandom.
45  */
46 static void get_rand_seed(struct tdb_wrap *secretsdb, int *new_seed) 
47 {
48         *new_seed = getpid();
49         if (secretsdb != NULL) {
50                 tdb_change_int32_atomic(secretsdb->tdb, "INFO/random_seed", new_seed, 1);
51         }
52 }
53
54 /**
55  * open up the secrets database
56  */
57 struct tdb_wrap *secrets_init(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
58 {
59         char *fname;
60         uint8_t dummy;
61         struct tdb_wrap *tdb;
62
63         fname = private_path(mem_ctx, lp_ctx, "secrets.tdb");
64
65         tdb = tdb_wrap_open(mem_ctx, fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
66
67         if (!tdb) {
68                 DEBUG(0,("Failed to open %s\n", fname));
69                 talloc_free(fname);
70                 return NULL;
71         }
72         talloc_free(fname);
73
74         /**
75          * Set a reseed function for the crypto random generator 
76          * 
77          * This avoids a problem where systems without /dev/urandom
78          * could send the same challenge to multiple clients
79          */
80         set_rand_reseed_callback((void (*) (void *, int *))get_rand_seed, tdb);
81
82         /* Ensure that the reseed is done now, while we are root, etc */
83         generate_random_buffer(&dummy, sizeof(dummy));
84
85         return tdb;
86 }
87
88 /**
89   connect to the secrets ldb
90 */
91 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx,
92                                         struct tevent_context *ev_ctx,
93                                         struct loadparm_context *lp_ctx)
94 {
95         char *path;
96         const char *url;
97         struct ldb_context *ldb;
98
99         url = lp_secrets_url(lp_ctx);
100         if (!url || !url[0]) {
101                 return NULL;
102         }
103
104         path = private_path(mem_ctx, lp_ctx, url);
105         if (!path) {
106                 return NULL;
107         }
108
109         /* Secrets.ldb *must* always be local.  If we call for a
110          * system_session() we will recurse */
111         ldb = ldb_init(mem_ctx, ev_ctx);
112         if (!ldb) {
113                 talloc_free(path);
114                 return NULL;
115         }
116
117         ldb_set_modules_dir(ldb, 
118                             talloc_asprintf(ldb, "%s/ldb", lp_modulesdir(lp_ctx)));
119
120         if (ldb_connect(ldb, path, 0, NULL) != 0) {
121                 talloc_free(path);
122                 return NULL;
123         }
124
125         /* the update_keytab module relies on this being setup */
126         if (ldb_set_opaque(ldb, "loadparm", lp_ctx) != LDB_SUCCESS) {
127                 talloc_free(path);
128                 talloc_free(ldb);
129                 return NULL;
130         }
131
132         talloc_free(path);
133         
134         return ldb;
135 }
136
137 /**
138  * Retrieve the domain SID from the secrets database.
139  * @return pointer to a SID object if the SID could be obtained, NULL otherwise
140  */
141 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
142                                        struct tevent_context *ev_ctx,
143                                        struct loadparm_context *lp_ctx,
144                                        const char *domain,
145                                        char **errstring)
146 {
147         struct ldb_context *ldb;
148         struct ldb_message *msg;
149         int ldb_ret;
150         const char *attrs[] = { "objectSid", NULL };
151         struct dom_sid *result = NULL;
152         const struct ldb_val *v;
153         enum ndr_err_code ndr_err;
154         *errstring = NULL;
155
156         ldb = secrets_db_connect(mem_ctx, ev_ctx, lp_ctx);
157         if (ldb == NULL) {
158                 DEBUG(5, ("secrets_db_connect failed\n"));
159                 return NULL;
160         }
161
162         ldb_ret = dsdb_search_one(ldb, ldb, &msg,
163                               ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN),
164                               LDB_SCOPE_ONELEVEL,
165                               attrs, 0, SECRETS_PRIMARY_DOMAIN_FILTER, domain);
166
167         if (ldb_ret != LDB_SUCCESS) {
168                 *errstring = talloc_asprintf(mem_ctx, "Failed to find record for %s in %s: %s: %s", 
169                                              domain, (char *) ldb_get_opaque(ldb, "ldb_url"),
170                                              ldb_strerror(ldb_ret), ldb_errstring(ldb));
171                 return NULL;
172         }
173         v = ldb_msg_find_ldb_val(msg, "objectSid");
174         if (v == NULL) {
175                 *errstring = talloc_asprintf(mem_ctx, "Failed to find a SID on record for %s in %s", 
176                                              domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
177                 return NULL;
178         }
179         result = talloc(mem_ctx, struct dom_sid);
180         if (result == NULL) {
181                 talloc_free(ldb);
182                 return NULL;
183         }
184
185         ndr_err = ndr_pull_struct_blob(v, result, result,
186                                        (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
187         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
188                 *errstring = talloc_asprintf(mem_ctx, "Failed to parse SID on record for %s in %s", 
189                                              domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
190                 talloc_free(result);
191                 talloc_free(ldb);
192                 return NULL;
193         }
194
195         return result;
196 }