if we are adding a new sambaAccount, make sure that we add a
[Samba.git] / source / passdb / smbpassfile.c
blobd931478839d7fe6a71e73de26fe3377513d64a23
1 /*
2 * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
4 *
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free
7 * Software Foundation; either version 2 of the License, or (at your option)
8 * any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc., 675
17 * Mass Ave, Cambridge, MA 02139, USA.
21 * This file also contains migration code to move from an old
22 * trust account password file stored in the file :
23 * ${SAMBA_HOME}/private/{domain}.{netbiosname}.mac
24 * into a record stored in the tdb ${SAMBA_HOME}/private/secrets.tdb
25 * database. JRA.
28 #include "includes.h"
30 extern pstring global_myname;
33 static int mach_passwd_lock_depth;
34 static FILE *mach_passwd_fp;
36 /***************************************************************
37 Lock an fd. Abandon after waitsecs seconds.
38 ****************************************************************/
40 static BOOL pw_file_lock(int fd, int type, int secs, int *plock_depth)
42 if (fd < 0)
43 return False;
45 if(*plock_depth == 0) {
46 if (!do_file_lock(fd, secs, type)) {
47 DEBUG(10,("pw_file_lock: locking file failed, error = %s.\n",
48 strerror(errno)));
49 return False;
53 (*plock_depth)++;
55 return True;
58 /***************************************************************
59 Unlock an fd. Abandon after waitsecs seconds.
60 ****************************************************************/
62 static BOOL pw_file_unlock(int fd, int *plock_depth)
64 BOOL ret=True;
66 if(*plock_depth == 1)
67 ret = do_file_lock(fd, 5, F_UNLCK);
69 if (*plock_depth > 0)
70 (*plock_depth)--;
72 if(!ret)
73 DEBUG(10,("pw_file_unlock: unlocking file failed, error = %s.\n",
74 strerror(errno)));
75 return ret;
78 /************************************************************************
79 Routine to get the name for an old trust account file.
80 ************************************************************************/
82 static void get_trust_account_file_name( char *domain, char *name, char *mac_file)
84 unsigned int mac_file_len;
86 /* strip the filename to the last '/' */
87 get_private_directory(mac_file);
88 pstrcat(mac_file, "/");
90 mac_file_len = strlen(mac_file);
92 if ((int)(sizeof(pstring) - mac_file_len - strlen(domain) - strlen(name) - 6) < 0) {
93 DEBUG(0,("trust_password_lock: path %s too long to add trust details.\n",
94 mac_file));
95 return;
98 pstrcat(mac_file, domain);
99 pstrcat(mac_file, ".");
100 pstrcat(mac_file, name);
101 pstrcat(mac_file, ".mac");
104 /************************************************************************
105 Routine to lock the old trust account password file for a domain.
106 As this is a function to migrate to the new secrets.tdb, we never
107 create the file here, only open it.
108 ************************************************************************/
110 static BOOL trust_password_file_lock(char *domain, char *name)
112 pstring mac_file;
114 if(mach_passwd_lock_depth == 0) {
115 int fd;
117 get_trust_account_file_name( domain, name, mac_file);
119 if ((fd = sys_open(mac_file, O_RDWR, 0)) == -1)
120 return False;
122 if((mach_passwd_fp = fdopen(fd, "w+b")) == NULL) {
123 DEBUG(0,("trust_password_lock: cannot open file %s - Error was %s.\n",
124 mac_file, strerror(errno) ));
125 return False;
128 if(!pw_file_lock(fileno(mach_passwd_fp), F_WRLCK, 60, &mach_passwd_lock_depth)) {
129 DEBUG(0,("trust_password_lock: cannot lock file %s\n", mac_file));
130 fclose(mach_passwd_fp);
131 return False;
136 return True;
139 /************************************************************************
140 Routine to unlock the old trust account password file for a domain.
141 ************************************************************************/
143 static BOOL trust_password_file_unlock(void)
145 BOOL ret = pw_file_unlock(fileno(mach_passwd_fp), &mach_passwd_lock_depth);
146 if(mach_passwd_lock_depth == 0)
147 fclose(mach_passwd_fp);
148 return ret;
151 /************************************************************************
152 Routine to delete the old trust account password file for a domain.
153 Note that this file must be locked as it is truncated before the
154 delete. This is to ensure it only gets deleted by one smbd.
155 ************************************************************************/
157 static BOOL trust_password_file_delete( char *domain, char *name )
159 pstring mac_file;
160 int ret;
162 get_trust_account_file_name( domain, name, mac_file);
163 if(sys_ftruncate(fileno(mach_passwd_fp),(SMB_OFF_T)0) == -1) {
164 DEBUG(0,("trust_password_file_delete: Failed to truncate file %s (%s)\n",
165 mac_file, strerror(errno) ));
167 ret = unlink( mac_file );
168 return (ret != -1);
171 /************************************************************************
172 Routine to get the old trust account password for a domain - to convert
173 to the new secrets.tdb entry.
174 The user of this function must have locked the trust password file.
175 ************************************************************************/
177 static BOOL get_trust_account_password_from_file( unsigned char *ret_pwd, time_t *pass_last_set_time)
179 char linebuf[256];
180 char *p;
181 int i;
182 SMB_STRUCT_STAT st;
183 linebuf[0] = '\0';
185 *pass_last_set_time = (time_t)0;
186 memset(ret_pwd, '\0', 16);
188 if(sys_fstat(fileno(mach_passwd_fp), &st) == -1) {
189 DEBUG(0,("get_trust_account_password: Failed to stat file. Error was %s.\n",
190 strerror(errno) ));
191 return False;
195 * If size is zero, another smbd has migrated this file
196 * to the secrets.tdb file, and we are in a race condition.
197 * Just ignore the file.
200 if (st.st_size == 0)
201 return False;
203 if(sys_fseek( mach_passwd_fp, (SMB_OFF_T)0, SEEK_SET) == -1) {
204 DEBUG(0,("get_trust_account_password: Failed to seek to start of file. Error was %s.\n",
205 strerror(errno) ));
206 return False;
209 fgets(linebuf, sizeof(linebuf), mach_passwd_fp);
210 if(ferror(mach_passwd_fp)) {
211 DEBUG(0,("get_trust_account_password: Failed to read password. Error was %s.\n",
212 strerror(errno) ));
213 return False;
216 if(linebuf[strlen(linebuf)-1] == '\n')
217 linebuf[strlen(linebuf)-1] = '\0';
220 * The length of the line read
221 * must be 45 bytes ( <---XXXX 32 bytes-->:TLC-12345678
224 if(strlen(linebuf) != 45) {
225 DEBUG(0,("get_trust_account_password: Malformed trust password file (wrong length \
226 - was %d, should be 45).\n", (int)strlen(linebuf)));
227 #ifdef DEBUG_PASSWORD
228 DEBUG(100,("get_trust_account_password: line = |%s|\n", linebuf));
229 #endif
230 return False;
234 * Get the hex password.
237 if (!pdb_gethexpwd((char *)linebuf, ret_pwd) || linebuf[32] != ':' ||
238 strncmp(&linebuf[33], "TLC-", 4)) {
239 DEBUG(0,("get_trust_account_password: Malformed trust password file (incorrect format).\n"));
240 #ifdef DEBUG_PASSWORD
241 DEBUG(100,("get_trust_account_password: line = |%s|\n", linebuf));
242 #endif
243 return False;
247 * Get the last changed time.
249 p = &linebuf[37];
251 for(i = 0; i < 8; i++) {
252 if(p[i] == '\0' || !isxdigit((int)p[i])) {
253 DEBUG(0,("get_trust_account_password: Malformed trust password file (no timestamp).\n"));
254 #ifdef DEBUG_PASSWORD
255 DEBUG(100,("get_trust_account_password: line = |%s|\n", linebuf));
256 #endif
257 return False;
262 * p points at 8 characters of hex digits -
263 * read into a time_t as the seconds since
264 * 1970 that the password was last changed.
267 *pass_last_set_time = (time_t)strtol(p, NULL, 16);
269 return True;
272 /************************************************************************
273 Migrate an old DOMAIN.MACINE.mac password file to the tdb secrets db.
274 ************************************************************************/
276 BOOL migrate_from_old_password_file(char *domain)
278 struct machine_acct_pass pass;
280 if (!trust_password_file_lock(domain, global_myname))
281 return True;
283 if (!get_trust_account_password_from_file( pass.hash, &pass.mod_time)) {
284 trust_password_file_unlock();
285 return False;
288 if (!secrets_store(trust_keystr(domain), (void *)&pass, sizeof(pass)))
289 return False;
291 trust_password_file_delete(domain, global_myname);
292 trust_password_file_unlock();
294 return True;