r26639: librpc: Pass iconv convenience on from RPC connection to NDR library, so...
[jelmer/samba4-debian.git] / source / 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 "lib/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
35 static struct tdb_wrap *tdb;
36
37 /**
38  * Use a TDB to store an incrementing random seed.
39  *
40  * Initialised to the current pid, the very first time Samba starts,
41  * and incremented by one each time it is needed.  
42  * 
43  * @note Not called by systems with a working /dev/urandom.
44  */
45 static void get_rand_seed(int *new_seed) 
46 {
47         *new_seed = getpid();
48         if (tdb != NULL) {
49                 tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
50         }
51 }
52
53 /**
54  * close the secrets database
55  */
56 void secrets_shutdown(void)
57 {
58        talloc_free(tdb);
59 }
60
61 /**
62  * open up the secrets database
63  */
64 bool secrets_init(struct loadparm_context *lp_ctx)
65 {
66         char *fname;
67         uint8_t dummy;
68
69         if (tdb != NULL)
70                 return true;
71
72         fname = private_path(NULL, lp_ctx, "secrets.tdb");
73
74         tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT, 
75                                                 O_RDWR|O_CREAT, 0600);
76
77         if (!tdb) {
78                 DEBUG(0,("Failed to open %s\n", fname));
79                 talloc_free(fname);
80                 return false;
81         }
82         talloc_free(fname);
83
84         /**
85          * Set a reseed function for the crypto random generator 
86          * 
87          * This avoids a problem where systems without /dev/urandom
88          * could send the same challenge to multiple clients
89          */
90         set_rand_reseed_callback(get_rand_seed);
91
92         /* Ensure that the reseed is done now, while we are root, etc */
93         generate_random_buffer(&dummy, sizeof(dummy));
94
95         return true;
96 }
97
98 /**
99   connect to the secrets ldb
100 */
101 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
102 {
103         char *path;
104         const char *url;
105         struct ldb_context *ldb;
106
107         url = lp_secrets_url(lp_ctx);
108         if (!url || !url[0]) {
109                 return NULL;
110         }
111
112         path = private_path(mem_ctx, lp_ctx, url);
113         if (!path) {
114                 return NULL;
115         }
116
117         /* Secrets.ldb *must* always be local.  If we call for a
118          * system_session() we will recurse */
119         ldb = ldb_init(mem_ctx);
120         if (!ldb) {
121                 talloc_free(path);
122                 return NULL;
123         }
124
125         ldb_set_modules_dir(ldb, 
126                             talloc_asprintf(ldb, "%s/ldb", lp_modulesdir(lp_ctx)));
127
128         if (ldb_connect(ldb, path, 0, NULL) != 0) {
129                 talloc_free(path);
130                 return NULL;
131         }
132
133         talloc_free(path);
134         
135         return ldb;
136 }
137
138 /**
139  * Retrieve the domain SID from the secrets database.
140  * @return pointer to a SID object if the SID could be obtained, NULL otherwise
141  */
142 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
143                                        struct loadparm_context *lp_ctx,
144                                        const char *domain)
145 {
146         struct ldb_context *ldb;
147         struct ldb_message **msgs;
148         int ldb_ret;
149         const char *attrs[] = { "objectSid", NULL };
150         struct dom_sid *result = NULL;
151         const struct ldb_val *v;
152         enum ndr_err_code ndr_err;
153
154         ldb = secrets_db_connect(mem_ctx, lp_ctx);
155         if (ldb == NULL) {
156                 DEBUG(5, ("secrets_db_connect failed\n"));
157                 return NULL;
158         }
159
160         ldb_ret = gendb_search(ldb, ldb,
161                                ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN), 
162                                &msgs, attrs,
163                                SECRETS_PRIMARY_DOMAIN_FILTER, domain);
164
165         if (ldb_ret == -1) {
166                 DEBUG(5, ("Error searching for domain SID for %s: %s", 
167                           domain, ldb_errstring(ldb))); 
168                 talloc_free(ldb);
169                 return NULL;
170         }
171
172         if (ldb_ret == 0) {
173                 DEBUG(5, ("Did not find domain record for %s\n", domain));
174                 talloc_free(ldb);
175                 return NULL;
176         }
177
178         if (ldb_ret > 1) {
179                 DEBUG(5, ("Found more than one (%d) domain records for %s\n",
180                           ldb_ret, domain));
181                 talloc_free(ldb);
182                 return NULL;
183         }
184
185         v = ldb_msg_find_ldb_val(msgs[0], "objectSid");
186         if (v == NULL) {
187                 DEBUG(0, ("Domain object for %s does not contain a SID!\n",
188                           domain));
189                 return NULL;
190         }
191         result = talloc(mem_ctx, struct dom_sid);
192         if (result == NULL) {
193                 talloc_free(ldb);
194                 return NULL;
195         }
196
197         ndr_err = ndr_pull_struct_blob(v, result, NULL, result,
198                                        (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
199         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
200                 talloc_free(result);
201                 talloc_free(ldb);
202                 return NULL;
203         }
204
205         return result;
206 }