]> andersk Git - moira.git/blob - dbck/phase1.dc
a1a6096b58cabceac2c58f79975ad696be5ca9d5
[moira.git] / dbck / phase1.dc
1 /* $Header$
2  *
3  *  (c) Copyright 1988 by the Massachusetts Institute of Technology.
4  *  For copying and distribution information, please see the file
5  *  <mit-copyright.h>.
6  */
7
8 #include <mit-copyright.h>
9 #include <moira.h>
10 #include "dbck.h"
11 EXEC SQL INCLUDE sqlca;
12
13
14 static char phase1_qc_rcsid[] = "$Header$";
15
16
17 show_user_id(u)
18 struct user *u;
19 {
20     printf("User %s (%s, status %d) has duplicate ID\n",
21            u->login, u->fullname, u->status);
22     return(0);
23 }
24
25 handle_duplicate_logins(sq)
26 struct save_queue *sq;
27 {
28     struct user *u, *uu, *tmp;
29
30     uu = (struct user *)0;
31     if(sq_get_data(sq,&uu)) {
32       while (sq_get_data(sq, &u)) {
33         if (!strcmp(u->login, uu->login)) {
34             if (uu->status == 1 || u->status == 0) {
35                 tmp = u;
36                 u = uu;
37                 uu = tmp;
38             }
39             printf("User %s (%s, status %d) and\n",
40                    u->login, u->fullname, u->status);
41             printf("User %s (%s, status %d) have duplicate logins\n",
42                    uu->login, uu->fullname, uu->status);
43             if (!strcmp(u->fullname, uu->fullname) &&
44                 single_fix("Delete the second one")) {
45                 single_delete("users", "users_id", uu->users_id);
46             } else if (single_fix("Unregister the second one"))
47               {
48                 EXEC SQL BEGIN DECLARE SECTION; 
49                 int id = uu->users_id, rowcount;
50                 EXEC SQL END DECLARE SECTION; 
51
52                 EXEC SQL UPDATE users SET login = '#'+CHAR(users.uid),
53                     status=0 WHERE users_id = :id;
54                 EXEC SQL INQUIRE_SQL(:rowcount = rowcount);
55                 if (rowcount > 0)
56                   printf("%d entr%s fixed\n", rowcount, rowcount==1?"y":"ies");
57                 else
58                   printf("Not fixed\n");
59                 modified("users");
60               }
61         } else {
62             uu = u;
63         }
64       }
65     }
66 }
67
68 fix_user_id(u)
69 struct user *u;
70 {
71     u->users_id = generic_fix_id("users", "users_id", "login",
72                                  u->users_id, u->login);
73 }
74
75
76 cant_fix(id)
77 int id;
78 {
79     printf("Sorry, don't know how to fix that\n");
80 }
81
82 show_mach_id(m)
83 struct machine *m;
84 {
85     printf("Machine %s has duplicate ID %d\n", m->name, m->mach_id);
86     return(0);
87 }
88
89 show_mach_name(m)
90 struct machine *m;
91 {
92     printf("Machine %s (%d) has duplicate name\n", m->name, m->mach_id);
93     return(0);
94 }
95
96 fix_mach_id(m)
97 struct machine *m;
98 {
99     m->mach_id = generic_fix_id("machine", "mach_id", "name",
100                                 m->mach_id, m->name);
101 }
102
103 show_clu_id(c)
104 struct cluster *c;
105 {
106     printf("Cluster %s has duplicate ID %d\n", c->name, c->clu_id);
107     return(0);
108 }
109
110 show_clu_name(c)
111 struct cluster *c;
112 {
113     printf("Cluster %s (%d) has duplicate name\n", c->name, c->clu_id);
114     return(0);
115 }
116
117 fix_clu_id(c)
118 struct cluster *c;
119 {
120     c->clu_id = generic_fix_id("cluster", "clu_id", "name", c->clu_id, c->name);
121 }
122
123 show_list_id(l)
124 struct list *l;
125 {
126     printf("List %s has duplicate ID %d\n", l->name, l->list_id);
127     return(0);
128 }
129
130 show_list_name(l)
131 struct list *l;
132 {
133     printf("List %s (%d) has duplicate name\n", l->name, l->list_id);
134     return(0);
135 }
136
137 fix_list_id(l)
138 struct list *l;
139 {
140     l->list_id = generic_fix_id("list", "list_id", "name", l->list_id, l->name);
141 }
142
143 show_fs_id(f)
144 struct filesys *f;
145 {
146     printf("Filesys %s has duplicate ID %d\n", f->name, f->filsys_id);
147     return(0);
148 }
149
150 fix_fs_id(f)
151 struct filesys *f;
152 {
153     f->filsys_id = generic_fix_id("filesys", "filsys_id", "label",
154                                   f->filsys_id, f->name);
155 }
156
157
158 show_np_id(n)
159 struct nfsphys *n;
160 {
161     printf("NfsPhys %s:%s has duplicate ID %d\n",
162            ((struct machine *)hash_lookup(machines, n->mach_id))->name,
163            n->dir, n->nfsphys_id);
164     return(0);
165 }
166
167 fix_np_id(n)
168 struct nfsphys *n;
169 {
170     n->nfsphys_id = generic_fix_id("nfsphys", "nfsphys_id", "dir",
171                                    n->nfsphys_id, n->dir);
172 }
173
174 show_str_id(s)
175 struct string *s;
176 {
177     printf("String %s has duplicate ID %d\n", s->name, s->string_id);
178     return(0);
179 }
180
181
182 phase1()
183 {
184     EXEC SQL BEGIN DECLARE SECTION; 
185     char name[81], name1[81], last[17], first[17], buf[257];
186     int id, id2, id3, aid, aid2, status, sid, sid2, sid3, sid4, sid5;
187     EXEC SQL END DECLARE SECTION; 
188     struct save_queue *sq;
189     struct user *u;
190     struct machine *m;
191     struct list *l;
192     struct cluster *c;
193     struct string *s;
194     struct filesys *f;
195     struct nfsphys *n;
196
197     printf("Phase 1 - Looking for duplicates\n");
198
199     dprintf("Loading strings...\n");
200     sq = sq_create();
201     strings = create_hash(5000);
202
203     EXEC SQL DECLARE csr101 CURSOR FOR
204       SELECT string_id, string FROM strings ORDER BY string_id;
205     if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
206     EXEC SQL OPEN csr101;
207     while(1) {
208         EXEC SQL FETCH csr101 INTO :id, :buf;
209         if (sqlca.sqlcode != 0) {
210           ingerr(&sqlca.sqlcode);
211           break;
212         }
213
214         s = (struct string *) malloc(sizeof(struct string));
215         if (s == NULL)
216           out_of_mem("storing strings");
217         s->name = strsave(strtrim(buf));
218         s->string_id = id;
219         s->refc = 0;
220         if (hash_store(strings, id, s)) {
221             sq_save_data(sq, hash_lookup(strings, id));
222             sq_save_data(sq, s);
223         }
224     }
225     EXEC SQL CLOSE csr101;
226     generic_delete(sq, show_str_id, "strings", "string_id", 0);
227     string_check(0);
228
229     dprintf("Loading users...\n");
230     sq = sq_create();
231     users = create_hash(10000);
232
233     EXEC SQL DECLARE csr102 CURSOR FOR 
234       SELECT users_id, login, last, first, status, potype, pop_id, box_id,
235           modby, fmodby, pmodby, comment, sigwho FROM users ORDER BY users_id;
236     if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
237     EXEC SQL OPEN csr102;
238     while(1) {
239         EXEC SQL FETCH csr102 INTO :id, :name, :last, :first, :status,
240             :buf, :id2, :id3, :sid, :sid2, :sid3, :sid4, :sid5;
241         if (sqlca.sqlcode != 0) {
242           ingerr(&sqlca.sqlcode);
243           break;
244         }
245
246         u = (struct user *) malloc(sizeof(struct user));
247         if (u == NULL)
248           out_of_mem("storing users");
249         strcpy(u->login, strtrim(name));
250         u->potype = buf[0];
251         sprintf(buf, "%s, %s", strtrim(last), strtrim(first));
252         u->fullname = strsave(buf);
253         u->status = status;
254         u->users_id = id;
255         switch (u->potype) {
256         case 'P':
257             u->pobox_id = id2;
258             break;
259         case 'S':
260             u->pobox_id = id3;
261             break;
262         default:
263             u->pobox_id = 0;
264         }
265         if (hash_store(users, id, u)) {
266             sq_save_data(sq, hash_lookup(users, id));
267             sq_save_data(sq, u);
268         }
269         if (sid < 0)
270           string_check(-sid);
271         if (sid2 < 0)
272           string_check(-sid2);
273         if (sid3 < 0)
274           string_check(-sid3);
275         if (sid4)
276           string_check(sid4);
277         if (sid5)
278           string_check(sid5);
279     }
280     EXEC SQL CLOSE csr102; 
281
282     generic_fix(sq, show_user_id, "Change ID", fix_user_id, 0);
283
284     if (!fast) {
285         sq = sq_create();
286
287         EXEC SQL DECLARE csr103 CURSOR FOR 
288             SELECT u1.users_id FROM users u1, users u2
289             WHERE u1.login = u2.login and u1.tid != u2.tid;
290         if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
291         EXEC SQL OPEN csr103;
292         while(1) {
293             EXEC SQL FETCH csr103 INTO :id;
294             if (sqlca.sqlcode != 0) {
295                 ingerr(&sqlca.sqlcode);
296                 break;
297             }
298             sq_save_data(sq, hash_lookup(users, id));
299         }
300         EXEC SQL CLOSE csr103; 
301         handle_duplicate_logins(sq);
302     }
303
304     dprintf("Loading machines...\n");
305     machines = create_hash(1000);
306     sq = sq_create();
307
308     EXEC SQL DECLARE csr104 CURSOR FOR
309         SELECT mach_id, name, modby FROM machine ORDER BY mach_id;
310     if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
311     EXEC SQL OPEN csr104;
312     while(1) {
313         EXEC SQL FETCH csr104 INTO :id, :name, :sid;
314         if (sqlca.sqlcode != 0) {
315           ingerr(&sqlca.sqlcode);
316           break;
317         } 
318
319         m = (struct machine *) malloc(sizeof(struct machine));
320         if (m == NULL)
321           out_of_mem("storing machines");
322         strcpy(m->name, strtrim(name));
323         m->mach_id = id;
324         m->clucount = 0;
325         if (hash_store(machines, id, m)) {
326             sq_save_data(sq, hash_lookup(machines, id));
327             sq_save_data(sq, m);
328         }
329         if (sid < 0)
330           string_check(-sid);
331     }
332     EXEC SQL CLOSE csr104; 
333     generic_fix(sq, show_mach_id, "Change ID", fix_mach_id, 0);
334
335     if (!fast) {
336         sq = sq_create();
337
338         EXEC SQL DECLARE csr105 CURSOR FOR
339             SELECT m1.mach_id FROM machine m1, machine m2
340                 WHERE m1.name = m2.name AND m1.tid != m2.tid;
341         if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
342         EXEC SQL OPEN csr105;
343         while(1) {
344             EXEC SQL FETCH csr105 INTO :id;
345             if (sqlca.sqlcode != 0) {
346                 ingerr(&sqlca.sqlcode);
347                 break;
348             } 
349
350             sq_save_data(sq, hash_lookup(machines, id));
351         }
352         EXEC SQL CLOSE csr105; 
353         generic_fix(sq, show_mach_name, "Change name", cant_fix, 0);
354     }   
355
356     dprintf("Loading clusters...\n");
357     sq = sq_create();
358     clusters = create_hash(100);
359
360     EXEC SQL DECLARE csr106 CURSOR FOR
361         SELECT clu_id, name, modby FROM cluster;
362     if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
363     EXEC SQL OPEN csr106;
364     while(1) {
365         EXEC SQL FETCH csr106 INTO :id, :name, :sid;
366         if (sqlca.sqlcode != 0) {
367           ingerr(&sqlca.sqlcode);
368           break;
369         } 
370
371         c = (struct cluster *) malloc(sizeof(struct cluster));
372         if (c == NULL)
373           out_of_mem("storing clusters");
374         strcpy(c->name, strtrim(name));
375         c->clu_id = id;
376         if (hash_store(clusters, id, c)) {
377             sq_save_data(sq, hash_lookup(clusters, id));
378             sq_save_data(sq, c);
379         }
380         if (sid < 0)
381           string_check(-sid);
382     }
383     EXEC SQL CLOSE csr106; 
384     generic_fix(sq, show_clu_id, "Change ID", fix_clu_id, 0);
385
386     if (!fast) {
387         sq = sq_create();
388
389         EXEC SQL DECLARE csr107 CURSOR FOR
390             SELECT c1.clu_id FROM cluster c1, cluster c2
391                 WHERE c1.name=c2.name AND c1.tid != c2.tid;
392         if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
393         EXEC SQL OPEN csr107;
394         while(1) {
395             EXEC SQL FETCH csr107 INTO :id;
396             if (sqlca.sqlcode != 0) {
397                 ingerr(&sqlca.sqlcode);
398                 break;
399             } 
400
401             sq_save_data(sq, hash_lookup(clusters, id));
402         }
403         EXEC SQL CLOSE csr107; 
404         generic_fix(sq, show_clu_name, "Change name", cant_fix, 0);
405     }
406
407     dprintf("Loading lists...\n");
408     sq = sq_create();
409     lists = create_hash(10000);
410
411     EXEC SQL DECLARE csr108 CURSOR FOR
412         SELECT list_id, name, acl_id, acl_type, modby  FROM list 
413         ORDER BY list_id;
414     if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
415     EXEC SQL OPEN csr108;
416     while(1) {
417         EXEC SQL FETCH csr108 INTO :id, :name, :aid, :buf, :sid;
418         if (sqlca.sqlcode != 0) {
419           ingerr(&sqlca.sqlcode);
420           break;
421         }
422         l = (struct list *) malloc(sizeof(struct list));
423         if (l == NULL)
424           out_of_mem("storing lists");
425         strcpy(l->name, strtrim(name));
426         l->acl_type = buf[0];
427         l->acl_id = aid;
428         l->list_id = id;
429         l->members = 0;
430         if (hash_store(lists, id, l)) {
431             sq_save_data(sq, hash_lookup(lists, id));
432             sq_save_data(sq, l);
433         }
434         if (sid < 0)
435           string_check(-sid);
436     }
437     EXEC SQL CLOSE csr108; 
438     generic_fix(sq, show_list_id, "Change ID", fix_list_id, 0);
439
440     if (!fast) {
441         sq = sq_create();
442
443         EXEC SQL DECLARE csr109 CURSOR FOR
444             SELECT l1.list_id FROM list l1, list l2
445                 WHERE l1.name=l2.name AND l1.tid != l2.tid;
446         if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
447         EXEC SQL OPEN csr109;
448         while(1) {
449             EXEC SQL FETCH csr109 INTO :id;
450             if (sqlca.sqlcode != 0) {
451                 ingerr(&sqlca.sqlcode);
452                 break;
453             } 
454             
455             sq_save_data(sq, hash_lookup(lists, id));
456         }
457         EXEC SQL CLOSE csr109;
458         generic_fix(sq, show_list_name, "Change name", cant_fix, 0);
459     }
460
461     dprintf("Loading filesys...\n");
462     sq = sq_create();
463     filesys = create_hash(10000);
464
465     EXEC SQL DECLARE csr110 CURSOR FOR
466         SELECT filsys_id, label, owner, owners, phys_id, mach_id,
467                 type, name, modby FROM filesys ORDER BY filsys_id;
468     if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
469     EXEC SQL OPEN csr110;
470     while(1) {
471         EXEC SQL FETCH csr110 INTO :id, :name, :aid, :aid2, :id2, :id3, 
472             :buf, :name1, :sid;
473         if (sqlca.sqlcode != 0) {
474           ingerr(&sqlca.sqlcode);
475           break;
476         }
477
478         f = (struct filesys *) malloc(sizeof(struct filesys));
479         if (f == NULL)
480           out_of_mem("storing filesystems");
481         strcpy(f->name, strtrim(name));
482         strcpy(f->dir, strtrim(name1));
483         f->filsys_id = id;
484         f->owner = aid;
485         f->owners = aid2;
486         f->phys_id = id2;
487         f->mach_id = id3;
488         f->type = buf[0];
489         if (hash_store(filesys, id, f)) {
490             sq_save_data(sq, hash_lookup(filesys, id));
491             sq_save_data(sq, f);
492         }
493         if (sid < 0)
494           string_check(-sid);
495     }
496     EXEC SQL CLOSE csr110;
497
498     generic_fix(sq, show_fs_id, "Change ID", fix_fs_id, 0);
499
500     dprintf("Loading nfsphys...\n");
501     sq = sq_create();
502     nfsphys = create_hash(500);
503
504     EXEC SQL DECLARE csr111 CURSOR FOR
505         SELECT nfsphys_id, dir, mach_id, allocated, modby FROM nfsphys;
506     if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
507     EXEC SQL OPEN csr111;
508     while(1) {
509         EXEC SQL FETCH csr111 INTO :id, :name, :id2, :id3, :sid;
510         if (sqlca.sqlcode != 0) {
511             ingerr(&sqlca.sqlcode);
512             break;
513         } 
514
515         n = (struct nfsphys *) malloc(sizeof(struct nfsphys));
516         if (n == NULL)
517           out_of_mem("storing nfsphys");
518         strcpy(n->dir, strtrim(name));
519         n->mach_id = id2;
520         n->nfsphys_id = id;
521         n->allocated = id3;
522         n->count = 0;
523         if (hash_store(nfsphys, id, n)) {
524             sq_save_data(sq, hash_lookup(nfsphys, id));
525             sq_save_data(sq, n);
526         }
527         if (sid < 0)
528           string_check(-sid);
529     }
530     EXEC SQL CLOSE csr111;
531
532     generic_fix(sq, show_np_id, "Change ID", fix_np_id, 0);
533
534     if (!fast) {
535
536         EXEC SQL DECLARE csr112 CURSOR FOR
537             SELECT s1.string_id, s1.string FROM strings s1, strings s2
538                 WHERE s1.string=s2.string AND s1.tid != s2.tid;
539         if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
540         EXEC SQL OPEN csr112;
541         while(1) {
542             EXEC SQL FETCH csr112 INTO :id, :buf;
543             if (sqlca.sqlcode != 0) {
544                 ingerr(&sqlca.sqlcode);
545                 break;
546             } 
547
548             printf("String %s(%d) is a duplicate!\n", strtrim(buf), id);
549             printf("Not fixing this error\n");
550         }
551         EXEC SQL CLOSE csr112;
552     }
553
554     if (!fast) {
555         dprintf("Scanning krbmap...\n");
556
557         EXEC SQL DECLARE csr113 CURSOR FOR
558             SELECT k1.users_id FROM krbmap k1, krbmap k2
559                 WHERE k1.users_id = k2.users_id AND k1.tid != k2.tid;
560         if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
561         EXEC SQL OPEN csr113;
562         while(1) {
563             EXEC SQL FETCH csr113 INTO :id;
564             if (sqlca.sqlcode != 0) {
565                 ingerr(&sqlca.sqlcode);
566                 break;
567             } 
568
569             printf("User %d is in the krbmap more than once!\n", id);
570             printf("Not fixing this error\n");
571         }
572         EXEC SQL CLOSE csr113; 
573
574         EXEC SQL DECLARE csr114 CURSOR FOR
575             SELECT k1.string_id FROM krbmap k1, krbmap k2
576                 WHERE k1.string_id = k2.string_id AND k1.tid != k2.tid;
577         if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
578         EXEC SQL OPEN csr114;
579         while(1) {
580             EXEC SQL FETCH csr114 INTO :id;
581             if (sqlca.sqlcode != 0) {
582                 ingerr(&sqlca.sqlcode);
583                 break;
584             } 
585
586             printf("Principal %d is in the krbmap more than once!\n", id);
587             printf("Not fixing this error\n");
588         }
589         EXEC SQL CLOSE csr114; 
590     }
591 }
This page took 0.07517 seconds and 3 git commands to generate.