r23792: convert Samba4 to GPLv3
[samba.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 "db_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 "dsdb/samdb/samdb.h"
33
34 static struct tdb_wrap *tdb;
35
36 /**
37  * Use a TDB to store an incrementing random seed.
38  *
39  * Initialised to the current pid, the very first time Samba starts,
40  * and incremented by one each time it is needed.  
41  * 
42  * @note Not called by systems with a working /dev/urandom.
43  */
44 static void get_rand_seed(int *new_seed) 
45 {
46         *new_seed = getpid();
47         if (tdb) {
48                 tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
49         }
50 }
51
52 /* close the secrets database */
53 void secrets_shutdown(void)
54 {
55        talloc_free(tdb);
56 }
57
58 /* open up the secrets database */
59 BOOL secrets_init(void)
60 {
61         char *fname;
62         uint8_t dummy;
63
64         if (tdb)
65                 return True;
66
67         asprintf(&fname, "%s/secrets.tdb", lp_private_dir());
68
69         tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
70
71         if (!tdb) {
72                 DEBUG(0,("Failed to open %s\n", fname));
73                 SAFE_FREE(fname);
74                 return False;
75         }
76         SAFE_FREE(fname);
77
78         /**
79          * Set a reseed function for the crypto random generator 
80          * 
81          * This avoids a problem where systems without /dev/urandom
82          * could send the same challenge to multiple clients
83          */
84         set_rand_reseed_callback(get_rand_seed);
85
86         /* Ensure that the reseed is done now, while we are root, etc */
87         generate_random_buffer(&dummy, sizeof(dummy));
88
89         return True;
90 }
91
92 /*
93   connect to the schannel ldb
94 */
95 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx)
96 {
97         char *path;
98         const char *url;
99         struct ldb_context *ldb;
100         BOOL existed;
101         const char *init_ldif = 
102                 "dn: @ATTRIBUTES\n" \
103                 "computerName: CASE_INSENSITIVE\n" \
104                 "flatname: CASE_INSENSITIVE\n";
105
106         url = lp_secrets_url();
107         if (!url || !url[0]) {
108                 return NULL;
109         }
110
111         path = private_path(mem_ctx, url);
112         if (!path) {
113                 return NULL;
114         }
115
116         existed = file_exist(path);
117
118         /* Secrets.ldb *must* always be local.  If we call for a
119          * system_session() we will recurse */
120         ldb = ldb_wrap_connect(mem_ctx, path, NULL, NULL, 0, NULL);
121         talloc_free(path);
122         if (!ldb) {
123                 return NULL;
124         }
125         
126         if (!existed) {
127                 gendb_add_ldif(ldb, init_ldif);
128         }
129
130         return ldb;
131 }
132
133 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
134                                        const char *domain)
135 {
136         struct ldb_context *ldb;
137         struct ldb_message **msgs;
138         int ldb_ret;
139         const char *attrs[] = { "objectSid", NULL };
140         struct dom_sid *result = NULL;
141
142         ldb = secrets_db_connect(mem_ctx);
143         if (ldb == NULL) {
144                 DEBUG(5, ("secrets_db_connect failed\n"));
145                 return NULL;
146         }
147
148         ldb_ret = gendb_search(ldb, ldb,
149                                ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN), 
150                                &msgs, attrs,
151                                SECRETS_PRIMARY_DOMAIN_FILTER, domain);
152
153         if (ldb_ret == -1) {
154                 DEBUG(5, ("Error searching for domain SID for %s: %s", 
155                           domain, ldb_errstring(ldb))); 
156                 talloc_free(ldb);
157                 return NULL;
158         }
159
160         if (ldb_ret == 0) {
161                 DEBUG(5, ("Did not find domain record for %s\n", domain));
162                 talloc_free(ldb);
163                 return NULL;
164         }
165
166         if (ldb_ret > 1) {
167                 DEBUG(5, ("Found more than one (%d) domain records for %s\n",
168                           ldb_ret, domain));
169                 talloc_free(ldb);
170                 return NULL;
171         }
172
173         result = samdb_result_dom_sid(mem_ctx, msgs[0], "objectSid");
174         if (result == NULL) {
175                 DEBUG(0, ("Domain object for %s does not contain a SID!\n",
176                           domain));
177                 talloc_free(ldb);
178                 return NULL;
179         }
180
181         return result;
182 }