r7827: Add in-memory keytab to Samba4, using the new MEMORY_WILDCARD keytab
[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
32 #undef DBGC_CLASS
33 #define DBGC_CLASS DBGC_PASSDB
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         pstring fname;
63         uint8_t dummy;
64
65         if (tdb)
66                 return True;
67
68         pstrcpy(fname, lp_private_dir());
69         pstrcat(fname,"/secrets.tdb");
70
71         tdb = tdb_wrap_open(talloc_autofree_context(), fname, 0, TDB_DEFAULT, O_RDWR|O_CREAT, 0600);
72
73         if (!tdb) {
74                 DEBUG(0,("Failed to open %s\n", fname));
75                 return False;
76         }
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 /* read a entry from the secrets database - the caller must free the result
93    if size is non-null then the size of the entry is put in there
94  */
95 static void *secrets_fetch(const char *key, size_t *size)
96 {
97         TDB_DATA kbuf, dbuf;
98         secrets_init();
99         if (!tdb)
100                 return NULL;
101         kbuf.dptr = strdup(key);
102         kbuf.dsize = strlen(key);
103         dbuf = tdb_fetch(tdb->tdb, kbuf);
104         if (size)
105                 *size = dbuf.dsize;
106         free(kbuf.dptr);
107         return dbuf.dptr;
108 }
109
110 /************************************************************************
111  Routine to fetch the plaintext machine account password for a realm
112 the password is assumed to be a null terminated ascii string
113 ************************************************************************/
114 char *secrets_fetch_machine_password(const char *domain)
115 {
116         char *key;
117         char *ret;
118         asprintf(&key, "%s/%s", SECRETS_MACHINE_PASSWORD, domain);
119         strupper(key);
120         ret = (char *)secrets_fetch(key, NULL);
121         free(key);
122         return ret;
123 }
124
125
126 /*
127   connect to the schannel ldb
128 */
129 struct ldb_context *secrets_db_connect(TALLOC_CTX *mem_ctx)
130 {
131         char *path;
132         struct ldb_context *ldb;
133         BOOL existed;
134         const char *init_ldif = 
135                 "dn: @ATTRIBUTES\n" \
136                 "computerName: CASE_INSENSITIVE\n" \
137                 "flatname: CASE_INSENSITIVE\n";
138
139         path = private_path(mem_ctx, "secrets.ldb");
140         if (!path) {
141                 return NULL;
142         }
143         
144         existed = file_exists(path);
145         
146         ldb = ldb_wrap_connect(mem_ctx, path, 0, NULL);
147         talloc_free(path);
148         if (!ldb) {
149                 return NULL;
150         }
151         
152         if (!existed) {
153                 gendb_add_ldif(ldb, init_ldif);
154         }
155
156         return ldb;
157 }
158