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;
25 static int mach_passwd_lock_depth;
26 static FILE *mach_passwd_fp;
28 /************************************************************************
29 Routine to get the name for a trust account file.
30 ************************************************************************/
32 static void get_trust_account_file_name( char *domain, char *name, char *mac_file)
34 unsigned int mac_file_len;
37 pstrcpy(mac_file, lp_smb_passwd_file());
38 p = strrchr(mac_file, '/');
42 mac_file_len = strlen(mac_file);
44 if ((int)(sizeof(pstring) - mac_file_len - strlen(domain) - strlen(name) - 6) < 0)
46 DEBUG(0,("trust_password_lock: path %s too long to add trust details.\n",
51 pstrcat(mac_file, domain);
52 pstrcat(mac_file, ".");
53 pstrcat(mac_file, name);
54 pstrcat(mac_file, ".mac");
57 /************************************************************************
58 Routine to lock the trust account password file for a domain.
59 ************************************************************************/
61 BOOL trust_password_lock( char *domain, char *name, BOOL update)
65 if(mach_passwd_lock_depth == 0) {
67 get_trust_account_file_name( domain, name, mac_file);
69 if((mach_passwd_fp = sys_fopen(mac_file, "r+b")) == NULL) {
70 if(errno == ENOENT && update) {
71 mach_passwd_fp = sys_fopen(mac_file, "w+b");
74 if(mach_passwd_fp == NULL) {
75 DEBUG(0,("trust_password_lock: cannot open file %s - Error was %s.\n",
76 mac_file, strerror(errno) ));
81 chmod(mac_file, 0600);
83 if(!file_lock(fileno(mach_passwd_fp), (update ? F_WRLCK : F_RDLCK),
84 60, &mach_passwd_lock_depth))
86 DEBUG(0,("trust_password_lock: cannot lock file %s\n", mac_file));
87 fclose(mach_passwd_fp);
96 /************************************************************************
97 Routine to unlock the trust account password file for a domain.
98 ************************************************************************/
100 BOOL trust_password_unlock(void)
102 BOOL ret = file_unlock(fileno(mach_passwd_fp), &mach_passwd_lock_depth);
103 if(mach_passwd_lock_depth == 0)
104 fclose(mach_passwd_fp);
108 /************************************************************************
109 Routine to delete the trust account password file for a domain.
110 ************************************************************************/
112 BOOL trust_password_delete( char *domain, char *name )
116 get_trust_account_file_name( domain, name, mac_file);
117 return (unlink( mac_file ) == 0);
120 /************************************************************************
121 Routine to get the trust account password for a domain.
122 The user of this function must have locked the trust password file.
123 ************************************************************************/
125 BOOL get_trust_account_password( unsigned char *ret_pwd, time_t *pass_last_set_time)
133 *pass_last_set_time = (time_t)0;
134 memset(ret_pwd, '\0', 16);
136 if(sys_fseek( mach_passwd_fp, (SMB_OFF_T)0, SEEK_SET) == -1) {
137 DEBUG(0,("get_trust_account_password: Failed to seek to start of file. Error was %s.\n",
142 fgets(linebuf, sizeof(linebuf), mach_passwd_fp);
143 if(ferror(mach_passwd_fp)) {
144 DEBUG(0,("get_trust_account_password: Failed to read password. Error was %s.\n",
149 if(linebuf[strlen(linebuf)-1] == '\n')
150 linebuf[strlen(linebuf)-1] = '\0';
153 * The length of the line read
154 * must be 45 bytes ( <---XXXX 32 bytes-->:TLC-12345678
157 if(strlen(linebuf) != 45) {
158 DEBUG(0,("get_trust_account_password: Malformed trust password file (wrong length \
159 - was %d, should be 45).\n", strlen(linebuf)));
160 #ifdef DEBUG_PASSWORD
161 DEBUG(100,("get_trust_account_password: line = |%s|\n", linebuf));
167 * Get the hex password.
170 if (!pwdb_gethexpwd((char *)linebuf, (char *)ret_pwd) || linebuf[32] != ':' ||
171 strncmp(&linebuf[33], "TLC-", 4)) {
172 DEBUG(0,("get_trust_account_password: Malformed trust password file (incorrect format).\n"));
173 #ifdef DEBUG_PASSWORD
174 DEBUG(100,("get_trust_account_password: line = |%s|\n", linebuf));
180 * Get the last changed time.
184 for(i = 0; i < 8; i++) {
185 if(p[i] == '\0' || !isxdigit((int)p[i])) {
186 DEBUG(0,("get_trust_account_password: Malformed trust password file (no timestamp).\n"));
187 #ifdef DEBUG_PASSWORD
188 DEBUG(100,("get_trust_account_password: line = |%s|\n", linebuf));
195 * p points at 8 characters of hex digits -
196 * read into a time_t as the seconds since
197 * 1970 that the password was last changed.
200 *pass_last_set_time = (time_t)strtol(p, NULL, 16);
205 /************************************************************************
206 Routine to get the trust account password for a domain.
207 The user of this function must have locked the trust password file.
208 ************************************************************************/
210 BOOL set_trust_account_password( unsigned char *md4_new_pwd)
215 if(sys_fseek( mach_passwd_fp, (SMB_OFF_T)0, SEEK_SET) == -1) {
216 DEBUG(0,("set_trust_account_password: Failed to seek to start of file. Error was %s.\n",
221 for (i = 0; i < 16; i++)
222 slprintf(&linebuf[(i*2)], sizeof(linebuf) - (i*2) - 1, "%02X", md4_new_pwd[i]);
224 slprintf(&linebuf[32], 32, ":TLC-%08X\n", (unsigned)time(NULL));
226 if(fwrite( linebuf, 1, 46, mach_passwd_fp)!= 46) {
227 DEBUG(0,("set_trust_account_password: Failed to write file. Warning - the trust \
228 account is now invalid. Please recreate. Error was %s.\n", strerror(errno) ));
232 fflush(mach_passwd_fp);
236 BOOL trust_get_passwd( unsigned char trust_passwd[16], char *domain, char *myname)
241 * Get the trust account password.
243 if(!trust_password_lock( domain, myname, False)) {
244 DEBUG(0,("domain_client_validate: unable to open the trust account password file for \
245 trust %s in domain %s.\n", myname, domain ));
249 if(get_trust_account_password( trust_passwd, &lct) == False) {
250 DEBUG(0,("domain_client_validate: unable to read the trust account password for \
251 trust %s in domain %s.\n", myname, domain ));
252 trust_password_unlock();
256 trust_password_unlock();
259 * Here we check the last change time to see if the trust
260 * password needs changing. JRA.
263 if(time(NULL) > lct + lp_machine_password_timeout())
265 global_machine_password_needs_changing = True;