r26003: Split up DB_WRAP, as first step in an attempt to sanitize dependencies.
[gd/samba-autobuild/.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 "ldb_wrap.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "lib/tdb/include/tdb.h"
32 #include "lib/util/util_tdb.h"
33 #include "lib/util/util_ldb.h"
34 #include "dsdb/samdb/samdb.h"
35
36 static struct tdb_wrap *tdb;
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(int *new_seed) 
47 {
48         *new_seed = getpid();
49         if (tdb) {
50                 tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
51         }
52 }
53
54 /* close the secrets database */
55 void secrets_shutdown(void)
56 {
57        talloc_free(tdb);
58 }
59
60 /* open up the secrets database */
61 bool secrets_init(void)
62 {
63         char *fname;
64         uint8_t dummy;
65
66         if (tdb)
67                 return true;
68
69         asprintf(&fname, "%s/secrets.tdb", lp_private_dir(global_loadparm));
70
71         tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
72
73         if (!tdb) {
74                 DEBUG(0,("Failed to open %s\n", fname));
75                 SAFE_FREE(fname);
76                 return false;
77         }
78         SAFE_FREE(fname);
79
80         /**
81          * Set a reseed function for the crypto random generator 
82          * 
83          * This avoids a problem where systems without /dev/urandom
84          * could send the same challenge to multiple clients
85          */
86         set_rand_reseed_callback(get_rand_seed);
87
88         /* Ensure that the reseed is done now, while we are root, etc */
89         generate_random_buffer(&dummy, sizeof(dummy));
90
91         return true;
92 }
93
94 /*
95   connect to the schannel ldb
96 */
97 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx)
98 {
99         char *path;
100         const char *url;
101         struct ldb_context *ldb;
102         bool existed;
103         const char *init_ldif = 
104                 "dn: @ATTRIBUTES\n" \
105                 "computerName: CASE_INSENSITIVE\n" \
106                 "flatname: CASE_INSENSITIVE\n";
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         existed = file_exist(path);
119
120         /* Secrets.ldb *must* always be local.  If we call for a
121          * system_session() we will recurse */
122         ldb = ldb_wrap_connect(mem_ctx, global_loadparm, path, NULL, NULL, 0, NULL);
123         talloc_free(path);
124         if (!ldb) {
125                 return NULL;
126         }
127         
128         if (!existed) {
129                 gendb_add_ldif(ldb, init_ldif);
130         }
131
132         return ldb;
133 }
134
135 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
136                                        const char *domain)
137 {
138         struct ldb_context *ldb;
139         struct ldb_message **msgs;
140         int ldb_ret;
141         const char *attrs[] = { "objectSid", NULL };
142         struct dom_sid *result = NULL;
143
144         ldb = secrets_db_connect(mem_ctx);
145         if (ldb == NULL) {
146                 DEBUG(5, ("secrets_db_connect failed\n"));
147                 return NULL;
148         }
149
150         ldb_ret = gendb_search(ldb, ldb,
151                                ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN), 
152                                &msgs, attrs,
153                                SECRETS_PRIMARY_DOMAIN_FILTER, domain);
154
155         if (ldb_ret == -1) {
156                 DEBUG(5, ("Error searching for domain SID for %s: %s", 
157                           domain, ldb_errstring(ldb))); 
158                 talloc_free(ldb);
159                 return NULL;
160         }
161
162         if (ldb_ret == 0) {
163                 DEBUG(5, ("Did not find domain record for %s\n", domain));
164                 talloc_free(ldb);
165                 return NULL;
166         }
167
168         if (ldb_ret > 1) {
169                 DEBUG(5, ("Found more than one (%d) domain records for %s\n",
170                           ldb_ret, domain));
171                 talloc_free(ldb);
172                 return NULL;
173         }
174
175         result = samdb_result_dom_sid(mem_ctx, msgs[0], "objectSid");
176         if (result == NULL) {
177                 DEBUG(0, ("Domain object for %s does not contain a SID!\n",
178                           domain));
179                 talloc_free(ldb);
180                 return NULL;
181         }
182
183         return result;
184 }