Exemplo n.º 1
0
def do_groups(session):
    for gid, name in (
        (1010, "quagga"),
        (1011, "admin"),
        ):
        session.add(models.create(models.Group, groupname=name, gid=gid))
    for grname in SYSTEMGROUPS:
        try:
            grent = grp.getgrnam(grname)
        except KeyError:
            continue
        session.add(models.create(models.Group, groupname=grent.gr_name, gid=grent.gr_gid))
    session.commit()
Exemplo n.º 2
0
def do_users(session):
    for username, uid, gid, gecos, homedir, shell, admin, first, last, pw in (
            ("admin", 1001, 1011, "Administrator", "/home/admin", "/usr/bin/vtysh", True, "Admin", "User", "gentoo"),
        ):
        session.add(models.create(
                models.User, username=username, uid=uid, gid=gid, gecos=gecos, is_admin=admin,
                first_name=first, last_name=last, shell=shell, homedir=homedir, password=pw))
    session.commit()
Exemplo n.º 3
0
def do_config(session):
    rn = models.create(models.Config, name="root", user=None, parent=None, value=aid.NULL)
    session.add(rn)
    session.commit()
    root = config.Container(session, rn)
    root["logfiledir"] = "/var/log"
    root["documentroot"] = "/var/www/localhost"
    session.commit()
Exemplo n.º 4
0
 def __setitem__(self, name, value):
     try:
         item = self.session.query(Config).filter(and_(Config.parent_id==self.node.id,
             Config.name==name)).one()
     except NoResultFound:
         me = self.node
         item = models.create(Config, name=name, value=value, container=me, user=self._user)
         self.session.add(item)
         self.session.commit()
     else:
         item.value = value
         self.session.add(item)
         self.session.commit()
Exemplo n.º 5
0
 def add_container(self, name):
     me = self.node
     if me.value is NULL:
         new = models.create(Config, name=name, value=NULL, container=me,
                 user=self._get_user(),
                 )
         try:
             self.session.add(new)
             self.session.commit()
         except IntegrityError as err:
             self.session.rollback()
             raise ConfigError(str(err))
         return Container(self.session, new,
                 user=self._user)
     else:
         raise ConfigError("Cannot add container to value pair.")
Exemplo n.º 6
0
def newuser(argv):
    """Create a new user
    newuser [-Mm] [<longopts>] <username>

    Options:
        -M  Do NOT make home directory.
        -m  Only make home directory for existing user with no home directory.

    Where long options are:
        --first_name=<firstname>
        --last_name=<lastname>
        --password=<newpass>
        --shell=<shell>
        --home=<home>
    You will be prompted for missing information.

    This function should be run as root user.
    """
    makehome = True
    onlyhome = False
    try:
        opts, longopts, args = getopt.getopt(argv[1:], "h?Mm")
    except getopt.GetoptError:
        print(newuser.__doc__)
        return
    for opt, optarg in opts:
        if opt in ("-?", "-h"):
            print(newuser.__doc__)
            return
        elif opt == "-M":
            makehome = False
        elif opt == "-m":
            onlyhome = True

    try:
        username = args[0]
    except IndexError:
        username = cliutils.get_input("Account name? ")

    try:
        pwent = passwd.getpwnam(username)
    except KeyError:
        pass
    else:
        if onlyhome:
            make_homedir(pwent.home, pwent.uid, pwent.gid)
            return
        else:
            print("User already exists, exiting.", file=sys.stderr)
            return

    password = longopts.get("password")
    if not password:
        password = ask_password()
        if not password:
            print("Passwords do not match, exiting.", file=sys.stderr)
            return

    # Get maximum UID value from passwd and database, not including system ones.
    uidl = [pwe.uid for pwe in passwd.getpwall() if pwe.uid < 10000]
    dblist = [u.uid for u in models.dbsession.query(models.User).all()]
    uidl.extend(dblist)
    uid = max(uidl) + 1

    first_name = longopts.get("first_name")
    if not first_name:
        first_name = cliutils.get_input("First Name? ")
    last_name = longopts.get("last_name")
    if not last_name:
        last_name = cliutils.get_input("Last Name? ")

    gecos = "{}, {}".format(last_name, first_name)

    shell = longopts.get("shell")
    if not shell:
        shell = cliutils.get_input("Shell? ", default="/bin/sh")

    home = longopts.get("home")
    if not home:
        home = cliutils.get_input("Homedir? ", default="/home/{}".format(username))

    email = longopts.get("email")
    if not email:
        email = cliutils.get_input("Email? ")

    primary_grp = models.dbsession.query(models.Group).filter(models.Group.groupname == "users").one()
    glist = models.dbsession.query(models.Group).all()
    glist.remove(primary_grp)
    sup_groups = cliutils.choose_multiple(glist, prompt="Extra groups?")

    superuser = cliutils.yes_no("Is admin?")

    user = models.create(
        models.User,
        username=username,
        uid=uid,
        gid=primary_grp.gid,
        gecos=gecos,
        first_name=first_name,
        last_name=last_name,
        shell=shell,
        homedir=home,
        email=email,
        is_superuser=superuser,
        is_active=True,
    )
    user.password = password
    models.dbsession.add(user)
    models.dbsession.commit()
    user.groups = sup_groups
    models.dbsession.commit()
    new_shadow(username, password)
    if makehome:
        make_homedir(home, user.uid, user.gid)
    return user