eeced9ddbe85fd0af22c45f968e0a4f6f06f8f87
[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 "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, struct loadparm_context *lp_ctx)
103 {
104         char *path;
105         const char *url;
106         struct ldb_context *ldb;
107
108         url = lp_secrets_url(lp_ctx);
109         if (!url || !url[0]) {
110                 return NULL;
111         }
112
113         path = private_path(mem_ctx, lp_ctx, 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         ldb_set_modules_dir(ldb, 
127                             talloc_asprintf(ldb, "%s/ldb", lp_modulesdir(lp_ctx)));
128
129         if (ldb_connect(ldb, path, 0, NULL) != 0) {
130                 talloc_free(path);
131                 return NULL;
132         }
133
134         talloc_free(path);
135         
136         return ldb;
137 }
138
139 /**
140  * Retrieve the domain SID from the secrets database.
141  * @return pointer to a SID object if the SID could be obtained, NULL otherwise
142  */
143 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_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
152         ldb = secrets_db_connect(mem_ctx, global_loadparm);
153         if (ldb == NULL) {
154                 DEBUG(5, ("secrets_db_connect failed\n"));
155                 return NULL;
156         }
157
158         ldb_ret = gendb_search(ldb, ldb,
159                                ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN), 
160                                &msgs, attrs,
161                                SECRETS_PRIMARY_DOMAIN_FILTER, domain);
162
163         if (ldb_ret == -1) {
164                 DEBUG(5, ("Error searching for domain SID for %s: %s", 
165                           domain, ldb_errstring(ldb))); 
166                 talloc_free(ldb);
167                 return NULL;
168         }
169
170         if (ldb_ret == 0) {
171                 DEBUG(5, ("Did not find domain record for %s\n", domain));
172                 talloc_free(ldb);
173                 return NULL;
174         }
175
176         if (ldb_ret > 1) {
177                 DEBUG(5, ("Found more than one (%d) domain records for %s\n",
178                           ldb_ret, domain));
179                 talloc_free(ldb);
180                 return NULL;
181         }
182
183         result = samdb_result_dom_sid(mem_ctx, msgs[0], "objectSid");
184         if (result == NULL) {
185                 DEBUG(0, ("Domain object for %s does not contain a SID!\n",
186                           domain));
187                 talloc_free(ldb);
188                 return NULL;
189         }
190
191         return result;
192 }