Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

passwd.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   This program is free software; you can redistribute it and/or modify  *
00004  *   it under the terms of the GNU General Public License as published by  *
00005  *   the Free Software Foundation; either version 2 of the License, or     *
00006  *   (at your option) any later version.                                   *
00007  *                                                                         *
00008  ***************************************************************************/
00009 
00010 #include "passwd.h"
00011 
00012 // Needed for crypt_make_salt();
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 #include <unistd.h>
00016 #include <time.h>
00017 
00018 Passwd::Passwd() {
00019 }
00020 
00021 Passwd::~Passwd() {
00022 }
00023 
00024 // This function is taken from 'busybox'.
00025 int Passwd::i64c(int i)
00026 {
00027         if (i <= 0)
00028                 return ('.');
00029         if (i == 1)
00030                 return ('/');
00031         if (i >= 2 && i < 12)
00032                 return ('0' - 2 + i);
00033         if (i >= 12 && i < 38)
00034                 return ('A' - 12 + i);
00035         if (i >= 38 && i < 63)
00036                 return ('a' - 38 + i);
00037         return ('z');
00038 }
00039 
00040 // This function is taken from 'busybox'.
00041 char *Passwd::crypt_make_salt() {
00042         time_t now;
00043         static unsigned long x;
00044         static char result[3];
00045 
00046         time(&now);
00047         x += now + getpid() + clock();
00048         result[0] = i64c(((x >> 18) ^ (x >> 6)) & 077);
00049         result[1] = i64c(((x >> 12) ^ x) & 077);
00050         result[2] = '\0';
00051         return result;
00052 }
00053 
00054 //  opens the files /etc/passwd & /etc/group and loads the contents into passwdStringList & groupStringList
00055 bool Passwd::open() {
00056         int returnvalue=false;
00057         
00058         QFile passwd_file("/etc/passwd");
00059         QFile group_file("/etc/group");
00060         passwdStringList.clear();
00061         groupStringList.clear();
00062         if((passwd_file.open(IO_ReadOnly))) {
00063                 if((group_file.open(IO_ReadOnly))) {
00064                         QTextStream ts_passwd(&passwd_file);
00065                         while(!(ts_passwd.eof())) {
00066                                 passwdStringList << ts_passwd.readLine();
00067                         }
00068                         QTextStream ts_group(&group_file);
00069                         while(!(ts_group.eof())) {
00070                                 groupStringList << ts_group.readLine();
00071                         }
00072                         returnvalue=true;
00073                         group_file.close();
00074                 }
00075                 passwd_file.close();
00076         }
00077         return returnvalue;
00078 }
00079 
00080 // Writes back the contents of passwdStringList to /etc/passwd & groupStringList to /etc/group
00081 bool Passwd::close() {
00082         int returnvalue=false;
00083         QFile passwd_file("/etc/passwd");
00084         QFile group_file("/etc/group");
00085         if((passwd_file.open(IO_WriteOnly))) {
00086                 if((group_file.open(IO_WriteOnly))) {
00087                         QTextStream ts_passwd(&passwd_file);
00088                         for(QStringList::Iterator it=passwdStringList.begin(); it!=passwdStringList.end(); ++it) {
00089                                 ts_passwd << (*it) + "\n";
00090                         }
00091                         QTextStream ts_group(&group_file);
00092                         for(QStringList::Iterator it=groupStringList.begin(); it!=groupStringList.end(); ++it) {
00093                                 ts_group << (*it) + "\n";
00094                         }
00095                         returnvalue=true;
00096                         group_file.close();
00097                 }
00098                 passwd_file.close();
00099         }
00100         return returnvalue;
00101 }
00102 
00103 // Splits a "passwd" line into the components and stores them in the pw_* variables.
00104 void Passwd::splitPasswdEntry(QString &userString) {
00105         userdataStringList=QStringList::split(":",userString,true);
00106         QStringList::Iterator it=userdataStringList.begin();
00107         pw_name=(*it++);
00108         pw_passwd=(*it++);
00109         pw_uid=(*it++).toInt();
00110         pw_gid=(*it++).toInt();
00111         pw_gecos=(*it++);
00112         pw_dir=(*it++);
00113         pw_shell=(*it++);
00114 }
00115 
00116 // Splits a "group" line into the components and stores them in the gr_* variables.
00117 void Passwd::splitGroupEntry(QString &groupString) {
00118         groupdataStringList=QStringList::split(":",groupString,true);
00119         QStringList::Iterator it=groupdataStringList.begin();
00120         gr_name=(*it++);
00121         it++;
00122         gr_gid=(*it++).toInt();
00123         gr_mem=QStringList::split(",",(*it++));
00124 }
00125 
00126 // Find a user in the passwdStringList. Return true if found and also fill the pw_* variables.
00127 bool Passwd::searchUser(QRegExp &userRegExp) {
00128         QStringList tempStringList(passwdStringList.grep(userRegExp));
00129         if((tempStringList.isEmpty())) {
00130                 return false;
00131         } else {
00132                 userString=(*(tempStringList.begin()));
00133                 splitPasswdEntry(userString);
00134         }
00135         return true;
00136 }
00137 
00138 // Find a user by login.
00139 bool Passwd::findUser(const char *username) {
00140         QRegExp userRegExp(QString("^%1\\:").arg(username));
00141         return searchUser(userRegExp);
00142 }
00143 
00144 // Find a user by uid.
00145 bool Passwd::findUser(int uid) {
00146         QRegExp userRegExp(QString(":%1\\:").arg(uid));
00147         return searchUser(userRegExp);
00148 }
00149 
00150 // Add a user to the passwdStringList, create home directory, and optionally create a group for the user.
00151 bool Passwd::addUser(QString pw_name, QString pw_passwd, int pw_uid, int pw_gid, QString pw_gecos,QString pw_dir, QString pw_shell, bool createGroup) {
00152         QString tempString;
00153         if((createGroup) && (!(findGroup(pw_gid)))) addGroup(pw_name,pw_gid);
00154         pw_passwd = crypt(pw_passwd, crypt_make_salt());
00155         tempString=pw_name+":"+pw_passwd+":"+QString::number(pw_uid)+":"+QString::number(pw_gid)+":"+pw_gecos+":"+pw_dir+":"+pw_shell;
00156         passwdStringList.append(tempString);
00157         // Make home dir.
00158         QDir d;
00159         if(!(d.exists(pw_dir))) {
00160                 d.mkdir(pw_dir);
00161                 chown(pw_dir,pw_uid,pw_gid);
00162                 chmod(pw_dir,S_IRUSR|S_IWUSR|S_IXUSR);
00163         }
00164         return 1;
00165 }
00166 
00167 // Update info for a user in passwdStringList, take info from the pw_* fields.
00168 bool Passwd::updateUser(QString login) {
00169         QRegExp userRegExp(QString("^%1\\:").arg(login));
00170         for(QStringList::Iterator it=passwdStringList.begin(); it!=passwdStringList.end(); ++it) {
00171                 if(userRegExp.find((*it),0)!=-1) {
00172                         *it=QString(pw_name+":"+pw_passwd+":"+QString::number(pw_uid)+":"+QString::number(pw_gid)+":"+pw_gecos+":"+pw_dir+":"+pw_shell);
00173                         return true;
00174                 }
00175         }
00176         return false;
00177 }
00178 
00179 // Delete a user from passwdStringList.
00180 bool Passwd::deleteUser(QRegExp &userRegExp, bool delGroup) {
00181         for(QStringList::Iterator it=passwdStringList.begin(); it!=passwdStringList.end(); ++it) {
00182                 if(userRegExp.find((*it),0)!=-1) {
00183                         splitPasswdEntry(*it);
00184                         if(delGroup) this->delGroup(pw_gid);
00185                         passwdStringList.remove(it);
00186                         return true;
00187                 }
00188         }
00189         return false;
00190 }
00191 
00192 // Delete a user by login, and optionally also delete group.
00193 bool Passwd::delUser(const char *username, bool delGroup) {
00194         QRegExp userRegExp(QString("^%1\\:").arg(username));
00195         return deleteUser(userRegExp,delGroup);
00196 }
00197 
00198 // Delete a user by uid, and optionally also delete group.
00199 bool Passwd::delUser(int uid, bool delGroup) {
00200         QRegExp userRegExp(QString(":%1\\:").arg(uid));
00201         return deleteUser(userRegExp,delGroup);
00202 }
00203 
00204 // Locate a group in the groupStringList, fill out the gr_* variables and return "true" if found.
00205 bool Passwd::searchGroup(QRegExp &groupRegExp) {
00206         QStringList tempStringList(groupStringList.grep(groupRegExp));
00207         if((tempStringList.isEmpty())) {
00208                 return false;
00209         } else {
00210                 for(QStringList::Iterator it=tempStringList.begin(); it!=tempStringList.end(); it++) {
00211                         groupString=*it;
00212                         if(!groupString.find(QRegExp("^#"),0)) {        // Skip commented lines.
00213                                 splitGroupEntry(groupString);
00214                                 return true;
00215                         }
00216                 }
00217         }
00218         return false;
00219 }
00220 
00221 // Find a group by groupname.
00222 bool Passwd::findGroup(const char *groupname) {
00223         QRegExp groupRegExp(QString("^%1\\:").arg(groupname));
00224         return searchGroup(groupRegExp);
00225 }
00226 
00227 // Find a group by gid.
00228 bool Passwd::findGroup(int gid) {
00229         QRegExp groupRegExp(QString(":%1\\:").arg(gid));
00230         return searchGroup(groupRegExp);
00231 }
00232 
00233 // Add a group to groupStringList
00234 bool Passwd::addGroup(QString gr_name, int gr_gid) {
00235         QString tempString;
00236         tempString=gr_name+":*:"+QString::number(gr_gid)+":";
00237         groupStringList.append(tempString);
00238         return 1;
00239 }
00240 
00241 // Update fields for a group in groupStringList, take info from the gr_* variables.
00242 bool Passwd::updateGroup(int gid) {
00243         QRegExp groupRegExp(QString(":%1\\:").arg(QString::number(gid)));
00244         for(QStringList::Iterator it=groupStringList.begin(); it!=groupStringList.end(); ++it) {
00245                 if(groupRegExp.find((*it),0)!=-1) {
00246                         *it=QString(gr_name+":*:"+QString::number(gr_gid)+":");
00247                         for(QStringList::Iterator member=gr_mem.begin(); member!=gr_mem.end();) {
00248                                 *it+=*member;
00249                                 ++member;
00250                                 if(member!=gr_mem.end()) *it+=",";
00251                         }
00252                         return true;
00253                 }
00254         }
00255         return false;
00256 }
00257 
00258 // Delete a group from groupStringList.
00259 bool Passwd::deleteGroup(QRegExp &groupRegExp) {
00260         for(QStringList::Iterator it=groupStringList.begin(); it!=groupStringList.end(); ++it) {
00261                 if(groupRegExp.find((*it),0)!=-1) {
00262                         groupStringList.remove(it);
00263                         return true;
00264                 }
00265         }
00266         return false;
00267 }
00268 
00269 // Delete a group by groupname.
00270 bool Passwd::delGroup(const char *groupname) {
00271         QRegExp groupRegExp(QString("^%1\\:").arg(groupname));
00272         return deleteGroup(groupRegExp);
00273 }
00274 
00275 // Delete a group by gid.
00276 bool Passwd::delGroup(int gid) {
00277         QRegExp groupRegExp(QString(":%1\\:").arg(gid));
00278         return deleteGroup(groupRegExp);
00279 }
00280 
00281 // Add a user as a member to a group in groupStringList.
00282 bool Passwd::addGroupMember(QString groupname, QString member) {
00283         if(!(findGroup(groupname))) return false;
00284         QRegExp memberRegExp(QString("^%1$").arg(member));
00285         QStringList templist=gr_mem.grep(memberRegExp);
00286         if(templist.isEmpty()) gr_mem << member;
00287         if(!(updateGroup(gr_gid))) return false;
00288         return true;
00289 }
00290 
00291 // Delete a user as a groupmember from a group in groupStringList.
00292 bool Passwd::delGroupMember(QString groupname, QString member) {
00293         if(!(findGroup(groupname))) return false;
00294         for(QStringList::Iterator it=gr_mem.begin(); it!=gr_mem.end(); ++it) {
00295                 if(*it==member) {
00296                         gr_mem.remove(it);
00297                         it=gr_mem.end();
00298                 }
00299         }
00300         if(!(updateGroup(gr_gid))) return false;
00301         return true;
00302 }
00303 
00304 // Global Object 
00305 Passwd *accounts;

Generated on Sat Nov 5 16:17:58 2005 for OPIE by  doxygen 1.4.2