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.
23 extern int DEBUGLEVEL;
34 do_pw_lock(int fd, int waitsecs, int type)
40 signal(SIGALRM, SIGNAL_CAST gotalarm_sig);
43 lock.l_whence = SEEK_SET;
49 ret = fcntl(fd, F_SETLKW, &lock);
51 signal(SIGALRM, SIGNAL_CAST SIG_DFL);
54 DEBUG(0, ("do_pw_lock: failed to %s SMB passwd file.\n",
55 type == F_UNLCK ? "unlock" : "lock"));
62 pw_file_lock(char *name, int type, int secs)
64 int fd = open(name, O_RDWR | O_CREAT, 0666);
67 if (do_pw_lock(fd, secs, type)) {
75 pw_file_unlock(int fd)
77 do_pw_lock(fd, 5, F_UNLCK);
82 * Routine to get the next 32 hex characters and turn them
83 * into a 16 byte array.
86 static int gethexpwd(char *p, char *pwd)
89 unsigned char lonybble, hinybble;
90 char *hexchars = "0123456789ABCDEF";
93 for (i = 0; i < 32; i += 2) {
94 hinybble = toupper(p[i]);
95 lonybble = toupper(p[i + 1]);
97 p1 = strchr(hexchars, hinybble);
98 p2 = strchr(hexchars, lonybble);
101 hinybble = PTR_DIFF(p1, hexchars);
102 lonybble = PTR_DIFF(p2, hexchars);
104 pwd[i / 2] = (hinybble << 4) | lonybble;
110 * Routine to search the smbpasswd file for an entry matching the username.
112 struct smb_passwd *get_smbpwnam(char *name)
114 /* Static buffers we will return. */
115 static struct smb_passwd pw_buf;
116 static pstring user_name;
117 static unsigned char smbpwd[16];
118 static unsigned char smbntpwd[16];
120 char readbuf[16 * 1024];
127 char *pfile = lp_smb_passwd_file();
130 DEBUG(0, ("No SMB password file set\n"));
133 DEBUG(10, ("get_smbpwnam: opening file %s\n", pfile));
135 fp = fopen(pfile, "r");
138 DEBUG(0, ("get_smbpwnam: unable to open file %s\n", pfile));
141 /* Set a 16k buffer to do more efficient reads */
142 setvbuf(fp, readbuf, _IOFBF, sizeof(readbuf));
144 if ((lockfd = pw_file_lock(pfile, F_RDLCK, 5)) < 0) {
145 DEBUG(0, ("get_smbpwnam: unable to lock file %s\n", pfile));
149 /* make sure it is only rw by the owner */
152 /* We have a read lock on the file. */
154 * Scan the file, a line at a time and check if the name matches.
159 fgets(linebuf, 256, fp);
162 pw_file_unlock(lockfd);
166 * Check if the string is terminated with a newline - if not
167 * then we must keep reading and discard until we get one.
169 linebuf_len = strlen(linebuf);
170 if (linebuf[linebuf_len - 1] != '\n') {
172 while (!ferror(fp) && !feof(fp)) {
178 linebuf[linebuf_len - 1] = '\0';
180 #ifdef DEBUG_PASSWORD
181 DEBUG(100, ("get_smbpwnam: got line |%s|\n", linebuf));
183 if ((linebuf[0] == 0) && feof(fp)) {
184 DEBUG(4, ("get_smbpwnam: end of file reached\n"));
188 * The line we have should be of the form :-
190 * username:uid:[32hex bytes]:....other flags presently
195 * username:uid:[32hex bytes]:[32hex bytes]:....ignored....
197 * if Windows NT compatible passwords are also present.
200 if (linebuf[0] == '#' || linebuf[0] == '\0') {
201 DEBUG(6, ("get_smbpwnam: skipping comment or blank line\n"));
204 p = (unsigned char *) strchr(linebuf, ':');
206 DEBUG(0, ("get_smbpwnam: malformed password entry (no :)\n"));
210 * As 256 is shorter than a pstring we don't need to check
211 * length here - if this ever changes....
213 strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
214 user_name[PTR_DIFF(p, linebuf)] = '\0';
215 if (!strequal(user_name, name))
218 /* User name matches - get uid and password */
219 p++; /* Go past ':' */
221 DEBUG(0, ("get_smbpwnam: malformed password entry (uid not number)\n"));
223 pw_file_unlock(lockfd);
226 uidval = atoi((char *) p);
227 while (*p && isdigit(*p))
230 DEBUG(0, ("get_smbpwnam: malformed password entry (no : after uid)\n"));
232 pw_file_unlock(lockfd);
236 * Now get the password value - this should be 32 hex digits
237 * which are the ascii representations of a 16 byte string.
238 * Get two at a time and put them into the password.
241 if (*p == '*' || *p == 'X') {
242 /* Password deliberately invalid - end here. */
243 DEBUG(10, ("get_smbpwnam: entry invalidated for user %s\n", user_name));
245 pw_file_unlock(lockfd);
248 if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
249 DEBUG(0, ("get_smbpwnam: malformed password entry (passwd too short)\n"));
251 pw_file_unlock(lockfd);
255 DEBUG(0, ("get_smbpwnam: malformed password entry (no terminating :)\n"));
257 pw_file_unlock(lockfd);
260 if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
261 pw_buf.smb_passwd = NULL;
263 if(!gethexpwd((char *)p,(char *)smbpwd)) {
264 DEBUG(0, ("Malformed Lanman password entry (non hex chars)\n"));
266 pw_file_unlock(lockfd);
269 pw_buf.smb_passwd = smbpwd;
271 pw_buf.smb_name = user_name;
272 pw_buf.smb_userid = uidval;
273 pw_buf.smb_nt_passwd = NULL;
275 /* Now check if the NT compatible password is
277 p += 33; /* Move to the first character of the line after
278 the lanman password. */
279 if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) {
280 if (*p != '*' && *p != 'X') {
281 if(gethexpwd((char *)p,(char *)smbntpwd))
282 pw_buf.smb_nt_passwd = smbntpwd;
287 pw_file_unlock(lockfd);
288 DEBUG(5, ("get_smbpwname: returning passwd entry for user %s, uid %d\n",
294 pw_file_unlock(lockfd);
298 void smbpass_dummy(void)
300 } /* To avoid compiler complaints */