]>
git.cameronkatri.com Git - pw-darwin.git/blob - adduser/addgroup.tcl
2 # Copyright (c) 1996 Wolfram Schneider <wosch@FreeBSD.org>. Berlin.
5 # addgroup - add a group or add users to a group
7 # addgroup [-g gid] group [user[,user,...]]
10 # addgroup -g 2000 foobar
12 # Add group `foobar' to group database. Group id is 2000 if
13 # possible or higher. Don't add group `foobar' if `foobar' is
14 # already in group database.
17 # addgroup foo blech,bar
19 # Add user `blech' and user `bar' to group `foo'. Create group
20 # `foo' with default gid if not exists.
23 # The option [-g gid] is only for new groups.
30 # add only users who exist
34 # set global variables
35 set etc_group
"/etc/group"; #set etc_group "/usr/tmp/group"
39 proc putsErr
{string} {
40 if {[catch {open "/dev/stderr" w
} stderr
]} {
49 putsErr
{usage
: addgroup group
[user
]}
50 putsErr
{ addgroup
[-g gid
] group
[user
[,user
,...
]]}
54 # check double user names: foo,bla,foo
55 proc double_name
{groupmembers
} {
56 set l
[split $groupmembers ","]
57 if {[llength $l] > 1} {
58 for {set i
0} {$i < [llength $l]} {incr i
} {
59 if {[lsearch [lrange $l [expr $i + 1] end
] \
60 [lindex $l $i]] != -1} {
61 putsErr
"Double user name: [lindex $l $i]"
71 upvar etc_group_new new
77 if {$argc < 1} { usage
}
80 switch -glob -- [lindex $argv 0] {
83 putsErr
"Missing group id"
86 set g
[lindex $argv 1]
87 if {$g < 100 ||
$g >= $gid_max} {
88 putsErr
"Group id out of range 100 < $g < $gid_max"
93 set argv
[lrange $argv 2 end
]
98 if {$argc < 1} { usage
}
101 set groupname
[lindex $argv 0]
102 if {[string match
"*:*" $groupname] != 0} {
103 putsErr
"Colon are not allowed in group name: ``$groupname''"
107 # read optional group members
109 set groupmembers
[lindex $argv 1]
110 if {[string match
"*:*" $groupmembers] != 0} {
111 putsErr
"Colon are not allowed in user names: ``$groupmembers''"
114 if {[double_name
$groupmembers] != 0} {
122 # open /etc/group database
123 if {[catch {open $etc_group r
} db
]} {
127 # open temporary database
128 set etc_group_new
"$etc_group.new";
129 if {[catch {open $etc_group_new w
} db_new
]} {
134 while {[gets $db line
] >= 0 } {
140 # ``group:passwd:gid:member''
142 set l
[split $line ":"]
143 set group
([lindex $l 0]) [lindex $l 2]
144 set gid
([lindex $l 2]) [lindex $l 0]
145 set member
([lindex $l 0]) [lindex $l 3]
147 # found existing group
148 if {[string compare
[lindex $l 0] $groupname] == 0} {
149 if {[string compare
$groupmembers ""] == 0} {
150 Err
"Group exists: ``$groupname''"
153 # add new group members
156 # group with no group members?
157 if {[string compare
$y ""] == 0} {
158 puts $db_new "$line$groupmembers"
160 if {[double_name
"$y,$groupmembers"] != 0} {
161 Err
"\t$line,$groupmembers"
163 puts $db_new "$line,$groupmembers"
174 for {set i
$gid_start} {$i < $gid_max} {incr i
} {
175 if {[info exists gid
($i)] == 0} {
176 puts $db_new "$groupname:*:$i:$groupmembers"
184 Err
"Cannot find free group id: ``$groupname''"
190 exec cp
-pf $etc_group "$etc_group.bak"
191 exec mv
-f $etc_group_new $etc_group