source: trunk/third/moira/afssync/script.pl @ 23095

Revision 23095, 5.4 KB checked in by ghudson, 16 years ago (diff)
Import the moira package from SIPB Debathena.
Line 
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/;
9if ($1 < 10000) {
10    die "Must 'unlimit' before running this script!\n";
11}
12
13&startquel;
14&quelcmd("set autocommit on");
15
16if (!$skip_to_members) {
17ptdumpagain:
18open(AUSERS, "rsh $host $ptdump 2>&1 |") || die "Couldn't get to $host\n";
19
20while (<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
32close(AUSERS);
33print "Got the users from AFS\n";
34
35$AUSERS{"nobody"} = 32767;
36delete $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");
43printf("Got %d users from Moira\n", scalar(@VAL));
44
45open(OUT, ">user.add") || die "Couldn't open output file\n";
46
47while ($_ = 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
58close OUT;
59open(OUT, ">user.del") || die "Couldn't open output file\n";
60
61foreach $login (keys %AUSERS) {
62    print OUT "/moira/bin/pts delete $login\n";
63}
64
65close OUT;
66undef @VAL;
67undef %AUSERS;
68print "Done processing users\n";
69
70groupagain:
71open(AGROUP, "rsh $host $ptdump -g 2>&1 |") || die "Couldn't get to $host\n";
72
73while (<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
85close(AGROUP);
86print "Got the groups from AFS\n";
87
88$AGROUP{"mit"} = 101;
89delete $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");
96printf("Got %d groups from Moira\n", scalar(@VAL));
97open(OUT, ">group.add") || die "Couldn't open output file\n";
98
99while ($_ = 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
110close OUT;
111open(OUT, ">group.del") || die "Couldn't open output file\n";
112
113foreach $group (keys %AGROUP) {
114    print OUT "/moira/bin/pts delete system:$group\n";
115}
116close OUT;
117
118undef @VAL;
119undef %AGROUP;
120print "Done processing groups\n";
121}
122
123memberagain:
124open(AMEM, "rsh $host $ptdump -g -c 2>&1 |") || die "Couldn't get to $host\n";
125
126while (<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
148close(AMEM);
149printf("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)");
152printf("Got %d members from Moira\n", scalar(@VAL));
153
154while ($_ = 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
162pop @MMEM;
163undef @VAL;
164@AMEM1 = sort @AMEM;
165@MMEM1 = sort @MMEM;
166
167open(OUT, ">member.del") || die "Couldn't open output file\n";
168open(OUT1, ">member.add") || die "Couldn't open output file\n";
169
170while ($#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
188close OUT;
189close OUT1;
190
191print "Done processing members.\n";
192
193system("cat member.del group.del user.del user.add group.add member.add > fixit");
194
195&stopquel;
196exit 0;
197
198
199sub 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
219sub 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
227sub stopquel {
228        print QUELIN "\\q\n";
229        close QUELIN;
230        close QUELOUT;
231}
232
233sub  handler {
234        local($sig) = @_;
235        &stopquel;
236        kill "HUP", $quelpid;
237        print("Caught a signal $sig\n");
238        exit(1);
239}
Note: See TracBrowser for help on using the repository browser.