Import Upstream version 1.8.5
[hcoop/debian/openafs.git] / src / tests / OpenAFS / pts.pm
1 # CMUCS AFStools
2 # Copyright (c) 1996, Carnegie Mellon University
3 # All rights reserved.
4 #
5 # See CMU_copyright.ph for use and distribution information
6 #
7 #: * pts.pm - Wrappers around PTS commands (user/group maintenance)
8 #: * This module provides wrappers around the various PTS commands, giving
9 #: * them a nice perl-based interface. Someday, they might talk to the
10 #: * ptserver directly instead of using 'pts', but not anytime soon.
11 #:
12
13 package OpenAFS::pts;
14 use OpenAFS::CMU_copyright;
15 use OpenAFS::util qw(:DEFAULT :afs_internal);
16 use OpenAFS::wrapper;
17 use Exporter;
18
19 $VERSION = '';
20 $VERSION = '1.00';
21 @ISA = qw(Exporter);
22 @EXPORT = qw(&AFS_pts_createuser &AFS_pts_listmax
23 &AFS_pts_creategroup &AFS_pts_setmax
24 &AFS_pts_delete &AFS_pts_add
25 &AFS_pts_rename &AFS_pts_remove
26 &AFS_pts_examine &AFS_pts_members
27 &AFS_pts_chown &AFS_pts_listown
28 &AFS_pts_setf);
29
30
31 #: AFS_pts_createuser($user, [$id], [$cell])
32 #: Create a PTS user with $user as its name.
33 #: If specified, use $id as the PTS id; otherwise, AFS picks one.
34 #: If specified, operate in cell $cell instead of the default cell.
35 #: On success, return the PTS id of the newly-created user.
36 #:
37 $AFS_Help{pts_createuser} = '$user, [$id], [$cell] => $uid';
38 sub AFS_pts_createuser {
39 my($user, $id, $cell) = @_;
40 my(@args, $uid);
41
42 @args = ('createuser', '-name', $user);
43 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
44 push(@args, '-id', $id) if ($id);
45 &wrapper('pts', \@args, [[ '^User .* has id (\d+)', \$uid ]]);
46 $uid;
47 }
48
49
50 #: AFS_pts_creategroup($group, [$id], [$owner], [$cell])
51 #: Create a PTS group with $group as its name.
52 #: If specified, use $id as the PTS id; otherwise, AFS picks one.
53 #: If specified, use $owner as the owner, instead of the current user.
54 #: If specified, operate in cell $cell instead of the default cell.
55 #: On success, return the PTS id of the newly-created group.
56 #:
57 $AFS_Help{pts_creategroup} = '$group, [$id], [$owner], [$cell] => $gid';
58 sub AFS_pts_creategroup {
59 my($group, $id, $owner, $cell) = @_;
60 my(@args, $uid);
61
62 @args = ('creategroup', '-name', $group);
63 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
64 push(@args, '-id', $id) if ($id);
65 push(@args, '-owner', $owner) if ($owner);
66 &wrapper('pts', \@args, [[ '^group .* has id (\-\d+)', \$uid ]]);
67 $uid;
68 }
69
70
71 #: AFS_pts_delete(\@objs, [$cell])
72 #: Attempt to destroy PTS objects listed in @objs.
73 #: If specified, operate in cell $cell instead of the default cell.
74 #: On success, return 1.
75 #: If multiple objects are specified and only some are destroyed, some
76 #: operations may be left untried.
77 #:
78 $AFS_Help{pts_delete} = '\@objs, [$cell] => Success?';
79 sub AFS_pts_delete {
80 my($objs, $cell) = @_;
81 my(@args);
82
83 @args = ('delete', '-nameorid', @$objs);
84 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
85 &wrapper('pts', \@args);
86 1;
87 }
88
89
90 #: AFS_pts_rename($old, $new, [$cell])
91 #: Rename the PTS object $old to have the name $new.
92 #: If specified, operate in cell $cell instead of the default cell.
93 #: On success, return 1.
94 #:
95 $AFS_Help{pts_rename} = '$old, $new, [$cell] => Success?';
96 sub AFS_pts_rename {
97 my($old, $new, $cell) = @_;
98 my(@args);
99
100 @args = ('rename', '-oldname', $old, '-newname', $new);
101 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
102 &wrapper('pts', \@args);
103 1;
104 }
105
106
107 #: AFS_pts_examine($obj, [$cell])
108 #: Examine the PTS object $obj, and return information about it.
109 #: If specified, operate in cell $cell instead of the default cell.
110 #: On success, return an associative array with some or all of the following:
111 #: - name Name of this object
112 #: - id ID of this object
113 #: - owner Name or ID of owner
114 #: - creator Name or ID of creator
115 #: - mem_count Number of members (group) or memberships (user)
116 #: - flags Privacy/access flags (as a string)
117 #: - group_quota Remaining group quota
118 #:
119 $AFS_Help{pts_examine} = '$obj, [$cell] => %info';
120 sub AFS_pts_examine {
121 my($obj, $cell) = @_;
122 my(@args);
123
124 @args = ('examine', '-nameorid', $obj);
125 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
126 &wrapper('pts', \@args,
127 [[ '^Name\: (.*)\, id\: ([\-0-9]+)\, owner\: (.*)\, creator\: (.*)\,$', #',
128 'name', 'id', 'owner', 'creator' ],
129 [ '^ membership\: (\d+)\, flags\: (.....)\, group quota\: (\d+)\.$', #',
130 'mem_count', 'flags', 'group_quota' ]
131 ]);
132 }
133
134
135 #: AFS_pts_chown($obj, $owner, [$cell])
136 #: Change the owner of the PTS object $obj to be $owner.
137 #: If specified, operate in cell $cell instead of the default cell.
138 #: On success, return 1.
139 #:
140 $AFS_Help{pts_chown} = '$obj, $owner, [$cell] => Success?';
141 sub AFS_pts_chown {
142 my($obj, $owner, $cell) = @_;
143 my(@args);
144
145 @args = ('chown', '-name', $obj, '-owner', $owner);
146 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
147 &wrapper('pts', \@args);
148 1;
149 }
150
151
152 #: AFS_pts_setf($obj, [$access], [$gquota], [$cell])
153 #: Change the access flags and/or group quota for the PTS object $obj.
154 #: If specified, $access specifies the new access flags in the standard 'SOMAR'
155 #: format; individual flags may be specified as '.' to keep the current value.
156 #: If specified, $gquota specifies the new group quota.
157 #: If specified, operate in cell $cell instead of the default cell.
158 #: On success, return 1.
159 #:
160 $AFS_Help{pts_setf} = '$obj, [$access], [$gquota], [$cell] => Success?';
161 sub AFS_pts_setf {
162 my($obj, $access, $gquota, $cell) = @_;
163 my(%result, @args);
164
165 @args = ('setfields', '-nameorid', $obj);
166 push(@args, '-groupquota', $gquota) if ($gquota ne '');
167 if ($access) {
168 my(@old, @new, $i);
169 # Ensure access is 5 characters
170 if (length($access) < 5) {
171 $access .= ('.' x (5 - length($access)));
172 } elsif (length($access) > 5) {
173 substr($access, 5) = '';
174 }
175
176 %result = &AFS_pts_examine($obj, $cell);
177
178 @old = split(//, $result{'flags'});
179 @new = split(//, $access);
180 foreach $i (0 .. 4) {
181 $new[$i] = $old[$i] if ($new[$i] eq '.');
182 }
183 $access = join('', @new);
184 if ($access ne $result{'flags'}) {
185 push(@args, '-access', $access);
186 }
187 }
188 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
189 &wrapper('pts', \@args);
190 1;
191 }
192
193
194 #: AFS_pts_listmax([$cell])
195 #: Fetch the maximum assigned group and user ID.
196 #: If specified, operate in cell $cell instead of the default cell.
197 #: On success, returns (maxuid, maxgid)
198 #:
199 $AFS_Help{pts_listmax} = '[$cell] => ($maxuid, $maxgid)';
200 sub AFS_pts_listmax {
201 my($cell) = @_;
202 my(@args, $uid, $gid);
203
204 @args = ('listmax');
205 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
206 &wrapper('pts', \@args,
207 [[ '^Max user id is (\d+) and max group id is (\-\d+).',
208 \$uid, \$gid ]]);
209 ($uid, $gid);
210 }
211
212
213 #: AFS_pts_setmax([$maxuser], [$maxgroup], [$cell])
214 #: Set the maximum assigned group and/or user ID.
215 #: If specified, $maxuser is the new maximum user ID
216 #: If specified, $maxgroup is the new maximum group ID
217 #: If specified, operate in cell $cell instead of the default cell.
218 #: On success, return 1.
219 #:
220 $AFS_Help{pts_setmax} = '[$maxuser], [$maxgroup], [$cell] => Success?';
221 sub AFS_pts_setmax {
222 my($maxuser, $maxgroup, $cell) = @_;
223 my(@args);
224
225 @args = ('setmax');
226 push(@args, '-group', $maxgroup) if ($maxgroup);
227 push(@args, '-user', $maxuser) if ($maxuser);
228 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
229 &wrapper('pts', \@args);
230 1;
231 }
232
233 #: AFS_pts_add(\@users, \@groups, [$cell])
234 #: Add users specified in @users to groups specified in @groups.
235 #: If specified, operate in cell $cell instead of the default cell.
236 #: On success, return 1.
237 #: If multiple users and/or groups are specified and only some memberships
238 #: are added, some operations may be left untried.
239 #:
240 $AFS_Help{pts_add} = '\@users, \@groups, [$cell] => Success?';
241 sub AFS_pts_add {
242 my($users, $groups, $cell) = @_;
243 my(@args);
244
245 @args = ('adduser', '-user', @$users, '-group', @$groups);
246 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
247 &wrapper('pts', \@args);
248 1;
249 }
250
251
252 #: AFS_pts_remove(\@users, \@groups, [$cell])
253 #: Remove users specified in @users from groups specified in @groups.
254 #: If specified, operate in cell $cell instead of the default cell.
255 #: On success, return 1.
256 #: If multiple users and/or groups are specified and only some memberships
257 #: are removed, some operations may be left untried.
258 #:
259 $AFS_Help{pts_remove} = '\@users, \@groups, [$cell] => Success?';
260 sub AFS_pts_remove {
261 my($users, $groups, $cell) = @_;
262 my(@args);
263
264 @args = ('removeuser', '-user', @$users, '-group', @$groups);
265 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
266 &wrapper('pts', \@args);
267 1;
268 }
269
270
271 #: AFS_pts_members($obj, [$cell])
272 #: If $obj specifies a group, retrieve a list of its members.
273 #: If $obj specifies a user, retrieve a list of groups to which it belongs.
274 #: If specified, operate in cell $cell instead of the default cell.
275 #: On success, return the resulting list.
276 #:
277 $AFS_Help{pts_members} = '$obj, [$cell] => @members';
278 sub AFS_pts_members {
279 my($obj, $cell) = @_;
280 my(@args, @grouplist);
281
282 @args = ('membership', '-nameorid', $obj);
283 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
284 &wrapper('pts', \@args, [[ '^ (.*)', \@grouplist ]]);
285 @grouplist;
286 }
287
288
289 #: AFS_pts_listown($owner, [$cell])
290 #: Retrieve a list of PTS groups owned by the PTS object $obj.
291 #: If specified, operate in cell $cell instead of the default cell.
292 #: On success, return the resulting list.
293 #:
294 $AFS_Help{pts_listown} = '$owner, [$cell] => @owned';
295 sub AFS_pts_listown {
296 my($owner, $cell) = @_;
297 my(@args, @grouplist);
298
299 @args = ('listowned', '-nameorid', $owner);
300 push(@args, '-cell', $cell ? $cell : $AFS_Parms{'cell'});
301 &wrapper('pts', \@args, [[ '^ (.*)', \@grouplist ]]);
302 @grouplist;
303 }
304
305
306 1;