r10491: First step towards wbinfo -t: This issues a name request for the primary
[samba.git] / source / passdb / 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 "lib/tdb/include/tdbutil.h"
27 #include "secrets.h"
28 #include "system/filesys.h"
29 #include "pstring.h"
30 #include "db_wrap.h"
31 #include "lib/ldb/include/ldb.h"
32
33 static struct tdb_wrap *tdb;
34
35 /**
36  * Use a TDB to store an incrementing random seed.
37  *
38  * Initialised to the current pid, the very first time Samba starts,
39  * and incremented by one each time it is needed.  
40  * 
41  * @note Not called by systems with a working /dev/urandom.
42  */
43 static void get_rand_seed(int *new_seed) 
44 {
45         *new_seed = getpid();
46         if (tdb) {
47                 tdb_change_int32_atomic(tdb->tdb, "INFO/random_seed", new_seed, 1);
48         }
49 }
50
51 /* close the secrets database */
52 void secrets_shutdown(void)
53 {
54         talloc_free(tdb);
55 }
56
57 /* open up the secrets database */
58 BOOL secrets_init(void)
59 {
60         pstring fname;
61         uint8_t dummy;
62
63         if (tdb)
64                 return True;
65
66         pstrcpy(fname, lp_private_dir());
67         pstrcat(fname,"/secrets.tdb");
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                 return False;
74         }
75
76         /**
77          * Set a reseed function for the crypto random generator 
78          * 
79          * This avoids a problem where systems without /dev/urandom
80          * could send the same challenge to multiple clients
81          */
82         set_rand_reseed_callback(get_rand_seed);
83
84         /* Ensure that the reseed is done now, while we are root, etc */
85         generate_random_buffer(&dummy, sizeof(dummy));
86
87         return True;
88 }
89
90 /* read a entry from the secrets database - the caller must free the result
91    if size is non-null then the size of the entry is put in there
92  */
93 static void *secrets_fetch(const char *key, size_t *size)
94 {
95         TDB_DATA kbuf, dbuf;
96         secrets_init();
97         if (!tdb)
98                 return NULL;
99         kbuf.dptr = strdup(key);
100         kbuf.dsize = strlen(key);
101         dbuf = tdb_fetch(tdb->tdb, kbuf);
102         if (size)
103                 *size = dbuf.dsize;
104         free(kbuf.dptr);
105         return dbuf.dptr;
106 }
107
108 /************************************************************************
109  Routine to fetch the plaintext machine account password for a realm
110 the password is assumed to be a null terminated ascii string
111 ************************************************************************/
112 char *secrets_fetch_machine_password(const char *domain)
113 {
114         char *key;
115         char *ret;
116         asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
117         strupper(key);
118         ret = (char *)secrets_fetch(key, NULL);
119         free(key);
120         return ret;
121 }
122
123
124 /*
125   connect to the schannel ldb
126 */
127 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx)
128 {
129         char *path;
130         struct ldb_context *ldb;
131         BOOL existed;
132         const char *init_ldif = 
133                 "dn: @ATTRIBUTES\n" \
134                 "computerName: CASE_INSENSITIVE\n" \
135                 "flatname: CASE_INSENSITIVE\n";
136
137         path = private_path(mem_ctx, "secrets.ldb");
138         if (!path) {
139                 return NULL;
140         }
141         
142         existed = file_exists(path);
143         
144         ldb = ldb_wrap_connect(mem_ctx, path, 0, NULL);
145         talloc_free(path);
146         if (!ldb) {
147                 return NULL;
148         }
149         
150         if (!existed) {
151                 gendb_add_ldif(ldb, init_ldif);
152         }
153
154         return ldb;
155 }
156
157 struct dom_sid *secrets_get_domain_sid(TALLOC_CTX *mem_ctx,
158                                        const char *domain)
159 {
160         struct ldb_context *ldb;
161         struct ldb_message **msgs;
162         int ldb_ret;
163         const char *attrs[] = { "objectSid", NULL };
164         struct dom_sid *result = NULL;
165
166         ldb = secrets_db_connect(mem_ctx);
167         if (ldb == NULL) {
168                 DEBUG(5, ("secrets_db_connect failed\n"));
169                 goto done;
170         }
171
172         ldb_ret = gendb_search(ldb, ldb,
173                                ldb_dn_explode(mem_ctx, SECRETS_PRIMARY_DOMAIN_DN), 
174                                &msgs, attrs,
175                                SECRETS_PRIMARY_DOMAIN_FILTER, domain);
176
177         if (ldb_ret == 0) {
178                 DEBUG(5, ("Did not find domain record for %s\n", domain));
179                 goto done;
180         }
181
182         if (ldb_ret > 1) {
183                 DEBUG(5, ("Found more than one (%d) domain records for %s\n",
184                           ldb_ret, domain));
185                 goto done;
186         }
187
188         result = samdb_result_dom_sid(mem_ctx, msgs[0], "objectSid");
189         if (result == NULL) {
190                 DEBUG(0, ("Domain object for %s does not contain a SID!\n",
191                           domain));
192                 goto done;
193         }
194
195  done:
196         talloc_free(ldb);
197         return result;
198 }