s4-loadparm: 2nd half of lp_ to lpcfg_ conversion
[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 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-samba/ldb_wrap.h"
30 #include "lib/ldb/include/ldb.h"
31 #include "../tdb/include/tdb.h"
32 #include "../lib/util/util_tdb.h"
33 #include "../lib/util/util_ldb.h"
34 #include "librpc/gen_ndr/ndr_security.h"
35 #include "dsdb/samdb/samdb.h"
36 #include "dsdb/common/util.h"
37 #include "dsdb/common/proto.h"
38
39 /**
40  * Use a TDB to store an incrementing random seed.
41  *
42  * Initialised to the current pid, the very first time Samba starts,
43  * and incremented by one each time it is needed.  
44  * 
45  * @note Not called by systems with a working /dev/urandom.
46  */
47 static void get_rand_seed(struct tdb_wrap *secretsdb, int *new_seed) 
48 {
49         *new_seed = getpid();
50         if (secretsdb != NULL) {
51                 tdb_change_int32_atomic(secretsdb->tdb, "INFO/random_seed", new_seed, 1);
52         }
53 }
54
55 /**
56  * open up the secrets database
57  */
58 struct tdb_wrap *secrets_init(TALLOC_CTX *mem_ctx, struct loadparm_context *lp_ctx)
59 {
60         char *fname;
61         uint8_t dummy;
62         struct tdb_wrap *tdb;
63
64         fname = private_path(mem_ctx, lp_ctx, "secrets.tdb");
65
66         tdb = tdb_wrap_open(mem_ctx, fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
67
68         if (!tdb) {
69                 DEBUG(0,("Failed to open %s\n", fname));
70                 talloc_free(fname);
71                 return NULL;
72         }
73         talloc_free(fname);
74
75         /**
76          * Set a reseed function for the crypto random generator 
77          * 
78          * This avoids a problem where systems without /dev/urandom
79          * could send the same challenge to multiple clients
80          */
81         set_rand_reseed_callback((void (*) (void *, int *))get_rand_seed, tdb);
82
83         /* Ensure that the reseed is done now, while we are root, etc */
84         generate_random_buffer(&dummy, sizeof(dummy));
85
86         return tdb;
87 }
88
89 /**
90   connect to the secrets ldb
91 */
92 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx,
93                                         struct tevent_context *ev_ctx,
94                                         struct loadparm_context *lp_ctx)
95 {
96         return ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx, lpcfg_secrets_url(lp_ctx),
97                                NULL, NULL, 0);
98 }
99
100 /**
101  * Retrieve the domain SID from the secrets database.
102  * @return pointer to a SID object if the SID could be obtained, NULL otherwise
103  */
104 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
105                                        struct tevent_context *ev_ctx,
106                                        struct loadparm_context *lp_ctx,
107                                        const char *domain,
108                                        char **errstring)
109 {
110         struct ldb_context *ldb;
111         struct ldb_message *msg;
112         int ldb_ret;
113         const char *attrs[] = { "objectSid", NULL };
114         struct dom_sid *result = NULL;
115         const struct ldb_val *v;
116         enum ndr_err_code ndr_err;
117         *errstring = NULL;
118
119         ldb = secrets_db_connect(mem_ctx, ev_ctx, lp_ctx);
120         if (ldb == NULL) {
121                 DEBUG(5, ("secrets_db_connect failed\n"));
122                 return NULL;
123         }
124
125         ldb_ret = dsdb_search_one(ldb, ldb, &msg,
126                               ldb_dn_new(mem_ctx, ldb, SECRETS_PRIMARY_DOMAIN_DN),
127                               LDB_SCOPE_ONELEVEL,
128                               attrs, 0, SECRETS_PRIMARY_DOMAIN_FILTER, domain);
129
130         if (ldb_ret != LDB_SUCCESS) {
131                 *errstring = talloc_asprintf(mem_ctx, "Failed to find record for %s in %s: %s: %s", 
132                                              domain, (char *) ldb_get_opaque(ldb, "ldb_url"),
133                                              ldb_strerror(ldb_ret), ldb_errstring(ldb));
134                 return NULL;
135         }
136         v = ldb_msg_find_ldb_val(msg, "objectSid");
137         if (v == NULL) {
138                 *errstring = talloc_asprintf(mem_ctx, "Failed to find a SID on record for %s in %s", 
139                                              domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
140                 return NULL;
141         }
142         result = talloc(mem_ctx, struct dom_sid);
143         if (result == NULL) {
144                 talloc_free(ldb);
145                 return NULL;
146         }
147
148         ndr_err = ndr_pull_struct_blob(v, result, result,
149                                        (ndr_pull_flags_fn_t)ndr_pull_dom_sid);
150         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
151                 *errstring = talloc_asprintf(mem_ctx, "Failed to parse SID on record for %s in %s", 
152                                              domain, (char *) ldb_get_opaque(ldb, "ldb_url"));
153                 talloc_free(result);
154                 talloc_free(ldb);
155                 return NULL;
156         }
157
158         return result;
159 }