views:

416

answers:

4

I want to create a user group using python on CentOS system. When I say 'using python' I mean I don't want to do something like os.system and give the unix command to create a new group. I would like to know if there is any python module that deals with this.

Searching on the net did not reveal much about what I want, except for python user groups.. so I had to ask this.

I learned about the grp module by searching here on SO, but couldn't find anything about creating a group.

EDIT: I dont know if I have to start a new question for this, but I would also like to know how to add (existing) users to the newly created group.

Any help appreciated. Thank you.

+3  A: 

I think you should use the commandline programs from your program, a lot of care has gone into making sure that they don't break the groups file if something goes wrong.

However the file format is quite straight forward to write something yourself if you choose to go that way

gnibbler
There must be some C functions and libraries behind the scenes.. (well made obviously) that the commands are using, I am expecting may be some kind of a python wrapper around them.. ??
Shrikant Sharat
+6  A: 

I don't know of a python module to do it, but the /etc/group and /etc/gshadow format is pretty standard, so if you wanted you could just open the files, parse their current contents and then add the new group if necessary.

Before you go doing this, consider:

  • What happens if you try to add a group that already exists on the system
  • What happens when multiple instances of your program try to add a group at the same time
  • What happens to your code when an incompatible change is made to the group format a couple releases down the line
  • NIS, LDAP, Kerberos, ...

If you're not willing to deal with these kinds of problems, just use the subprocess module and run groupadd. It will be way less likely to break your customers machines.

Another thing you could do that would be less fragile than writing your own would be to wrap the code in groupadd.c (in the shadow package) in Python and do it that way. I don't see this buying you much versus just exec'ing it, though, and it would add more complexity and fragility to your build.

Jack Lloyd
Thanks for an excellent answer. The use case I am writing script for does not have any of the problems you mentioned, but adding my entries to those 'holy' linux files... makes me feel guilty :)... I think I'll just have to go with the subprocess solution. Thanks again.
Shrikant Sharat
+1  A: 

There are no library calls for creating a group. This is because there's really no such thing as creating a group. A GID is simply a number assigned to a process or a file. All these numbers exist already - there is nothing you need to do to start using a GID. With the appropriate privileges, you can call chown(2) to set the GID of a file to any number, or setgid(2) to set the GID of the current process (there's a little more to it than that, with effective IDs, supplementary IDs, etc).

Giving a name to a GID is done by an entry in /etc/group on basic Unix/Linux/POSIX systems, but that's really just a convention adhered to by the Unix/Linux/POSIX userland tools. Other network-based directories also exist, as mentioned by Jack Lloyd.

The man page group(5) describes the format of the /etc/group file, but it is not recommended that you write to it directly. Your distribution will have policies on how unnamed GIDs are allocated, such as reserving certain spaces for different purposes (fixed system groups, dynamic system groups, user groups, etc). The range of these number spaces differs on different distributions. These policies are usually encoded in the command-line tools that a sysadmin uses to assign unnamed GIDs.

This means the best way to add a group locally is to use the command-line tools.

camh
"there's really no such thing as creating a group"... that is definitely something good to know... Thanks so much for a great answer... I think I'll go with exec-ing the command line tools.
Shrikant Sharat
A: 

If you are looking at Python, then try this program. Its fairly simple to use, and the code can easily be customized http://aleph-null.tv/downloads/mpb-adduser-1.tgz

ramdaz