3 * (c) Copyright 1988 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
8 #include <mit-copyright.h>
12 EXEC SQL INCLUDE sqlca;
15 static char phase1_qc_rcsid[] = "$Header$";
21 printf("User %s (%s, status %d) has duplicate ID\n",
22 u->login, u->fullname, u->status);
26 handle_duplicate_logins(sq)
27 struct save_queue *sq;
29 struct user *u, *uu, *tmp;
31 uu = (struct user *)0;
32 if(sq_get_data(sq,&uu)) {
33 while (sq_get_data(sq, &u)) {
34 if (!strcmp(u->login, uu->login)) {
35 if (uu->status == 1 || u->status == 0) {
40 printf("User %s (%s, status %d) and\n",
41 u->login, u->fullname, u->status);
42 printf("User %s (%s, status %d) have duplicate logins\n",
43 uu->login, uu->fullname, uu->status);
44 if (!strcmp(u->fullname, uu->fullname) &&
45 single_fix("Delete the second one")) {
46 single_delete("users", "users_id", uu->users_id);
47 } else if (single_fix("Unregister the second one"))
49 EXEC SQL BEGIN DECLARE SECTION;
50 int id = uu->users_id, rowcount;
51 EXEC SQL END DECLARE SECTION;
53 EXEC SQL UPDATE users SET login = '#'+CHAR(users.uid),
54 status=0 WHERE users_id = :id;
55 EXEC SQL INQUIRE_SQL(:rowcount = rowcount);
57 printf("%d entr%s fixed\n", rowcount, rowcount==1?"y":"ies");
59 printf("Not fixed\n");
72 u->users_id = generic_fix_id("users", "users_id", "login",
73 u->users_id, u->login);
80 printf("Sorry, don't know how to fix that\n");
86 printf("Machine %s has duplicate ID %d\n", m->name, m->mach_id);
93 printf("Machine %s (%d) has duplicate name\n", m->name, m->mach_id);
100 m->mach_id = generic_fix_id("machine", "mach_id", "name",
101 m->mach_id, m->name);
107 printf("Cluster %s has duplicate ID %d\n", c->name, c->clu_id);
114 printf("Cluster %s (%d) has duplicate name\n", c->name, c->clu_id);
121 c->clu_id = generic_fix_id("cluster", "clu_id", "name", c->clu_id, c->name);
127 printf("List %s has duplicate ID %d\n", l->name, l->list_id);
134 printf("List %s (%d) has duplicate name\n", l->name, l->list_id);
141 l->list_id = generic_fix_id("list", "list_id", "name", l->list_id, l->name);
147 printf("Filesys %s has duplicate ID %d\n", f->name, f->filsys_id);
154 f->filsys_id = generic_fix_id("filesys", "filsys_id", "label",
155 f->filsys_id, f->name);
162 printf("NfsPhys %s:%s has duplicate ID %d\n",
163 ((struct machine *)hash_lookup(machines, n->mach_id))->name,
164 n->dir, n->nfsphys_id);
171 n->nfsphys_id = generic_fix_id("nfsphys", "nfsphys_id", "dir",
172 n->nfsphys_id, n->dir);
178 printf("String %s has duplicate ID %d\n", s->name, s->string_id);
185 printf("String %d is a duplicate\n", id);
189 print_dup_map(key, data, hint)
194 printf("String %d is a duplicate of string %d\n", key, data);
200 EXEC SQL BEGIN DECLARE SECTION;
201 char name[81], name1[81], last[17], first[17], buf[257];
202 int id, id1, id2, id3, aid, aid2, status;
203 int sid, sid2, sid3, sid4, sid5;
204 EXEC SQL END DECLARE SECTION;
205 int i, q, retval, tmp;
206 struct save_queue *sq;
216 printf("Phase 1 - Looking for duplicates\n");
218 /* self-join strings table on "string" to get duplicate strings, then
219 build a duplicates table to merge them. */
221 dprintf("Looking for duplicate strings...\n");
222 string_dups = create_hash( 100 );
223 if(!string_dups) out_of_mem("storing duplicate strings");
225 EXEC SQL DECLARE csr116 CURSOR FOR
226 SELECT s1.string_id, s2.string_id FROM strings s1, strings s2
227 where s1.string = s2.string and s1.string_id < s2.string_id;
228 EXEC SQL OPEN csr116;
229 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
230 /* The SELECT gives us two columns, both with non-negative integers.
231 * The number in the left column is always the smaller of the two,
232 * and each row includes string IDs for identical strings. We use
233 * them to make a mapping from id-to-delete to id-to-keep for all
238 EXEC SQL FETCH csr116 INTO :id1, :id2;
239 if (sqlca.sqlcode != 0) {
240 ingerr(&sqlca.sqlcode);
244 /* If id2 is already stored, skip this row. */
245 i = int_hash_lookup( string_dups, id2 );
246 if( i > 0 ) { continue; }
247 /* Follow the chain of id1 equivalent IDs back to the lowest one. */
249 while((tmp=int_hash_lookup(string_dups, id))>0)
251 int_hash_store( string_dups, id2, id );
253 EXEC SQL CLOSE csr116;
254 dprintf("found %d duplicates\n", q);
255 int_hash_step(string_dups, print_dup_map, NULL);
256 /* We don't want to delete the duplicates now because if the dbck
257 is cancelled, a LOT of state will be lost. So, we'll just let
258 them not get marked as used and then phase3 will clean them up */
260 dprintf("Loading strings...\n");
262 strings = create_hash(75000);
263 if(!sq || !strings) out_of_mem("loading strings");
265 EXEC SQL DECLARE csr101 CURSOR FOR
266 SELECT string_id, string, tid FROM strings ORDER BY string_id;
267 EXEC SQL OPEN csr101;
268 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
271 EXEC SQL FETCH csr101 INTO :id, :buf, :id1;
272 if (sqlca.sqlcode != 0) {
273 ingerr(&sqlca.sqlcode);
277 s = (struct string *) malloc(sizeof(struct string));
279 out_of_mem("storing strings");
280 s->name = strsave(strtrim(buf));
283 retval = hash_store(strings, id, s);
284 if ( retval == -1 ) {
285 out_of_mem("storing strings in hash table");
286 } else if ( retval == 1 ) { /* duplicate string_id*/
287 sq_save_data(sq, hash_lookup(strings, id));
291 EXEC SQL CLOSE csr101;
292 /* I'm not at all convinced this will work, so...
293 generic_delete(sq, show_str_id, "strings", "string_id", 0);
297 printf("Loaded %d strings\n", q);
299 dprintf("Loading users...\n");
301 users = create_hash(30000);
302 if(!sq || !users) out_of_mem("loading users");
304 EXEC SQL DECLARE csr102 CURSOR FOR
305 SELECT users_id, login, last, first, status, potype, pop_id, box_id,
306 modby, fmodby, pmodby, comment, sigwho FROM users
308 EXEC SQL OPEN csr102;
309 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
311 EXEC SQL FETCH csr102 INTO :id, :name, :last, :first, :status,
312 :buf, :id2, :id3, :sid, :sid2, :sid3, :sid4, :sid5;
313 if (sqlca.sqlcode != 0) {
314 ingerr(&sqlca.sqlcode);
318 u = (struct user *) malloc(sizeof(struct user));
320 out_of_mem("storing users");
321 strcpy(u->login, strtrim(name));
323 sprintf(buf, "%s, %s", strtrim(last), strtrim(first));
324 u->fullname = strsave(buf);
337 /* If potype is SMTP, box_id is a string_id for the strings tbl */
343 retval = hash_store(users, id, u);
344 if ( retval == -1 ) {
345 out_of_mem("storing users in hash table");
346 } else if ( retval == 1 ) {
347 sq_save_data(sq, hash_lookup(users, id));
351 EXEC SQL CLOSE csr102;
353 generic_fix(sq, show_user_id, "Change ID", fix_user_id, 0);
357 if(!sq) out_of_mem("finding duplicate logins");
359 EXEC SQL DECLARE csr103 CURSOR FOR
360 SELECT u1.users_id FROM users u1, users u2
361 WHERE u1.login = u2.login and u1.tid != u2.tid;
362 EXEC SQL OPEN csr103;
363 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
365 EXEC SQL FETCH csr103 INTO :id;
366 if (sqlca.sqlcode != 0) {
367 ingerr(&sqlca.sqlcode);
370 sq_save_data(sq, hash_lookup(users, id));
372 EXEC SQL CLOSE csr103;
373 handle_duplicate_logins(sq);
377 dprintf("Scanning krbmap...\n");
379 EXEC SQL DECLARE csr113 CURSOR FOR
380 SELECT k1.users_id FROM krbmap k1, krbmap k2
381 WHERE k1.users_id = k2.users_id AND k1.tid != k2.tid;
382 EXEC SQL OPEN csr113;
383 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
385 EXEC SQL FETCH csr113 INTO :id;
386 if (sqlca.sqlcode != 0) {
387 ingerr(&sqlca.sqlcode);
391 printf("User %d is in the krbmap more than once!\n", id);
392 printf("Not fixing this error\n");
394 EXEC SQL CLOSE csr113;
396 EXEC SQL DECLARE csr114 CURSOR FOR
397 SELECT k1.string_id FROM krbmap k1, krbmap k2
398 WHERE k1.string_id = k2.string_id AND k1.tid != k2.tid;
399 EXEC SQL OPEN csr114;
400 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
402 EXEC SQL FETCH csr114 INTO :id;
403 if (sqlca.sqlcode != 0) {
404 ingerr(&sqlca.sqlcode);
408 printf("Principal %d is in the krbmap more than once!\n", id);
409 printf("Not fixing this error\n");
411 EXEC SQL CLOSE csr114;
414 dprintf("Loading machines...\n");
416 machines = create_hash(20000);
417 if(!sq || !machines) out_of_mem("loading machines");
419 EXEC SQL DECLARE csr104 CURSOR FOR
420 SELECT mach_id, name, snet_id, owner_type, owner_id,
421 acomment, ocomment, creator, modby
422 FROM machine ORDER BY mach_id;
423 EXEC SQL OPEN csr104;
424 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
426 EXEC SQL FETCH csr104 INTO :id, :name, :id2, :buf, :id3, :sid2,
428 if (sqlca.sqlcode != 0) {
429 ingerr(&sqlca.sqlcode);
433 m = (struct machine *) malloc(sizeof(struct machine));
435 out_of_mem("storing machines");
436 strcpy(m->name, strtrim(name));
437 m->owner_type = buf[0];
446 retval = hash_store(machines, id, m);
447 if ( retval == -1 ) {
448 out_of_mem("storing machines in hash table");
449 } else if ( retval == 1 ) {
450 sq_save_data(sq, hash_lookup(machines, id));
454 EXEC SQL CLOSE csr104;
455 generic_fix(sq, show_mach_id, "Change ID", fix_mach_id, 0);
459 if(!sq) out_of_mem("looking for duplicate machine names");
461 EXEC SQL DECLARE csr105 CURSOR FOR
462 SELECT m1.mach_id FROM machine m1, machine m2
463 WHERE m1.name = m2.name AND m1.tid != m2.tid;
464 EXEC SQL OPEN csr105;
465 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
467 EXEC SQL FETCH csr105 INTO :id;
468 if (sqlca.sqlcode != 0) {
469 ingerr(&sqlca.sqlcode);
473 sq_save_data(sq, hash_lookup(machines, id));
475 EXEC SQL CLOSE csr105;
476 generic_fix(sq, show_mach_name, "Change name", cant_fix, 0);
478 EXEC SQL DECLARE csr_hal1 CURSOR FOR
479 SELECT h1.name, m1.mach_id, m2.mach_id
480 FROM hostalias h1, machine m1, hostalias h2, machine m2
481 WHERE h1.name=h2.name AND h1.mach_id!=h2.mach_id
482 AND m1.mach_id=h1.mach_id AND m2.mach_id=h2.mach_id;
483 EXEC SQL OPEN csr_hal1;
484 if(sqlca.sqlcode!=0) ingerr(&sqlca.sqlcode);
486 EXEC SQL FETCH csr_hal1 INTO :name, :id1, :id2;
487 if(sqlca.sqlcode!=0) {
488 ingerr(&sqlca.sqlcode);
491 printf("Aliases for machines %d and %d have duplicate name %s\n",
492 id1, id2, strtrim(name));
495 EXEC SQL CLOSE csr_hal1;
497 EXEC SQL DECLARE csr_hal2 CURSOR FOR
498 SELECT h1.name, m1.mach_id, m2.mach_id
499 FROM hostalias h1, machine m1, machine m2
500 WHERE h1.name=m1.name AND h1.mach_id=m2.mach_id;
501 EXEC SQL OPEN csr_hal2;
502 if(sqlca.sqlcode!=0) ingerr(&sqlca.sqlcode);
504 EXEC SQL FETCH csr_hal2 INTO :name, :id1, :id2;
505 if(sqlca.sqlcode!=0) {
506 ingerr(&sqlca.sqlcode);
509 printf("Machine %d has alias `%s' that conflicts with machine %d\n",
510 id2, strtrim(name), id1);
513 EXEC SQL CLOSE csr_hal2;
516 dprintf("Loading subnets...\n");
517 subnets = create_hash(254);
518 if(!subnets) out_of_mem("loading subnets");
520 EXEC SQL DECLARE csr115 CURSOR FOR
521 SELECT snet_id, name, owner_type, owner_id, modby from subnet;
522 EXEC SQL OPEN csr115;
523 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
525 EXEC SQL FETCH csr115 INTO :id, :name, :buf, :id2, :sid;
526 if (sqlca.sqlcode != 0) {
527 ingerr(&sqlca.sqlcode);
531 sn = (struct subnet *) malloc(sizeof(struct machine));
533 out_of_mem("storing subnets");
534 strcpy(sn->name, strtrim(name));
535 sn->owner_type=buf[0];
539 retval = hash_store(subnets, id, sn);
540 if ( retval == -1 ) {
541 out_of_mem("storing subnets in hash table");
542 } else if ( retval == 1 ) {
543 printf("Duplicate subnet ID: %d (%s)\n", id, name);
544 /* should add code to delete */
547 EXEC SQL CLOSE csr115;
549 dprintf("Loading clusters...\n");
551 clusters = create_hash(100);
552 if(!sq || !clusters) out_of_mem("loading clusters");
554 EXEC SQL DECLARE csr106 CURSOR FOR
555 SELECT clu_id, name, modby FROM cluster;
556 EXEC SQL OPEN csr106;
557 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
559 EXEC SQL FETCH csr106 INTO :id, :name, :sid;
560 if (sqlca.sqlcode != 0) {
561 ingerr(&sqlca.sqlcode);
565 c = (struct cluster *) malloc(sizeof(struct cluster));
567 out_of_mem("storing clusters");
568 strcpy(c->name, strtrim(name));
571 retval = hash_store(clusters, id, c);
572 if ( retval == -1 ) {
573 out_of_mem("storing clusters in hash table");
574 } else if ( retval == 1 ) {
575 sq_save_data(sq, hash_lookup(clusters, id));
579 EXEC SQL CLOSE csr106;
580 generic_fix(sq, show_clu_id, "Change ID", fix_clu_id, 0);
584 if(!sq) out_of_mem("looking for duplicate cluster names");
586 EXEC SQL DECLARE csr107 CURSOR FOR
587 SELECT c1.clu_id FROM cluster c1, cluster c2
588 WHERE c1.name=c2.name AND c1.tid != c2.tid;
589 EXEC SQL OPEN csr107;
590 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
592 EXEC SQL FETCH csr107 INTO :id;
593 if (sqlca.sqlcode != 0) {
594 ingerr(&sqlca.sqlcode);
598 sq_save_data(sq, hash_lookup(clusters, id));
600 EXEC SQL CLOSE csr107;
601 generic_fix(sq, show_clu_name, "Change name", cant_fix, 0);
604 dprintf("Loading lists...\n");
606 lists = create_hash(50000);
607 if(!sq || !lists) out_of_mem("loading lists");
609 EXEC SQL DECLARE csr108 CURSOR FOR
610 SELECT list_id, name, acl_id, acl_type, modby FROM list
612 EXEC SQL OPEN csr108;
613 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
615 EXEC SQL FETCH csr108 INTO :id, :name, :aid, :buf, :sid;
616 if (sqlca.sqlcode != 0) {
617 ingerr(&sqlca.sqlcode);
620 l = (struct list *) malloc(sizeof(struct list));
622 out_of_mem("storing lists");
623 strcpy(l->name, strtrim(name));
624 l->acl_type = buf[0];
628 retval = hash_store(lists, id, l);
629 if ( retval == -1 ) {
630 out_of_mem("storing lists in hash table");
631 } else if ( retval == 1 ) {
632 sq_save_data(sq, hash_lookup(lists, id));
636 EXEC SQL CLOSE csr108;
637 generic_fix(sq, show_list_id, "Change ID", fix_list_id, 0);
641 if(!sq) out_of_mem("looking for duplicate list names");
643 EXEC SQL DECLARE csr109 CURSOR FOR
644 SELECT l1.list_id FROM list l1, list l2
645 WHERE l1.name=l2.name AND l1.tid != l2.tid;
646 EXEC SQL OPEN csr109;
647 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
649 EXEC SQL FETCH csr109 INTO :id;
650 if (sqlca.sqlcode != 0) {
651 ingerr(&sqlca.sqlcode);
655 sq_save_data(sq, hash_lookup(lists, id));
657 EXEC SQL CLOSE csr109;
658 generic_fix(sq, show_list_name, "Change name", cant_fix, 0);
661 dprintf("Loading filesys...\n");
663 filesys = create_hash(30000);
664 if(!sq || !filesys) out_of_mem("loading filesys");
666 EXEC SQL DECLARE csr110 CURSOR FOR
667 SELECT filsys_id, label, owner, owners, phys_id, mach_id,
668 type, name, modby FROM filesys ORDER BY filsys_id;
669 EXEC SQL OPEN csr110;
670 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
672 EXEC SQL FETCH csr110 INTO :id, :name, :aid, :aid2, :id2, :id3,
674 if (sqlca.sqlcode != 0) {
675 ingerr(&sqlca.sqlcode);
679 f = (struct filesys *) malloc(sizeof(struct filesys));
681 out_of_mem("storing filesystems");
682 strcpy(f->name, strtrim(name));
683 strcpy(f->dir, strtrim(name1));
690 retval = hash_store(filesys, id, f);
691 if ( retval == -1 ) {
692 out_of_mem("storing filesys in hash table");
693 } else if ( retval == 1 ) {
694 sq_save_data(sq, hash_lookup(filesys, id));
698 EXEC SQL CLOSE csr110;
700 generic_fix(sq, show_fs_id, "Change ID", fix_fs_id, 0);
702 dprintf("Loading nfsphys...\n");
704 nfsphys = create_hash(500);
705 if(!sq || !nfsphys) out_of_mem("loading nfsphs");
707 EXEC SQL DECLARE csr111 CURSOR FOR
708 SELECT nfsphys_id, dir, mach_id, allocated, modby FROM nfsphys;
709 EXEC SQL OPEN csr111;
710 if (sqlca.sqlcode != 0) ingerr(&sqlca.sqlcode);
712 EXEC SQL FETCH csr111 INTO :id, :name, :id2, :id3, :sid;
713 if (sqlca.sqlcode != 0) {
714 ingerr(&sqlca.sqlcode);
718 n = (struct nfsphys *) malloc(sizeof(struct nfsphys));
720 out_of_mem("storing nfsphys");
721 strcpy(n->dir, strtrim(name));
726 retval = hash_store(nfsphys, id, n);
727 if ( retval == -1 ) {
728 out_of_mem("storing nfsphys in hash table");
729 } else if ( retval == 1 ) {
730 sq_save_data(sq, hash_lookup(nfsphys, id));
734 EXEC SQL CLOSE csr111;
736 generic_fix(sq, show_np_id, "Change ID", fix_np_id, 0);