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