r20806: make it possible to configure the secrets.ldb url
[bbaumbach/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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 /* the Samba secrets database stores any generated, private information
23    such as the local SID and machine trust password */
24
25 #include "includes.h"
26 #include "secrets.h"
27 #include "param/param.h"
28 #include "system/filesys.h"
29 #include "db_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 "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) {
49                 tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
50         }
51 }
52
53 /* close the secrets database */
54 void secrets_shutdown(void)
55 {
56        talloc_free(tdb);
57 }
58
59 /* open up the secrets database */
60 BOOL secrets_init(void)
61 {
62         char *fname;
63         uint8_t dummy;
64
65         if (tdb)
66                 return True;
67
68         asprintf(&fname, "%s/secrets.tdb", lp_private_dir());
69
70         tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
71
72         if (!tdb) {
73                 DEBUG(0,("Failed to open %s\n", fname));
74                 SAFE_FREE(fname);
75                 return False;
76         }
77         SAFE_FREE(fname);
78
79         /**
80          * Set a reseed function for the crypto random generator 
81          * 
82          * This avoids a problem where systems without /dev/urandom
83          * could send the same challenge to multiple clients
84          */
85         set_rand_reseed_callback(get_rand_seed);
86
87         /* Ensure that the reseed is done now, while we are root, etc */
88         generate_random_buffer(&dummy, sizeof(dummy));
89
90         return True;
91 }
92
93 /*
94   connect to the schannel ldb
95 */
96 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx)
97 {
98         char *path;
99         const char *url;
100         struct ldb_context *ldb;
101         BOOL existed;
102         const char *init_ldif = 
103                 "dn: @ATTRIBUTES\n" \
104                 "computerName: CASE_INSENSITIVE\n" \
105                 "flatname: CASE_INSENSITIVE\n";
106
107         url = lp_secrets_url();
108         if (!url || !url[0]) {
109                 return NULL;
110         }
111
112         path = private_path(mem_ctx, url);
113         if (!path) {
114                 return NULL;
115         }
116
117         existed = file_exist(path);
118
119         /* Secrets.ldb *must* always be local.  If we call for a
120          * system_session() we will recurse */
121         ldb = ldb_wrap_connect(mem_ctx, path, NULL, NULL, 0, NULL);
122         talloc_free(path);
123         if (!ldb) {
124                 return NULL;
125         }
126         
127         if (!existed) {
128                 gendb_add_ldif(ldb, init_ldif);
129         }
130
131         return ldb;
132 }
133
134 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
135                                        const char *domain)
136 {
137         struct ldb_context *ldb;
138         struct ldb_message **msgs;
139         int ldb_ret;
140         const char *attrs[] = { "objectSid", NULL };
141         struct dom_sid *result = NULL;
142
143         ldb = secrets_db_connect(mem_ctx);
144         if (ldb == NULL) {
145                 DEBUG(5, ("secrets_db_connect failed\n"));
146                 return NULL;
147         }
148
149         ldb_ret = gendb_search(ldb, ldb,
150                                ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN), 
151                                &msgs, attrs,
152                                SECRETS_PRIMARY_DOMAIN_FILTER, domain);
153
154         if (ldb_ret == -1) {
155                 DEBUG(5, ("Error searching for domain SID for %s: %s", 
156                           domain, ldb_errstring(ldb))); 
157                 talloc_free(ldb);
158                 return NULL;
159         }
160
161         if (ldb_ret == 0) {
162                 DEBUG(5, ("Did not find domain record for %s\n", domain));
163                 talloc_free(ldb);
164                 return NULL;
165         }
166
167         if (ldb_ret > 1) {
168                 DEBUG(5, ("Found more than one (%d) domain records for %s\n",
169                           ldb_ret, domain));
170                 talloc_free(ldb);
171                 return NULL;
172         }
173
174         result = samdb_result_dom_sid(mem_ctx, msgs[0], "objectSid");
175         if (result == NULL) {
176                 DEBUG(0, ("Domain object for %s does not contain a SID!\n",
177                           domain));
178                 talloc_free(ldb);
179                 return NULL;
180         }
181
182         return result;
183 }