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