]> git.samba.org - abartlet/samba.git/.git/blob - source4/param/secrets.c
r26203: Avoid using ldb_wrap for secrets database.
[abartlet/samba.git/.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 "lib/tdb/include/tdb.h"
31 #include "lib/util/util_tdb.h"
32 #include "lib/util/util_ldb.h"
33 #include "dsdb/samdb/samdb.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(void)
65 {
66         char *fname;
67         uint8_t dummy;
68
69         if (tdb != NULL)
70                 return true;
71
72         fname = private_path(NULL, global_loadparm,
73                                                  "secrets.tdb");
74
75         tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT, 
76                                                 O_RDWR|O_CREAT, 0600);
77
78         if (!tdb) {
79                 DEBUG(0,("Failed to open %s\n", fname));
80                 talloc_free(fname);
81                 return false;
82         }
83         talloc_free(fname);
84
85         /**
86          * Set a reseed function for the crypto random generator 
87          * 
88          * This avoids a problem where systems without /dev/urandom
89          * could send the same challenge to multiple clients
90          */
91         set_rand_reseed_callback(get_rand_seed);
92
93         /* Ensure that the reseed is done now, while we are root, etc */
94         generate_random_buffer(&dummy, sizeof(dummy));
95
96         return true;
97 }
98
99 /**
100   connect to the secrets ldb
101 */
102 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx)
103 {
104         char *path;
105         const char *url;
106         struct ldb_context *ldb;
107
108         url = lp_secrets_url(global_loadparm);
109         if (!url || !url[0]) {
110                 return NULL;
111         }
112
113         path = private_path(mem_ctx, global_loadparm, url);
114         if (!path) {
115                 return NULL;
116         }
117
118         /* Secrets.ldb *must* always be local.  If we call for a
119          * system_session() we will recurse */
120         ldb = ldb_init(mem_ctx);
121         if (!ldb) {
122                 talloc_free(path);
123                 return NULL;
124         }
125
126         if (ldb_connect(ldb, path, 0, NULL) != 0) {
127                 talloc_free(path);
128                 return NULL;
129         }
130
131         talloc_free(path);
132         
133         return ldb;
134 }
135
136 /**
137  * Retrieve the domain SID from the secrets database.
138  * @return pointer to a SID object if the SID could be obtained, NULL otherwise
139  */
140 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
141                                        const char *domain)
142 {
143         struct ldb_context *ldb;
144         struct ldb_message **msgs;
145         int ldb_ret;
146         const char *attrs[] = { "objectSid", NULL };
147         struct dom_sid *result = NULL;
148
149         ldb = secrets_db_connect(mem_ctx);
150         if (ldb == NULL) {
151                 DEBUG(5, ("secrets_db_connect failed\n"));
152                 return NULL;
153         }
154
155         ldb_ret = gendb_search(ldb, ldb,
156                                ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN), 
157                                &msgs, attrs,
158                                SECRETS_PRIMARY_DOMAIN_FILTER, domain);
159
160         if (ldb_ret == -1) {
161                 DEBUG(5, ("Error searching for domain SID for %s: %s", 
162                           domain, ldb_errstring(ldb))); 
163                 talloc_free(ldb);
164                 return NULL;
165         }
166
167         if (ldb_ret == 0) {
168                 DEBUG(5, ("Did not find domain record for %s\n", domain));
169                 talloc_free(ldb);
170                 return NULL;
171         }
172
173         if (ldb_ret > 1) {
174                 DEBUG(5, ("Found more than one (%d) domain records for %s\n",
175                           ldb_ret, domain));
176                 talloc_free(ldb);
177                 return NULL;
178         }
179
180         result = samdb_result_dom_sid(mem_ctx, msgs[0], "objectSid");
181         if (result == NULL) {
182                 DEBUG(0, ("Domain object for %s does not contain a SID!\n",
183                           domain));
184                 talloc_free(ldb);
185                 return NULL;
186         }
187
188         return result;
189 }