3 * Unix SMB/Netbios implementation. Version 1.9. SMB parameters and setup
4 * Copyright (C) Andrew Tridgell 1992-1995 Modified by Jeremy Allison 1995.
6 * This program is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc., 675
18 * Mass Ave, Cambridge, MA 02139, USA.
24 extern int DEBUGLEVEL;
35 do_pw_lock(int fd, int waitsecs, int type)
41 signal(SIGALRM, SIGNAL_CAST gotalarm_sig);
44 lock.l_whence = SEEK_SET;
50 ret = fcntl(fd, F_SETLKW, &lock);
52 signal(SIGALRM, SIGNAL_CAST SIG_DFL);
55 DEBUG(0, ("do_pw_lock: failed to %s SMB passwd file.\n",
56 type == F_UNLCK ? "unlock" : "lock"));
63 pw_file_lock(char *name, int type, int secs)
65 int fd = open(name, O_RDWR | O_CREAT, 0666);
68 if (do_pw_lock(fd, secs, type)) {
76 pw_file_unlock(int fd)
78 do_pw_lock(fd, 5, F_UNLCK);
83 * Routine to get the next 32 hex characters and turn them
84 * into a 16 byte array.
87 static int gethexpwd(char *p, char *pwd)
90 unsigned char lonybble, hinybble;
91 char *hexchars = "0123456789ABCDEF";
94 for (i = 0; i < 32; i += 2) {
95 hinybble = toupper(p[i]);
96 lonybble = toupper(p[i + 1]);
98 p1 = strchr(hexchars, hinybble);
99 p2 = strchr(hexchars, lonybble);
102 hinybble = PTR_DIFF(p1, hexchars);
103 lonybble = PTR_DIFF(p2, hexchars);
105 pwd[i / 2] = (hinybble << 4) | lonybble;
111 * Routine to search the smbpasswd file for an entry matching the username.
114 get_smbpwnam(char *name)
116 /* Static buffers we will return. */
117 static struct smb_passwd pw_buf;
118 static pstring user_name;
119 static unsigned char smbpwd[16];
120 static unsigned char smbntpwd[16];
122 char readbuf[16 * 1024];
129 char *pfile = lp_smb_passwd_file();
132 DEBUG(0, ("No SMB password file set\n"));
135 DEBUG(10, ("get_smbpwnam: opening file %s\n", pfile));
137 fp = fopen(pfile, "r");
140 DEBUG(0, ("get_smbpwnam: unable to open file %s\n", pfile));
143 /* Set a 16k buffer to do more efficient reads */
144 setvbuf(fp, readbuf, _IOFBF, sizeof(readbuf));
146 if ((lockfd = pw_file_lock(pfile, F_RDLCK, 5)) < 0) {
147 DEBUG(0, ("get_smbpwnam: unable to lock file %s\n", pfile));
151 /* make sure it is only rw by the owner */
154 /* We have a read lock on the file. */
156 * Scan the file, a line at a time and check if the name matches.
161 fgets(linebuf, 256, fp);
164 pw_file_unlock(lockfd);
168 * Check if the string is terminated with a newline - if not
169 * then we must keep reading and discard until we get one.
171 linebuf_len = strlen(linebuf);
172 if (linebuf[linebuf_len - 1] != '\n') {
174 while (!ferror(fp) && !feof(fp)) {
180 linebuf[linebuf_len - 1] = '\0';
182 #ifdef DEBUG_PASSWORD
183 DEBUG(100, ("get_smbpwnam: got line |%s|\n", linebuf));
185 if ((linebuf[0] == 0) && feof(fp)) {
186 DEBUG(4, ("get_smbpwnam: end of file reached\n"));
190 * The line we have should be of the form :-
192 * username:uid:[32hex bytes]:....other flags presently
197 * username:uid:[32hex bytes]:[32hex bytes]:....ignored....
199 * if Windows NT compatible passwords are also present.
202 if (linebuf[0] == '#' || linebuf[0] == '\0') {
203 DEBUG(6, ("get_smbpwnam: skipping comment or blank line\n"));
206 p = (unsigned char *) strchr(linebuf, ':');
208 DEBUG(0, ("get_smbpwnam: malformed password entry (no :)\n"));
212 * As 256 is shorter than a pstring we don't need to check
213 * length here - if this ever changes....
215 strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
216 user_name[PTR_DIFF(p, linebuf)] = '\0';
217 if (!strequal(user_name, name))
220 /* User name matches - get uid and password */
221 p++; /* Go past ':' */
223 DEBUG(0, ("get_smbpwnam: malformed password entry (uid not number)\n"));
225 pw_file_unlock(lockfd);
228 uidval = atoi((char *) p);
229 while (*p && isdigit(*p))
232 DEBUG(0, ("get_smbpwnam: malformed password entry (no : after uid)\n"));
234 pw_file_unlock(lockfd);
238 * Now get the password value - this should be 32 hex digits
239 * which are the ascii representations of a 16 byte string.
240 * Get two at a time and put them into the password.
243 if (*p == '*' || *p == 'X') {
244 /* Password deliberately invalid - end here. */
245 DEBUG(10, ("get_smbpwnam: entry invalidated for user %s\n", user_name));
247 pw_file_unlock(lockfd);
250 if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
251 DEBUG(0, ("get_smbpwnam: malformed password entry (passwd too short)\n"));
253 pw_file_unlock(lockfd);
257 DEBUG(0, ("get_smbpwnam: malformed password entry (no terminating :)\n"));
259 pw_file_unlock(lockfd);
262 if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
263 pw_buf.smb_passwd = NULL;
265 if(!gethexpwd((char *)p,(char *)smbpwd)) {
266 DEBUG(0, ("Malformed Lanman password entry (non hex chars)\n"));
268 pw_file_unlock(lockfd);
271 pw_buf.smb_passwd = smbpwd;
273 pw_buf.smb_name = user_name;
274 pw_buf.smb_userid = uidval;
275 pw_buf.smb_nt_passwd = NULL;
277 /* Now check if the NT compatible password is
279 p += 33; /* Move to the first character of the line after
280 the lanman password. */
281 if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) {
282 if (*p != '*' && *p != 'X') {
283 if(gethexpwd((char *)p,(char *)smbntpwd))
284 pw_buf.smb_nt_passwd = smbntpwd;
289 pw_file_unlock(lockfd);
290 DEBUG(5, ("get_smbpwname: returning passwd entry for user %s, uid %d\n",
296 pw_file_unlock(lockfd);
303 } /* To avoid compiler complaints */