Func Users & Groups Module v0.3

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hey list. The 0.3 version of my users and group management module. The revisions in this release address a few code style cleanups as well as adding a stack of new methods dealing with the creation, deletion, and modification of both users and groups.

The code has been attached, and the project's changelog at http://zenit.senecac.on.ca/wiki/index.php/Func_-_Users_And_Groups_Module has been updated.

Enjoy!

Gregory Masseau.

------------------------------------------------------------------------

# Gregory Masseau <gjmasseau@xxxxxxxxxxxxxxxxxxx>
#
# This software may be freely redistributed under the terms of the GNU
# general public license.
#
# 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.

"""
User management module for func.
"""

import func_module
import sub_process
import pwd
import grp
from os import system

class UsersModule(func_module.FuncModule):

 version = "0,3"
 api_version = "0,3"
 description = "A module for the management of users and groups."

# GROUPADD METHODS #############################################################

 def __groupadd(self,group,*switches):
   """
   Constructs the proper argument sequence for self.__command and returns it's result.
   """
   if self.group_exists(group):
     return False
   else:
     return self.__command("/usr/sbin/groupadd",group,*switches)

 def group_add(self,group,*gid):
    """
    Adds a group.
    """
    if gid:
      if self.gid_exists(gid[0]):
        return False
      else:
        print str(gid[0]) + "<-"
        return self.__groupadd(group,"-g",gid[0])
    else:
      return self.__groupadd(group)

 def group_add_non_unique(self,group,*gid):
   """
   Adds a group.
   """
   if gid:
     if self.gid_exists(gid[0]):
       return False
     else:
       return self.__groupadd(group,"-o","-g",gid[0])
   else:
     return self.__groupadd(group,"-o")

# GROUPDEL METHODS #############################################################

 def __groupdel(self,group,*switches):
   """
   Constructs the proper argument sequence for self.__command and returns it's result.
   """
   if self.group_exists(group):
     return self.__command("/usr/sbin/groupdel",group,*switches)
   else:
     return False

 def group_del(self,group):
   """
   Deletes a group.
   """
   return self.__groupdel(group)

# GROUPMOD METHODS #############################################################

 def __groupmod(self,group,*switches):
   """
   Constructs the proper argument sequence for self.__command and returns it's result.
   """
   if self.group_exists(group):
     if switches:
       return self.__command("/usr/sbin/groupmod",group,*switches)
     else:
       return self.__command("/usr/sbin/groupmod",group)
   else:
     return False

 def group_set_gid_non_unique(self,group,gid):
   """
   Changes the GID of the specified group, allowing non-unique gids.
   """
   return self.__groupmod(group,"-o","-g",gid)


 def group_set_gid(self,group,gid):
   """
   Changes the GID of the specified group.
   """
   if self.gid_exists(gid):
     return False
   else:
     return self.__groupmod(group,"-g",gid)

 def group_set_groupname(self,group,groupname):
   """
   Changes the name of the specified group.
   """
   if self.group_exists(groupname):
     return False
   else:
     return self.__groupmod(group,"-n",groupname)

# USERADD METHODS ##############################################################

 def __useradd(self,user,*switches):
   """
   Constructs the proper argument sequence for self.__command and returns it's result.
   """
   if self.user_exists(user):
     return False
   else:
     return self.__command("/usr/sbin/useradd",user)

 def user_add(self,user):
   """
   Adds a user with no special options.
   """
return self.__useradd(user)
# USERDEL METHODS ##############################################################
 def __userdel(self,user,*switches):
   """
   Constructs the proper argument sequence for self.__command and returns it's result.
   """
   if self.user_exists(user):
     return self.__command("/usr/sbin/userdel",user,*switches)
   else:
     return False

 def user_del(self,user,*options):
   """
   Deletes a user.
   """
   switches=[]
   if options:
     for option in options:
       if   option == 'force':
         switches.append('-f')
       elif option == 'remove':
         switches.append('-r')
else: return False return self.__userdel(user,*switches)
# USERMOD METHODS ##############################################################

 def __usermod(self,user,*switches):
   """
   Constructs the proper argument sequence for self.__command and returns it's result.
   """
   if self.user_exists(user):
     command = []
     if switches:
       command = list(switches)
     command.append(user)
     return self.__command("/usr/sbin/usermod",*command)
   else:
     return False

 def user_lock(self,user):
   """
   Locks a user account.
   """
   return self.__usermod(user,"-L")

 def user_set_shell(self,user,shell):
   """
   Set a specified user's shell.
   """
   return self.__usermod(user,"-s",shell)

 def user_set_home(self,user,home):
   """
   Change (but don't move the contents of) a user's home folder.
   """
   return self.__usermod(user,"-d",home)

 def user_set_loginname(self,user,loginname):
   """
   Change a user's login name.
   """
   return self.__usermod(user,"-l",loginname)

 def user_set_comment(self,user,comment):
   """
   Change the value of a user's GECOS field -- maybe replace this with a field sensitive version?
   """
   return self.__usermod(user,"-c",comment)

 def user_set_expiredate(self,user,expiredate):
   """
   Set the expity date for a specified user.
   """
   return self.__usermod(user,"-e",expiredate)

 def user_set_uid_non_unique(self,user):
   """
   Change a user's UID, allowing non-unique UIDs.
   """
   return self.__usermod(user,"-u",uid,"-o")

 def user_set_uid(self,user,uid):
   """
   Change a user's UID.
   """
   return self.__usermod(user,"-u",uid)

 def user_set_inactive(self,user,inactive):
   """
   Set the inactivity timer on a user.
   """
   return self.__usermod(user,"-f",inactive)

 def user_set_gid(self,user,gid):
   """
   Change a users primary group by GID.
   """
   if self.gid_exists(gid):
     return self.__usermod(user,"-g",gid)
   else:
     return False

 def user_move_home(self,user,home):
   """
   Changes and moves a users home folder.
   """
   return self.__usermod(user,"-d",home,"-m")

 def user_unlock(self,user):
   """
   Unlocks a specified user account.
   """
   return self.__usermod(user,"-U")

 def user_add_to_group(self,user,group):
   """
   Appends the user to a specified group.
   """
   if self.group_exists(group):
     return self.__usermod(user,"-aG",group)
   else:
     return False

 def user_set_group(self,user,group):
   """
   Changes a users primary group.
   """
   if self.group_exists(group):
     gid = self.group_to_gid(group)
     return self.__usermod(user,"-g",gid)
   else:
     return False

# INFORMATIONAL METHODS ########################################################

# EXISTANCE TEST METHODS

 def user_exists(self, user):
   """
   Checks to see if a given user exists on the target system(s).
   """
   try:
     if pwd.getpwnam(user):
       return True
   except KeyError:
     return False

 def uid_exists(self, uid):
   """
   Checks to see if a given uid exists on the target system(s).
   """
   try:
     if pwd.getpwuid(int(uid)):
       return True
   except KeyError:
     return False

 def group_exists(self, group):
   """
   Checks to see if a given group exists on the target system(s).
   """

   try:
     if grp.getgrnam(group):
       return True
   except KeyError:
     return False

 def gid_exists(self, gid):
   """
   Checks to see if a given gid exists on the target system(s).
   """
   try:
     if grp.getgrgid(int(gid)):
       return True
   except KeyError:
     return False

# LISTING METHODS

 def user_list(self):
   """
   Lists all users.
   """
   users = []
   for user in pwd.getpwall():
     users.append(user[0])
   return users

 def uid_list(self):
   """
   Lists all UIDs..
   """
   uids = []
   for user in pwd.getpwall():
     uids.append(user[2])
   return uids

 def group_list(self):
   """
   Lists all groups.
   """
   groups = []
   for group in grp.getgrall():
     groups.append(group[0])
   return groups

 def gid_list(self):
   """
   Lists all GIDs.
   """
   gids = []
   for group in grp.getgrall():
     gids.append(group[2])
   return gids

# INFO METHODS

 def user_info(self, user):
   """
   Returns user info or false for a specified user on the target system(s).
   """
   try:
     if pwd.getpwnam(user):
       info = pwd.getpwnam(user)
       return info
   except KeyError:
     return False

 def uid_info(self, uid):
   """
   Returns user info or false for a specified user (by UID) on the target system(s).
   """
   try:
     if pwd.getpwuid(uid):
       info = pwd.getpwuid(int(uid))
       return info
   except KeyError:
     return False

 def group_info(self, group):
   """
   Returns group info or false for a specified group on the target system(s).
   """
   try:
     if grp.getgrnam(group):
       info = grp.getgrnam(group)
       return info
   except KeyError:
     return False

 def gid_info(self, gid):
   """
   Returns group info or false for a specified group (by GID) on the target system(s).
   """
   try:
     if grp.getgrgid(int(gid)):
       info = grp.getgrgid(int(gid))
       return info
   except KeyError:
     return False

# INVENTORY METHODS

 def user_inventory(self):
   """
   Returns user info for all users.
   """
   return pwd.getpwall()


 def group_inventory(self):
   """
   Returns group info for all users.
   """
   return grp.getgrall()

# CONVERSION METHODS

 def user_to_uid(self, user):
   """
   Takes a user name and converts it to the matching UID.
   """
   try:
     username = pwd.getpwnam(user)[2]
     return username
   except KeyError:
     return False

 def uid_to_user(self, uid):
   """
   Takes a UID and converts it to the matching user name.
   """
   try:
     user = pwd.getpwuid(int(uid))[0]
     return user
   except KeyError:
     return False

 def group_to_gid(self, group):
   """
   Takes a group name and converts it to the matching GID.
   """
   try:
     groupname = grp.getgrnam(group)[2]
     return groupname
   except KeyError:
     return False

 def gid_to_group(self, gid):
   """
   Takes a GID and converts it to the matching group name.
   """
   try:
     group = grp.getgrgid(int(gid))[0]
     return group
   except KeyError:
     return False

# INTERNALLY USED METHODS ########################################################

 def __command(self,*list):
   """
   This method is used internally by this module when invoking external commands. It should remain private.
   This method should probably be improved to check the elems for suspicious characters like ';','&&','||'.
   """
   cmd = ''
   for elem in list:
     if elem == '':
       pass
     else:
       cmd = cmd + ' "' + str(elem) + '"'
   print "Command [ " + cmd + "]"
   if system(cmd):
     return False
   else:
     return True

# Simple user add method ########################################################

 def user_add_simple_old(self,user,*uid):
   """
   Adds a user.
   """
   if self.user_exists(user):
     return False
   else:
     cmd ="/usr/sbin/useradd "
     if uid:
       if self.uid_exists(uid[0]):
         return False
       else:
         cmd = cmd + "-u " + str(uid[0])
       cmd = cmd + " " + user
       system(cmd)
     return self.user_info("user")


_______________________________________________
Func-list mailing list
Func-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/func-list

[Index of Archives]     [Fedora Users]     [Linux Networking]     [Fedora Legacy List]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]

  Powered by Linux