]> andersk Git - moira.git/blame - server/increment.pc
If a user has the secure bit set and a PIN defined in the database, ask
[moira.git] / server / increment.pc
CommitLineData
7ac48069 1/* $Id$
32cfe906 2 *
7ac48069 3 * Deal with incremental updates
5eaef520 4 *
7ac48069 5 * Copyright (C) 1989-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
32cfe906 8 */
9
32cfe906 10#include <mit-copyright.h>
32cfe906 11#include "mr_server.h"
03c05291 12#include "query.h"
13#include "qrtn.h"
7ac48069 14
15#include <signal.h>
85330553 16#include <stdio.h>
7ac48069 17#include <stdlib.h>
18#include <string.h>
19#include <unistd.h>
20
45bf7573 21EXEC SQL INCLUDE sqlca;
32cfe906 22
7ac48069 23RCSID("$Header$");
24
32cfe906 25extern char *whoami;
03c05291 26extern char *table_name[];
27extern int num_tables;
32cfe906 28
29int inc_pid = 0;
30int inc_running = 0;
31time_t inc_started;
32
33#define MAXARGC 15
34
03c05291 35EXEC SQL WHENEVER SQLERROR DO dbmserr();
45bf7573 36
32cfe906 37/* structures to save before args */
32cfe906 38static char *before[MAXARGC];
32cfe906 39static int beforec;
03c05291 40static enum tables beforetable;
32cfe906 41
42/* structures to save after args */
32cfe906 43static char *after[MAXARGC];
32cfe906 44static int afterc;
45
46/* structures to save entire sets of incremental changes */
47struct save_queue *incremental_sq = NULL;
48struct save_queue *incremental_exec = NULL;
49struct iupdate {
5eaef520 50 char *table;
51 int beforec;
52 char **before;
53 int afterc;
54 char **after;
55 char *service;
32cfe906 56};
57
03c05291 58void next_incremental(void);
fc053494 59char **copy_argv(char **argv, int argc);
03c05291 60void free_argv(char **argv, int argc);
61int table_num(char *table);
32cfe906 62
03c05291 63void incremental_init(void)
32cfe906 64{
5eaef520 65 int i;
32cfe906 66
5eaef520 67 if (!incremental_sq)
68 incremental_sq = sq_create();
69 if (!incremental_exec)
70 incremental_exec = sq_create();
03c05291 71
5eaef520 72 for (i = 0; i < MAXARGC; i++)
73 {
e688520a 74 before[i] = xmalloc(MAX_FIELD_WIDTH);
75 after[i] = xmalloc(MAX_FIELD_WIDTH);
03c05291 76 }
32cfe906 77}
78
79
03c05291 80/* record the state of a table row before it is changed */
81
5eaef520 82void incremental_before(enum tables table, char *qual, char **argv)
32cfe906 83{
5eaef520 84 EXEC SQL BEGIN DECLARE SECTION;
85 int id;
86 EXEC SQL END DECLARE SECTION;
87
88acd4d3 88 char *name, *name2;
5eaef520 89
90 beforetable = table;
91
92 switch (table)
93 {
94 case USERS_TABLE:
d6759517 95 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
96 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
88acd4d3 97 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
5eaef520 98 dosql(before);
88acd4d3 99 beforec = 11;
5eaef520 100 break;
101 case MACHINE_TABLE:
88acd4d3 102 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
5eaef520 103 "WHERE %s", qual);
104 dosql(before);
88acd4d3 105 beforec = 3;
5eaef520 106 break;
e688520a 107 case CLUSTERS_TABLE:
88acd4d3 108 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
109 "c.clu_id FROM clusters c WHERE %s", qual);
5eaef520 110 dosql(before);
88acd4d3 111 beforec = 4;
5eaef520 112 break;
bb5c9457 113 case CONTAINERS_TABLE:
114 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
115 "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s",
116 qual);
117 dosql(before);
118 beforec = 7;
429aec78 119 name = xmalloc(0);
120 id = atoi(before[5]);
121 if (!strncmp(before[4], "USER", 4))
122 {
123 id_to_name(id, USERS_TABLE, &name);
124 strcpy(before[5], name);
125 }
126 else if (!strncmp(before[4], "LIST", 4))
127 {
128 id_to_name(id, LIST_TABLE, &name);
129 strcpy(before[5], name);
130 }
131 else if (!strncmp(before[4], "KERBEROS", 8))
132 {
133 id_to_name(id, STRINGS_TABLE, &name);
134 strcpy(before[5], name);
135 }
bb5c9457 136 break;
5eaef520 137 case MCMAP_TABLE:
138 strcpy(before[0], argv[0]);
139 strcpy(before[1], argv[1]);
140 beforec = 2;
141 break;
142 case SVC_TABLE:
143 strcpy(before[0], argv[0]);
144 strcpy(before[1], argv[1]);
145 strcpy(before[2], argv[2]);
146 beforec = 3;
147 break;
148 case FILESYS_TABLE:
149 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
150 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
88acd4d3 151 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
152 "WHERE %s", qual);
5eaef520 153 dosql(before);
88acd4d3 154 name = xmalloc(0);
5eaef520 155 id = atoi(before[2]);
156 id_to_name(id, MACHINE_TABLE, &name);
157 strcpy(before[2], name);
158 id = atoi(before[7]);
159 id_to_name(id, USERS_TABLE, &name);
160 strcpy(before[7], name);
161 id = atoi(before[8]);
162 id_to_name(id, LIST_TABLE, &name);
163 strcpy(before[8], name);
164 free(name);
88acd4d3 165 beforec = 12;
5eaef520 166 break;
167 case QUOTA_TABLE:
168 strcpy(before[0], "?");
169 strcpy(before[1], argv[1]);
170 strcpy(before[2], "?");
171 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
172 "WHERE %s AND fs.filsys_id = q.filsys_id", qual);
173 dosql(&(before[3]));
174 strcpy(before[2], argv[1]);
175 beforec = 5;
176 break;
177 case LIST_TABLE:
178 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
179 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
88acd4d3 180 "l.description, l.list_id FROM list l WHERE %s", qual);
5eaef520 181 dosql(before);
88acd4d3 182 beforec = 11;
5eaef520 183 break;
184 case IMEMBERS_TABLE:
185 id = (int) argv[0];
186 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
187 "grouplist, gid FROM list WHERE list_id = %d", id);
188 dosql(&(before[3]));
88acd4d3 189 name = xmalloc(0);
5eaef520 190 id_to_name(id, LIST_TABLE, &name);
88acd4d3 191 name2 = xmalloc(0);
5eaef520 192 strcpy(before[0], name);
193 strcpy(before[1], argv[1]);
194 id = (int) argv[2];
88acd4d3 195 beforec = 10;
5eaef520 196 if (!strcmp(before[1], "USER"))
197 {
88acd4d3 198 id_to_name(id, USERS_TABLE, &name2);
199 EXEC SQL SELECT status, users_id INTO :before[9], :before[11]
200 FROM users WHERE users_id = :id;
201 EXEC SQL SELECT list_id INTO :before[10] FROM list
202 WHERE name = :name;
203 beforec = 12;
5eaef520 204 }
205 else if (!strcmp(before[1], "LIST"))
88acd4d3 206 {
207 id_to_name(id, LIST_TABLE, &name2);
208 EXEC SQL SELECT list_id INTO :before[9] FROM list
209 WHERE name = :name;
210 sprintf(before[10], "%d", id);
211 beforec = 11;
212 }
5eaef520 213 else if (!strcmp(before[1], "STRING") || !strcmp(before[1], "KERBEROS"))
88acd4d3 214 {
215 id_to_name(id, STRINGS_TABLE, &name2);
216 EXEC SQL SELECT list_id INTO :before[9] FROM list
217 WHERE name = :name;
218 }
5f7b0741 219 else if (!strcmp(before[1], "MACHINE"))
220 {
221 id_to_name(id, MACHINE_TABLE, &name2);
222 EXEC SQL SELECT mach_id INTO :before[9] FROM machine
223 WHERE name = :name;
224 }
88acd4d3 225 strcpy(before[2], name2);
5eaef520 226 free(name);
88acd4d3 227 free(name2);
5eaef520 228 break;
229 default:
03c05291 230 /*
231 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
232 table_name[table]);
233 */
5eaef520 234 break;
235 }
a313cad2 236}
32cfe906 237
238
03c05291 239void incremental_clear_before(void)
32cfe906 240{
5eaef520 241 beforec = 0;
32cfe906 242}
243
32cfe906 244
03c05291 245/* add an element to the incremental queue for the changed row */
32cfe906 246
5eaef520 247void incremental_after(enum tables table, char *qual, char **argv)
a313cad2 248{
88acd4d3 249 char *name, *name2;
5eaef520 250 EXEC SQL BEGIN DECLARE SECTION;
251 int id;
252 EXEC SQL END DECLARE SECTION;
253 struct iupdate *iu;
254
255 switch (table)
256 {
257 case USERS_TABLE:
d6759517 258 sprintf(stmt_buf, "SELECT u.login, u.unix_uid, u.shell, "
259 "u.winconsoleshell, u.last, u.first, u.middle, u.status, "
88acd4d3 260 "u.clearid, u.type, u.users_id FROM users u WHERE %s", qual);
5eaef520 261 dosql(after);
88acd4d3 262 afterc = 11;
5eaef520 263 break;
264 case MACHINE_TABLE:
88acd4d3 265 sprintf(stmt_buf, "SELECT m.name, m.vendor, m.mach_id FROM machine m "
5eaef520 266 "WHERE %s", qual);
267 dosql(after);
88acd4d3 268 afterc = 3;
5eaef520 269 break;
e688520a 270 case CLUSTERS_TABLE:
88acd4d3 271 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, "
272 "c.clu_id FROM clusters c WHERE %s", qual);
5eaef520 273 dosql(after);
88acd4d3 274 afterc = 4;
5eaef520 275 break;
bb5c9457 276 case CONTAINERS_TABLE:
277 sprintf(stmt_buf, "SELECT c.name, c.description, c.location, c.contact, "
278 "c.acl_type, c.acl_id, c.cnt_id FROM containers c WHERE %s",
279 qual);
280 dosql(after);
281 afterc = 7;
429aec78 282 name = xmalloc(0);
283 id = atoi(after[5]);
284 if (!strncmp(after[4], "USER", 4))
285 {
286 id_to_name(id, USERS_TABLE, &name);
287 strcpy(after[5], name);
288 }
289 else if (!strncmp(after[4], "LIST", 4))
290 {
291 id_to_name(id, LIST_TABLE, &name);
292 strcpy(after[5], name);
293 }
294 else if (!strncmp(after[4], "KERBEROS", 8))
295 {
296 id_to_name(id, STRINGS_TABLE, &name);
297 strcpy(after[5], name);
298 }
bb5c9457 299 break;
5eaef520 300 case MCMAP_TABLE:
301 strcpy(after[0], argv[0]);
302 strcpy(after[1], argv[1]);
303 afterc = 2;
304 break;
305 case SVC_TABLE:
306 strcpy(after[0], argv[0]);
307 strcpy(after[1], argv[1]);
308 strcpy(after[2], argv[2]);
309 afterc = 3;
310 break;
311 case FILESYS_TABLE:
312 sprintf(stmt_buf, "SELECT fs.label, fs.type, fs.mach_id, fs.name, "
313 "fs.mount, fs.rwaccess, fs.comments, fs.owner, fs.owners, "
88acd4d3 314 "fs.createflg, fs.lockertype, fs.filsys_id FROM filesys fs "
315 "WHERE %s", qual);
5eaef520 316 dosql(after);
88acd4d3 317 name = xmalloc(0);
5eaef520 318 id = atoi(after[2]);
319 id_to_name(id, MACHINE_TABLE, &name);
320 strcpy(after[2], name);
321 id = atoi(after[7]);
322 id_to_name(id, USERS_TABLE, &name);
323 strcpy(after[7], name);
324 id = atoi(after[8]);
325 id_to_name(id, LIST_TABLE, &name);
326 strcpy(after[8], name);
327 free(name);
88acd4d3 328 afterc = 12;
5eaef520 329 break;
330 case QUOTA_TABLE:
331 strcpy(after[0], "?");
332 strcpy(after[1], argv[1]);
333 strcpy(after[2], "?");
334 sprintf(stmt_buf, "SELECT q.quota, fs.name FROM quota q, filesys fs "
335 "WHERE %s and fs.filsys_id = q.filsys_id and q.type = '%s'",
336 qual, argv[1]);
337 dosql(&(after[3]));
338 afterc = 5;
339 break;
340 case LIST_TABLE:
341 sprintf(stmt_buf, "SELECT l.name, l.active, l.publicflg, l.hidden, "
342 "l.maillist, l.grouplist, l.gid, l.acl_type, l.acl_id, "
88acd4d3 343 "l.description, l.list_id FROM list l WHERE %s", qual);
5eaef520 344 dosql(after);
88acd4d3 345 afterc = 11;
5eaef520 346 break;
347 case IMEMBERS_TABLE:
348 id = (int) argv[0];
349 sprintf(stmt_buf, "SELECT active, publicflg, hidden, maillist, "
350 "grouplist, gid FROM list WHERE list_id = %d", id);
351 dosql(&(after[3]));
88acd4d3 352 name = xmalloc(0);
5eaef520 353 id_to_name(id, LIST_TABLE, &name);
88acd4d3 354 name2 = xmalloc(0);
5eaef520 355 strcpy(after[0], name);
356 strcpy(after[1], argv[1]);
357 id = (int) argv[2];
88acd4d3 358 afterc = 10;
5eaef520 359 if (!strcmp(after[1], "USER"))
360 {
88acd4d3 361 id_to_name(id, USERS_TABLE, &name2);
362 EXEC SQL SELECT status, users_id INTO :after[9], :after[11]
363 FROM users WHERE users_id = :id;
364 EXEC SQL SELECT list_id INTO :after[10] FROM list
365 WHERE name = :name;
366 afterc = 12;
32cfe906 367 }
5eaef520 368 else if (!strcmp(after[1], "LIST"))
88acd4d3 369 {
370 id_to_name(id, LIST_TABLE, &name2);
371 EXEC SQL SELECT list_id INTO :after[9] FROM list
372 WHERE name = :name;
373 sprintf(after[10], "%d", id);
374 afterc = 11;
375 }
5eaef520 376 else if (!strcmp(after[1], "STRING") || !strcmp(after[1], "KERBEROS"))
88acd4d3 377 {
378 id_to_name(id, STRINGS_TABLE, &name2);
379 EXEC SQL SELECT list_id INTO :after[9] FROM list
380 WHERE name = :name;
381 }
5f7b0741 382 else if (!strcmp(after[1], "MACHINE"))
383 {
384 id_to_name(id, MACHINE_TABLE, &name2);
385 EXEC SQL SELECT mach_id INTO :after[9] FROM machine
386 WHERE name = :name;
387 }
88acd4d3 388 strcpy(after[2], name2);
5eaef520 389 free(name);
88acd4d3 390 free(name2);
5eaef520 391 break;
392 case NO_TABLE:
393 afterc = 0;
394 table = beforetable;
395 break;
396 default:
03c05291 397 /*
398 com_err(whoami, 0, "requested incremental on unexpected table `%s'",
399 table_name[table]);
400 */
5eaef520 401 break;
32cfe906 402 }
5eaef520 403
e688520a 404 iu = xmalloc(sizeof(struct iupdate));
5eaef520 405 iu->table = table_name[table];
406 iu->beforec = beforec;
fc053494 407 iu->before = copy_argv(before, beforec);
5eaef520 408 iu->afterc = afterc;
fc053494 409 iu->after = copy_argv(after, afterc);
5eaef520 410 sq_save_data(incremental_sq, iu);
a313cad2 411}
32cfe906 412
03c05291 413void incremental_clear_after(void)
414{
5eaef520 415 incremental_after(NO_TABLE, NULL, NULL);
03c05291 416}
417
32cfe906 418
419/* Called when the current transaction is committed to start any queued
420 * incremental updates. This caches the update table the first time it
421 * is called.
422 */
423
424struct inc_cache {
5eaef520 425 struct inc_cache *next;
426 char *table, *service;
32cfe906 427};
428
429
03c05291 430void incremental_update(void)
32cfe906 431{
5eaef520 432 static int inited = 0;
433 static struct inc_cache *cache;
434 struct inc_cache *c;
435 EXEC SQL BEGIN DECLARE SECTION;
e688520a 436 char tab[INCREMENTAL_TABLE_NAME_SIZE], serv[INCREMENTAL_SERVICE_SIZE];
5eaef520 437 EXEC SQL END DECLARE SECTION;
772c26b3 438 struct iupdate *iu, *iu_save;
5eaef520 439
440 if (!inited)
441 {
442 inited++;
443
444 EXEC SQL DECLARE inc CURSOR FOR SELECT table_name, service
445 FROM incremental;
446 EXEC SQL OPEN inc;
447 while (1)
448 {
449 EXEC SQL FETCH inc INTO :tab, :serv;
450 if (sqlca.sqlcode)
451 break;
e688520a 452 c = xmalloc(sizeof(struct inc_cache));
5eaef520 453 c->next = cache;
e688520a 454 c->table = xstrdup(strtrim(tab));
455 c->service = xstrdup(strtrim(serv));
5eaef520 456 cache = c;
32cfe906 457 }
5eaef520 458 EXEC SQL CLOSE inc;
459 EXEC SQL COMMIT WORK;
32cfe906 460 }
461
5eaef520 462 while (sq_remove_data(incremental_sq, &iu))
463 {
464 for (c = cache; c; c = c->next)
465 {
466 if (!strcmp(c->table, iu->table))
467 {
468 iu->service = c->service;
772c26b3 469 iu_save = xmalloc(sizeof(struct iupdate));
470 iu_save->service = iu->service;
471 iu_save->table = iu->table;
472 iu_save->beforec = iu->beforec;
473 iu_save->afterc = iu->afterc;
474 iu_save->before = copy_argv(iu->before, iu->beforec);
475 iu_save->after = copy_argv(iu->after, iu->afterc);
476 sq_save_data(incremental_exec, iu_save);
32cfe906 477 }
478 }
e688520a 479 if (!c)
480 {
481 free_argv(iu->before, iu->beforec);
482 free_argv(iu->after, iu->afterc);
483 free(iu);
484 }
32cfe906 485 }
5eaef520 486 if (inc_running == 0)
487 next_incremental();
32cfe906 488}
489
f18a32ee 490/* Pro*C 2.2.4 can't cope with the sigset_t below, at least in Solaris 2.6.
491 We add DEFINE=_PROC_ to the proc invocation and then #ifndef that around
492 this function so proc will pass it through without reading it. */
32cfe906 493
f18a32ee 494#ifndef _PROC_
03c05291 495void next_incremental(void)
32cfe906 496{
5eaef520 497 struct iupdate *iu;
e688520a 498 char *argv[MAXARGC * 2 + 4], cafter[3], cbefore[3], prog[MAXPATHLEN];
5eaef520 499 int i;
500 sigset_t sigs;
501
502 if (!incremental_exec)
503 incremental_init();
504
505 if (sq_empty(incremental_exec) ||
506 (inc_running && now - inc_started < INC_TIMEOUT))
507 return;
508
509 if (inc_running)
510 com_err(whoami, 0, "incremental timeout on pid %d", inc_pid);
511
512 sq_remove_data(incremental_exec, &iu);
513 argv[1] = iu->table;
514 sprintf(cbefore, "%d", iu->beforec);
515 argv[2] = cbefore;
516 sprintf(cafter, "%d", iu->afterc);
517 argv[3] = cafter;
518 for (i = 0; i < iu->beforec; i++)
519 argv[4 + i] = iu->before[i];
520 for (i = 0; i < iu->afterc; i++)
521 argv[4 + iu->beforec + i] = iu->after[i];
522
523 sprintf(prog, "%s/%s.incr", BIN_DIR, iu->service);
524 argv[0] = prog;
525 argv[4 + iu->beforec + iu->afterc] = 0;
526
527 sigemptyset(&sigs);
528 sigaddset(&sigs, SIGCHLD);
529 sigprocmask(SIG_BLOCK, &sigs, NULL);
530 inc_pid = vfork();
531 switch (inc_pid)
532 {
32cfe906 533 case 0:
5eaef520 534 execv(prog, argv);
535 _exit(1);
32cfe906 536 case -1:
5eaef520 537 com_err(whoami, 0, "Failed to start incremental update");
538 break;
32cfe906 539 default:
5eaef520 540 inc_running = 1;
541 inc_started = now;
32cfe906 542 }
5eaef520 543 sigprocmask(SIG_UNBLOCK, &sigs, NULL);
32cfe906 544
5eaef520 545 free_argv(iu->before, iu->beforec);
546 free_argv(iu->after, iu->afterc);
547 free(iu);
32cfe906 548}
f18a32ee 549#endif
32cfe906 550
551/* Called when the current transaction is aborted to throw away any queued
552 * incremental updates
553 */
554
03c05291 555void incremental_flush(void)
32cfe906 556{
5eaef520 557 struct iupdate *iu;
32cfe906 558
5eaef520 559 while (sq_get_data(incremental_sq, &iu))
560 {
561 free_argv(iu->before, iu->beforec);
562 free_argv(iu->after, iu->afterc);
563 free(iu);
32cfe906 564 }
5eaef520 565 sq_destroy(incremental_sq);
566 incremental_sq = sq_create();
32cfe906 567}
568
569
fc053494 570char **copy_argv(char **argv, int argc)
571{
572 char **ret = xmalloc(sizeof(char *) * argc);
573 while (--argc >= 0)
574 ret[argc] = xstrdup(strtrim(argv[argc]));
575 return ret;
576}
577
5eaef520 578void free_argv(char **argv, int argc)
32cfe906 579{
5eaef520 580 while (--argc >= 0)
581 free(argv[argc]);
582 free(argv);
32cfe906 583}
03c05291 584
585int table_num(char *name)
586{
587 int i;
588
5eaef520 589 for (i = num_tables - 1; i; i--)
590 {
591 if (!strcmp(table_name[i], name))
592 break;
593 }
03c05291 594
595 return i; /* 0 = "none" if no match */
596}
This page took 0.194536 seconds and 5 git commands to generate.