Skip to content

Commit 59a5388

Browse files
author
Christopher Perrin
committed
Added tools to import from local passwd and groups
- Some Bugfixes
1 parent 401c895 commit 59a5388

3 files changed

Lines changed: 86 additions & 4 deletions

File tree

pammysqltools/manager.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ def moduser(self, username_old, username=None, gid=None, uid=None, gecos=None, h
121121
values.append(username_old)
122122
sql = "UPDATE `{user}` SET {fields} WHERE `{username}` = %s;".format(user=s_tables.get('user', 'user'),
123123
fields=fields,
124-
username=s_fields('username', 'username'))
124+
username=s_fields.get('username',
125+
'username'))
125126

126127
with self.dbs.cursor() as cur:
127128
self.getuserbyusername(username_old)

pammysqltools/scripts.py

Lines changed: 82 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import __main__
99
import click
1010
import grp
11+
1112
from pammysqltools.helpers import get_config, find_new_uid, find_new_gid, connect_db, get_useradd_conf, get_defs, \
1213
create_home, get_gid, get_uid
1314
from pammysqltools.manager import UserManager, GroupListManager, GroupManager
@@ -187,7 +188,7 @@ def userdel(force, remove, config, login):
187188
gm.delgroup(gid=str(gr.gr_gid))
188189
except ValueError:
189190
print(_('Warning: Primary group "{group}" of user is empty but not in Database. Try "groupdel {group}"').format(
190-
group=gr.gr_gid))
191+
group=gr.gr_gid))
191192
exit(1)
192193

193194
dbs.commit()
@@ -237,8 +238,8 @@ def usermod(comment, home_dir, expiredate, inactive, gid, groups, append, login_
237238

238239
if expiredate:
239240
expiredate = (expiredate - REFDATE).days
240-
241-
gid = get_gid(gid)
241+
if gid:
242+
gid = get_gid(gid)
242243

243244
dbs = connect_db(conf)
244245
pm = UserManager(conf, dbs)
@@ -416,6 +417,84 @@ def groupdel(config, group):
416417
dbs.close()
417418

418419

420+
@click.command()
421+
@click.option('--config', help=_('path to the config file for this tool'))
422+
@click.argument('lower', type=int)
423+
@click.argument('upper', type=int)
424+
def importusers(config, lower, upper):
425+
conf = get_config(config)
426+
users = {}
427+
428+
with open('/etc/passwd') as passwd:
429+
for line in passwd:
430+
line = line.strip()
431+
u = line.split(':')
432+
if lower <= int(u[2]) <= upper:
433+
for i in range(len(u)):
434+
if not u[i].strip():
435+
u[i] = None
436+
users[u[0]] = u
437+
438+
dbs = connect_db(conf)
439+
um = UserManager(conf, dbs)
440+
gm = GroupManager(conf, dbs)
441+
442+
with open('/etc/shadow') as shadow:
443+
for line in shadow:
444+
line.strip()
445+
s = line.split(':')
446+
447+
if s[0] in users.keys():
448+
for i in range(len(s)):
449+
if not s[i].strip():
450+
s[i] = None
451+
u = users[s[0]]
452+
um.adduser(u[0], uid=u[2], gid=u[3], gecos=u[4], homedir=u[5], shell=u[6], password=s[1], lstchg=s[2],
453+
mini=s[3], maxi=s[4], warn=s[5], inact=s[6], expire=s[7], flag=s[8])
454+
gm.addgroup(u[0], gid=u[3])
455+
dbs.commit()
456+
dbs.close()
457+
458+
459+
@click.command()
460+
@click.option('--config', help=_('path to the config file for this tool'))
461+
@click.argument('lower', type=int)
462+
@click.argument('upper', type=int)
463+
def importgroups(config, lower, upper):
464+
conf = get_config(config)
465+
groups = {}
466+
467+
with open('/etc/group') as group:
468+
for line in group:
469+
line = line.strip()
470+
g = line.split(':')
471+
if lower <= int(g[2]) <= upper:
472+
for i in range(len(g)):
473+
if not g[i].strip():
474+
g[i] = None
475+
groups[g[0]] = g
476+
477+
dbs = connect_db(conf)
478+
gm = GroupManager(conf, dbs)
479+
glm = GroupListManager(conf, dbs)
480+
481+
with open('/etc/gshadow') as gshadow:
482+
for line in gshadow:
483+
line.strip()
484+
gs = line.split(':')
485+
486+
if gs[0] in groups.keys():
487+
for i in range(len(gs)):
488+
if not gs[i].strip():
489+
gs[i] = None
490+
g = groups[gs[0]]
491+
gm.addgroup(g[0], gid=g[2], password=gs[1])
492+
for user in g[3].split(','):
493+
glm.addgroupuser(username=user, gid=g[2])
494+
dbs.commit()
495+
dbs.close()
496+
497+
419498
cli.add_command(useradd)
420499
cli.add_command(usermod)
421500
cli.add_command(userdel)

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ def readme():
2727
'mygroupadd=pammysqltools.scripts:groupadd',
2828
'mygroupdel=pammysqltools.scripts:groupdel',
2929
'mygroupmod=pammysqltools.scripts:groupmod',
30+
'myimportusers=pammysqltools.scripts:importusers',
31+
'myimportgroups=pammysqltools.scripts:importgroups',
3032
]
3133
},
3234
package_data={

0 commit comments

Comments
 (0)