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