3 * (c) Copyright 1988-1998 by the Massachusetts Institute of Technology.
4 * For copying and distribution information, please see the file
8 #include <mit-copyright.h>
15 EXEC SQL INCLUDE sqlca;
19 EXEC SQL WHENEVER SQLERROR DO dbmserr();
20 int show_user_id(void *user);
21 void handle_duplicate_logins(struct save_queue *sq);
22 void fix_user_id(void *user);
23 void cant_fix(void *id);
24 int show_mach_id(void *machine);
25 int show_mach_name(void *machine);
26 void fix_mach_id(void *machine);
27 int show_snet_name(void *subnet);
28 int show_clu_id(void *cluster);
29 int show_clu_name(void *cluster);
30 void fix_clu_id(void *cluster);
31 int show_list_id(void *list);
32 int show_list_name(void *list);
33 void fix_list_id(void *list);
34 int show_fs_id(void *filesys);
35 void fix_fs_id(void *filesys);
36 int show_fs_name(void *fs);
37 int show_np_id(void *nfsphys);
38 void fix_np_id(void *nfsphys);
39 int show_str_id(void *string);
40 int print_str_id(void *id);
41 void print_dup_map(int key, void *data, void *hint);
43 int show_user_id(void *user)
45 struct user *u = user;
46 printf("User %s (%s, status %d) has duplicate ID\n",
47 u->login, u->fullname, u->status);
51 void handle_duplicate_logins(struct save_queue *sq)
53 struct user *u, *uu, *tmp;
56 if (sq_get_data(sq, &uu))
58 while (sq_get_data(sq, &u))
60 if (!strcmp(u->login, uu->login))
62 if (uu->status == 1 || u->status == 0)
68 printf("User %s (%s, status %d) and\n",
69 u->login, u->fullname, u->status);
70 printf("User %s (%s, status %d) have duplicate logins\n",
71 uu->login, uu->fullname, uu->status);
72 if (!strcmp(u->fullname, uu->fullname) &&
73 single_fix("Delete the second one", 0))
74 single_delete("users", "users_id", uu->users_id);
75 else if (single_fix("Unregister the second one", 0))
77 EXEC SQL BEGIN DECLARE SECTION;
78 int id = uu->users_id, rowcount;
79 EXEC SQL END DECLARE SECTION;
82 SET login = '#' || CHAR(users.unix_uid), status = 0
84 rowcount = sqlca.sqlerrd[2];
87 printf("%d entr%s fixed\n", rowcount,
88 rowcount == 1 ? "y" : "ies");
91 printf("Not fixed\n");
101 void fix_user_id(void *user)
103 struct user *u = user;
104 u->users_id = generic_fix_id("users", "users_id", "login",
105 u->users_id, u->login);
109 void cant_fix(void *id)
111 printf("Sorry, don't know how to fix that\n");
114 int show_mach_id(void *machine)
116 struct machine *m = machine;
117 printf("Machine %s has duplicate ID %d\n", m->name, m->mach_id);
121 int show_mach_name(void *machine)
123 struct machine *m = machine;
124 printf("Machine %s (%d) has duplicate name\n", m->name, m->mach_id);
128 void fix_mach_id(void *machine)
130 struct machine *m = machine;
131 m->mach_id = generic_fix_id("machine", "mach_id", "name",
132 m->mach_id, m->name);
135 int show_snet_name(void *subnet)
137 struct subnet *s = subnet;
138 printf("Subnet %s (%d) has duplicate name\n", s->name, s->snet_id);
142 int show_clu_id(void *cluster)
144 struct cluster *c = cluster;
145 printf("Cluster %s has duplicate ID %d\n", c->name, c->clu_id);
149 int show_clu_name(void *cluster)
151 struct cluster *c = cluster;
152 printf("Cluster %s (%d) has duplicate name\n", c->name, c->clu_id);
156 void fix_clu_id(void *cluster)
158 struct cluster *c = cluster;
159 c->clu_id = generic_fix_id("cluster", "clu_id", "name", c->clu_id, c->name);
162 int show_list_id(void *list)
164 struct list *l = list;
165 printf("List %s has duplicate ID %d\n", l->name, l->list_id);
169 int show_list_name(void *list)
171 struct list *l = list;
172 printf("List %s (%d) has duplicate name\n", l->name, l->list_id);
176 void fix_list_id(void *list)
178 struct list *l = list;
179 l->list_id = generic_fix_id("list", "list_id", "name", l->list_id, l->name);
182 int show_fs_id(void *filesys)
184 struct filesys *f = filesys;
185 printf("Filesys %s has duplicate ID %d\n", f->name, f->filsys_id);
189 void fix_fs_id(void *filesys)
191 struct filesys *f = filesys;
192 f->filsys_id = generic_fix_id("filesys", "filsys_id", "label",
193 f->filsys_id, f->name);
196 int show_fs_name(void *filesys)
198 struct filesys *fs = filesys;
199 printf("Filesys %s (%d) has duplicate name\n", fs->name, fs->filsys_id);
203 int show_np_id(void *nfsphys)
205 struct nfsphys *n = nfsphys;
206 printf("NfsPhys %s:%s has duplicate ID %d\n",
207 ((struct machine *)hash_lookup(machines, n->mach_id))->name,
208 n->dir, n->nfsphys_id);
212 void fix_np_id(void *nfsphys)
214 struct nfsphys *n = nfsphys;
215 n->nfsphys_id = generic_fix_id("nfsphys", "nfsphys_id", "dir",
216 n->nfsphys_id, n->dir);
219 int show_str_id(void *string)
221 struct string *s = string;
222 printf("String %s has duplicate ID %d\n", s->name, s->string_id);
226 int print_str_id(void *id)
228 printf("String %d is a duplicate\n", (int)id);
232 void print_dup_map(int key, void *data, void *hint)
234 printf("String %d is a duplicate of string %d\n", key, (int)data);
239 EXEC SQL BEGIN DECLARE SECTION;
240 char name[81], name1[81], last[17], first[17], buf[257];
241 int id, id1, id2, id3, aid, aid2, status;
242 int sid, sid2, sid3, sid4, sid5;
243 EXEC SQL END DECLARE SECTION;
244 int i, q, retval, tmp;
245 struct save_queue *sq;
255 printf("Phase 1 - Looking for duplicates\n");
257 /* self-join strings table on "string" to get duplicate strings, then
258 build a duplicates table to merge them. */
260 dprintf("Looking for duplicate strings...\n");
261 string_dups = create_hash(100);
263 out_of_mem("storing duplicate strings");
265 EXEC SQL DECLARE csr116 CURSOR FOR
266 SELECT s1.string_id, s2.string_id FROM strings s1, strings s2
267 WHERE s1.string = s2.string and s1.string_id < s2.string_id;
268 EXEC SQL OPEN csr116;
269 /* The SELECT gives us two columns, both with non-negative integers.
270 * The number in the left column is always the smaller of the two,
271 * and each row includes string IDs for identical strings. We use
272 * them to make a mapping from id-to-delete to id-to-keep for all
278 EXEC SQL FETCH csr116 INTO :id1, :id2;
282 /* If id2 is already stored, skip this row. */
283 i = (int)hash_lookup(string_dups, id2);
286 /* Follow the chain of id1 equivalent IDs back to the lowest one. */
288 while ((tmp = (int)hash_lookup(string_dups, id)) > 0)
290 hash_store(string_dups, id2, (void *)id);
292 EXEC SQL CLOSE csr116;
293 dprintf("found %d duplicates\n", q);
294 hash_step(string_dups, print_dup_map, NULL);
295 /* We don't want to delete the duplicates now because if the dbck
296 is cancelled, a LOT of state will be lost. So, we'll just let
297 them not get marked as used and then phase3 will clean them up */
299 dprintf("Loading strings...\n");
301 strings = create_hash(75000);
303 out_of_mem("loading strings");
305 EXEC SQL DECLARE csr101 CURSOR FOR
306 SELECT string_id, string FROM strings ORDER BY string_id;
307 EXEC SQL OPEN csr101;
311 EXEC SQL FETCH csr101 INTO :id, :buf;
315 s = malloc(sizeof(struct string));
317 out_of_mem("storing strings");
318 s->name = strdup(strtrim(buf));
321 retval = hash_store(strings, id, s);
323 out_of_mem("storing strings in hash table");
324 else if (retval == 1) /* duplicate string_id */
326 sq_save_data(sq, hash_lookup(strings, id));
330 EXEC SQL CLOSE csr101;
331 /* keep string id 0 (the empty string) even if unreferenced */
334 printf("Loaded %d strings\n", q);
336 dprintf("Loading users...\n");
338 users = create_hash(30000);
340 out_of_mem("loading users");
342 EXEC SQL DECLARE csr102 CURSOR FOR
343 SELECT users_id, login, last, first, status, potype, pop_id, box_id,
344 modby, fmodby, pmodby, comments, sigwho FROM users
346 EXEC SQL OPEN csr102;
349 EXEC SQL FETCH csr102 INTO :id, :name, :last, :first, :status,
350 :buf, :id2, :id3, :sid, :sid2, :sid3, :sid4, :sid5;
354 u = malloc(sizeof(struct user));
356 out_of_mem("storing users");
357 strcpy(u->login, strtrim(name));
359 sprintf(buf, "%s, %s", strtrim(last), strtrim(first));
360 u->fullname = strdup(buf);
374 /* If potype is SMTP, box_id is a string_id for the strings tbl */
380 retval = hash_store(users, id, u);
382 out_of_mem("storing users in hash table");
383 else if (retval == 1)
385 sq_save_data(sq, hash_lookup(users, id));
389 EXEC SQL CLOSE csr102;
391 generic_fix(sq, show_user_id, "Change ID", fix_user_id, 0);
397 out_of_mem("finding duplicate logins");
399 EXEC SQL DECLARE csr103 CURSOR FOR
400 SELECT u1.users_id FROM users u1, users u2
401 WHERE u1.login = u2.login and u1.rowid != u2.rowid;
402 EXEC SQL OPEN csr103;
405 EXEC SQL FETCH csr103 INTO :id;
408 sq_save_data(sq, hash_lookup(users, id));
410 EXEC SQL CLOSE csr103;
411 handle_duplicate_logins(sq);
416 dprintf("Scanning krbmap...\n");
418 EXEC SQL DECLARE csr113 CURSOR FOR
419 SELECT k1.users_id FROM krbmap k1, krbmap k2
420 WHERE k1.users_id = k2.users_id AND k1.rowid != k2.rowid;
421 EXEC SQL OPEN csr113;
424 EXEC SQL FETCH csr113 INTO :id;
428 printf("User %d is in the krbmap more than once!\n", id);
429 printf("Not fixing this error\n");
431 EXEC SQL CLOSE csr113;
433 EXEC SQL DECLARE csr114 CURSOR FOR
434 SELECT k1.string_id FROM krbmap k1, krbmap k2
435 WHERE k1.string_id = k2.string_id AND k1.rowid != k2.rowid;
436 EXEC SQL OPEN csr114;
439 EXEC SQL FETCH csr114 INTO :id;
443 printf("Principal %d is in the krbmap more than once!\n", id);
444 printf("Not fixing this error\n");
446 EXEC SQL CLOSE csr114;
449 dprintf("Loading machines...\n");
451 machines = create_hash(20000);
452 if (!sq || !machines)
453 out_of_mem("loading machines");
455 EXEC SQL DECLARE csr104 CURSOR FOR
456 SELECT mach_id, name, snet_id, owner_type, owner_id,
457 acomment, ocomment, creator, modby
458 FROM machine ORDER BY mach_id;
459 EXEC SQL OPEN csr104;
462 EXEC SQL FETCH csr104 INTO :id, :name, :id2, :buf, :id3, :sid2,
467 m = malloc(sizeof(struct machine));
469 out_of_mem("storing machines");
470 strcpy(m->name, strtrim(name));
471 m->owner_type = buf[0];
480 retval = hash_store(machines, id, m);
482 out_of_mem("storing machines in hash table");
483 else if (retval == 1)
485 sq_save_data(sq, hash_lookup(machines, id));
489 EXEC SQL CLOSE csr104;
490 generic_fix(sq, show_mach_id, "Change ID", fix_mach_id, 0);
496 out_of_mem("looking for duplicate machine names");
498 EXEC SQL DECLARE csr105 CURSOR FOR
499 SELECT m1.mach_id FROM machine m1, machine m2
500 WHERE m1.name = m2.name AND m1.rowid != m2.rowid;
501 EXEC SQL OPEN csr105;
504 EXEC SQL FETCH csr105 INTO :id;
508 sq_save_data(sq, hash_lookup(machines, id));
510 EXEC SQL CLOSE csr105;
511 generic_fix(sq, show_mach_name, "Change name", cant_fix, 0);
513 EXEC SQL DECLARE csr_hal1 CURSOR FOR
514 SELECT h1.name, m1.mach_id, m2.mach_id
515 FROM hostalias h1, machine m1, hostalias h2, machine m2
516 WHERE h1.name = h2.name AND h1.mach_id != h2.mach_id
517 AND m1.mach_id = h1.mach_id AND m2.mach_id = h2.mach_id;
518 EXEC SQL OPEN csr_hal1;
521 EXEC SQL FETCH csr_hal1 INTO :name, :id1, :id2;
524 printf("Aliases for machines %d and %d have duplicate name %s\n",
525 id1, id2, strtrim(name));
528 EXEC SQL CLOSE csr_hal1;
530 EXEC SQL DECLARE csr_hal2 CURSOR FOR
531 SELECT h1.name, m1.mach_id, m2.mach_id
532 FROM hostalias h1, machine m1, machine m2
533 WHERE h1.name = m1.name AND h1.mach_id = m2.mach_id;
534 EXEC SQL OPEN csr_hal2;
537 EXEC SQL FETCH csr_hal2 INTO :name, :id1, :id2;
540 printf("Machine %d has alias %s that conflicts with machine %d\n",
541 id2, strtrim(name), id1);
544 EXEC SQL CLOSE csr_hal2;
547 dprintf("Loading subnets...\n");
548 subnets = create_hash(254);
550 out_of_mem("loading subnets");
552 EXEC SQL DECLARE csr115 CURSOR FOR
553 SELECT snet_id, name, owner_type, owner_id, modby from subnet;
554 EXEC SQL OPEN csr115;
557 EXEC SQL FETCH csr115 INTO :id, :name, :buf, :id2, :sid;
561 sn = malloc(sizeof(struct machine));
563 out_of_mem("storing subnets");
564 strcpy(sn->name, strtrim(name));
565 sn->owner_type = buf[0];
569 retval = hash_store(subnets, id, sn);
571 out_of_mem("storing subnets in hash table");
572 else if (retval == 1)
574 printf("Duplicate subnet ID: %d (%s)\n", id, name);
575 /* should add code to delete */
579 EXEC SQL CLOSE csr115;
585 out_of_mem("looking for duplicate subnet names");
587 EXEC SQL DECLARE csr117 CURSOR FOR
588 SELECT s1.snet_id FROM subnet s1, subnet s2
589 WHERE s1.name = s2.name AND s1.rowid != s2.rowid;
590 EXEC SQL OPEN csr117;
593 EXEC SQL FETCH csr117 INTO :id;
597 sq_save_data(sq, hash_lookup(subnets, id));
599 EXEC SQL CLOSE csr117;
600 generic_fix(sq, show_snet_name, "Change name", cant_fix, 0);
603 dprintf("Loading clusters...\n");
605 clusters = create_hash(100);
606 if (!sq || !clusters)
607 out_of_mem("loading clusters");
609 EXEC SQL DECLARE csr106 CURSOR FOR
610 SELECT clu_id, name, modby FROM clusters;
611 EXEC SQL OPEN csr106;
614 EXEC SQL FETCH csr106 INTO :id, :name, :sid;
618 c = malloc(sizeof(struct cluster));
620 out_of_mem("storing clusters");
621 strcpy(c->name, strtrim(name));
624 retval = hash_store(clusters, id, c);
626 out_of_mem("storing clusters in hash table");
627 else if (retval == 1)
629 sq_save_data(sq, hash_lookup(clusters, id));
633 EXEC SQL CLOSE csr106;
634 generic_fix(sq, show_clu_id, "Change ID", fix_clu_id, 0);
640 out_of_mem("looking for duplicate cluster names");
642 EXEC SQL DECLARE csr107 CURSOR FOR
643 SELECT c1.clu_id FROM clusters c1, clusters c2
644 WHERE c1.name = c2.name AND c1.rowid != c2.rowid;
645 EXEC SQL OPEN csr107;
648 EXEC SQL FETCH csr107 INTO :id;
652 sq_save_data(sq, hash_lookup(clusters, id));
654 EXEC SQL CLOSE csr107;
655 generic_fix(sq, show_clu_name, "Change name", cant_fix, 0);
658 dprintf("Loading lists...\n");
660 lists = create_hash(50000);
662 out_of_mem("loading lists");
664 EXEC SQL DECLARE csr108 CURSOR FOR
665 SELECT list_id, name, acl_id, acl_type, modby FROM list
667 EXEC SQL OPEN csr108;
670 EXEC SQL FETCH csr108 INTO :id, :name, :aid, :buf, :sid;
673 l = malloc(sizeof(struct list));
675 out_of_mem("storing lists");
676 strcpy(l->name, strtrim(name));
677 l->acl_type = buf[0];
681 retval = hash_store(lists, id, l);
683 out_of_mem("storing lists in hash table");
684 else if (retval == 1)
686 sq_save_data(sq, hash_lookup(lists, id));
690 EXEC SQL CLOSE csr108;
691 generic_fix(sq, show_list_id, "Change ID", fix_list_id, 0);
697 out_of_mem("looking for duplicate list names");
699 EXEC SQL DECLARE csr109 CURSOR FOR
700 SELECT l1.list_id FROM list l1, list l2
701 WHERE l1.name = l2.name AND l1.rowid != l2.rowid;
702 EXEC SQL OPEN csr109;
705 EXEC SQL FETCH csr109 INTO :id;
709 sq_save_data(sq, hash_lookup(lists, id));
711 EXEC SQL CLOSE csr109;
712 generic_fix(sq, show_list_name, "Change name", cant_fix, 0);
715 dprintf("Loading filesys...\n");
717 filesys = create_hash(30000);
719 out_of_mem("loading filesys");
721 EXEC SQL DECLARE csr110 CURSOR FOR
722 SELECT filsys_id, label, owner, owners, phys_id, mach_id,
723 type, name, modby FROM filesys ORDER BY filsys_id;
724 EXEC SQL OPEN csr110;
727 EXEC SQL FETCH csr110 INTO :id, :name, :aid, :aid2, :id2, :id3,
732 f = malloc(sizeof(struct filesys));
734 out_of_mem("storing filesystems");
735 strcpy(f->name, strtrim(name));
736 strcpy(f->dir, strtrim(name1));
743 retval = hash_store(filesys, id, f);
745 out_of_mem("storing filesys in hash table");
746 else if (retval == 1)
748 sq_save_data(sq, hash_lookup(filesys, id));
752 EXEC SQL CLOSE csr110;
754 generic_fix(sq, show_fs_id, "Change ID", fix_fs_id, 0);
760 out_of_mem("looking for duplicate filesys names");
762 EXEC SQL DECLARE csr118 CURSOR FOR
763 SELECT fs1.filsys_id FROM filesys fs1, filesys fs2
764 WHERE fs1.label = fs2.label AND fs1.rowid != fs2.rowid;
765 EXEC SQL OPEN csr118;
768 EXEC SQL FETCH csr118 INTO :id;
772 sq_save_data(sq, hash_lookup(filesys, id));
774 EXEC SQL CLOSE csr118;
775 generic_fix(sq, show_fs_name, "Change name", cant_fix, 0);
778 dprintf("Loading nfsphys...\n");
780 nfsphys = create_hash(500);
782 out_of_mem("loading nfsphs");
784 EXEC SQL DECLARE csr111 CURSOR FOR
785 SELECT nfsphys_id, dir, mach_id, allocated, modby FROM nfsphys;
786 EXEC SQL OPEN csr111;
789 EXEC SQL FETCH csr111 INTO :id, :name, :id2, :id3, :sid;
793 n = malloc(sizeof(struct nfsphys));
795 out_of_mem("storing nfsphys");
796 strcpy(n->dir, strtrim(name));
801 retval = hash_store(nfsphys, id, n);
803 out_of_mem("storing nfsphys in hash table");
804 else if (retval == 1)
806 sq_save_data(sq, hash_lookup(nfsphys, id));
810 EXEC SQL CLOSE csr111;
812 generic_fix(sq, show_np_id, "Change ID", fix_np_id, 0);
814 dprintf("Checking printcap...\n");
816 EXEC SQL DECLARE csr119 CURSOR FOR
817 SELECT p1.name FROM printcap p1, printcap p2
818 WHERE p1.name = p2.name AND p1.rowid < p2.rowid;
819 EXEC SQL OPEN csr119;
822 EXEC SQL FETCH csr119 INTO :name;
826 printf("Printer %s has duplicate name\n", name);
829 EXEC SQL CLOSE csr119;