📄 pdb_smbpasswd.c
字号:
/* * Unix SMB/CIFS implementation. * SMB parameters and setup * Copyright (C) Andrew Tridgell 1992-1998 * Modified by Jeremy Allison 1995. * Modified by Gerald (Jerry) Carter 2000-2001,2003 * Modified by Andrew Bartlett 2002. * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 675 * Mass Ave, Cambridge, MA 02139, USA. */#include "includes.h"#undef DBGC_CLASS#define DBGC_CLASS DBGC_PASSDB/* smb_passwd is analogous to sam_passwd used everywhere else. However, smb_passwd is limited to the information stored by an smbpasswd entry */ struct smb_passwd{ uint32 smb_userid; /* this is actually the unix uid_t */ const char *smb_name; /* username string */ const unsigned char *smb_passwd; /* Null if no password */ const unsigned char *smb_nt_passwd; /* Null if no password */ uint16 acct_ctrl; /* account info (ACB_xxxx bit-mask) */ time_t pass_last_set_time; /* password last set time */};struct smbpasswd_privates{ /* used for maintain locks on the smbpasswd file */ int pw_file_lock_depth; /* Global File pointer */ FILE *pw_file; /* formerly static variables */ struct smb_passwd pw_buf; pstring user_name; unsigned char smbpwd[16]; unsigned char smbntpwd[16]; /* retrive-once info */ const char *smbpasswd_file;};enum pwf_access_type { PWF_READ, PWF_UPDATE, PWF_CREATE };/*************************************************************** Lock an fd. Abandon after waitsecs seconds.****************************************************************/static BOOL pw_file_lock(int fd, int type, int secs, int *plock_depth){ if (fd < 0) { return False; } if(*plock_depth == 0) { if (!do_file_lock(fd, secs, type)) { DEBUG(10,("pw_file_lock: locking file failed, error = %s.\n", strerror(errno))); return False; } } (*plock_depth)++; return True;}/*************************************************************** Unlock an fd. Abandon after waitsecs seconds.****************************************************************/static BOOL pw_file_unlock(int fd, int *plock_depth){ BOOL ret=True; if (fd == 0 || *plock_depth == 0) { return True; } if(*plock_depth == 1) { ret = do_file_lock(fd, 5, F_UNLCK); } if (*plock_depth > 0) { (*plock_depth)--; } if(!ret) { DEBUG(10,("pw_file_unlock: unlocking file failed, error = %s.\n", strerror(errno))); } return ret;}/************************************************************** Intialize a smb_passwd struct *************************************************************/static void pdb_init_smb(struct smb_passwd *user){ if (user == NULL) return; ZERO_STRUCTP (user); user->pass_last_set_time = (time_t)0;}/*************************************************************** Internal fn to enumerate the smbpasswd list. Returns a void pointer to ensure no modification outside this module. Checks for atomic rename of smbpasswd file on update or create once the lock has been granted to prevent race conditions. JRA.****************************************************************/static FILE *startsmbfilepwent(const char *pfile, enum pwf_access_type type, int *lock_depth){ FILE *fp = NULL; const char *open_mode = NULL; int race_loop = 0; int lock_type = F_RDLCK; if (!*pfile) { DEBUG(0, ("startsmbfilepwent: No SMB password file set\n")); return (NULL); } switch(type) { case PWF_READ: open_mode = "rb"; lock_type = F_RDLCK; break; case PWF_UPDATE: open_mode = "r+b"; lock_type = F_WRLCK; break; case PWF_CREATE: /* * Ensure atomic file creation. */ { int i, fd = -1; for(i = 0; i < 5; i++) { if((fd = sys_open(pfile, O_CREAT|O_TRUNC|O_EXCL|O_RDWR, 0600))!=-1) { break; } sys_usleep(200); /* Spin, spin... */ } if(fd == -1) { DEBUG(0,("startsmbfilepwent_internal: too many race conditions \creating file %s\n", pfile)); return NULL; } close(fd); open_mode = "r+b"; lock_type = F_WRLCK; break; } } for(race_loop = 0; race_loop < 5; race_loop++) { DEBUG(10, ("startsmbfilepwent_internal: opening file %s\n", pfile)); if((fp = sys_fopen(pfile, open_mode)) == NULL) { /* * If smbpasswd file doesn't exist, then create new one. This helps to avoid * confusing error msg when adding user account first time. */ if (errno == ENOENT) { if ((fp = sys_fopen(pfile, "a+")) != NULL) { DEBUG(0, ("startsmbfilepwent_internal: file %s did not \exist. File successfully created.\n", pfile)); } else { DEBUG(0, ("startsmbfilepwent_internal: file %s did not \exist. Couldn't create new one. Error was: %s", pfile, strerror(errno))); return NULL; } } else { DEBUG(0, ("startsmbfilepwent_internal: unable to open file %s. \Error was: %s\n", pfile, strerror(errno))); return NULL; } } if (!pw_file_lock(fileno(fp), lock_type, 5, lock_depth)) { DEBUG(0, ("startsmbfilepwent_internal: unable to lock file %s. \Error was %s\n", pfile, strerror(errno) )); fclose(fp); return NULL; } /* * Only check for replacement races on update or create. * For read we don't mind if the data is one record out of date. */ if(type == PWF_READ) { break; } else { SMB_STRUCT_STAT sbuf1, sbuf2; /* * Avoid the potential race condition between the open and the lock * by doing a stat on the filename and an fstat on the fd. If the * two inodes differ then someone did a rename between the open and * the lock. Back off and try the open again. Only do this 5 times to * prevent infinate loops. JRA. */ if (sys_stat(pfile,&sbuf1) != 0) { DEBUG(0, ("startsmbfilepwent_internal: unable to stat file %s. \Error was %s\n", pfile, strerror(errno))); pw_file_unlock(fileno(fp), lock_depth); fclose(fp); return NULL; } if (sys_fstat(fileno(fp),&sbuf2) != 0) { DEBUG(0, ("startsmbfilepwent_internal: unable to fstat file %s. \Error was %s\n", pfile, strerror(errno))); pw_file_unlock(fileno(fp), lock_depth); fclose(fp); return NULL; } if( sbuf1.st_ino == sbuf2.st_ino) { /* No race. */ break; } /* * Race occurred - back off and try again... */ pw_file_unlock(fileno(fp), lock_depth); fclose(fp); } } if(race_loop == 5) { DEBUG(0, ("startsmbfilepwent_internal: too many race conditions opening file %s\n", pfile)); return NULL; } /* Set a buffer to do more efficient reads */ setvbuf(fp, (char *)NULL, _IOFBF, 1024); /* Make sure it is only rw by the owner */#ifdef HAVE_FCHMOD if(fchmod(fileno(fp), S_IRUSR|S_IWUSR) == -1) {#else if(chmod(pfile, S_IRUSR|S_IWUSR) == -1) {#endif DEBUG(0, ("startsmbfilepwent_internal: failed to set 0600 permissions on password file %s. \Error was %s\n.", pfile, strerror(errno) )); pw_file_unlock(fileno(fp), lock_depth); fclose(fp); return NULL; } /* We have a lock on the file. */ return fp;}/*************************************************************** End enumeration of the smbpasswd list.****************************************************************/static void endsmbfilepwent(FILE *fp, int *lock_depth){ if (!fp) { return; } pw_file_unlock(fileno(fp), lock_depth); fclose(fp); DEBUG(7, ("endsmbfilepwent_internal: closed password file.\n"));}/************************************************************************* Routine to return the next entry in the smbpasswd list. *************************************************************************/static struct smb_passwd *getsmbfilepwent(struct smbpasswd_privates *smbpasswd_state, FILE *fp){ /* Static buffers we will return. */ struct smb_passwd *pw_buf = &smbpasswd_state->pw_buf; char *user_name = smbpasswd_state->user_name; unsigned char *smbpwd = smbpasswd_state->smbpwd; unsigned char *smbntpwd = smbpasswd_state->smbntpwd; char linebuf[256]; int c; unsigned char *p; long uidval; size_t linebuf_len; char *status; if(fp == NULL) { DEBUG(0,("getsmbfilepwent: Bad password file pointer.\n")); return NULL; } pdb_init_smb(pw_buf); pw_buf->acct_ctrl = ACB_NORMAL; /* * Scan the file, a line at a time and check if the name matches. */ status = linebuf; while (status && !feof(fp)) { linebuf[0] = '\0'; status = fgets(linebuf, 256, fp); if (status == NULL && ferror(fp)) { return NULL; } /* * Check if the string is terminated with a newline - if not * then we must keep reading and discard until we get one. */ if ((linebuf_len = strlen(linebuf)) == 0) { continue; } if (linebuf[linebuf_len - 1] != '\n') { c = '\0'; while (!ferror(fp) && !feof(fp)) { c = fgetc(fp); if (c == '\n') { break; } } } else { linebuf[linebuf_len - 1] = '\0'; }#ifdef DEBUG_PASSWORD DEBUG(100, ("getsmbfilepwent: got line |%s|\n", linebuf));#endif if ((linebuf[0] == 0) && feof(fp)) { DEBUG(4, ("getsmbfilepwent: end of file reached\n")); break; } /* * The line we have should be of the form :- * * username:uid:32hex bytes:[Account type]:LCT-12345678....other flags presently * ignored.... * * or, * * username:uid:32hex bytes:32hex bytes:[Account type]:LCT-12345678....ignored.... * * if Windows NT compatible passwords are also present. * [Account type] is an ascii encoding of the type of account. * LCT-(8 hex digits) is the time_t value of the last change time. */ if (linebuf[0] == '#' || linebuf[0] == '\0') { DEBUG(6, ("getsmbfilepwent: skipping comment or blank line\n")); continue; } p = (unsigned char *) strchr_m(linebuf, ':'); if (p == NULL) { DEBUG(0, ("getsmbfilepwent: malformed password entry (no :)\n")); continue; } /* * As 256 is shorter than a pstring we don't need to check * length here - if this ever changes.... */ SMB_ASSERT(sizeof(pstring) > sizeof(linebuf)); strncpy(user_name, linebuf, PTR_DIFF(p, linebuf)); user_name[PTR_DIFF(p, linebuf)] = '\0'; /* Get smb uid. */ p++; /* Go past ':' */ if(*p == '-') { DEBUG(0, ("getsmbfilepwent: user name %s has a negative uid.\n", user_name)); continue; } if (!isdigit(*p)) { DEBUG(0, ("getsmbfilepwent: malformed password entry for user %s (uid not number)\n", user_name)); continue; } uidval = atoi((char *) p); while (*p && isdigit(*p)) { p++; } if (*p != ':') { DEBUG(0, ("getsmbfilepwent: malformed password entry for user %s (no : after uid)\n", user_name)); continue; } pw_buf->smb_name = user_name; pw_buf->smb_userid = uidval; /* * Now get the password value - this should be 32 hex digits * which are the ascii representations of a 16 byte string. * Get two at a time and put them into the password. */ /* Skip the ':' */ p++; if (linebuf_len < (PTR_DIFF(p, linebuf) + 33)) { DEBUG(0, ("getsmbfilepwent: malformed password entry for user %s (passwd too short)\n", user_name )); continue; } if (p[32] != ':') { DEBUG(0, ("getsmbfilepwent: malformed password entry for user %s (no terminating :)\n", user_name)); continue; } if (strnequal((char *) p, "NO PASSWORD", 11)) { pw_buf->smb_passwd = NULL; pw_buf->acct_ctrl |= ACB_PWNOTREQ; } else { if (*p == '*' || *p == 'X') { /* NULL LM password */ pw_buf->smb_passwd = NULL; DEBUG(10, ("getsmbfilepwent: LM password for user %s invalidated\n", user_name)); } else if (pdb_gethexpwd((char *)p, smbpwd)) { pw_buf->smb_passwd = smbpwd; } else { pw_buf->smb_passwd = NULL; DEBUG(0, ("getsmbfilepwent: Malformed Lanman password entry for user %s \(non hex chars)\n", user_name)); } } /* * Now check if the NT compatible password is * available. */ pw_buf->smb_nt_passwd = NULL; p += 33; /* Move to the first character of the line after the lanman password. */ if ((linebuf_len >= (PTR_DIFF(p, linebuf) + 33)) && (p[32] == ':')) { if (*p != '*' && *p != 'X') { if(pdb_gethexpwd((char *)p,smbntpwd)) { pw_buf->smb_nt_passwd = smbntpwd; } } p += 33; /* Move to the first character of the line after the NT password. */ } DEBUG(5,("getsmbfilepwent: returning passwd entry for user %s, uid %ld\n", user_name, uidval)); if (*p == '[') { unsigned char *end_p = (unsigned char *)strchr_m((char *)p, ']'); pw_buf->acct_ctrl = pdb_decode_acct_ctrl((char*)p); /* Must have some account type set. */ if(pw_buf->acct_ctrl == 0) { pw_buf->acct_ctrl = ACB_NORMAL; } /* Now try and get the last change time. */ if(end_p) { p = end_p + 1; } if(*p == ':') { p++; if(*p && (StrnCaseCmp((char *)p, "LCT-", 4)==0)) { int i; p += 4; for(i = 0; i < 8; i++) { if(p[i] == '\0' || !isxdigit(p[i])) { break; } } if(i == 8) { /* * p points at 8 characters of hex digits - * read into a time_t as the seconds since * 1970 that the password was last changed. */ pw_buf->pass_last_set_time = (time_t)strtol((char *)p, NULL, 16); } } } } else { /* 'Old' style file. Fake up based on user name. */ /* * Currently trust accounts are kept in the same * password file as 'normal accounts'. If this changes * we will have to fix this code. JRA. */ if(pw_buf->smb_name[strlen(pw_buf->smb_name) - 1] == '$') { pw_buf->acct_ctrl &= ~ACB_NORMAL; pw_buf->acct_ctrl |= ACB_WSTRUST; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -