Converted to call lib/wins_srv.c:wins_srv_ip() instead of lp_wins_server()
[Samba.git] / source3 / passdb / secrets.c
blob0e98d60f4a18abdf65c29d75fd5e08820ac4c6e4
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0.
4 Samba registry functions
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
22 /* the Samba secrets database stores any generated, private information
23 such as the local SID and machine trust password */
25 #include "includes.h"
27 static TDB_CONTEXT *tdb;
29 /* open up the secrets database */
30 BOOL secrets_init(void)
32 pstring fname;
33 char *p;
35 if (tdb) return True;
37 pstrcpy(fname, lp_smb_passwd_file());
38 p = strrchr(fname, '/');
39 if(!p) return False;
41 *p = 0;
42 pstrcat(fname,"/secrets.tdb");
44 tdb = tdb_open(fname, 0, 0, O_RDWR|O_CREAT, 0600);
46 if (!tdb) {
47 DEBUG(0,("Failed to open %s\n", fname));
48 return False;
50 return True;
53 /* read a entry from the secrets database - the caller must free the result
54 if size is non-null then the size of the entry is put in there
56 void *secrets_fetch(char *key, size_t *size)
58 TDB_DATA kbuf, dbuf;
59 if (!tdb) return False;
60 kbuf.dptr = key;
61 kbuf.dsize = strlen(key);
62 dbuf = tdb_fetch(tdb, kbuf);
63 if (size) *size = dbuf.dsize;
64 return dbuf.dptr;
67 /* store a secrets entry
69 BOOL secrets_store(char *key, void *data, size_t size)
71 TDB_DATA kbuf, dbuf;
72 if (!tdb) return False;
73 kbuf.dptr = key;
74 kbuf.dsize = strlen(key);
75 dbuf.dptr = data;
76 dbuf.dsize = size;
77 return tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) == 0;
81 /* delete a secets database entry
83 BOOL secrets_delete(char *key)
85 TDB_DATA kbuf;
86 if (!tdb) return False;
87 kbuf.dptr = key;
88 kbuf.dsize = strlen(key);
89 return tdb_delete(tdb, kbuf) == 0;
92 BOOL secrets_store_domain_sid(char *domain, DOM_SID *sid)
94 fstring key;
96 slprintf(key, sizeof(key), "%s/%s", SECRETS_DOMAIN_SID, domain);
97 return secrets_store(key, sid, sizeof(DOM_SID));
100 BOOL secrets_fetch_domain_sid(char *domain, DOM_SID *sid)
102 DOM_SID *dyn_sid;
103 fstring key;
104 size_t size;
106 slprintf(key, sizeof(key), "%s/%s", SECRETS_DOMAIN_SID, domain);
107 dyn_sid = (DOM_SID *)secrets_fetch(key, &size);
109 if (dyn_sid == NULL)
110 return False;
112 if (size != sizeof(DOM_SID))
114 free(dyn_sid);
115 return False;
118 *sid = *dyn_sid;
119 free(dyn_sid);
120 return True;
124 /************************************************************************
125 form a key for fetching a domain trust password
126 ************************************************************************/
127 static char *trust_keystr(char *domain)
129 static fstring keystr;
130 slprintf(keystr,sizeof(keystr),"%s/%s", SECRETS_MACHINE_ACCT_PASS, domain);
131 return keystr;
134 /************************************************************************
135 Routine to get the trust account password for a domain.
136 The user of this function must have locked the trust password file.
137 ************************************************************************/
138 BOOL secrets_fetch_trust_account_password(char *domain, uint8 ret_pwd[16],
139 time_t *pass_last_set_time)
141 struct machine_acct_pass *pass;
142 size_t size;
144 if (!(pass = secrets_fetch(trust_keystr(domain), &size)) ||
145 size != sizeof(*pass)) return False;
147 if (pass_last_set_time) *pass_last_set_time = pass->mod_time;
148 memcpy(ret_pwd, pass->hash, 16);
149 free(pass);
150 return True;
154 /************************************************************************
155 Routine to set the trust account password for a domain.
156 ************************************************************************/
157 BOOL secrets_store_trust_account_password(char *domain, uint8 new_pwd[16])
159 struct machine_acct_pass pass;
161 pass.mod_time = time(NULL);
162 memcpy(pass.hash, new_pwd, 16);
164 return secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass));