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"));
61 int pw_file_lock(char *name, int type, int secs)
63 int fd = open(name, O_RDWR | O_CREAT, 0666);
66 if (do_pw_lock(fd, secs, type)) {
73 int pw_file_unlock(int fd)
75 do_pw_lock(fd, 5, F_UNLCK);
80 * Routine to get the next 32 hex characters and turn them
81 * into a 16 byte array.
84 static int gethexpwd(char *p, char *pwd)
87 unsigned char lonybble, hinybble;
88 char *hexchars = "0123456789ABCDEF";
91 for (i = 0; i < 32; i += 2) {
92 hinybble = toupper(p[i]);
93 lonybble = toupper(p[i + 1]);
95 p1 = strchr(hexchars, hinybble);
96 p2 = strchr(hexchars, lonybble);
99 hinybble = PTR_DIFF(p1, hexchars);
100 lonybble = PTR_DIFF(p2, hexchars);
102 pwd[i / 2] = (hinybble << 4) | lonybble;
108 * Routine to search the smbpasswd file for an entry matching the username.
110 struct smb_passwd *get_smbpwnam(char *name)
112 /* Static buffers we will return. */
113 static struct smb_passwd pw_buf;
114 static pstring user_name;
115 static unsigned char smbpwd[16];
116 static unsigned char smbntpwd[16];
118 char readbuf[16 * 1024];
125 char *pfile = lp_smb_passwd_file();
128 DEBUG(0, ("No SMB password file set\n"));
131 DEBUG(10, ("get_smbpwnam: opening file %s\n", pfile));
133 fp = fopen(pfile, "r");
136 DEBUG(0, ("get_smbpwnam: unable to open file %s\n", pfile));
139 /* Set a 16k buffer to do more efficient reads */
140 setvbuf(fp, readbuf, _IOFBF, sizeof(readbuf));
142 if ((lockfd = pw_file_lock(pfile, F_RDLCK, 5)) < 0) {
143 DEBUG(0, ("get_smbpwnam: unable to lock file %s\n", pfile));
147 /* make sure it is only rw by the owner */
150 /* We have a read lock on the file. */
152 * Scan the file, a line at a time and check if the name matches.
157 fgets(linebuf, 256, fp);
160 pw_file_unlock(lockfd);
164 * Check if the string is terminated with a newline - if not
165 * then we must keep reading and discard until we get one.
167 linebuf_len = strlen(linebuf);
168 if (linebuf[linebuf_len - 1] != '\n') {
170 while (!ferror(fp) && !feof(fp)) {
176 linebuf[linebuf_len - 1] = '\0';
178 #ifdef DEBUG_PASSWORD
179 DEBUG(100, ("get_smbpwnam: got line |%s|\n", linebuf));
181 if ((linebuf[0] == 0) && feof(fp)) {
182 DEBUG(4, ("get_smbpwnam: end of file reached\n"));
186 * The line we have should be of the form :-
188 * username:uid:[32hex bytes]:....other flags presently
193 * username:uid:[32hex bytes]:[32hex bytes]:....ignored....
195 * if Windows NT compatible passwords are also present.
198 if (linebuf[0] == '#' || linebuf[0] == '\0') {
199 DEBUG(6, ("get_smbpwnam: skipping comment or blank line\n"));
202 p = (unsigned char *) strchr(linebuf, ':');
204 DEBUG(0, ("get_smbpwnam: malformed password entry (no :)\n"));
208 * As 256 is shorter than a pstring we don't need to check
209 * length here - if this ever changes....
211 strncpy(user_name, linebuf, PTR_DIFF(p, linebuf));
212 user_name[PTR_DIFF(p, linebuf)] = '\0';
213 if (!strequal(user_name, name))
216 /* User name matches - get uid and password */
217 p++; /* Go past ':' */
219 DEBUG(0, ("get_smbpwnam: malformed password entry (uid not number)\n"));
221 pw_file_unlock(lockfd);
224 uidval = atoi((char *) p);
225 while (*p && isdigit(*p))
228 DEBUG(0, ("get_smbpwnam: malformed password entry (no : after uid)\n"));
230 pw_file_unlock(lockfd);
234 * Now get the password value - this should be 32 hex digits
235 * which are the ascii representations of a 16 byte string.
236 * Get two at a time and put them into the password.
239 if (*p == '*' || *p == 'X') {
240 /* Password deliberately invalid - end here. */
241 DEBUG(10, ("get_smbpwnam: entry invalidated for user %s\n", user_name));
243 pw_file_unlock(lockfd);
246 if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) {
247 DEBUG(0, ("get_smbpwnam: malformed password entry (passwd too short)\n"));
249 pw_file_unlock(lockfd);
253 DEBUG(0, ("get_smbpwnam: malformed password entry (no terminating :)\n"));
255 pw_file_unlock(lockfd);
258 if (!strncasecmp((char *) p, "NO PASSWORD", 11)) {
259 pw_buf.smb_passwd = NULL;
261 if(!gethexpwd((char *)p,(char *)smbpwd)) {
262 DEBUG(0, ("Malformed Lanman password entry (non hex chars)\n"));
264 pw_file_unlock(lockfd);
267 pw_buf.smb_passwd = smbpwd;
269 pw_buf.smb_name = user_name;
270 pw_buf.smb_userid = uidval;
271 pw_buf.smb_nt_passwd = NULL;
273 /* Now check if the NT compatible password is
275 p += 33; /* Move to the first character of the line after
276 the lanman password. */
277 if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) {
278 if (*p != '*' && *p != 'X') {
279 if(gethexpwd((char *)p,(char *)smbntpwd))
280 pw_buf.smb_nt_passwd = smbntpwd;
285 pw_file_unlock(lockfd);
286 DEBUG(5, ("get_smbpwname: returning passwd entry for user %s, uid %d\n",
292 pw_file_unlock(lockfd);
296 void smbpass_dummy(void)
298 } /* To avoid compiler complaints */