]>
Commit | Line | Data |
---|---|---|
3a3bd871 | 1 | #!/moira/bin/perl |
2 | ||
3 | $host = "maeander"; | |
4 | $ptdump = "/var/local/ptdump"; | |
5 | $skip_to_members = 0; | |
6 | ||
7 | # make sure process is unlimited | |
8 | `csh -fc 'limit datasize'` =~ /datasize\s*(\w*) kbytes/; | |
9 | if ($1 < 10000) { | |
10 | die "Must 'unlimit' before running this script!\n"; | |
11 | } | |
12 | ||
13 | &startquel; | |
14 | &quelcmd("set autocommit on"); | |
15 | ||
16 | if (!$skip_to_members) { | |
17 | ptdumpagain: | |
18 | open(AUSERS, "rsh $host $ptdump 2>&1 |") || die "Couldn't get to $host\n"; | |
19 | ||
20 | while (<AUSERS>) { | |
21 | if (/Ubik Version number changed during execution/) { | |
22 | close AUSERS; | |
23 | undef %AUSERS; | |
24 | print "AFS database changed, trying again.\n"; | |
25 | goto ptdumpagain; | |
26 | } | |
27 | if (/Name: (\w*) ID: (\d*)/ && $2<32767) { | |
28 | $AUSERS{$1} = $2; | |
29 | } | |
30 | } | |
31 | ||
32 | close(AUSERS); | |
33 | print "Got the users from AFS\n"; | |
34 | ||
35 | $AUSERS{"nobody"} = 32767; | |
36 | delete $AUSERS{"anonymous"}; | |
37 | #$AUSERS{"CLU"} = 60; | |
38 | #delete $AUSERS{"clu"}; | |
39 | #$AUSERS{"Saltzer"} = 994; | |
40 | #delete $AUSERS{"saltzer"}; | |
41 | ||
42 | @VAL = &quelcmd("select login, uid from users where status=1 or status=2"); | |
43 | printf("Got %d users from Moira\n", scalar(@VAL)); | |
44 | ||
45 | open(OUT, ">user.add") || die "Couldn't open output file\n"; | |
46 | ||
47 | while ($_ = shift @VAL) { | |
48 | tr/A-Z/a-z/; | |
49 | if ($_ =~ /\|(\w*)\s*\|\s*(\d*)\|/) { | |
50 | if ($AUSERS{$1} == $2) { | |
51 | delete $AUSERS{$1}; | |
52 | } else { | |
53 | print OUT "/moira/bin/pts createuser -name $1 -id $2\n"; | |
54 | } | |
55 | } | |
56 | } | |
57 | ||
58 | close OUT; | |
59 | open(OUT, ">user.del") || die "Couldn't open output file\n"; | |
60 | ||
61 | foreach $login (keys %AUSERS) { | |
62 | print OUT "/moira/bin/pts delete $login\n"; | |
63 | } | |
64 | ||
65 | close OUT; | |
66 | undef @VAL; | |
67 | undef %AUSERS; | |
68 | print "Done processing users\n"; | |
69 | ||
70 | groupagain: | |
71 | open(AGROUP, "rsh $host $ptdump -g 2>&1 |") || die "Couldn't get to $host\n"; | |
72 | ||
73 | while (<AGROUP>) { | |
74 | if (/Ubik Version number changed during execution/) { | |
75 | close AGROUP; | |
76 | undef %AGROUP; | |
77 | print "AFS database changed, trying again.\n"; | |
78 | goto groupagain; | |
79 | } | |
80 | if (/Name: system:(\w*) ID: -(\d*)/ && $2<32767) { | |
81 | $AGROUP{$1} = $2; | |
82 | } | |
83 | } | |
84 | ||
85 | close(AGROUP); | |
86 | print "Got the groups from AFS\n"; | |
87 | ||
88 | $AGROUP{"mit"} = 101; | |
89 | delete $AGROUP{"anyuser"}; | |
90 | #$AGROUP{"CLU"} = 184; | |
91 | #delete $AGROUP{"clu"}; | |
92 | #$AGROUP{"Saltzer"} = 185; | |
93 | #delete $AGROUP{"saltzer"}; | |
94 | ||
95 | @VAL = &quelcmd("select name, gid from list where active=1 and grouplist=1"); | |
96 | printf("Got %d groups from Moira\n", scalar(@VAL)); | |
97 | open(OUT, ">group.add") || die "Couldn't open output file\n"; | |
98 | ||
99 | while ($_ = shift @VAL) { | |
100 | tr/A-Z/a-z/; | |
101 | if ($_ =~ /\|(\w*)\s*\|\s*(\d*)\|/) { | |
102 | if ($AGROUP{$1} == $2) { | |
103 | delete $AGROUP{$1}; | |
104 | } else { | |
105 | print OUT "/moira/bin/pts creategroup -name system:$1 -owner system:administrators -id -$2\n"; | |
106 | } | |
107 | } | |
108 | } | |
109 | ||
110 | close OUT; | |
111 | open(OUT, ">group.del") || die "Couldn't open output file\n"; | |
112 | ||
113 | foreach $group (keys %AGROUP) { | |
114 | print OUT "/moira/bin/pts delete system:$group\n"; | |
115 | } | |
116 | close OUT; | |
117 | ||
118 | undef @VAL; | |
119 | undef %AGROUP; | |
120 | print "Done processing groups\n"; | |
121 | } | |
122 | ||
123 | memberagain: | |
124 | open(AMEM, "rsh $host $ptdump -g -c 2>&1 |") || die "Couldn't get to $host\n"; | |
125 | ||
126 | while (<AMEM>) { | |
127 | if (/Ubik Version number changed during execution/) { | |
128 | close AMEM; | |
129 | undef @AMEM; | |
130 | print "AFS database changed, trying again.\n"; | |
131 | goto memberagain; | |
132 | } | |
133 | if (/Group: system:([^\s]*)/) { | |
134 | $list = $1; | |
135 | } elsif (/Group: /) { | |
136 | $list = ""; | |
137 | } elsif (/Member: ([^\s.]*)$/) { | |
138 | if ($list) { | |
139 | push(@AMEM, $list . " " . $1); | |
140 | } | |
141 | } elsif (/Member\(co\): ([^\s.]*)$/) { | |
142 | if ($list) { | |
143 | push(@AMEM, $list . " " . $1); | |
144 | } | |
145 | } | |
146 | } | |
147 | ||
148 | close(AMEM); | |
149 | printf("Got %d members from AFS\n", scalar(@AMEM)); | |
150 | ||
151 | @VAL = &quelcmd("select l.name, u.login from list l, users u, imembers m where l.list_id=m.list_id and u.users_id=m.member_id and m.member_type='USER' and l.grouplist=1 and l.active=1 and (u.status=1 or u.status=2)"); | |
152 | printf("Got %d members from Moira\n", scalar(@VAL)); | |
153 | ||
154 | while ($_ = shift @VAL) { | |
155 | tr/A-Z/a-z/; | |
156 | if ($_ =~ /\|([^\s\|]*)\s*\|([^\s\|]*)\s*\|/) { | |
157 | push(@MMEM, $1 . " " . $2); | |
158 | } | |
159 | } | |
160 | ||
161 | # throw away column headings | |
162 | pop @MMEM; | |
163 | undef @VAL; | |
164 | @AMEM1 = sort @AMEM; | |
165 | @MMEM1 = sort @MMEM; | |
166 | ||
167 | open(OUT, ">member.del") || die "Couldn't open output file\n"; | |
168 | open(OUT1, ">member.add") || die "Couldn't open output file\n"; | |
169 | ||
170 | while ($#AMEM1 + $#MMEM1 > 0) { | |
171 | if ($AMEM1[0] eq $MMEM1[0]) { | |
172 | shift @AMEM1; | |
173 | shift @MMEM1; | |
174 | } elsif ($AMEM1[0] lt $MMEM1[0] && $#AMEM1 > 0) { | |
175 | ($list, $user) = split(/ /, shift @AMEM1); | |
176 | print OUT "/moira/bin/pts removeuser -user $user -group system:$list\n"; | |
177 | } elsif ($MMEM1[0] lt $AMEM1[0] && $#MMEM1 > 0) { | |
178 | ($list, $user) = split(/ /, shift @MMEM1); | |
179 | if (($user eq "login" && $list eq "name") || $user eq "root") { | |
180 | next; | |
181 | } | |
182 | print OUT1 "/moira/bin/pts adduser -user $user -group system:$list\n"; | |
183 | } else { | |
184 | last; | |
185 | } | |
186 | } | |
187 | ||
188 | close OUT; | |
189 | close OUT1; | |
190 | ||
191 | print "Done processing members.\n"; | |
192 | ||
193 | system("cat member.del group.del user.del user.add group.add member.add > fixit"); | |
194 | ||
195 | &stopquel; | |
196 | exit 0; | |
197 | ||
198 | ||
199 | sub startquel { | |
200 | $SIG{'INT'} = $SIG{'TERM'} = $SIG{'HUP'} = 'handler'; | |
201 | $ENV{"TERM"} = "glass"; | |
202 | pipe(CREAD, QUELIN); | |
203 | pipe(QUELOUT, COUT); | |
204 | select(QUELIN), $|=1; | |
205 | select(STDOUT); | |
206 | $quelpid = fork; | |
207 | if (! $quelpid) { | |
208 | select(COUT), $|=1; | |
209 | open(STDOUT, ">&COUT"); | |
210 | open(STDERR, ">/dev/null"); | |
211 | open(STDIN, "<&CREAD"); | |
212 | exec "sql moira"; | |
213 | exit(1); | |
214 | } | |
215 | close(CREAD); | |
216 | close(COUT); | |
217 | } | |
218 | ||
219 | sub quelcmd { | |
220 | local (@RETVAL); | |
221 | print QUELIN $_[0], "\\g\n"; | |
222 | while (($_ = <QUELOUT>) !~ /Executing \. \. \./) { next; } | |
223 | while (($_ = <QUELOUT>) !~ /^continue$/) { push(@RETVAL, $_); } | |
224 | return @RETVAL; | |
225 | } | |
226 | ||
227 | sub stopquel { | |
228 | print QUELIN "\\q\n"; | |
229 | close QUELIN; | |
230 | close QUELOUT; | |
231 | } | |
232 | ||
233 | sub handler { | |
234 | local($sig) = @_; | |
235 | &stopquel; | |
236 | kill "HUP", $quelpid; | |
237 | print("Caught a signal $sig\n"); | |
238 | exit(1); | |
239 | } |