]> andersk Git - moira.git/blob - dbck/phase1.pc
Check object IDs for user sponsors.
[moira.git] / dbck / phase1.pc
1 /* $Id$
2  *
3  * (c) Copyright 1988-1998 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
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 EXEC SQL INCLUDE sqlca;
16
17 RCSID("$Header$");
18
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);
42 int show_cnt_name(void *cnt);
43
44 int show_user_id(void *user)
45 {
46   struct user *u = user;
47   printf("User %s (%s, status %d) has duplicate ID\n",
48          u->login, u->fullname, u->status);
49   return 0;
50 }
51
52 void handle_duplicate_logins(struct save_queue *sq)
53 {
54   struct user *u, *uu, *tmp;
55
56   uu = NULL;
57   if (sq_get_data(sq, &uu))
58     {
59       while (sq_get_data(sq, &u))
60         {
61           if (!strcmp(u->login, uu->login))
62             {
63               if (uu->status == 1 || u->status == 0)
64                 {
65                   tmp = u;
66                   u = uu;
67                   uu = tmp;
68                 }
69               printf("User %s (%s, status %d) and\n",
70                      u->login, u->fullname, u->status);
71               printf("User %s (%s, status %d) have duplicate logins\n",
72                      uu->login, uu->fullname, uu->status);
73               if (!strcmp(u->fullname, uu->fullname) &&
74                   single_fix("Delete the second one", 0))
75                 single_delete("users", "users_id", uu->users_id);
76               else if (single_fix("Unregister the second one", 0))
77                 {
78                   EXEC SQL BEGIN DECLARE SECTION;
79                   int id = uu->users_id, rowcount;
80                   EXEC SQL END DECLARE SECTION;
81
82                   EXEC SQL UPDATE users
83                     SET login = '#' || CHAR(users.unix_uid), status = 0
84                     WHERE users_id = :id;
85                   rowcount = sqlca.sqlerrd[2];
86                   if (rowcount > 0)
87                     {
88                       printf("%d entr%s fixed\n", rowcount,
89                              rowcount == 1 ? "y" : "ies");
90                     }
91                   else
92                     printf("Not fixed\n");
93                   modified("users");
94                 }
95             }
96           else
97             uu = u;
98         }
99     }
100 }
101
102 void fix_user_id(void *user)
103 {
104   struct user *u = user;
105   u->users_id = generic_fix_id("users", "users_id", "login",
106                                u->users_id, u->login);
107 }
108
109
110 void cant_fix(void *id)
111 {
112   printf("Sorry, don't know how to fix that\n");
113 }
114
115 int show_mach_id(void *machine)
116 {
117   struct machine *m = machine;
118   printf("Machine %s has duplicate ID %d\n", m->name, m->mach_id);
119   return 0;
120 }
121
122 int show_mach_name(void *machine)
123 {
124   struct machine *m = machine;
125   printf("Machine %s (%d) has duplicate name\n", m->name, m->mach_id);
126   return 0;
127 }
128
129 void fix_mach_id(void *machine)
130 {
131   struct machine *m = machine;
132   m->mach_id = generic_fix_id("machine", "mach_id", "name",
133                               m->mach_id, m->name);
134 }
135
136 int show_snet_name(void *subnet)
137 {
138   struct subnet *s = subnet;
139   printf("Subnet %s (%d) has duplicate name\n", s->name, s->snet_id);
140   return 0;
141 }
142
143 int show_clu_id(void *cluster)
144 {
145   struct cluster *c = cluster;
146   printf("Cluster %s has duplicate ID %d\n", c->name, c->clu_id);
147   return 0;
148 }
149
150 int show_clu_name(void *cluster)
151 {
152   struct cluster *c = cluster;
153   printf("Cluster %s (%d) has duplicate name\n", c->name, c->clu_id);
154   return 0;
155 }
156
157 void fix_clu_id(void *cluster)
158 {
159   struct cluster *c = cluster;
160   c->clu_id = generic_fix_id("cluster", "clu_id", "name", c->clu_id, c->name);
161 }
162
163 int show_list_id(void *list)
164 {
165   struct list *l = list;
166   printf("List %s has duplicate ID %d\n", l->name, l->list_id);
167   return 0;
168 }
169
170 int show_list_name(void *list)
171 {
172   struct list *l = list;
173   printf("List %s (%d) has duplicate name\n", l->name, l->list_id);
174   return 0;
175 }
176
177 void fix_list_id(void *list)
178 {
179   struct list *l = list;
180   l->list_id = generic_fix_id("list", "list_id", "name", l->list_id, l->name);
181 }
182
183 int show_fs_id(void *filesys)
184 {
185   struct filesys *f = filesys;
186   printf("Filesys %s has duplicate ID %d\n", f->name, f->filsys_id);
187   return 0;
188 }
189
190 void fix_fs_id(void *filesys)
191 {
192   struct filesys *f = filesys;
193   f->filsys_id = generic_fix_id("filesys", "filsys_id", "label",
194                                 f->filsys_id, f->name);
195 }
196
197 int show_fs_name(void *filesys)
198 {
199   struct filesys *fs = filesys;
200   printf("Filesys %s (%d) has duplicate name\n", fs->name, fs->filsys_id);
201   return 0;
202 }
203
204 int show_np_id(void *nfsphys)
205 {
206   struct nfsphys *n = nfsphys;
207   printf("NfsPhys %s:%s has duplicate ID %d\n",
208          ((struct machine *)hash_lookup(machines, n->mach_id))->name,
209          n->dir, n->nfsphys_id);
210   return 0;
211 }
212
213 void fix_np_id(void *nfsphys)
214 {
215   struct nfsphys *n = nfsphys;
216   n->nfsphys_id = generic_fix_id("nfsphys", "nfsphys_id", "dir",
217                                  n->nfsphys_id, n->dir);
218 }
219
220 int show_str_id(void *string)
221 {
222   struct string *s = string;
223   printf("String %s has duplicate ID %d\n", s->name, s->string_id);
224   return 0;
225 }
226
227 int print_str_id(void *id)
228 {
229   printf("String %d is a duplicate\n", (int)id);
230   return 0;
231 }
232
233 void print_dup_map(int key, void *data, void *hint)
234 {
235   printf("String %d is a duplicate of string %d\n", key, (int)data);
236 }
237
238 int show_cnt_name(void *container)
239 {
240   struct container *cnt = container;
241   printf("Container %s (%d) has duplicate name\n", cnt->name, cnt->cnt_id);
242   return 0;
243 }
244
245 void phase1(void)
246 {
247   EXEC SQL BEGIN DECLARE SECTION;
248   int id;
249   EXEC SQL END DECLARE SECTION;
250   int i, q, retval, tmp;
251   struct save_queue *sq;
252   struct user *u;
253   struct machine *m;
254   struct subnet *sn;
255   struct list *l;
256   struct cluster *c;
257   struct string *s;
258   struct filesys *f;
259   struct nfsphys *n;
260   struct printserver *ps;
261   struct container *cnt;
262
263   printf("Phase 1 - Looking for duplicates\n");
264
265   /* self-join strings table on "string" to get duplicate strings, then
266      build a duplicates table to merge them. */
267
268   dprintf("Looking for duplicate strings...\n");
269   string_dups = create_hash(100);
270   if (!string_dups)
271     out_of_mem("storing duplicate strings");
272
273   EXEC SQL DECLARE csr116 CURSOR FOR
274     SELECT s1.string_id, s2.string_id FROM strings s1, strings s2
275     WHERE s1.string = s2.string and s1.string_id < s2.string_id;
276   EXEC SQL OPEN csr116;
277   /*  The SELECT gives us two columns, both with non-negative integers.
278    *  The number in the left column is always the smaller of the two,
279    *  and each row includes string IDs for identical strings.  We use
280    *  them to make a mapping from id-to-delete to id-to-keep for all
281    *  superflous IDs.
282    */
283   q = 0;
284   while (1)
285     {
286       EXEC SQL BEGIN DECLARE SECTION;
287       int id1, id2;
288       EXEC SQL END DECLARE SECTION;
289
290       EXEC SQL FETCH csr116 INTO :id1, :id2;
291       if (sqlca.sqlcode)
292         break;
293       q++;
294       /*  If id2 is already stored, skip this row. */
295       i = (int)hash_lookup(string_dups, id2);
296       if (i > 0)
297         continue;
298       /*  Follow the chain of id1 equivalent IDs back to the lowest one. */
299       id = id1;
300       while ((tmp = (int)hash_lookup(string_dups, id)) > 0)
301         id = tmp;
302       hash_store(string_dups, id2, (void *)id);
303     }
304   EXEC SQL CLOSE csr116;
305   dprintf("found %d duplicates\n", q);
306   hash_step(string_dups, print_dup_map, NULL);
307   /* We don't want to delete the duplicates now because if the dbck
308      is cancelled, a LOT of state will be lost. So, we'll just let
309      them not get marked as used and then phase3 will clean them up */
310
311   dprintf("Loading strings...\n");
312   sq = sq_create();
313   strings = create_hash(75000);
314   if (!sq || !strings)
315     out_of_mem("loading strings");
316
317   EXEC SQL DECLARE csr101 CURSOR FOR
318     SELECT string_id, string FROM strings ORDER BY string_id;
319   EXEC SQL OPEN csr101;
320   q = 0;
321   while (1)
322     {
323       EXEC SQL BEGIN DECLARE SECTION;
324       int id;
325       char buf[STRINGS_STRING_SIZE];
326       EXEC SQL END DECLARE SECTION;
327
328       EXEC SQL FETCH csr101 INTO :id, :buf;
329       if (sqlca.sqlcode)
330         break;
331       q++;
332       s = malloc(sizeof(struct string));
333       if (!s)
334         out_of_mem("storing strings");
335       s->name = strdup(strtrim(buf));
336       s->string_id = id;
337       s->refc = 0;
338       retval = hash_store(strings, id, s);
339       if (retval == -1)
340         out_of_mem("storing strings in hash table");
341       else if (retval == 1) /* duplicate string_id */
342         {
343           sq_save_data(sq, hash_lookup(strings, id));
344           sq_save_data(sq, s);
345         }
346     }
347   EXEC SQL CLOSE csr101;
348   /* keep string id 0 (the empty string) even if unreferenced */
349   string_check(0);
350
351   printf("Loaded %d strings\n", q);
352
353   dprintf("Loading users...\n");
354   sq = sq_create();
355   users = create_hash(65000);
356   if (!sq || !users)
357     out_of_mem("loading users");
358
359   EXEC SQL DECLARE csr102 CURSOR FOR
360     SELECT users_id, login, last, first, status, potype, pop_id, box_id,
361     imap_id, modby, fmodby, pmodby, comments, sigwho, sponsor_type, sponsor_id
362     FROM users ORDER BY users_id;
363   EXEC SQL OPEN csr102;
364   while (1)
365     {
366       EXEC SQL BEGIN DECLARE SECTION;
367       char login[USERS_LOGIN_SIZE], nbuf[USERS_FIRST_SIZE + USERS_LAST_SIZE];
368       char last[USERS_LAST_SIZE], first[USERS_FIRST_SIZE];
369       char potype[USERS_POTYPE_SIZE], sponsor_type[USERS_SPONSOR_TYPE_SIZE];
370       int users_id, status, pop_id, box_id, imap_id, modby, fmodby, pmodby;
371       int comments, sigwho, sponsor_id;
372       EXEC SQL END DECLARE SECTION;
373
374       EXEC SQL FETCH csr102 INTO :users_id, :login, :last, :first,
375         :status, :potype, :pop_id, :box_id, :imap_id, :modby, :fmodby,
376         :pmodby, :comments, :sigwho, :sponsor_type, :sponsor_id;
377       if (sqlca.sqlcode)
378         break;
379
380       u = malloc(sizeof(struct user));
381       if (!u)
382         out_of_mem("storing users");
383       strcpy(u->login, strtrim(login));
384       u->potype = potype[0];
385       sprintf(nbuf, "%s, %s", strtrim(last), strtrim(first));
386       u->fullname = strdup(nbuf);
387       u->status = status;
388       u->users_id = users_id;
389       u->modby = modby;
390       u->fmodby = fmodby;
391       u->pmodby = pmodby;
392       u->comment = comments;
393       u->sigwho = sigwho;
394       u->sponsor_type = sponsor_type[0];
395       u->sponsor_id = sponsor_id;
396       switch (u->potype)
397         {
398         case 'P':
399           u->pobox_id = pop_id;
400           break;
401         case 'S':
402           /*  If potype is SMTP, box_id is a string_id for the strings tbl */
403           u->pobox_id = box_id;
404           break;
405         case 'I':
406           u->pobox_id = imap_id;
407           break;
408         default:
409           u->pobox_id = 0;
410         }
411       retval = hash_store(users, users_id, u);
412       if (retval == -1)
413         out_of_mem("storing users in hash table");
414       else if (retval == 1)
415         {
416           sq_save_data(sq, hash_lookup(users, users_id));
417           sq_save_data(sq, u);
418         }
419     }
420   EXEC SQL CLOSE csr102;
421
422   generic_fix(sq, show_user_id, "Change ID", fix_user_id, 0);
423
424   if (!fast)
425     {
426       sq = sq_create();
427       if (!sq)
428         out_of_mem("finding duplicate logins");
429
430       EXEC SQL DECLARE csr103 CURSOR FOR
431         SELECT u1.users_id FROM users u1, users u2
432         WHERE u1.login = u2.login and u1.rowid != u2.rowid;
433       EXEC SQL OPEN csr103;
434       while (1)
435         {
436           EXEC SQL FETCH csr103 INTO :id;
437           if (sqlca.sqlcode)
438             break;
439           sq_save_data(sq, hash_lookup(users, id));
440         }
441       EXEC SQL CLOSE csr103;
442       handle_duplicate_logins(sq);
443     }
444
445   if (!fast)
446     {
447       dprintf("Scanning krbmap...\n");
448
449       EXEC SQL DECLARE csr113 CURSOR FOR
450         SELECT k1.users_id FROM krbmap k1, krbmap k2
451         WHERE k1.users_id = k2.users_id AND k1.rowid != k2.rowid;
452       EXEC SQL OPEN csr113;
453       while (1)
454         {
455           EXEC SQL FETCH csr113 INTO :id;
456           if (sqlca.sqlcode)
457             break;
458
459           printf("User %d is in the krbmap more than once!\n", id);
460           printf("Not fixing this error\n");
461         }
462       EXEC SQL CLOSE csr113;
463
464       EXEC SQL DECLARE csr114 CURSOR FOR
465         SELECT k1.string_id FROM krbmap k1, krbmap k2
466         WHERE k1.string_id = k2.string_id AND k1.rowid != k2.rowid;
467       EXEC SQL OPEN csr114;
468       while (1)
469         {
470           EXEC SQL FETCH csr114 INTO :id;
471           if (sqlca.sqlcode)
472             break;
473
474           printf("Principal %d is in the krbmap more than once!\n", id);
475           printf("Not fixing this error\n");
476         }
477       EXEC SQL CLOSE csr114;
478     }
479
480   dprintf("Loading machines...\n");
481   sq = sq_create();
482   machines = create_hash(20000);
483   if (!sq || !machines)
484     out_of_mem("loading machines");
485
486   EXEC SQL DECLARE csr104 CURSOR FOR
487     SELECT mach_id, name, snet_id, owner_type, owner_id,
488     acomment, ocomment, creator, modby
489     FROM machine ORDER BY mach_id;
490   EXEC SQL OPEN csr104;
491   while (1)
492     {
493       EXEC SQL BEGIN DECLARE SECTION;
494       int mach_id, snet_id, owner_id, acomment, ocomment, creator, modby;
495       char name[MACHINE_NAME_SIZE], owner_type[MACHINE_OWNER_TYPE_SIZE];
496       EXEC SQL END DECLARE SECTION;
497
498       EXEC SQL FETCH csr104 INTO :mach_id, :name, :snet_id,
499         :owner_type, :owner_id, :acomment, :ocomment, :creator, :modby;
500       if (sqlca.sqlcode)
501         break;
502
503       m = malloc(sizeof(struct machine));
504       if (!m)
505         out_of_mem("storing machines");
506       strcpy(m->name, strtrim(name));
507       m->owner_type = owner_type[0];
508       m->owner_id = owner_id;
509       m->snet_id = snet_id;
510       m->mach_id = mach_id;
511       m->clucount = 0;
512       m->acomment = acomment;
513       m->ocomment = ocomment;
514       m->creator = creator;
515       m->modby = modby;
516       retval = hash_store(machines, mach_id, m);
517       if (retval == -1)
518         out_of_mem("storing machines in hash table");
519       else if (retval == 1)
520         {
521           sq_save_data(sq, hash_lookup(machines, mach_id));
522           sq_save_data(sq, m);
523         }
524     }
525   EXEC SQL CLOSE csr104;
526   generic_fix(sq, show_mach_id, "Change ID", fix_mach_id, 0);
527
528   if (!fast)
529     {
530       EXEC SQL BEGIN DECLARE SECTION;
531       char name[HOSTALIAS_NAME_SIZE];
532       int id1, id2;
533       EXEC SQL END DECLARE SECTION;
534
535       sq = sq_create();
536       if (!sq)
537         out_of_mem("looking for duplicate machine names");
538
539       EXEC SQL DECLARE csr105 CURSOR FOR
540         SELECT m1.mach_id FROM machine m1, machine m2
541         WHERE m1.name = m2.name AND m1.rowid != m2.rowid;
542       EXEC SQL OPEN csr105;
543       while (1)
544         {
545           EXEC SQL FETCH csr105 INTO :id;
546           if (sqlca.sqlcode)
547             break;
548
549           sq_save_data(sq, hash_lookup(machines, id));
550         }
551       EXEC SQL CLOSE csr105;
552       generic_fix(sq, show_mach_name, "Change name", cant_fix, 0);
553
554       EXEC SQL DECLARE csr_hal1 CURSOR FOR
555         SELECT h1.name, m1.mach_id, m2.mach_id
556         FROM hostalias h1, machine m1, hostalias h2, machine m2
557         WHERE h1.name = h2.name AND h1.mach_id != h2.mach_id
558         AND m1.mach_id = h1.mach_id AND m2.mach_id = h2.mach_id;
559       EXEC SQL OPEN csr_hal1;
560       while (1)
561         {
562           EXEC SQL FETCH csr_hal1 INTO :name, :id1, :id2;
563           if (sqlca.sqlcode)
564             break;
565           printf("Aliases for machines %d and %d have duplicate name %s\n",
566                  id1, id2, strtrim(name));
567           cant_fix(0);
568         }
569       EXEC SQL CLOSE csr_hal1;
570
571       EXEC SQL DECLARE csr_hal2 CURSOR FOR
572         SELECT h1.name, m1.mach_id, m2.mach_id
573         FROM hostalias h1, machine m1, machine m2
574         WHERE h1.name = m1.name AND h1.mach_id = m2.mach_id;
575       EXEC SQL OPEN csr_hal2;
576         while (1)
577           {
578             EXEC SQL FETCH csr_hal2 INTO :name, :id1, :id2;
579             if (sqlca.sqlcode)
580               break;
581             printf("Machine %d has alias %s that conflicts with machine %d\n",
582                    id2, strtrim(name), id1);
583             cant_fix(0);
584           }
585         EXEC SQL CLOSE csr_hal2;
586     }
587
588   dprintf("Loading subnets...\n");
589   subnets = create_hash(254);
590   if (!subnets)
591     out_of_mem("loading subnets");
592
593   EXEC SQL DECLARE csr115 CURSOR FOR
594     SELECT snet_id, name, owner_type, owner_id, modby from subnet;
595   EXEC SQL OPEN csr115;
596   while (1)
597     {
598       EXEC SQL BEGIN DECLARE SECTION;
599       char name[SUBNET_NAME_SIZE], owner_type[SUBNET_OWNER_TYPE_SIZE];
600       int snet_id, owner_id, modby;
601       EXEC SQL END DECLARE SECTION;
602
603       EXEC SQL FETCH csr115 INTO :snet_id, :name, :owner_type,
604         :owner_id, :modby;
605       if (sqlca.sqlcode)
606         break;
607
608       sn = malloc(sizeof(struct machine));
609       if (!sn)
610         out_of_mem("storing subnets");
611       strcpy(sn->name, strtrim(name));
612       sn->owner_type = owner_type[0];
613       sn->owner_id = owner_id;
614       sn->snet_id = snet_id;
615       sn->modby = modby;
616       retval = hash_store(subnets, snet_id, sn);
617       if (retval == -1)
618         out_of_mem("storing subnets in hash table");
619       else if (retval == 1)
620         {
621           printf("Duplicate subnet ID: %d (%s)\n", id, name);
622           /* should add code to delete */
623           cant_fix(0);
624         }
625     }
626   EXEC SQL CLOSE csr115;
627
628   if (!fast)
629     {
630       sq = sq_create();
631       if (!sq)
632         out_of_mem("looking for duplicate subnet names");
633
634       EXEC SQL DECLARE csr117 CURSOR FOR
635         SELECT s1.snet_id FROM subnet s1, subnet s2
636         WHERE s1.name = s2.name AND s1.rowid != s2.rowid;
637       EXEC SQL OPEN csr117;
638       while (1)
639         {
640           EXEC SQL FETCH csr117 INTO :id;
641           if (sqlca.sqlcode)
642             break;
643
644           sq_save_data(sq, hash_lookup(subnets, id));
645         }
646       EXEC SQL CLOSE csr117;
647       generic_fix(sq, show_snet_name, "Change name", cant_fix, 0);
648     }
649
650   dprintf("Loading clusters...\n");
651   sq = sq_create();
652   clusters = create_hash(100);
653   if (!sq || !clusters)
654     out_of_mem("loading clusters");
655
656   EXEC SQL DECLARE csr106 CURSOR FOR
657     SELECT clu_id, name, modby FROM clusters;
658   EXEC SQL OPEN csr106;
659   while (1)
660     {
661       EXEC SQL BEGIN DECLARE SECTION;
662       int clu_id, modby;
663       char name[CLUSTERS_NAME_SIZE];
664       EXEC SQL END DECLARE SECTION;
665
666       EXEC SQL FETCH csr106 INTO :clu_id, :name, :modby;
667       if (sqlca.sqlcode)
668         break;
669
670       c = malloc(sizeof(struct cluster));
671       if (!c)
672         out_of_mem("storing clusters");
673       strcpy(c->name, strtrim(name));
674       c->clu_id = clu_id;
675       c->modby = modby;
676       retval = hash_store(clusters, clu_id, c);
677       if (retval == -1)
678         out_of_mem("storing clusters in hash table");
679       else if (retval == 1)
680         {
681           sq_save_data(sq, hash_lookup(clusters, clu_id));
682           sq_save_data(sq, c);
683         }
684     }
685   EXEC SQL CLOSE csr106;
686   generic_fix(sq, show_clu_id, "Change ID", fix_clu_id, 0);
687
688   if (!fast)
689     {
690       sq = sq_create();
691       if (!sq)
692         out_of_mem("looking for duplicate cluster names");
693
694       EXEC SQL DECLARE csr107 CURSOR FOR
695         SELECT c1.clu_id FROM clusters c1, clusters c2
696         WHERE c1.name = c2.name AND c1.rowid != c2.rowid;
697       EXEC SQL OPEN csr107;
698       while (1)
699         {
700           EXEC SQL FETCH csr107 INTO :id;
701           if (sqlca.sqlcode)
702             break;
703
704           sq_save_data(sq, hash_lookup(clusters, id));
705         }
706       EXEC SQL CLOSE csr107;
707       generic_fix(sq, show_clu_name, "Change name", cant_fix, 0);
708     }
709
710   dprintf("Loading lists...\n");
711   sq = sq_create();
712   lists = create_hash(50000);
713   if (!sq || !lists)
714     out_of_mem("loading lists");
715
716   EXEC SQL DECLARE csr108 CURSOR FOR
717     SELECT list_id, name, acl_id, acl_type, memacl_id, memacl_type, modby
718     FROM list
719     ORDER BY list_id;
720   EXEC SQL OPEN csr108;
721   while (1)
722     {
723       EXEC SQL BEGIN DECLARE SECTION;
724       int list_id, acl_id, memacl_id, modby;
725       char name[LIST_NAME_SIZE], acl_type[LIST_ACL_TYPE_SIZE];
726       char memacl_type[LIST_ACL_TYPE_SIZE];
727       EXEC SQL END DECLARE SECTION;
728
729       EXEC SQL FETCH csr108 INTO :list_id, :name, :acl_id, :acl_type, 
730         :memacl_id, :memacl_type, :modby;
731       if (sqlca.sqlcode)
732         break;
733       l = malloc(sizeof(struct list));
734       if (!l)
735         out_of_mem("storing lists");
736       strcpy(l->name, strtrim(name));
737       l->acl_type = acl_type[0];
738       l->acl_id = acl_id;
739       l->memacl_type = memacl_type[0];
740       l->memacl_id = memacl_id;
741       l->list_id = list_id;
742       l->modby = modby;
743       l->members = 0;
744       retval = hash_store(lists, list_id, l);
745       if (retval == -1)
746         out_of_mem("storing lists in hash table");
747       else if (retval == 1)
748         {
749           sq_save_data(sq, hash_lookup(lists, list_id));
750           sq_save_data(sq, l);
751         }
752     }
753   EXEC SQL CLOSE csr108;
754   generic_fix(sq, show_list_id, "Change ID", fix_list_id, 0);
755
756   if (!fast)
757     {
758       sq = sq_create();
759       if (!sq)
760         out_of_mem("looking for duplicate list names");
761
762       EXEC SQL DECLARE csr109 CURSOR FOR
763         SELECT l1.list_id FROM list l1, list l2
764         WHERE l1.name = l2.name AND l1.rowid != l2.rowid;
765       EXEC SQL OPEN csr109;
766       while (1)
767         {
768           EXEC SQL FETCH csr109 INTO :id;
769           if (sqlca.sqlcode)
770             break;
771
772           sq_save_data(sq, hash_lookup(lists, id));
773         }
774       EXEC SQL CLOSE csr109;
775       generic_fix(sq, show_list_name, "Change name", cant_fix, 0);
776     }
777
778   dprintf("Loading filesys...\n");
779   sq = sq_create();
780   filesys = create_hash(30000);
781   if (!sq || !filesys)
782     out_of_mem("loading filesys");
783
784   EXEC SQL DECLARE csr110 CURSOR FOR
785     SELECT filsys_id, label, owner, owners, phys_id, mach_id,
786     type, name, modby FROM filesys ORDER BY filsys_id;
787   EXEC SQL OPEN csr110;
788   while (1)
789     {
790       EXEC SQL BEGIN DECLARE SECTION;
791       int filsys_id, owner, owners, phys_id, mach_id, modby;
792       char label[FILESYS_LABEL_SIZE], type[FILESYS_TYPE_SIZE];
793       char name[FILESYS_NAME_SIZE];
794       EXEC SQL END DECLARE SECTION;
795
796       EXEC SQL FETCH csr110 INTO :filsys_id, :label, :owner, :owners,
797         :phys_id, :mach_id, :type, :name, :modby;
798       if (sqlca.sqlcode)
799         break;
800
801       f = malloc(sizeof(struct filesys));
802       if (!f)
803         out_of_mem("storing filesystems");
804       strcpy(f->name, strtrim(label));
805       strcpy(f->dir, strtrim(name));
806       f->filsys_id = filsys_id;
807       f->owner = owner;
808       f->owners = owners;
809       f->phys_id = phys_id;
810       f->mach_id = mach_id;
811       f->type = type[0];
812       retval = hash_store(filesys, filsys_id, f);
813       if (retval == -1)
814         out_of_mem("storing filesys in hash table");
815       else if (retval == 1)
816         {
817           sq_save_data(sq, hash_lookup(filesys, filsys_id));
818           sq_save_data(sq, f);
819         }
820     }
821   EXEC SQL CLOSE csr110;
822
823   generic_fix(sq, show_fs_id, "Change ID", fix_fs_id, 0);
824
825   if (!fast)
826     {
827       sq = sq_create();
828       if (!sq)
829         out_of_mem("looking for duplicate filesys names");
830
831       EXEC SQL DECLARE csr118 CURSOR FOR
832         SELECT fs1.filsys_id FROM filesys fs1, filesys fs2
833         WHERE fs1.label = fs2.label AND fs1.rowid != fs2.rowid;
834       EXEC SQL OPEN csr118;
835       while (1)
836         {
837           EXEC SQL FETCH csr118 INTO :id;
838           if (sqlca.sqlcode)
839             break;
840
841           sq_save_data(sq, hash_lookup(filesys, id));
842         }
843       EXEC SQL CLOSE csr118;
844       generic_fix(sq, show_fs_name, "Change name", cant_fix, 0);
845     }
846
847   dprintf("Loading nfsphys...\n");
848   sq = sq_create();
849   nfsphys = create_hash(500);
850   if (!sq || !nfsphys)
851     out_of_mem("loading nfsphs");
852
853   EXEC SQL DECLARE csr111 CURSOR FOR
854     SELECT nfsphys_id, dir, mach_id, TO_CHAR(allocated), modby FROM nfsphys;
855   EXEC SQL OPEN csr111;
856   while (1)
857     {
858       EXEC SQL BEGIN DECLARE SECTION;
859       int nfsphys_id, mach_id, modby;
860       char dir[NFSPHYS_DIR_SIZE];
861       char allocated[39];
862       EXEC SQL END DECLARE SECTION;
863
864       EXEC SQL FETCH csr111 INTO :nfsphys_id, :dir, :mach_id,
865         :allocated, :modby;
866       if (sqlca.sqlcode)
867         break;
868
869       n = malloc(sizeof(struct nfsphys));
870       if (!n)
871         out_of_mem("storing nfsphys");
872       strcpy(n->dir, strtrim(dir));
873       n->mach_id = mach_id;
874       n->nfsphys_id = nfsphys_id;
875       n->allocated = strtoull(allocated, NULL, 0);
876       n->modby = modby;
877       n->count = 0;
878       retval = hash_store(nfsphys, nfsphys_id, n);
879       if (retval == -1)
880         out_of_mem("storing nfsphys in hash table");
881       else if (retval == 1)
882         {
883           sq_save_data(sq, hash_lookup(nfsphys, nfsphys_id));
884           sq_save_data(sq, n);
885         }
886     }
887   EXEC SQL CLOSE csr111;
888
889   generic_fix(sq, show_np_id, "Change ID", fix_np_id, 0);
890
891   if (!fast)
892     {
893       dprintf("Checking printers...\n");
894
895       EXEC SQL DECLARE csr119 CURSOR FOR
896         SELECT p1.name FROM printers p1, printers p2
897         WHERE ( p1.name = p2.name AND p1.rowid < p2.rowid )
898         OR ( p1.name = p2.duplexname );
899       EXEC SQL OPEN csr119;
900       while (1)
901         {
902           EXEC SQL BEGIN DECLARE SECTION;
903           char name[PRINTERS_NAME_SIZE];
904           EXEC SQL END DECLARE SECTION;
905
906           EXEC SQL FETCH csr119 INTO :name;
907           if (sqlca.sqlcode)
908             break;
909
910           printf("Printer %s has duplicate name\n", name);
911           cant_fix(0);
912         }
913       EXEC SQL CLOSE csr119;
914     }
915
916   dprintf("Loading printservers...\n");
917   printservers = create_hash(100);
918   if (!printservers)
919     out_of_mem("loading printservers");
920
921   EXEC SQL DECLARE csr_ps CURSOR FOR
922     SELECT mach_id, printer_types, owner_type, owner_id, lpc_acl, modby
923     FROM printservers;
924   EXEC SQL OPEN csr_ps;
925   while (1)
926     {
927       EXEC SQL BEGIN DECLARE SECTION;
928       int mach_id, printer_types, owner_id, lpc_acl, modby;
929       char owner_type[PRINTSERVERS_OWNER_TYPE_SIZE];
930       EXEC SQL END DECLARE SECTION;
931
932       EXEC SQL FETCH csr_ps INTO :mach_id, :printer_types, :owner_type,
933         :owner_id, :lpc_acl, :modby;
934       if (sqlca.sqlcode)
935         break;
936
937       ps = malloc(sizeof(struct printserver));
938       if (!ps)
939         out_of_mem("storing printserver");
940       ps->mach_id = mach_id;
941       ps->printer_types = printer_types;
942       ps->owner_type = owner_type[0];
943       ps->owner_id = owner_id;
944       ps->lpc_acl = lpc_acl;
945       ps->modby = modby;
946       retval = hash_store(printservers, mach_id, ps);
947       if (retval == -1)
948         out_of_mem("storing printserver in hash table");
949       else if (retval == 1)
950         {
951           printf("Duplicate printserver mach_id %d\n", mach_id);
952           cant_fix(0);
953         }
954     }
955   EXEC SQL CLOSE csr_ps;
956
957   if (!fast)
958     {
959       dprintf("Checking zephyr...\n");
960       
961       EXEC SQL DECLARE csr120 CURSOR FOR
962         SELECT z1.class FROM zephyr z1, zephyr z2
963         WHERE (z1.class = z2.class AND z1.rowid < z1.rowid);
964       EXEC SQL OPEN csr120;
965       while (1)
966         {
967           EXEC SQL BEGIN DECLARE SECTION;
968           char class[ZEPHYR_CLASS_SIZE];
969           EXEC SQL END DECLARE SECTION;
970
971           EXEC SQL FETCH csr120 INTO :class;
972           if (sqlca.sqlcode)
973             break;
974
975           printf("Zephyr class %s has duplicate name\n", class);
976           cant_fix(0);
977         }
978       EXEC SQL CLOSE csr120;
979     }
980
981   dprintf("Loading containers...\n");
982   containers = create_hash(1000);
983   if (!containers)
984     out_of_mem("loading containers");
985
986   EXEC SQL DECLARE csr_cnts CURSOR FOR
987     SELECT name, cnt_id, list_id, acl_type, acl_id, memacl_type, memacl_id,
988     modby FROM containers;
989   EXEC SQL OPEN csr_cnts;
990   while (1)
991     {
992       EXEC SQL BEGIN DECLARE SECTION;
993       int cnt_id, list_id, acl_id, memacl_id, modby;
994       char name[CONTAINERS_NAME_SIZE];
995       char acl_type[CONTAINERS_ACL_TYPE_SIZE];
996       char memacl_type[CONTAINERS_MEMACL_TYPE_SIZE];
997       EXEC SQL END DECLARE SECTION;
998
999       EXEC SQL FETCH csr_cnts INTO :name, :cnt_id, :list_id, :acl_type,
1000         :acl_id, :memacl_type, :memacl_id, :modby;
1001       if (sqlca.sqlcode)
1002         break;
1003
1004       cnt = malloc(sizeof(struct container));
1005       if (!cnt)
1006         out_of_mem("storing container");
1007       strcpy(cnt->name, strtrim(name));
1008       cnt->cnt_id = cnt_id;
1009       cnt->list_id = list_id;
1010       cnt->acl_type = acl_type[0];
1011       cnt->acl_id = acl_id;
1012       cnt->memacl_type = memacl_type[0];
1013       cnt->memacl_id = memacl_id;
1014       cnt->modby = modby;
1015       retval = hash_store(containers, cnt_id, cnt);
1016       if (retval == -1)
1017         out_of_mem("storing container in hash table");
1018       else if (retval == 1)
1019         {
1020           printf("Duplicate container cnt_id %d\n", cnt_id);
1021           cant_fix(0);
1022         }
1023     }
1024   EXEC SQL CLOSE csr_cnts;
1025
1026   if (!fast)
1027     {
1028       sq = sq_create();
1029       if (!sq)
1030         out_of_mem("looking for duplicate container names");
1031
1032       EXEC SQL DECLARE csr121 CURSOR FOR
1033         SELECT cnt1.cnt_id FROM containers cnt1, containers cnt2
1034         WHERE cnt1.name = cnt2.name AND cnt1.cnt_id != cnt2.cnt_id;
1035       EXEC SQL OPEN csr121;
1036       while (1)
1037         {
1038           EXEC SQL FETCH csr121 INTO :id;
1039           if (sqlca.sqlcode)
1040             break;
1041
1042           sq_save_data(sq, hash_lookup(containers, id));
1043         }
1044       EXEC SQL CLOSE csr121;
1045       generic_fix(sq, show_cnt_name, "Change name", cant_fix, 0);
1046     }
1047 }
This page took 0.120983 seconds and 5 git commands to generate.