2 * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
3 * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
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)
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
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.
22 extern int DEBUGLEVEL;
24 BOOL global_machine_password_needs_changing = False;
26 /***************************************************************
27 Lock an fd. Abandon after waitsecs seconds.
28 ****************************************************************/
30 BOOL pw_file_lock(int fd, int type, int secs, int *plock_depth)
35 if(*plock_depth == 0) {
36 if (!do_file_lock(fd, secs, type)) {
37 DEBUG(10,("pw_file_lock: locking file failed, error = %s.\n",
48 /***************************************************************
49 Unlock an fd. Abandon after waitsecs seconds.
50 ****************************************************************/
52 BOOL pw_file_unlock(int fd, int *plock_depth)
57 ret = do_file_lock(fd, 5, F_UNLCK);
63 DEBUG(10,("pw_file_unlock: unlocking file failed, error = %s.\n",
68 static int mach_passwd_lock_depth;
69 static FILE *mach_passwd_fp;
71 /************************************************************************
72 Routine to get the name for a trust account file.
73 ************************************************************************/
75 static void get_trust_account_file_name( char *domain, char *name, char *mac_file)
77 unsigned int mac_file_len;
80 pstrcpy(mac_file, lp_smb_passwd_file());
81 p = strrchr(mac_file, '/');
85 mac_file_len = strlen(mac_file);
87 if ((int)(sizeof(pstring) - mac_file_len - strlen(domain) - strlen(name) - 6) < 0)
89 DEBUG(0,("trust_password_lock: path %s too long to add trust details.\n",
94 pstrcat(mac_file, domain);
95 pstrcat(mac_file, ".");
96 pstrcat(mac_file, name);
97 pstrcat(mac_file, ".mac");
100 /************************************************************************
101 Routine to lock the trust account password file for a domain.
102 ************************************************************************/
104 BOOL trust_password_lock( char *domain, char *name, BOOL update)
108 if(mach_passwd_lock_depth == 0) {
110 get_trust_account_file_name( domain, name, mac_file);
112 if((mach_passwd_fp = sys_fopen(mac_file, "r+b")) == NULL) {
113 if(errno == ENOENT && update) {
114 mach_passwd_fp = sys_fopen(mac_file, "w+b");
117 if(mach_passwd_fp == NULL) {
118 DEBUG(0,("trust_password_lock: cannot open file %s - Error was %s.\n",
119 mac_file, strerror(errno) ));
124 chmod(mac_file, 0600);
126 if(!pw_file_lock(fileno(mach_passwd_fp), (update ? F_WRLCK : F_RDLCK),
127 60, &mach_passwd_lock_depth))
129 DEBUG(0,("trust_password_lock: cannot lock file %s\n", mac_file));
130 fclose(mach_passwd_fp);
139 /************************************************************************
140 Routine to unlock the trust account password file for a domain.
141 ************************************************************************/
143 BOOL trust_password_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);
151 /************************************************************************
152 Routine to delete the trust account password file for a domain.
153 ************************************************************************/
155 BOOL trust_password_delete( char *domain, char *name )
159 get_trust_account_file_name( domain, name, mac_file);
160 return (unlink( mac_file ) == 0);
163 /************************************************************************
164 Routine to get the trust account password for a domain.
165 The user of this function must have locked the trust password file.
166 ************************************************************************/
168 BOOL get_trust_account_password( unsigned char *ret_pwd, time_t *pass_last_set_time)
176 *pass_last_set_time = (time_t)0;
177 memset(ret_pwd, '\0', 16);
179 if(sys_fseek( mach_passwd_fp, (SMB_OFF_T)0, SEEK_SET) == -1) {
180 DEBUG(0,("get_trust_account_password: Failed to seek to start of file. Error was %s.\n",
185 fgets(linebuf, sizeof(linebuf), mach_passwd_fp);
186 if(ferror(mach_passwd_fp)) {
187 DEBUG(0,("get_trust_account_password: Failed to read password. Error was %s.\n",
192 if(linebuf[strlen(linebuf)-1] == '\n')
193 linebuf[strlen(linebuf)-1] = '\0';
196 * The length of the line read
197 * must be 45 bytes ( <---XXXX 32 bytes-->:TLC-12345678
200 if(strlen(linebuf) != 45) {
201 DEBUG(0,("get_trust_account_password: Malformed trust password file (wrong length \
202 - was %d, should be 45).\n", (int)strlen(linebuf)));
203 #ifdef DEBUG_PASSWORD
204 DEBUG(100,("get_trust_account_password: line = |%s|\n", linebuf));
210 * Get the hex password.
213 if (!pdb_gethexpwd((char *)linebuf, ret_pwd) || linebuf[32] != ':' ||
214 strncmp(&linebuf[33], "TLC-", 4)) {
215 DEBUG(0,("get_trust_account_password: Malformed trust password file (incorrect format).\n"));
216 #ifdef DEBUG_PASSWORD
217 DEBUG(100,("get_trust_account_password: line = |%s|\n", linebuf));
223 * Get the last changed time.
227 for(i = 0; i < 8; i++) {
228 if(p[i] == '\0' || !isxdigit((int)p[i])) {
229 DEBUG(0,("get_trust_account_password: Malformed trust password file (no timestamp).\n"));
230 #ifdef DEBUG_PASSWORD
231 DEBUG(100,("get_trust_account_password: line = |%s|\n", linebuf));
238 * p points at 8 characters of hex digits -
239 * read into a time_t as the seconds since
240 * 1970 that the password was last changed.
243 *pass_last_set_time = (time_t)strtol(p, NULL, 16);
248 /************************************************************************
249 Routine to get the trust account password for a domain.
250 The user of this function must have locked the trust password file.
251 ************************************************************************/
253 BOOL set_trust_account_password( unsigned char *md4_new_pwd)
258 if(sys_fseek( mach_passwd_fp, (SMB_OFF_T)0, SEEK_SET) == -1) {
259 DEBUG(0,("set_trust_account_password: Failed to seek to start of file. Error was %s.\n",
264 for (i = 0; i < 16; i++)
265 slprintf(&linebuf[(i*2)], sizeof(linebuf) - (i*2) - 1, "%02X", md4_new_pwd[i]);
267 slprintf(&linebuf[32], 32, ":TLC-%08X\n", (unsigned)time(NULL));
269 if(fwrite( linebuf, 1, 46, mach_passwd_fp)!= 46) {
270 DEBUG(0,("set_trust_account_password: Failed to write file. Warning - the trust \
271 account is now invalid. Please recreate. Error was %s.\n", strerror(errno) ));
275 fflush(mach_passwd_fp);
279 BOOL trust_get_passwd( unsigned char trust_passwd[16], char *domain, char *myname)
284 * Get the machine account password.
286 if(!trust_password_lock( domain, myname, False)) {
287 DEBUG(0,("domain_client_validate: unable to open the machine account password file for \
288 machine %s in domain %s.\n", myname, domain ));
292 if(get_trust_account_password( trust_passwd, &lct) == False) {
293 DEBUG(0,("domain_client_validate: unable to read the machine account password for \
294 machine %s in domain %s.\n", myname, domain ));
295 trust_password_unlock();
299 trust_password_unlock();
302 * Here we check the last change time to see if the machine
303 * password needs changing. JRA.
306 if(time(NULL) > lct + lp_machine_password_timeout())
308 global_machine_password_needs_changing = True;