3 * Query followup routines
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
11 #include <mit-copyright.h>
12 #include "mr_server.h"
21 EXEC SQL INCLUDE sqlca;
25 extern char *whoami, *table_name[];
26 extern int dbms_errno, mr_errcode;
28 EXEC SQL BEGIN DECLARE SECTION;
29 extern char stmt_buf[];
30 EXEC SQL END DECLARE SECTION;
32 EXEC SQL WHENEVER SQLERROR DO dbmserr();
35 /* FOLLOWUP ROUTINES */
37 /* generic set_modtime routine. This takes the table id from the query,
38 * and will update the modtime, modby, and modwho fields in the entry in
39 * the table whose name field matches argv[0].
42 int set_modtime(struct query *q, char *argv[], client *cl)
44 char *name, *entity, *table;
49 table = table_name[q->rtable];
52 sprintf(stmt_buf, "UPDATE %s SET modtime = SYSDATE, modby = %d, "
53 "modwith = '%s' WHERE name = '%s'", table, who, entity, name);
54 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
59 /* generic set_modtime_by_id routine. This takes the table id from
60 * the query, and the id name from the validate record,
61 * and will update the modtime, modby, and modwho fields in the entry in
62 * the table whose id matches argv[0].
65 int set_modtime_by_id(struct query *q, char *argv[], client *cl)
67 char *entity, *table, *id_name;
72 table = table_name[q->rtable];
73 id_name = q->validate->object_id;
76 sprintf(stmt_buf, "UPDATE %s SET modtime = SYSDATE, modby = %d, "
77 "modwith = '%s' WHERE %s = %d", table, who, entity, id_name, id);
78 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
83 /* Sets the finger modtime on a user record. The users_id will be in argv[0].
86 int set_finger_modtime(struct query *q, char *argv[], client *cl)
88 EXEC SQL BEGIN DECLARE SECTION;
91 EXEC SQL END DECLARE SECTION;
95 users_id = *(int *)argv[0];
97 EXEC SQL UPDATE users SET fmodtime = SYSDATE, fmodby = :who,
98 fmodwith = :entity WHERE users_id = :users_id;
104 /* Sets the pobox modtime on a user record. The users_id will be in argv[0].
107 int set_pobox_modtime(struct query *q, char *argv[], client *cl)
109 EXEC SQL BEGIN DECLARE SECTION;
112 EXEC SQL END DECLARE SECTION;
116 users_id = *(int *)argv[0];
118 EXEC SQL UPDATE users SET pmodtime = SYSDATE, pmodby = :who,
119 pmodwith = :entity WHERE users_id = :users_id;
125 /* Like set_modtime, but uppercases the name first.
128 int set_uppercase_modtime(struct query *q, char *argv[], client *cl)
130 char *name, *entity, *table;
135 table = table_name[q->rtable];
138 sprintf(stmt_buf, "UPDATE %s SET modtime = SYSDATE, modby = %d, "
139 "modwith = '%s' WHERE name = UPPER('%s')", table, who, entity, name);
140 EXEC SQL EXECUTE IMMEDIATE :stmt_buf;
146 /* Sets the modtime on the machine whose mach_id is in argv[0]. This routine
147 * is necessary for add_machine_to_cluster becuase the table that query
148 * operates on is "mcm", not "machine".
151 int set_mach_modtime_by_id(struct query *q, char *argv[], client *cl)
153 EXEC SQL BEGIN DECLARE SECTION;
156 EXEC SQL END DECLARE SECTION;
160 id = *(int *)argv[0];
161 EXEC SQL UPDATE machine SET modtime = SYSDATE, modby = :who,
162 modwith = :entity WHERE mach_id = :id;
168 /* Sets the modtime on the cluster whose mach_id is in argv[0]. This routine
169 * is necessary for add_cluster_data and delete_cluster_data becuase the
170 * table that query operates on is "svc", not "cluster".
173 int set_cluster_modtime_by_id(struct query *q, char *argv[], client *cl)
175 EXEC SQL BEGIN DECLARE SECTION;
178 EXEC SQL END DECLARE SECTION;
183 id = *(int *)argv[0];
184 EXEC SQL UPDATE clusters SET modtime = SYSDATE, modby = :who,
185 modwith = :entity WHERE clu_id = :id;
190 /* sets the modtime on the serverhost where the service name is in argv[0]
191 * and the mach_id is in argv[1].
194 int set_serverhost_modtime(struct query *q, char *argv[], client *cl)
196 EXEC SQL BEGIN DECLARE SECTION;
199 EXEC SQL END DECLARE SECTION;
205 id = *(int *)argv[1];
206 EXEC SQL UPDATE serverhosts
207 SET modtime = SYSDATE, modby = :who, modwith = :entity
208 WHERE service = :serv AND mach_id = :id;
213 /* sets the modtime on the nfsphys where the mach_id is in argv[0] and the
214 * directory name is in argv[1].
217 int set_nfsphys_modtime(struct query *q, char *argv[], client *cl)
219 EXEC SQL BEGIN DECLARE SECTION;
222 EXEC SQL END DECLARE SECTION;
227 id = *(int *)argv[0];
229 EXEC SQL UPDATE nfsphys SET modtime = SYSDATE, modby = :who,
230 modwith = :entity WHERE dir = :dir AND mach_id = :id;
235 /* sets the modtime on a filesystem, where argv[0] contains the filesys
239 int set_filesys_modtime(struct query *q, char *argv[], client *cl)
241 EXEC SQL BEGIN DECLARE SECTION;
242 char *label, *entity;
244 extern int _var_phys_id;
245 EXEC SQL END DECLARE SECTION;
251 if (!strcmp(q->shortname, "ufil"))
254 EXEC SQL UPDATE filesys SET modtime = SYSDATE, modby = :who,
255 modwith = :entity, phys_id = :_var_phys_id
256 WHERE label = :label;
261 /* sets the modtime on a zephyr class, where argv[0] contains the class
265 int set_zephyr_modtime(struct query *q, char *argv[], client *cl)
267 EXEC SQL BEGIN DECLARE SECTION;
268 char *class, *entity;
270 EXEC SQL END DECLARE SECTION;
277 EXEC SQL UPDATE zephyr SET modtime = SYSDATE, modby = :who,
278 modwith = :entity WHERE class = :class;
284 /* fixes the modby field. This will be the second to last thing in the
285 * argv, the argv length is determined from the query structure. It is
286 * passed as a pointer to an integer. This will either turn it into a
287 * username, or # + the users_id.
289 int followup_fix_modby(struct query *q, struct save_queue *sq,
290 struct validate *v, int (*action)(int, char *[], void *),
291 void *actarg, client *cl)
298 while (sq_get_data(sq, &argv))
302 status = id_to_name(id, USERS_TABLE, &argv[i]);
304 status = id_to_name(-id, STRINGS_TABLE, &argv[i]);
305 if (status && status != MR_NO_MATCH)
307 (*action)(q->vcnt, argv, actarg);
308 for (j = 0; j < q->vcnt; j++)
317 ** followup_ausr - add finger and pobox entries, set_user_modtime
320 ** argv[0] - login (add_user)
321 ** argv[3] - last name
322 ** argv[4] - first name
323 ** argv[5] - middle name
327 int followup_ausr(struct query *q, char *argv[], client *cl)
329 EXEC SQL BEGIN DECLARE SECTION;
331 char *login, *entity, *name;
332 char fullname[USERS_FIRST_SIZE + USERS_MIDDLE_SIZE + USERS_LAST_SIZE];
333 EXEC SQL END DECLARE SECTION;
336 if (strlen(argv[4]) && strlen(argv[5]))
337 sprintf(fullname, "%s %s %s", argv[4], argv[5], argv[3]);
338 else if (strlen(argv[4]))
339 sprintf(fullname, "%s %s", argv[4], argv[3]);
341 sprintf(fullname, "%s", argv[3]);
347 /* create finger entry, pobox & set modtime on user */
348 EXEC SQL UPDATE users
349 SET modtime = SYSDATE, modby = :who, modwith = :entity,
350 fullname = NVL(:fullname, CHR(0)), affiliation = type,
351 fmodtime = SYSDATE, fmodby = :who, fmodwith = :entity,
352 potype = 'NONE', pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity
353 WHERE login = :login;
358 /* followup_gpob: fixes argv[2] and argv[3] based on the pobox type.
359 * Then completes the upcall to the user.
361 * argv[2] is the users_id on input and should be converted to the
362 * pobox name on output. argv[3] is empty on input and should be
363 * converted to an email address on output.
366 int followup_gpob(struct query *q, struct save_queue *sq, struct validate *v,
367 int (*action)(int, char *[], void *), void *actarg,
372 int mid, sid, status, i;
373 EXEC SQL BEGIN DECLARE SECTION;
374 int users_id, pid, iid, bid;
375 char mach[MACHINE_NAME_SIZE], fs[FILESYS_LABEL_SIZE];
376 char str[STRINGS_STRING_SIZE];
377 EXEC SQL END DECLARE SECTION;
380 while (sq_get_data(sq, &argv))
382 mr_trim_args(4, argv);
384 users_id = atoi(argv[2]);
386 EXEC SQL SELECT pop_id, imap_id, box_id INTO :pid, :iid, :bid
387 FROM users WHERE users_id = :users_id;
394 EXEC SQL SELECT string INTO :str FROM strings
395 WHERE string_id = :bid;
399 /* If SMTP, don't bother fetching IMAP and POP boxes. */
405 /* IMAP, or SPLIT with IMAP */
406 EXEC SQL SELECT f.label, m.name INTO :fs, :mach
407 FROM filesys f, machine m
408 WHERE f.filsys_id = :iid AND f.mach_id = m.mach_id;
414 /* POP, or SPLIT with POP */
415 EXEC SQL SELECT m.name INTO :mach FROM machine m, users u
416 WHERE u.users_id = :users_id AND u.pop_id = m.mach_id;
424 /* Now assemble the right answer. */
425 if (!strcmp(ptype, "POP"))
427 argv[2] = xstrdup(strtrim(mach));
428 argv[3] = xmalloc(strlen(argv[0]) + strlen(argv[2]) + 2);
429 sprintf(argv[3], "%s@%s", argv[0], argv[2]);
431 else if (!strcmp(ptype, "SMTP"))
433 argv[2] = xstrdup(strtrim(str));
434 argv[3] = xstrdup(str);
436 else if (!strcmp(ptype, "IMAP"))
438 argv[2] = xstrdup(strtrim(fs));
439 argv[3] = xmalloc(strlen(argv[0]) + strlen(strtrim(mach)) + 2);
440 sprintf(argv[3], "%s@%s", argv[0], mach);
442 else if (!strcmp(ptype, "SPLIT"))
444 argv[2] = xstrdup(strtrim(str));
445 argv[3] = xmalloc(strlen(argv[0]) + strlen(strtrim(mach)) +
447 sprintf(argv[3], "%s@%s, %s", argv[0], mach, str);
449 else /* ptype == "NONE" */
452 if (!strcmp(q->shortname, "gpob"))
456 status = id_to_name(sid, USERS_TABLE, &argv[5]);
458 status = id_to_name(-sid, STRINGS_TABLE, &argv[5]);
459 if (status && status != MR_NO_MATCH)
463 (*action)(q->vcnt, argv, actarg);
465 /* free saved data */
466 for (i = 0; i < q->vcnt; i++)
475 /* Fix an ace_name, based on its type. */
477 static int fix_ace(char *type, char **name)
479 int id = atoi(*name);
481 if (!strcmp(type, "LIST"))
482 return id_to_name(id, LIST_TABLE, name);
483 else if (!strcmp(type, "USER"))
484 return id_to_name(id, USERS_TABLE, name);
485 else if (!strcmp(type, "KERBEROS"))
486 return id_to_name(id, STRINGS_TABLE, name);
490 if (!strcmp(type, "NONE"))
491 *name = xstrdup("NONE");
493 *name = xstrdup("???");
499 /* followup_gsnt: fix the ace_name and modby */
501 int followup_gsnt(struct query *q, struct save_queue *sq, struct validate *v,
502 int (*action)(int, char *[], void *), void *actarg,
508 while (sq_get_data(sq, &argv))
510 mr_trim_args(q->vcnt, argv);
512 status = fix_ace(argv[7], &argv[8]);
513 if (status && status != MR_NO_MATCH)
517 return followup_fix_modby(q, sq, v, action, actarg, cl);
521 /* followup_ghst: fix the ace_name, strings and modby */
523 int followup_ghst(struct query *q, struct save_queue *sq, struct validate *v,
524 int (*action)(int, char *[], void *), void *actarg,
530 while (sq_get_data(sq, &argv))
532 mr_trim_args(q->vcnt, argv);
535 status = id_to_name(id, STRINGS_TABLE, &argv[13]);
539 status = id_to_name(id, STRINGS_TABLE, &argv[14]);
544 status = id_to_name(-id, STRINGS_TABLE, &argv[16]);
546 status = id_to_name(id, USERS_TABLE, &argv[16]);
547 if (status && status != MR_NO_MATCH)
550 status = fix_ace(argv[11], &argv[12]);
551 if (status && status != MR_NO_MATCH)
555 return followup_fix_modby(q, sq, v, action, actarg, cl);
559 /* followup_glin: fix the ace_name, modace_name, expiration, and modby */
561 int followup_glin(struct query *q, struct save_queue *sq, struct validate *v,
562 int (*action)(int, char *[], void *), void *actarg,
566 int id, i, idx, status;
569 if (!strcmp(q->shortname, "glin") && q->version > 2)
571 if (!strcmp(q->shortname, "gsin"))
574 while (sq_get_data(sq, &argv))
576 mr_trim_args(q->vcnt, argv);
578 status = fix_ace(argv[idx], &argv[idx + 1]);
579 if (status && status != MR_NO_MATCH)
582 if (!strcmp(q->shortname, "glin") && atoi(argv[6]) == -1)
584 argv[6] = xrealloc(argv[6], strlen(UNIQUE_GID) + 1);
585 strcpy(argv[6], UNIQUE_GID);
589 return followup_fix_modby(q, sq, v, action, actarg, cl);
592 int followup_gpsv(struct query *q, struct save_queue *sq, struct validate *v,
593 int (*action)(int, char *[], void *), void *actarg,
599 while (sq_get_data(sq, &argv))
601 mr_trim_args(q->vcnt, argv);
603 status = fix_ace(argv[PRINTSERVER_OWNER_TYPE],
604 &argv[PRINTSERVER_OWNER_NAME]);
605 if (status && status != MR_NO_MATCH)
609 return followup_fix_modby(q, sq, v, action, actarg, cl);
613 /* followup_gqot: Fix the entity name, directory name & modby fields
614 * argv[0] = filsys_id
616 * argv[2] = entity_id
617 * argv[3] = ascii(quota)
620 int followup_gqot(struct query *q, struct save_queue *sq, struct validate *v,
621 int (*action)(int, char *[], void *), void *actarg,
626 EXEC SQL BEGIN DECLARE SECTION;
629 EXEC SQL END DECLARE SECTION;
632 if (!strcmp(q->name, "get_quota") ||
633 !strcmp(q->name, "get_quota_by_filesys"))
638 while (sq_get_data(sq, &argv))
645 status = id_to_name(atoi(argv[2]), USERS_TABLE, &argv[2]);
649 status = id_to_name(atoi(argv[2]), LIST_TABLE, &argv[2]);
653 argv[2] = xstrdup("system:anyuser");
657 argv[2] = xmalloc(8);
658 sprintf(argv[2], "%d", id);
661 id = atoi(argv[idx]);
663 argv[idx] = xmalloc(id ? NFSPHYS_DIR_SIZE : FILESYS_NAME_SIZE);
669 EXEC SQL SELECT name INTO :name FROM filesys
670 WHERE label = :label;
674 EXEC SQL SELECT dir INTO :name FROM nfsphys
675 WHERE nfsphys_id = :id;
677 if (sqlca.sqlerrd[2] != 1)
678 sprintf(argv[idx], "#%d", id);
680 id = atoi(argv[idx + 3]);
682 status = id_to_name(id, USERS_TABLE, &argv[idx + 3]);
684 status = id_to_name(-id, STRINGS_TABLE, &argv[idx + 3]);
685 if (status && status != MR_NO_MATCH)
687 (*action)(q->vcnt, argv, actarg);
688 for (j = 0; j < q->vcnt; j++)
697 /* followup_aqot: Add allocation to nfsphys after creating quota.
698 * argv[0] = filsys_id
699 * argv[1] = type if "add_quota" or "update_quota"
701 * argv[3 or 2] = ascii(quota)
704 int followup_aqot(struct query *q, char *argv[], client *cl)
706 EXEC SQL BEGIN DECLARE SECTION;
707 int quota, id, fs, who, physid, table;
708 char *entity, *qtype, *tname;
709 EXEC SQL END DECLARE SECTION;
715 tname = table_name[table];
716 fs = *(int *)argv[0];
717 EXEC SQL SELECT phys_id INTO :physid FROM filesys
718 WHERE filsys_id = :fs;
722 if (!strcmp(q->shortname, "aqot") || !strcmp(q->shortname, "uqot"))
725 id = *(int *)argv[2];
726 quota = atoi(argv[3]);
727 sprintf(incr_qual, "q.filsys_id = %d", fs);
732 id = *(int *)argv[1];
733 quota = atoi(argv[2]);
734 sprintf(incr_qual, "q.filsys_id = %d AND q.type = '%s' AND "
735 "q.entity_id = %d", fs, qtype, id);
738 /* quota case of incremental_{before|after} only looks at slot 1 */
739 incr_argv[1] = qtype;
741 /* Follows one of many possible gross hacks to fix these particular
742 * conflicts between what is possible in the query table and what
743 * is possible in SQL.
745 if (q->type == APPEND)
747 incremental_clear_before();
748 EXEC SQL INSERT INTO quota
749 (filsys_id, type, entity_id, quota, phys_id)
750 VALUES (:fs, NVL(:qtype, CHR(0)), :id, :quota, :physid);
751 incremental_after(table, incr_qual, incr_argv);
755 incremental_before(table, incr_qual, incr_argv);
756 EXEC SQL UPDATE quota SET quota = :quota
757 WHERE filsys_id = :fs AND type = :qtype AND entity_id = :id;
759 incremental_after(table, incr_qual, incr_argv);
764 flush_name(argv[0], table);
765 if (q->type == APPEND)
767 EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = SYSDATE
768 WHERE table_name = :tname;
772 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
773 WHERE table_name = :tname;
776 /* Proceed with original followup */
780 EXEC SQL UPDATE quota
781 SET modtime = SYSDATE, modby = :who, modwith = :entity
782 WHERE filsys_id = :fs and type = :qtype and entity_id = :id;
783 EXEC SQL UPDATE nfsphys SET allocated = allocated + :quota
784 WHERE nfsphys_id = :physid;
791 /* Necessitated by the requirement of a correlation name by the incremental
792 * routines, since query table deletes don't provide one.
794 int followup_dqot(struct query *q, char **argv, client *cl)
799 EXEC SQL BEGIN DECLARE SECTION;
800 char incr_qual[80], *tname;
801 EXEC SQL END DECLARE SECTION;
804 tname = table_name[table];
805 fs = *(int *)argv[0];
806 if (!strcmp(q->shortname, "dqot"))
809 id = *(int *)argv[2];
814 id = *(int *)argv[1];
816 sprintf(incr_qual, "q.filsys_id = %d AND q.type = '%s' AND q.entity_id = %d",
819 /* quota case of incremental_{before|after} only looks at slot 1 */
820 incr_argv[1] = qtype;
822 incremental_before(table, incr_qual, incr_argv);
823 EXEC SQL DELETE FROM quota q WHERE q.filsys_id = :fs AND q.type = :qtype
824 AND q.entity_id = :id;
825 incremental_clear_after();
829 flush_name(argv[0], table);
831 EXEC SQL UPDATE tblstats SET deletes = deletes + 1, modtime = SYSDATE
832 WHERE table_name = :tname;
839 int followup_gzcl(struct query *q, struct save_queue *sq, struct validate *v,
840 int (*action)(int, char *[], void *), void *actarg,
846 while (sq_get_data(sq, &argv))
848 mr_trim_args(q->vcnt, argv);
850 for (i = 1; i < 8; i += 2)
852 status = fix_ace(argv[i], &argv[i + 1]);
853 if (status && status != MR_NO_MATCH)
858 return followup_fix_modby(q, sq, v, action, actarg, cl);
865 int followup_gsha(struct query *q, struct save_queue *sq, struct validate *v,
866 int (*action)(int, char *[], void *), void *actarg,
872 while (sq_get_data(sq, &argv))
874 mr_trim_args(q->vcnt, argv);
876 status = fix_ace(argv[1], &argv[2]);
877 if (status && status != MR_NO_MATCH)
881 return followup_fix_modby(q, sq, v, action, actarg, cl);
885 int _sdl_followup(struct query *q, char *argv[], client *cl)
888 EXEC SQL ALTER SESSION SET SQL_TRACE TRUE;
890 EXEC SQL ALTER SESSION SET SQL_TRACE FALSE;
896 int trigger_dcm(struct query *q, char *argv[], client *cl)
899 char prog[MAXPATHLEN];
901 sprintf(prog, "%s/startdcm", BIN_DIR);
906 execl(prog, "startdcm", 0);