int main(int argc, char *argv[]) { off_t who_am_i = getuid(); struct spwd *sp, *sp2; char *user = "******"; if (who_am_i != 0) { fprintf(stderr, "Only root can read shadow password database baby!\n"); exit(EXIT_FAILURE); } if ((sp = getspnam("b3h3m0th")) == NULL) { fprintf(stderr, "Err. %s reading gestpnam()\n", strerror(errno)); exit(EXIT_FAILURE); } printf(" user name: %s\n", sp->sp_namp); printf("encrypted pass: %s\n", sp->sp_pwdp); setspent(); while ((sp2 = getspent()) != NULL) { if(strcmp(user, sp2->sp_namp) == 0) { printf("User %s presente nel sistema\n", user); break; } printf("Searching...\n"); } endspent(); return(EXIT_SUCCESS); }
/* * Read /etc/shadow. */ void readshadow(void) { #ifdef SHADOW_PWD struct login *lp; struct spwd *sp; size_t sz; setspent(); while (sp = getspent()) { if ((lp = findlogin(sp->sp_namp)) == NULL) continue; sz = strlen(sp->sp_pwdp); lp->l_pwdp = salloc(sz + 1); strcpy(lp->l_pwdp, sp->sp_pwdp); lp->l_lstchg = sp->sp_lstchg; lp->l_min = sp->sp_min; lp->l_max = sp->sp_max; lp->l_warn = sp->sp_warn; lp->l_inact = sp->sp_inact; lp->l_expire = sp->sp_expire; lp->l_flag = sp->sp_flag; } endspent(); #endif /* SHADOW_PWD */ }
struct spwd * mygetspnam(const char *name){ struct spwd *sp; setspent(); while((sp=getspent()) != NULL) if(strcmp(name, sp->sp_namp) == 0) break;/*found a match*/ endspent(); return(sp); }
static VALUE rb_shadow_getspent(VALUE self) { struct spwd *entry; VALUE result; entry = getspent(); if( entry == NULL ) return Qnil; return convert_pw_struct( entry ); };
int main(void) { struct spwd *spwd; spwd = getspnam("qy"); printf("shadow password = %s\n", spwd->sp_pwdp); setspent(); while ((spwd = getspent()) != NULL) { printf("shadow password = %s\n", spwd->sp_pwdp); } endspent(); return 0; }
int main(int argc,char* argv[]){ struct spwd* user; setspent(); while((user=getspent())!=NULL){ printf("loginname:%s,password:%s\n",user->sp_namp,user->sp_pwdp); } endspent(); user=getspnam("linuxmin"); if(user!=NULL) printf("loginname:%s,password:%s\n",user->sp_namp,user->sp_pwdp); printf("......\n"); return 0; }
/* getspnam - get a shadow entry by name */ struct spwd *getspnam(const char *name) { struct spwd *sp; if (!name || !strlen(name)) return NULL; setspent(); while ((sp = getspent()) != NULL) { if (strcmp(name, sp->sp_namp) == 0) break; } endspent(); return (sp); }
int main(void) { struct spwd *ptr; setspent(); while ((ptr = getspent()) != NULL) { printf("name= %s\tshaow passwd = %s\n", ptr->sp_namp, ptr->sp_pwdp); } endspent(); return 0; }
/* getspuid - get a shadow entry by uid */ struct spwd *getspuid(uid_t uid) { struct spwd *sp; struct passwd *mypw; if ((mypw = getpwuid(getuid())) == NULL) { return (NULL); } setspent(); while ((sp = getspent()) != NULL) { if (strcmp(mypw->pw_name, sp->sp_namp) == 0) break; } endspent(); return (sp); }
// 读取/etc/shadow文件 // 执行时sudo, 才能读取到 void op_shadow() { struct spwd *sp; char name[] = "root"; // 将指针移到开始位置 setspent(); // 获取第一条记录 while((sp = getspent()) != NULL) { //if (strcmp(name, sp->pw_name) == 0) //{ // printf("pw_name:%s, pw_uid:%d, pw_shell:%s\n", ptr->pw_name, ptr->pw_uid, ptr->pw_shell); // break; //} printf("sp_namp:%s, sp_pwdp:%s, sp_expire:%ld\n", sp->sp_namp, sp->sp_pwdp, sp->sp_expire); } // 结束读取 endspent(); }
static PyObject * spwd_getspall(PyObject *self, PyObject *args) { PyObject *d; struct spwd *p; if ((d = PyList_New(0)) == NULL) return NULL; setspent(); while ((p = getspent()) != NULL) { PyObject *v = mkspent(p); if (v == NULL || PyList_Append(d, v) != 0) { Py_XDECREF(v); Py_DECREF(d); endspent(); return NULL; } Py_DECREF(v); } endspent(); return d; }
int main(void) { struct spwd *spwent; time_t when, now; (void) time(&now); while ((spwent = getspent()) != NULL) { if (spwent->sp_expire <= 0) continue; when = spwent->sp_expire * 24 * 60 * 60; if (when > now) continue; (void) printf("%s\n", spwent->sp_namp); } return 0; }
static PyObject * spwd_getspall_impl(PyObject *module) /*[clinic end generated code: output=4fda298d6bf6d057 input=b2c84b7857d622bd]*/ { PyObject *d; struct spwd *p; if ((d = PyList_New(0)) == NULL) return NULL; setspent(); while ((p = getspent()) != NULL) { PyObject *v = mkspent(p); if (v == NULL || PyList_Append(d, v) != 0) { Py_XDECREF(v); Py_DECREF(d); endspent(); return NULL; } Py_DECREF(v); } endspent(); return d; }
int main(int argc, char * argv[]) { struct spwd * sp; if(argc < 2) { printf("Usage: %s username\n", argv[0]); exit(-1); } setspent(); if((sp = getspnam(argv[1])) != NULL) { printf("%s, %s\n", sp->sp_namp, sp->sp_pwdp); } setspent(); while((sp = getspent()) != NULL) { printf("%s, %s\n", sp->sp_namp, sp->sp_pwdp); } endspent(); return 0; }
static VALUE rb_shadow_getspent(VALUE self) { struct spwd *entry; VALUE result; entry = getspent(); if( entry == NULL ) return Qnil; result = rb_struct_new(rb_sPasswdEntry, rb_tainted_str_new2(entry->sp_namp), rb_tainted_str_new2(entry->sp_pwdp), INT2FIX(entry->sp_lstchg), INT2FIX(entry->sp_min), INT2FIX(entry->sp_max), INT2FIX(entry->sp_warn), INT2FIX(entry->sp_inact), INT2FIX(entry->sp_expire), INT2FIX(entry->sp_flag), 0); return result; };
static const char* set_passwd(const char *name, const char *passwd, char **msg) { FILE *f = NULL; struct spwd *spwd, new_spwd; const char *en_passwd; /* encrypted password */ struct stat st; assert(name); assert(passwd); /* check password format */ if ((passwd[0] != '$') || (passwd[1] != '0' && passwd[1] != '1' && passwd[1] != '5' && passwd[1] != '6') || (passwd[2] != '$')) { asprintf(msg, "Wrong password format (user %s).", name); return (NULL); } if (passwd[1] == '0') { /* encrypt the password */ get_login_defs(); en_passwd = pw_encrypt(&(passwd[3]), crypt_make_salt(NULL, NULL)); } else { en_passwd = passwd; } /* * store encrypted password into shadow */ /* lock shadow file */ if (lckpwdf() != 0) { *msg = strdup("Failed to acquire shadow file lock."); return (NULL); } /* init position in shadow */ setspent(); /* open new shadow */ f = fopen(SHADOW_COPY, "w"); if (f == NULL) { asprintf(msg, "Unable to prepare shadow copy (%s).", strerror(errno)); endspent(); ulckpwdf(); return (NULL); } /* get file stat of the original file to make a nice copy of it */ stat(SHADOW_ORIG, &st); fchmod(fileno(f), st.st_mode); fchown(fileno(f), st.st_uid, st.st_gid); while ((spwd = getspent()) != NULL) { if (strcmp(spwd->sp_namp, name) == 0) { /* * we have the entry to change, * make the copy, modifying the original * structure doesn't seem as a good idea */ memcpy(&new_spwd, spwd, sizeof(struct spwd)); new_spwd.sp_pwdp = (char*) en_passwd; spwd = &new_spwd; } /* store the record into the shadow copy */ putspent(spwd, f); } endspent(); fclose(f); if (rename(SHADOW_COPY, SHADOW_ORIG) == -1) { asprintf(msg, "Unable to rewrite shadow database (%s).", strerror(errno)); unlink(SHADOW_COPY); ulckpwdf(); return (NULL); } ulckpwdf(); return (en_passwd); }
bool KUserFiles::loadpwd() { passwd *p; KU::KUser *tmpKU = 0; struct stat st; QString filename; QString passwd_filename; QString nispasswd_filename; int rc = 0; int passwd_errno = 0; int nispasswd_errno = 0; char processing_file = '\0'; #define P_PASSWD 0x01 #define P_NISPASSWD 0x02 #define MAXFILES 2 // Read KUser configuration passwd_filename = mCfg->passwdsrc(); nispasswd_filename = mCfg->nispasswdsrc(); // Handle unconfigured environments if(passwd_filename.isEmpty() && nispasswd_filename.isEmpty()) { mCfg->setPasswdsrc( PASSWORD_FILE ); mCfg->setGroupsrc( GROUP_FILE ); passwd_filename = mCfg->passwdsrc(); KMessageBox::error( 0, i18n("KUser sources were not configured.\nLocal passwd source set to %1\nLocal group source set to %2.").arg(mCfg->passwdsrc().arg(mCfg->groupsrc())) ); } if(!passwd_filename.isEmpty()) { processing_file = processing_file | P_PASSWD; filename.append(passwd_filename); } // Start reading passwd file(s) for(int i = 0; i < MAXFILES; i++) { rc = stat(QFile::encodeName(filename), &st); if(rc != 0) { KMessageBox::error( 0, i18n("Stat call on file %1 failed: %2\nCheck KUser settings.").arg(filename).arg(QString::fromLocal8Bit(strerror(errno))) ); if( (processing_file & P_PASSWD) != 0 ) { passwd_errno = errno; if(!nispasswd_filename.isEmpty()) { processing_file = processing_file & ~P_PASSWD; processing_file = processing_file | P_NISPASSWD; filename.truncate(0); filename.append(nispasswd_filename); } continue; } else{ nispasswd_errno = errno; break; } } pwd_mode = st.st_mode & 0666; pwd_uid = st.st_uid; pwd_gid = st.st_gid; // We are reading our configuration specified passwd file QString tmp; #ifdef HAVE_FGETPWENT FILE *fpwd = fopen(QFile::encodeName(filename), "r"); if(fpwd == NULL) { KMessageBox::error( 0, i18n("Error opening %1 for reading.").arg(filename) ); return FALSE; } while ((p = fgetpwent(fpwd)) != NULL) { #else setpwent(); //This should be enough for BSDs while ((p = getpwent()) != NULL) { #endif tmpKU = new KU::KUser(); tmpKU->setCaps( KU::KUser::Cap_POSIX ); tmpKU->setUID(p->pw_uid); tmpKU->setGID(p->pw_gid); tmpKU->setName(QString::fromLocal8Bit(p->pw_name)); tmp = QString::fromLocal8Bit( p->pw_passwd ); if ( tmp != "x" && tmp != "*" && !tmp.startsWith("!") ) tmpKU->setDisabled( false ); else tmpKU->setDisabled( true ); if ( tmp.startsWith("!") ) tmp.remove(0, 1); tmpKU->setPwd( tmp ); tmpKU->setHomeDir(QString::fromLocal8Bit(p->pw_dir)); tmpKU->setShell(QString::fromLocal8Bit(p->pw_shell)); #if defined(__FreeBSD__) || defined(__bsdi__) tmpKU->setClass(QString::fromLatin1(p->pw_class)); tmpKU->setLastChange(p->pw_change); tmpKU->setExpire(p->pw_expire); #endif if ((p->pw_gecos != 0) && (p->pw_gecos[0] != 0)) fillGecos(tmpKU, p->pw_gecos); mUsers.append(tmpKU); } // End reading passwd_filename #ifdef HAVE_FGETPWENT fclose(fpwd); #else endpwent(); #endif if((!nispasswd_filename.isEmpty()) && (nispasswd_filename != passwd_filename)) { processing_file = processing_file & ~P_PASSWD; processing_file = processing_file | P_NISPASSWD; filename.truncate(0); filename.append(nispasswd_filename); } else break; } // end of processing files, for loop if( (passwd_errno == 0) && (nispasswd_errno == 0) ) return (TRUE); if( (passwd_errno != 0) && (nispasswd_errno != 0) ) return (FALSE); else return(TRUE); } // Load shadow passwords bool KUserFiles::loadsdw() { #ifdef HAVE_SHADOW QString shadow_file,tmp; struct spwd *spw; KU::KUser *up = NULL; struct stat st; shadow_file = mCfg->shadowsrc(); if ( shadow_file.isEmpty() ) return TRUE; stat( QFile::encodeName(shadow_file), &st); sdw_mode = st.st_mode & 0666; sdw_uid = st.st_uid; sdw_gid = st.st_gid; #ifdef HAVE_FGETSPENT FILE *f; kdDebug() << "open shadow file: " << shadow_file << endl; if ((f = fopen( QFile::encodeName(shadow_file), "r")) == NULL) { KMessageBox::error( 0, i18n("Error opening %1 for reading.").arg(shadow_file) ); caps &= ~Cap_Shadow; return TRUE; } while ((spw = fgetspent( f ))) { // read a shadow password structure #else setspent(); while ((spw = getspent())) { // read a shadow password structure #endif kdDebug() << "shadow entry: " << spw->sp_namp << endl; if ((up = lookup(QString::fromLocal8Bit(spw->sp_namp))) == NULL) { KMessageBox::error( 0, i18n("No /etc/passwd entry for %1.\nEntry will be removed at the next `Save'-operation.").arg(QString::fromLocal8Bit(spw->sp_namp)) ); continue; } tmp = QString::fromLocal8Bit( spw->sp_pwdp ); if ( tmp.startsWith("!!") || tmp == "*" ) { up->setDisabled( true ); tmp.remove( 0, 2 ); } else up->setDisabled( false ); up->setSPwd( tmp ); // cp the encrypted pwd up->setLastChange( daysToTime( spw->sp_lstchg ) ); up->setMin(spw->sp_min); up->setMax(spw->sp_max); #ifndef _SCO_DS up->setWarn(spw->sp_warn); up->setInactive(spw->sp_inact); up->setExpire( daysToTime( spw->sp_expire ) ); up->setFlag(spw->sp_flag); #endif } #ifdef HAVE_FGETSPENT fclose(f); #else endspent(); #endif #endif // HAVE_SHADOW return TRUE; } // Save password file #define escstr(a,b) tmp2 = user->a(); \ tmp2.replace(':',"_"); \ tmp2.replace(',',"_"); \ user->b( tmp2 ); bool KUserFiles::savepwd() { FILE *passwd_fd = NULL; FILE *nispasswd_fd = NULL; uid_t minuid = 0; int nis_users_written = 0; uid_t tmp_uid = 0; QString s; QString s1; QString tmp, tmp2; QString passwd_filename; QString nispasswd_filename; char errors_found = '\0'; #define NOMINUID 0x01 #define NONISPASSWD 0x02 // Read KUser configuration info passwd_filename = mCfg->passwdsrc(); nispasswd_filename = mCfg->nispasswdsrc(); QString new_passwd_filename = passwd_filename + QString::fromLatin1(KU_CREATE_EXT); QString new_nispasswd_filename = nispasswd_filename+QString::fromLatin1(KU_CREATE_EXT); if( nispasswd_filename != passwd_filename ) { minuid = mCfg->nisminuid(); } // Backup file(s) if(!passwd_filename.isEmpty()) { if (!pw_backuped) { if (!backup(passwd_filename)) return FALSE; pw_backuped = TRUE; } } if(!nispasswd_filename.isEmpty() && (nispasswd_filename != passwd_filename)) { if (!pn_backuped) { if (!backup(nispasswd_filename)) return FALSE; pn_backuped = TRUE; } } // Open file(s) if(!passwd_filename.isEmpty()) { if ((passwd_fd = fopen(QFile::encodeName(new_passwd_filename),"w")) == NULL) KMessageBox::error( 0, i18n("Error opening %1 for writing.").arg(passwd_filename) ); } if(!nispasswd_filename.isEmpty() && (nispasswd_filename != passwd_filename)){ if ((nispasswd_fd = fopen(QFile::encodeName(new_nispasswd_filename),"w")) == NULL) KMessageBox::error( 0, i18n("Error opening %1 for writing.").arg(nispasswd_filename) ); } QPtrListIterator<KU::KUser> it( mUsers ); KU::KUser *user; bool addok = false; user = (*it); while (true) { if ( user == 0 ) { if ( addok ) break; it = QPtrListIterator<KU::KUser> ( mAdd ); user = (*it); addok = true; if ( user == 0 ) break; }; if ( mDel.containsRef( user ) ) { ++it; user = (*it); continue; } if ( mMod.contains( user ) ) user = &( mMod[ user ] ); tmp_uid = user->getUID(); if ( caps & Cap_Shadow ) tmp = "x"; else { tmp = user->getPwd(); if ( user->getDisabled() && tmp != "x" && tmp != "*" ) tmp = "!" + tmp; } escstr( getName, setName ); escstr( getHomeDir, setHomeDir ); escstr( getShell, setShell ); escstr( getName, setName ); escstr( getFullName, setFullName ); #if defined(__FreeBSD__) || defined(__bsdi__) escstr( getClass, setClass ); escstr( getOffice, setOffice ); escstr( getWorkPhone, setWorkPhone ); escstr( getHomePhone, setHomePhone ); s = user->getName() + ":" + tmp + ":" + QString::number( user->getUID() ) + ":" + QString::number( user->getGID() ) + ":" + user->getClass() + ":" + QString::number( user->getLastChange() ) + ":" + QString::number( user->getExpire() ) + ":"; s1 = user->getFullName() + "," + user->getOffice() + "," + user->getWorkPhone() + "," + user->getHomePhone(); #else escstr( getOffice1, setOffice1 ); escstr( getOffice2, setOffice2 ); escstr( getAddress, setAddress ); s = user->getName() + ":" + tmp + ":" + QString::number( user->getUID() ) + ":" + QString::number( user->getGID() ) + ":"; s1 = user->getFullName() + "," + user->getOffice1() + "," + user->getOffice2() + "," + user->getAddress(); #endif for (int j=(s1.length()-1); j>=0; j--) { if (s1[j] != ',') break; s1.truncate(j); } s += s1 + ":" + user->getHomeDir() + ":" + user->getShell() + "\n"; if( (nispasswd_fd != 0) && (minuid != 0) ) { if (minuid <= tmp_uid) { fputs(s.local8Bit().data(), nispasswd_fd); nis_users_written++; ++it; user = (*it); continue; } } if( (nispasswd_fd != 0) && (minuid == 0) ) { errors_found = errors_found | NOMINUID; } if( (nispasswd_fd == 0) && (minuid != 0) ) { errors_found = errors_found | NONISPASSWD; } kdDebug() << s << endl; fputs(s.local8Bit().data(), passwd_fd); ++it; user = (*it); } if(passwd_fd) { fclose(passwd_fd); chmod(QFile::encodeName(new_passwd_filename), pwd_mode); chown(QFile::encodeName(new_passwd_filename), pwd_uid, pwd_gid); rename(QFile::encodeName(new_passwd_filename), QFile::encodeName(passwd_filename)); } if(nispasswd_fd) { fclose(nispasswd_fd); chmod(QFile::encodeName(new_nispasswd_filename), pwd_mode); chown(QFile::encodeName(new_nispasswd_filename), pwd_uid, pwd_gid); rename(QFile::encodeName(new_nispasswd_filename), QFile::encodeName(nispasswd_filename)); } if( (errors_found & NOMINUID) != 0 ) { KMessageBox::error( 0, i18n("Unable to process NIS passwd file without a minimum UID specified.\nPlease update KUser settings (Files).") ); } if( (errors_found & NONISPASSWD) != 0 ) { KMessageBox::error( 0, i18n("Specifying NIS minimum UID requires NIS file(s).\nPlease update KUser settings (Files).") ); } // need to run a utility program to build /etc/passwd, /etc/pwd.db // and /etc/spwd.db from /etc/master.passwd #if defined(__FreeBSD__) || defined(__bsdi__) if (system(PWMKDB) != 0) { KMessageBox::error( 0, i18n("Unable to build password database.") ); return FALSE; } #else if( (nis_users_written > 0) || (nispasswd_filename == passwd_filename) ) { if (system(PWMKDB) != 0) { KMessageBox::error( 0, i18n("Unable to build password databases.") ); return FALSE; } } #endif return TRUE; }
struct spwd *getspnam (const char *name) { struct spwd *sp; #ifdef USE_NIS char buf[BUFSIZ]; static char save_name[16]; bool nis_disabled = false; #endif setspent (); #ifdef USE_NIS /* * Search the shadow.byname map for this user. */ if (!nis_ignore && !nis_bound) { bind_nis (); } if (!nis_ignore && nis_bound) { char *cp; if (yp_match (nis_domain, "shadow.byname", name, strlen (name), &nis_val, &nis_vallen) == 0) { cp = strchr (nis_val, '\n'); if (NULL != cp) { *cp = '\0'; } nis_state = middle; sp = my_sgetspent (nis_val); if (NULL != sp) { strcpy (save_name, sp->sp_namp); nis_key = save_name; nis_keylen = strlen (save_name); } endspent (); return sp; } else { nis_state = native2; } } #endif #ifdef USE_NIS /* * NEEDSWORK -- this is a mess, and it is the same mess in the * other three files. I can't just blindly turn off NIS because * this might be the first pass through the local files. In * that case, I never discover that NIS is present. */ if (nis_used) { nis_ignore = true; nis_disabled = true; } #endif while ((sp = getspent ()) != (struct spwd *) 0) { if (strcmp (name, sp->sp_namp) == 0) { break; } } #ifdef USE_NIS if (nis_disabled) { nis_ignore = false; } #endif endspent (); return (sp); }