Skip to content

Commit 77cc815

Browse files
authored
operations/server.user: fix user addition/modification operations for FreeBSD
1 parent 1b48f8b commit 77cc815

File tree

10 files changed

+176
-12
lines changed

10 files changed

+176
-12
lines changed

pyinfra/facts/server.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,10 @@ def command(self):
526526
for i in `cat /etc/passwd | cut -d: -f1`; do
527527
ENTRY=`grep ^$i: /etc/passwd`;
528528
LASTLOG=`(((lastlog -u $i || lastlogin $i) 2> /dev/null) | grep ^$i | tr -s ' ')`;
529-
PASSWORD=`grep ^$i: /etc/shadow | cut -d: -f2`;
529+
PASSWORD=`(grep ^$i: /etc/shadow || grep ^$i: /etc/master.passwd) 2> /dev/null | cut -d: -f2`;
530530
echo "$ENTRY|`id -gn $i`|`id -Gn $i`|$LASTLOG|$PASSWORD";
531531
done
532-
""".strip()
532+
""".strip() # noqa
533533

534534
default = dict
535535

pyinfra/operations/server.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -899,22 +899,29 @@ def user(
899899

900900
if create_home:
901901
args.append("-m")
902-
else:
902+
elif os_type != "FreeBSD":
903903
args.append("-M")
904904

905-
if password:
905+
if password and os_type != "FreeBSD":
906906
args.append("-p '{0}'".format(password))
907907

908908
# Users are often added by other operations (package installs), so check
909909
# for the user at runtime before adding.
910910
add_user_command = "useradd"
911+
911912
if os_type == "FreeBSD":
912913
add_user_command = "pw useradd"
913-
yield "{0} -n {2} {1}".format(
914-
add_user_command,
915-
" ".join(args),
916-
user,
917-
)
914+
915+
if password:
916+
yield "echo '{3}' | {0} -n {2} -H 0 {1}".format(
917+
add_user_command, " ".join(args), user, password
918+
)
919+
else:
920+
yield "{0} -n {2} {1}".format(
921+
add_user_command,
922+
" ".join(args),
923+
user,
924+
)
918925
else:
919926
yield "{0} {1} {2}".format(
920927
add_user_command,
@@ -951,7 +958,10 @@ def user(
951958
args.append("-c '{0}'".format(comment))
952959

953960
if password and existing_user["password"] != password:
954-
args.append("-p '{0}'".format(password))
961+
if os_type == "FreeBSD":
962+
yield "echo '{0}' | pw usermod -n {1} -H 0".format(password, user)
963+
else:
964+
args.append("-p '{0}'".format(password))
955965

956966
# Need to mod the user?
957967
if args:

tests/facts/server.Users/mixed.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"freebsd:*:1001:1001:FreeBSD:/home/freebsd:/bin/sh|freebsd|freebsd|freebsd pts/0 10.1.10.10 Thu Aug 31 05:37:58 2023|$bsdpw$",
99
"freebsdnologin:*:1001:1001:FreeBSD NoLogin:/home/freebsdnologin:/bin/sh|freebsdnologin|freebsdnologin||"
1010
],
11-
"command": "for i in `cat /etc/passwd | cut -d: -f1`; do\n ENTRY=`grep ^$i: /etc/passwd`;\n LASTLOG=`(((lastlog -u $i || lastlogin $i) 2> /dev/null) | grep ^$i | tr -s ' ')`;\n PASSWORD=`grep ^$i: /etc/shadow | cut -d: -f2`;\n echo \"$ENTRY|`id -gn $i`|`id -Gn $i`|$LASTLOG|$PASSWORD\";\n done",
11+
"command": "for i in `cat /etc/passwd | cut -d: -f1`; do\n ENTRY=`grep ^$i: /etc/passwd`;\n LASTLOG=`(((lastlog -u $i || lastlogin $i) 2> /dev/null) | grep ^$i | tr -s ' ')`;\n PASSWORD=`(grep ^$i: /etc/shadow || grep ^$i: /etc/master.passwd) 2> /dev/null | cut -d: -f2`;\n echo \"$ENTRY|`id -gn $i`|`id -Gn $i`|$LASTLOG|$PASSWORD\";\n done",
1212
"fact": {
1313
"root": {
1414
"home": "/root",
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"args": ["someuser"],
3+
"kwargs": {
4+
"home": "homedir",
5+
"shell": "shellbin",
6+
"group": "mygroup",
7+
"groups": ["secondary_group", "another"],
8+
"uid" : 1000,
9+
"system": true,
10+
"comment": "Full Name",
11+
"create_home": true,
12+
"password": "$somecryptedpassword$"
13+
},
14+
"facts": {
15+
"server.Os": "FreeBSD",
16+
"server.Users": {},
17+
"server.Groups": {},
18+
"files.Directory": {
19+
"path=homedir": null
20+
}
21+
},
22+
"commands": [
23+
"echo '$somecryptedpassword$' | pw useradd -n someuser -H 0 -d homedir -s shellbin -g mygroup -G secondary_group,another -u 1000 -c 'Full Name' -m",
24+
"mkdir -p homedir",
25+
"chown someuser:mygroup homedir"
26+
]
27+
}

tests/operations/server.user/add.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
"files.Directory": {
1919
"path=homedir": null
2020
},
21-
"server.Os": "Linux"
2221
},
2322
"commands": [
2423
"useradd -d homedir -s shellbin -g mygroup -G secondary_group,another -r --uid 1000 -c 'Full Name' -m -p '$somecryptedpassword$' someuser",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"args": ["someuser"],
3+
"facts": {
4+
"server.Users": {},
5+
"server.Groups": [
6+
"someuser"
7+
],
8+
"files.Directory": {
9+
"path=/home/someuser": null
10+
},
11+
"server.Os": "FreeBSD"
12+
},
13+
"commands": [
14+
"pw useradd -n someuser -d /home/someuser -g someuser",
15+
"mkdir -p /home/someuser",
16+
"chown someuser:someuser /home/someuser"
17+
]
18+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"args": ["someuser"],
3+
"kwargs": {
4+
"home": "homedir"
5+
},
6+
"facts": {
7+
"server.Os": "FreeBSD",
8+
"server.Users": {},
9+
"server.Groups": {},
10+
"files.Directory": {
11+
"path=homedir": false
12+
},
13+
"files.Link": {
14+
"path=homedir": true
15+
}
16+
},
17+
"commands": [
18+
"pw useradd -n someuser -d homedir"
19+
]
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"args": ["someuser"],
3+
"kwargs": {
4+
"unique": false,
5+
"ensure_home": false
6+
},
7+
"facts": {
8+
"server.Os": "FreeBSD",
9+
"server.Users": {},
10+
"server.Groups": {}
11+
},
12+
"commands": [
13+
"pw useradd -n someuser -d /home/someuser -o"
14+
]
15+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{
2+
"args": ["someuser"],
3+
"kwargs": {
4+
"group": "somegroup",
5+
"groups": [
6+
"group3", "group4"
7+
],
8+
"append": true
9+
},
10+
"facts": {
11+
"server.Os": "FreeBSD",
12+
"server.Users": {
13+
"someuser": {
14+
"home": "/home/someuser",
15+
"group": "somegroup",
16+
"groups": [
17+
"group1", "group2"
18+
]
19+
}
20+
},
21+
"files.Directory": {
22+
"path=/home/someuser": {
23+
"user": "someuser",
24+
"group": "somegroup"
25+
},
26+
"path=/home/someotheruser": {
27+
"user": "someuser",
28+
"group": "somegroup"
29+
}
30+
},
31+
"server.Groups": {}
32+
},
33+
"commands": [
34+
"pw usermod -n someuser -a -G group3,group4"
35+
]
36+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"args": ["someuser"],
3+
"kwargs": {
4+
"shell": "/bin/false",
5+
"group": "somegroup",
6+
"groups": ["group1", "group2", "group3"],
7+
"home": "/home/someotheruser",
8+
"comment": "New Full Name"
9+
},
10+
"facts": {
11+
"server.Os": "FreeBSD",
12+
"server.Users": {
13+
"someuser": {
14+
"comment": "Full Name",
15+
"home": "/home/someuser",
16+
"shell": "/bin/bash",
17+
"group": "nowt",
18+
"groups": [
19+
"group1", "group2"
20+
],
21+
"password": "$somepw$"
22+
}
23+
},
24+
"files.Directory": {
25+
"path=/home/someuser": {
26+
"user": "someuser",
27+
"group": "somegroup"
28+
},
29+
"path=/home/someotheruser": {
30+
"user": "someuser",
31+
"group": "somegroup"
32+
}
33+
},
34+
"server.Groups": {}
35+
},
36+
"commands": [
37+
"pw usermod -n someuser -d /home/someotheruser -s /bin/false -g somegroup -G group1,group2,group3 -c 'New Full Name'"
38+
]
39+
}

0 commit comments

Comments
 (0)