forked from briandfoy/PerlPowerTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid
executable file
·234 lines (187 loc) · 5.48 KB
/
id
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/perl
=begin metadata
Name: id
Description: show user information
Author: Theo Van Dinter, felicity@kluge.net
License:
=end metadata
=cut
#
# An implementation of the 'id' utility in Perl. Written for the Perl Power
# Tools (PPT) project by Theo Van Dinter (felicity@kluge.net).
#
# $Id: id,v 1.2 2004/08/05 14:17:43 cwest Exp $
#
use strict;
use Getopt::Std;
use vars qw($opt_G $opt_n $opt_u $opt_g $opt_r $opt_a $opt_p);
getopts('Gnugrap') or help();
if ( ($opt_G + $opt_g + $opt_p + $opt_u) > 1 ) {
print STDERR "You may only choose one of -G, -g, -p, or -u. Doh!\n\n";
&help;
}
my($user,$pw,$uid,$gid,$tp);
if ( @ARGV ) { # user specified
($user,$pw,$uid,$gid) = getpwnam $ARGV[0];
if (!defined($uid) && $ARGV[0] =~ m/\A[0-9]+\Z/) {
($user,$pw,$uid,$gid) = getpwuid $ARGV[0];
}
die "id: $ARGV[0]: No such user\n" unless (defined $uid);
}
if ( $opt_u ) { # print uid
$tp = defined $uid ? $uid : $opt_r ? $< : $>;
$tp = scalar getpwuid $tp || $tp if ( $opt_n );
}
elsif ( $opt_g ) { # print gid
$tp = ($gid)?$gid:(split(/\s+/,($opt_r)?$(:$)))[0];
$tp = scalar getgrgid $tp || $tp if ( $opt_n );
}
elsif ( $opt_p ) { # human-readable form (names when possible, etc.)
my($rgid,@rgids);
if ( $user ) {
$tp.="uid $user\n";
$tp.="rgid $gid\n";
@rgids=($gid);
while ( my($name,$pw,$gid,$members) = getgrent ) {
push(@rgids,$gid) if ( grep($_ eq $user,split(/\s+/,$members)) );
}
}
else {
my($login) = getlogin || die "getlogin failed!";
$tp.="login $login\n" if ( $login ne scalar getpwuid $< );
my($uid) = scalar getpwuid $< || $<;
$tp.="uid $uid\n";
my($euid) = scalar getpwuid $> || $>;
$tp.="euid $euid\n" if ( $< != $> );
($rgid,@rgids)=split(/\s+/,$();
my($egid)=split(/\s+/,$));
my($nrgid) = scalar getgrgid $rgid || $rgid;
my($negid) = scalar getgrgid $egid || $egid;
$tp.="rgid $nrgid\n";
$tp.="egid $negid\n" if ( $rgid != $egid );
}
my(%done);
foreach ( @rgids ) {
$done{$_} = scalar getgrgid $_ || $_;
}
$tp.=join(" ","groups",values %done);
}
elsif ( $opt_G ) { # print full group info
my(%done);
my(@rgids);
if ( $user ) {
@rgids=($gid);
while ( my($name,$pw,$gid,$members) = getgrent ) {
push(@rgids,$gid) if ( grep($_ eq $user,split(/\s+/,$members)) );
}
}
else {
@rgids = split(/\s+/, $();
}
foreach ( @rgids ) {
if ( $opt_n ) {
$done{$_} = scalar getgrgid $_ || $_;
}
else {
$done{$_} = $_;
}
}
$tp=join(" ",values %done);
}
else { # uid=#(name) gid=#(name) euid=#(name) egid=#(name) groups=#(name) ...
my($rgid,@rgids,$egid,$nruid,$neuid,$nrgid,$negid);
if ( $user ) {
$egid = $rgid = $gid;
@rgids=($gid);
while ( my($name,$pw,$gid,$members) = getgrent ) {
push(@rgids,$gid) if ( grep($_ eq $user,split(/\s+/,$members)) );
}
$nruid = $user;
$nrgid = scalar getgrgid $gid;
}
else {
($rgid,@rgids)=split(/\s+/,$();
$egid = (split(/\s+/,$)))[0];
$nruid = scalar getpwuid $<;
$neuid = scalar getpwuid $>;
$nrgid = scalar getgrgid $rgid;
$negid = scalar getgrgid $egid;
}
$tp=join("=","uid",($user)?$uid:$<);
$tp.=($nruid)?"($nruid) ":" ";
if ( !($user) && ($< != $>) ) {
$tp.="euid=$>";
$tp.=($neuid)?"($neuid) ":" ";
}
$tp.=join("=","gid",($user)?$gid:$rgid);
$tp.=($nrgid)?"($nrgid) ":" ";
if ( $rgid != $egid ) {
$tp.="egid=$egid";
$tp.=($negid)?"($negid) ":" ";
}
my(%done);
$tp.="groups=";
foreach ( @rgids ) {
my($i) = scalar getgrgid $_;
my($i2) = "$_";
$i2 .= "($i)" if ( $i );
$done{$_} = "$i2";
}
$tp.=join(",",values %done);
}
print "$tp\n";
exit 0;
sub help {
require Pod::Usage;
Pod::Usage::pod2usage({ -exitval => 1, -verbose => 0 });
}
=head1 NAME
id - show user information
=head1 SYNOPSIS
id [user]
id -G [-n] [user]
id -g [-nr] [user]
id -u [-nr] [user]
id -p [user]
=head1 DESCRIPTION
id displays the user and group names and numeric IDs of the calling
process. If the real and effective IDs are different, both are displayed,
otherwise only the real ID is displayed.
If a user (username or user ID) is specified, the information about that user
is displayed instead of the information from the calling process.
=head1 OPTIONS AND ARGUMENTS
=over 4
=item I<-G>
Display all of the group IDs (effective, real, etc) separated by a space.
The IDs are not in a given order.
=item I<-a>
Nothing. The option is included for compatibility with some versions of id.
=item I<-g>
Display the effective group ID.
=item I<-n>
Force the options C<-G>, C<-g> and C<-u> to display the matching name
instead of the number for the user and group IDs. If any of the ID
numbers do not have a matching name, the number will be displayed
as usual.
=item I<-p>
Display the user/group information on separate lines. If the username
returned by getlogin is different than the username associated with
the calling program's user ID, it is displayed preceded by the phrase
"login". The real ID, effective user ID (if different), real group ID,
and effective group ID (if different) are displayed preceded by "uid",
"euid", "rgid", and "egid" respectively. Finally, group membership
is displayed with each group separated by a space. All information is
displayed as names unless there is no name matching the ID. Then the
ID is shown as usual.
=item I<-r>
Force the options C<-g> and <-u> to display the information associated
with the real user/group IDs instead of the effective ID information.
=item I<-u>
Display the effective user ID.
=back
=head1 NOTES
id returns 0 on success or 1 if an error occurred.
=head1 AUTHOR
Theo Van Dinter (felicity@kluge.net)
=head1 SEE ALSO
who(1)