|
8 | 8 | import __main__ |
9 | 9 | import click |
10 | 10 | import grp |
| 11 | + |
11 | 12 | from pammysqltools.helpers import get_config, find_new_uid, find_new_gid, connect_db, get_useradd_conf, get_defs, \ |
12 | 13 | create_home, get_gid, get_uid |
13 | 14 | from pammysqltools.manager import UserManager, GroupListManager, GroupManager |
@@ -187,7 +188,7 @@ def userdel(force, remove, config, login): |
187 | 188 | gm.delgroup(gid=str(gr.gr_gid)) |
188 | 189 | except ValueError: |
189 | 190 | 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)) |
191 | 192 | exit(1) |
192 | 193 |
|
193 | 194 | dbs.commit() |
@@ -237,8 +238,8 @@ def usermod(comment, home_dir, expiredate, inactive, gid, groups, append, login_ |
237 | 238 |
|
238 | 239 | if expiredate: |
239 | 240 | expiredate = (expiredate - REFDATE).days |
240 | | - |
241 | | - gid = get_gid(gid) |
| 241 | + if gid: |
| 242 | + gid = get_gid(gid) |
242 | 243 |
|
243 | 244 | dbs = connect_db(conf) |
244 | 245 | pm = UserManager(conf, dbs) |
@@ -416,6 +417,84 @@ def groupdel(config, group): |
416 | 417 | dbs.close() |
417 | 418 |
|
418 | 419 |
|
| 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 | + |
419 | 498 | cli.add_command(useradd) |
420 | 499 | cli.add_command(usermod) |
421 | 500 | cli.add_command(userdel) |
|
0 commit comments