line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package App::LDAP::Command::Add::User; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
8
|
use Modern::Perl; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
9
|
|
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
617
|
use Moose; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
with qw( App::LDAP::Role::Command |
8
|
|
|
|
|
|
|
App::LDAP::Role::Bindable ); |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
has shell => ( |
11
|
|
|
|
|
|
|
is => "rw", |
12
|
|
|
|
|
|
|
isa => "Str", |
13
|
|
|
|
|
|
|
documentation => "the login shell. default /bin/bash.", |
14
|
|
|
|
|
|
|
); |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
has home => ( |
17
|
|
|
|
|
|
|
is => "rw", |
18
|
|
|
|
|
|
|
isa => "Str", |
19
|
|
|
|
|
|
|
documentation => 'the home directory. default /home/\$username', |
20
|
|
|
|
|
|
|
); |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has group => ( |
23
|
|
|
|
|
|
|
is => "rw", |
24
|
|
|
|
|
|
|
isa => "Str", |
25
|
|
|
|
|
|
|
documentation => 'the group name. default $username', |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
has base => ( |
29
|
|
|
|
|
|
|
is => "rw", |
30
|
|
|
|
|
|
|
isa => "Str", |
31
|
|
|
|
|
|
|
documentation => 'the organizational unit this user belongs to. default /etc/ldap/ldap.conf nss_base_passwd.', |
32
|
|
|
|
|
|
|
); |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
# inetOrgPerson |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
has surname => ( |
37
|
|
|
|
|
|
|
is => "rw", |
38
|
|
|
|
|
|
|
isa => "ArrayRef[Str]", |
39
|
|
|
|
|
|
|
default => sub { ["NULL"] }, |
40
|
|
|
|
|
|
|
documentation => 'the surname. default $username', |
41
|
|
|
|
|
|
|
); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
has mail => ( |
44
|
|
|
|
|
|
|
is => "rw", |
45
|
|
|
|
|
|
|
isa => "ArrayRef", |
46
|
|
|
|
|
|
|
required => 1, |
47
|
|
|
|
|
|
|
documentation => "the email addresses. this option can be multiple values" |
48
|
|
|
|
|
|
|
); |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
use App::LDAP::LDIF::User; |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
around prepare => sub { |
53
|
|
|
|
|
|
|
my $orig = shift; |
54
|
|
|
|
|
|
|
my $self = shift; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
$self->group($self->extra_argv->[2]) unless $self->group; |
57
|
|
|
|
|
|
|
# $self->group is the same as user name if undefined |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$self->$orig(@_); |
60
|
|
|
|
|
|
|
}; |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
# {{{ sub run |
63
|
|
|
|
|
|
|
sub run { |
64
|
|
|
|
|
|
|
my ($self) = shift; |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $uid = next_uid(); |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
my $username = $self->extra_argv->[2] or die "no username specified"; |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
die "user $username already exists" if App::LDAP::LDIF::User->search( |
71
|
|
|
|
|
|
|
base => config()->{nss_base_passwd}->[0], |
72
|
|
|
|
|
|
|
scope => config()->{nss_base_passwd}->[1], |
73
|
|
|
|
|
|
|
filter => "uid=$username", |
74
|
|
|
|
|
|
|
); |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $user = App::LDAP::LDIF::User->new( |
77
|
|
|
|
|
|
|
base => $self->base // config()->{nss_base_passwd}->[0], |
78
|
|
|
|
|
|
|
uid => $username, |
79
|
|
|
|
|
|
|
userPassword => encrypt(new_password()), |
80
|
|
|
|
|
|
|
uidNumber => $uid->get_value("uidNumber"), |
81
|
|
|
|
|
|
|
gidNumber => $self->gid_of( $self->group ), |
82
|
|
|
|
|
|
|
sn => $self->surname, |
83
|
|
|
|
|
|
|
mail => $self->mail, |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
$user->loginShell ( $self->shell ) if $self->shell; |
87
|
|
|
|
|
|
|
$user->homeDirectory ( $self->home ) if $self->home; |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
$user->save; |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
$uid->replace(uidNumber => $uid->get_value("uidNumber")+1)->update(ldap()); |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
$user; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
# }}} |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub next_uid { |
98
|
|
|
|
|
|
|
ldap()->search( |
99
|
|
|
|
|
|
|
base => config()->{base}, |
100
|
|
|
|
|
|
|
filter => "(objectClass=uidnext)", |
101
|
|
|
|
|
|
|
)->entry(0); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub gid_of { |
105
|
|
|
|
|
|
|
my ($self, $groupname) = @_; |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
use App::LDAP::LDIF::Group; |
108
|
|
|
|
|
|
|
my $group = App::LDAP::LDIF::Group->search( |
109
|
|
|
|
|
|
|
base => config()->{nss_base_group}->[0], |
110
|
|
|
|
|
|
|
scope => config()->{nss_base_group}->[1], |
111
|
|
|
|
|
|
|
filter => "cn=$groupname", |
112
|
|
|
|
|
|
|
); |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
return $group ? $group->gidNumber : $self->create_group($groupname)->gidNumber; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub create_group { |
118
|
|
|
|
|
|
|
my ($self, $groupname) = @_; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
use App::LDAP::Command::Add::Group; |
121
|
|
|
|
|
|
|
local *ARGV = ['add', 'group', $groupname]; |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
App::LDAP::Command::Add::Group->new_with_options->run; |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
127
|
|
|
|
|
|
|
no Moose; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
1; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=pod |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
=head1 NAME |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
App::LDAP::Command::Add::User - handler for adding users |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 SYNOPSIS |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# ldap add user hello --mail hello@example.com |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
# ldap add user mark --mail mark@facebook.com \ |
142
|
|
|
|
|
|
|
--surname Zuckerberg \ |
143
|
|
|
|
|
|
|
--group founder \ |
144
|
|
|
|
|
|
|
--shell zsh \ |
145
|
|
|
|
|
|
|
--home /home/developer/mark |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=cut |