]> andersk Git - moira.git/blame - server/qsupport.pc
Support MACHINE members of lists.
[moira.git] / server / qsupport.pc
CommitLineData
7ac48069 1/* $Id$
b070f8a1 2 *
7ac48069 3 * Special query routines
b070f8a1 4 *
7ac48069 5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
7 * <mit-copyright.h>.
b070f8a1 8 */
9
b070f8a1 10#include <mit-copyright.h>
b070f8a1 11#include "mr_server.h"
03c05291 12#include "query.h"
7ac48069 13#include "qrtn.h"
14
b070f8a1 15#include <ctype.h>
7ac48069 16#include <stdlib.h>
03c05291 17#include <string.h>
7ac48069 18
5d677c81 19EXEC SQL INCLUDE sqlca;
7ac48069 20
21RCSID("$Header$");
b070f8a1 22
03c05291 23extern char *whoami, *table_name[];
24extern int dbms_errno, mr_errcode;
b070f8a1 25
0fc7ab46 26EXEC SQL BEGIN DECLARE SECTION;
45bf7573 27extern char stmt_buf[];
0fc7ab46 28EXEC SQL END DECLARE SECTION;
5d677c81 29
03c05291 30EXEC SQL WHENEVER SQLERROR DO dbmserr();
31
7ac48069 32int get_ace_internal(char *atypex, int aid,
33 int (*action)(int, char *[], void *), void *actarg);
7ac48069 34int qualified_get(struct query *q, char *argv[],
35 int (*action)(int, char *[], void *), void *actarg,
03c05291 36 char *start, char *range, char *field, char *flags[]);
453b99fe 37
0fc7ab46 38
0659551f 39/* set_pobox - this does all of the real work.
40 * argv = user_id, type, box
41 * if type is POP, then box should be a machine, and its ID should be put in
17cb3de8 42 * pop_id. If type is IMAP, then box should be a filesys, and its ID should
43 * be put in pop_id. If type is SMTP, then box should be a string and its
44 * ID should be put in box_id. If type is NONE, then box doesn't matter.
0fc7ab46 45 */
b070f8a1 46
5eaef520 47int set_pobox(struct query *q, char **argv, client *cl)
5d677c81 48{
5eaef520 49 EXEC SQL BEGIN DECLARE SECTION;
50 int user, id;
e688520a 51 char *box, potype[USERS_POTYPE_SIZE];
5eaef520 52 EXEC SQL END DECLARE SECTION;
53 int status;
54
55 box = argv[2];
56 user = *(int *)argv[0];
57
58 EXEC SQL SELECT pop_id, potype INTO :id, :potype FROM users
59 WHERE users_id = :user;
60 if (dbms_errno)
61 return mr_errcode;
450849e9 62 if (!strcmp(strtrim(potype), "POP") ||
63 (!strcmp(strtrim(potype), "SPLIT") && id))
5eaef520 64 set_pop_usage(id, -1);
65
66 if (!strcmp(argv[1], "POP"))
67 {
68 status = name_to_id(box, MACHINE_TABLE, &id);
69 if (status == MR_NO_MATCH)
70 return MR_MACHINE;
71 else if (status)
72 return status;
17cb3de8 73 EXEC SQL UPDATE users SET potype = 'POP', pop_id = :id, imap_id = 0
5eaef520 74 WHERE users_id = :user;
75 set_pop_usage(id, 1);
76 }
450849e9 77 else if (!strcmp(argv[1], "SMTP") || !strcmp(argv[1], "SPLIT"))
5eaef520 78 {
79 if (strchr(box, '/') || strchr(box, '|'))
80 return MR_BAD_CHAR;
81 status = name_to_id(box, STRINGS_TABLE, &id);
82 if (status == MR_NO_MATCH)
83 id = add_string(box);
84 else if (status)
85 return status;
450849e9 86
87 /* If going from SMTP or NONE to SPLIT, make sure we have a valid
88 * POP or IMAP box.
89 */
730a5904 90 if ((!strcmp(potype, "SMTP") || !strcmp(potype, "NONE")) &&
91 !strcmp(argv[1], "SPLIT"))
450849e9 92 {
93 status = set_pobox_pop(q, argv, cl);
94 if (status)
95 return status;
96 }
97 strlcpy(potype, argv[1], sizeof(potype));
98 EXEC SQL UPDATE users SET potype = :potype, box_id = :id
5eaef520 99 WHERE users_id = :user;
100 }
7b3eb008 101 else if (!strcmp(argv[1], "IMAP"))
102 {
17cb3de8 103 EXEC SQL SELECT filsys_id INTO :id FROM filesys
104 WHERE label = :box AND type = 'IMAP';
105 if (sqlca.sqlcode)
7b3eb008 106 return MR_FILESYS;
17cb3de8 107 EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :id, pop_id = 0
7b3eb008 108 WHERE users_id = :user;
109 }
5eaef520 110 else /* argv[1] == "NONE" */
111 {
112 EXEC SQL UPDATE users SET potype = 'NONE'
113 WHERE users_id = :user;
b070f8a1 114 }
115
5eaef520 116 set_pobox_modtime(q, argv, cl);
117 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
118 WHERE table_name = 'users';
119 if (dbms_errno)
120 return mr_errcode;
121 return MR_SUCCESS;
5d677c81 122}
b070f8a1 123
17cb3de8 124/* set_pobox_pop: Revert to existing POP or IMAP pobox.
125 * Also take care of keeping track of the post office usage.
126 */
127int set_pobox_pop(struct query *q, char **argv, client *cl)
128{
129 EXEC SQL BEGIN DECLARE SECTION;
130 int id, pid, iid, mid;
131 char type[USERS_POTYPE_SIZE];
132 EXEC SQL END DECLARE SECTION;
133
134 id = *(int *)argv[0];
135 EXEC SQL SELECT potype, pop_id, imap_id INTO :type, :pid, :iid
136 FROM users WHERE users_id = :id;
137 if (sqlca.sqlerrd[2] == 0 || (pid == 0 && iid == 0))
138 return MR_MACHINE;
139
140 if (pid)
141 {
142 EXEC SQL SELECT mach_id INTO :mid FROM machine
143 WHERE mach_id = :pid;
144 if (sqlca.sqlerrd[2] == 0)
145 return MR_MACHINE;
146 EXEC SQL UPDATE users SET potype = 'POP' WHERE users_id = :id;
450849e9 147 if (!strcmp(strtrim(type), "POP"))
17cb3de8 148 set_pop_usage(mid, 1);
149 }
150 else
151 {
152 EXEC SQL SELECT filsys_id INTO :mid FROM filesys
153 WHERE filsys_id = :iid;
154 if (sqlca.sqlerrd[2] == 0)
155 return MR_MACHINE;
156 EXEC SQL UPDATE users SET potype = 'IMAP' WHERE users_id = :id;
157 }
158
159 set_pobox_modtime(q, argv, cl);
160 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
161 WHERE table_name = 'users';
162 if (dbms_errno)
163 return mr_errcode;
164 return MR_SUCCESS;
165}
166
167
0659551f 168/* Add_member_to_list: do list flattening as we go! MAXLISTDEPTH is
169 * how many different ancestors a member is allowed to have.
b070f8a1 170 */
171
e20ac4f2 172#define MAXLISTDEPTH 2048
b070f8a1 173
5eaef520 174int add_member_to_list(struct query *q, char **argv, client *cl)
5d677c81 175{
5eaef520 176 EXEC SQL BEGIN DECLARE SECTION;
46b6f1f6 177 int id, lid, mid, tag, error, who, ref, rowcnt;
e688520a 178 char *mtype, dtype[IMEMBERS_MEMBER_TYPE_SIZE], *entity;
5eaef520 179 EXEC SQL END DECLARE SECTION;
180 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
181 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
182 int status;
183 char *dtypes[MAXLISTDEPTH];
184 char *iargv[3], *buf;
185
186 lid = *(int *)argv[0];
187 mtype = argv[1];
188 mid = *(int *)argv[2];
46b6f1f6 189 tag = !strcmp(q->shortname, "atml") ? *(int *)argv[3] : 0;
a37b0b99 190
191 if (acl_access_check(lid, cl))
192 return MR_PERM;
193
5eaef520 194 /* if the member is already a direct member of the list, punt */
195 EXEC SQL SELECT COUNT(list_id) INTO :rowcnt FROM imembers
196 WHERE list_id = :lid AND member_id = :mid
197 AND member_type = :mtype AND direct = 1;
198 if (rowcnt > 0)
199 return MR_EXISTS;
200 if (!strcasecmp(mtype, "STRING"))
201 {
202 buf = malloc(0);
203 status = id_to_name(mid, STRINGS_TABLE, &buf);
204 if (status)
205 return status;
206 if (strchr(buf, '/') || strchr(buf, '|'))
207 {
208 free(buf);
209 return MR_BAD_CHAR;
0659551f 210 }
5eaef520 211 free(buf);
0659551f 212 }
b070f8a1 213
5eaef520 214 ancestors[0] = lid;
215 aref[0] = 1;
216 acount = 1;
217 EXEC SQL DECLARE csr103 CURSOR FOR
218 SELECT list_id, ref_count FROM imembers
219 WHERE member_id = :lid AND member_type = 'LIST';
220 if (dbms_errno)
221 return mr_errcode;
222 EXEC SQL OPEN csr103;
223 if (dbms_errno)
224 return mr_errcode;
225 while (1)
226 {
227 EXEC SQL FETCH csr103 INTO :id, :ref;
228 if (sqlca.sqlcode)
229 break;
230 aref[acount] = ref;
231 ancestors[acount++] = id;
232 if (acount >= MAXLISTDEPTH)
233 break;
0659551f 234 }
5eaef520 235 EXEC SQL CLOSE csr103;
236 if (dbms_errno)
237 return mr_errcode;
238 if (acount >= MAXLISTDEPTH)
239 return MR_INTERNAL;
240 descendants[0] = mid;
241 dtypes[0] = mtype;
242 dref[0] = 1;
243 dcount = 1;
244 error = 0;
245 if (!strcmp(mtype, "LIST"))
246 {
247 EXEC SQL DECLARE csr104 CURSOR FOR
248 SELECT member_id, member_type, ref_count
249 FROM imembers
250 WHERE list_id = :mid;
251 if (dbms_errno)
252 return mr_errcode;
253 EXEC SQL OPEN csr104;
254 if (dbms_errno)
255 return mr_errcode;
256 while (1)
257 {
258 EXEC SQL FETCH csr104 INTO :id, :dtype, :ref;
259 if (sqlca.sqlcode)
260 break;
261 switch (dtype[0])
262 {
0659551f 263 case 'L':
5eaef520 264 dtypes[dcount] = "LIST";
265 break;
0659551f 266 case 'U':
5eaef520 267 dtypes[dcount] = "USER";
268 break;
0659551f 269 case 'S':
5eaef520 270 dtypes[dcount] = "STRING";
271 break;
0659551f 272 case 'K':
5eaef520 273 dtypes[dcount] = "KERBEROS";
274 break;
5f7b0741 275 case 'M':
276 dtypes[dcount] = "MACHINE";
277 break;
0659551f 278 default:
5eaef520 279 error++;
280 break;
0659551f 281 }
5eaef520 282 dref[dcount] = ref;
283 descendants[dcount++] = id;
284 if (dcount >= MAXLISTDEPTH)
285 {
286 error++;
287 break;
0659551f 288 }
289 }
5eaef520 290 EXEC SQL CLOSE csr104;
291 if (dbms_errno)
292 return mr_errcode;
293 if (error)
294 return MR_INTERNAL;
b070f8a1 295 }
5eaef520 296 for (a = 0; a < acount; a++)
297 {
298 lid = ancestors[a];
299 for (d = 0; d < dcount; d++)
300 {
301 mid = descendants[d];
302 mtype = dtypes[d];
303 if (mid == lid && !strcmp(mtype, "LIST"))
304 return MR_LISTLOOP;
305 EXEC SQL SELECT COUNT(ref_count) INTO :rowcnt
306 FROM imembers
307 WHERE list_id = :lid AND member_id = :mid
308 AND member_type = :mtype;
309 ref = aref[a] * dref[d];
310 if (rowcnt > 0)
311 {
312 if (a == 0 && d == 0)
313 {
314 EXEC SQL UPDATE imembers
46b6f1f6 315 SET ref_count = ref_count + :ref, direct = 1, tag = :tag
5eaef520 316 WHERE list_id = :lid AND member_id = :mid
317 AND member_type = :mtype;
0659551f 318 }
5eaef520 319 else
320 {
321 EXEC SQL UPDATE imembers
322 SET ref_count = ref_count + :ref
323 WHERE list_id = :lid AND member_id = :mid
324 AND member_type = :mtype;
325 }
326 }
327 else
328 {
329 incremental_clear_before();
330 if (a == 0 && d == 0)
331 {
03c05291 332 EXEC SQL INSERT INTO imembers
46b6f1f6 333 (list_id, member_type, member_id, tag, direct, ref_count)
abb59943 334 VALUES (:lid, :mtype, :mid, :tag, 1, :ref);
5eaef520 335 }
336 else
337 {
03c05291 338 EXEC SQL INSERT INTO imembers
46b6f1f6 339 (list_id, member_type, member_id, tag, direct, ref_count)
abb59943 340 VALUES (:lid, :mtype, :mid, :tag, 0, :ref);
0659551f 341 }
5eaef520 342 iargv[0] = (char *)lid;
343 iargv[1] = mtype;
344 iargv[2] = (char *)mid;
345 incremental_after(IMEMBERS_TABLE, 0, iargv);
0659551f 346 }
347 }
b070f8a1 348 }
5eaef520 349 lid = *(int *)argv[0];
350 entity = cl->entity;
351 who = cl->client_id;
352 EXEC SQL UPDATE list
353 SET modtime = SYSDATE, modby = :who, modwith = :entity
354 WHERE list_id = :lid;
355 if (dbms_errno)
356 return mr_errcode;
357 return MR_SUCCESS;
5d677c81 358}
b070f8a1 359
360
0659551f 361/* Delete_member_from_list: do list flattening as we go!
b070f8a1 362 */
363
5eaef520 364int delete_member_from_list(struct query *q, char **argv, client *cl)
5d677c81 365{
5eaef520 366 EXEC SQL BEGIN DECLARE SECTION;
367 int id, lid, mid, cnt, error, who, ref;
e688520a 368 char *mtype, dtype[IMEMBERS_MEMBER_TYPE_SIZE], *entity;
5eaef520 369 EXEC SQL END DECLARE SECTION;
370 int ancestors[MAXLISTDEPTH], aref[MAXLISTDEPTH], acount, a;
371 int descendants[MAXLISTDEPTH], dref[MAXLISTDEPTH], dcount, d;
372 char *dtypes[MAXLISTDEPTH];
373 char *iargv[3];
374
375 lid = *(int *)argv[0];
376 mtype = argv[1];
377 mid = *(int *)argv[2];
a37b0b99 378
379 if (acl_access_check(lid, cl))
380 return MR_PERM;
381
5eaef520 382 /* if the member is not a direct member of the list, punt */
383 EXEC SQL SELECT COUNT(list_id) INTO :cnt FROM imembers
384 WHERE list_id = :lid AND member_id = :mid
385 AND member_type = :mtype AND direct = 1;
386 if (dbms_errno)
387 return mr_errcode;
388 if (cnt == 0)
389 return MR_NO_MATCH;
390 ancestors[0] = lid;
391 aref[0] = 1;
392 acount = 1;
393 EXEC SQL DECLARE csr105 CURSOR FOR
394 SELECT list_id, ref_count FROM imembers
395 WHERE member_id = :lid AND member_type = 'LIST';
396 if (dbms_errno)
397 return mr_errcode;
398 EXEC SQL OPEN csr105;
399 if (dbms_errno)
400 return mr_errcode;
401 while (1)
402 {
403 EXEC SQL FETCH csr105 INTO :id, :ref;
404 if (sqlca.sqlcode)
405 break;
406 aref[acount] = ref;
407 ancestors[acount++] = id;
408 if (acount >= MAXLISTDEPTH)
409 break;
b070f8a1 410 }
5eaef520 411 EXEC SQL CLOSE csr105;
412 if (dbms_errno)
413 return mr_errcode;
414 if (acount >= MAXLISTDEPTH)
415 return MR_INTERNAL;
416 descendants[0] = mid;
417 dtypes[0] = mtype;
418 dref[0] = 1;
419 dcount = 1;
420 error = 0;
421 if (!strcmp(mtype, "LIST"))
422 {
423 EXEC SQL DECLARE csr106 CURSOR FOR
424 SELECT member_id, member_type, ref_count FROM imembers
425 WHERE list_id = :mid;
426 if (dbms_errno)
427 return mr_errcode;
428 EXEC SQL OPEN csr106;
429 if (dbms_errno)
430 return mr_errcode;
431 while (1)
432 {
433 EXEC SQL FETCH csr106 INTO :id, :dtype, :ref;
434 if (sqlca.sqlcode)
435 break;
436 switch (dtype[0])
437 {
0659551f 438 case 'L':
5eaef520 439 dtypes[dcount] = "LIST";
440 break;
0659551f 441 case 'U':
5eaef520 442 dtypes[dcount] = "USER";
443 break;
0659551f 444 case 'S':
5eaef520 445 dtypes[dcount] = "STRING";
446 break;
0659551f 447 case 'K':
5eaef520 448 dtypes[dcount] = "KERBEROS";
449 break;
5f7b0741 450 case 'M':
451 dtypes[dcount] = "MACHINE";
452 break;
0659551f 453 default:
5eaef520 454 error++;
455 break;
0659551f 456 }
5eaef520 457 dref[dcount] = ref;
458 descendants[dcount++] = id;
459 if (dcount >= MAXLISTDEPTH)
460 break;
0659551f 461 }
5eaef520 462 EXEC SQL CLOSE csr106;
463 if (dbms_errno)
464 return mr_errcode;
465 if (error)
466 return MR_INTERNAL;
3beadd83 467 }
5eaef520 468 for (a = 0; a < acount; a++)
469 {
470 lid = ancestors[a];
471 for (d = 0; d < dcount; d++)
472 {
473 mid = descendants[d];
474 mtype = dtypes[d];
475 if (mid == lid && !strcmp(mtype, "LIST"))
476 return MR_LISTLOOP;
477 EXEC SQL SELECT ref_count INTO :cnt FROM imembers
478 WHERE list_id = :lid AND member_id = :mid AND member_type = :mtype;
479 ref = aref[a] * dref[d];
480 if (cnt <= ref)
481 {
482 iargv[0] = (char *)lid;
483 iargv[1] = mtype;
484 iargv[2] = (char *)mid;
485 incremental_before(IMEMBERS_TABLE, 0, iargv);
486 EXEC SQL DELETE FROM imembers
487 WHERE list_id = :lid AND member_id = :mid
488 AND member_type= :mtype;
489 incremental_clear_after();
0659551f 490 }
5eaef520 491 else if (a == 0 && d == 0)
492 {
493 EXEC SQL UPDATE imembers
494 SET ref_count = ref_count - :ref, direct = 0
495 WHERE list_id = :lid AND member_id = :mid
496 AND member_type = :mtype;
497 }
498 else
499 {
500 EXEC SQL UPDATE imembers
501 SET ref_count = ref_count - :ref
502 WHERE list_id = :lid AND member_id = :mid
503 AND member_type = :mtype;
0659551f 504 }
505 }
3beadd83 506 }
5eaef520 507 lid = *(int *)argv[0];
508 entity = cl->entity;
509 who = cl->client_id;
510 EXEC SQL UPDATE list SET modtime = SYSDATE, modby = :who, modwith = :entity
511 WHERE list_id = :lid;
512 if (dbms_errno)
513 return mr_errcode;
514 return MR_SUCCESS;
5d677c81 515}
b070f8a1 516
20541c25 517int tag_member_of_list(struct query *q, char **argv, client *cl)
518{
519 EXEC SQL BEGIN DECLARE SECTION;
520 int lid, mid, cnt, tag;
521 char *mtype;
522 EXEC SQL END DECLARE SECTION;
523 char *iargv[3];
524
525 lid = *(int *)argv[0];
526 mtype = argv[1];
527 mid = *(int *)argv[2];
528 tag = *(int *)argv[3];
529
530 EXEC SQL SELECT COUNT(member_id) INTO :cnt FROM imembers
531 WHERE member_id = :mid AND member_type = :mtype AND
532 list_id = :lid;
533 if (dbms_errno)
534 return mr_errcode;
535 if (cnt == 0)
536 return MR_NO_MATCH;
537
538 incremental_clear_before();
539 EXEC SQL UPDATE imembers SET tag = :tag WHERE list_id = :lid
540 AND member_type = :mtype AND member_id = :mid;
541 if (dbms_errno)
542 return mr_errcode;
543
544 iargv[0] = (char *)lid;
545 iargv[1] = mtype;
546 iargv[2] = (char *)mid;
547 incremental_after(IMEMBERS_TABLE, 0, iargv);
548
549 return MR_SUCCESS;
550}
b070f8a1 551
7f6ea9fd 552/* Don't allow someone to add someone to a list which is the acl of a
553 * query unless they're on the list acl, even if they're on the amtl
554 * query acl! Also, don't allow someone proxying to add someone to a
555 * capacl.
556 */
a37b0b99 557int acl_access_check(int list_id, client *cl)
558{
559 EXEC SQL BEGIN DECLARE SECTION;
59c3208b 560 int c1, c2, lid = list_id, acl_id, memacl_id;
561 char acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
a37b0b99 562 EXEC SQL END DECLARE SECTION;
563
564 /* Check if the list is directly a capacl */
565 EXEC SQL SELECT COUNT(list_id) INTO :c1 FROM capacls WHERE list_id=:lid;
566
567 /* Check if the list is a member (direct or indirect) of a list that
568 is a capacl */
569 EXEC SQL SELECT COUNT(l1.list_id) INTO :c2 FROM list l1, list l2,
570 imembers im, capacls c WHERE c.list_id = l2.list_id AND
571 im.list_id = l2.list_id AND im.member_type = 'LIST' AND
572 im.member_id = l1.list_id AND l1.list_id = :lid;
573
574 if (c1 == 0 && c2 == 0)
575 return 0;
576
7f6ea9fd 577 if (cl->proxy_id)
578 return 1;
579
59c3208b 580 EXEC SQL SELECT acl_type, acl_id, memacl_type, memacl_id
581 INTO :acl_type, :acl_id, :memacl_type, :memacl_id
a37b0b99 582 FROM list WHERE list_id=:lid;
59c3208b 583
584 if (!find_member(acl_type, acl_id, cl))
585 {
586 if (!find_member(memacl_type, memacl_id, cl))
587 return 1;
588 }
589
590 return 0;
a37b0b99 591}
592
593
0659551f 594/* get_ace_use - given a type and a name, return a type and a name.
595 * The ace_type is one of "LIST", "USER", "RLIST", or "RUSER" in argv[0],
596 * and argv[1] will contain the ID of the entity in question. The R*
597 * types mean to recursively look at every containing list, not just
598 * when the object in question is a direct member. On return, the
599 * usage type will be one of LIST, SERVICE, FILESYS, QUOTA, QUERY, or ZEPHYR.
b070f8a1 600 */
601
5eaef520 602int get_ace_use(struct query *q, char *argv[], client *cl,
7ac48069 603 int (*action)(int, char *[], void *), void *actarg)
5d677c81 604{
5eaef520 605 int found = 0;
606 EXEC SQL BEGIN DECLARE SECTION;
607 char *atype;
608 int aid, listid, id;
609 EXEC SQL END DECLARE SECTION;
7ac48069 610 struct save_queue *sq;
5eaef520 611
612 atype = argv[0];
613 aid = *(int *)argv[1];
614 if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
615 !strcmp(atype, "KERBEROS"))
616 return get_ace_internal(atype, aid, action, actarg);
617
618 sq = sq_create();
619 if (!strcmp(atype, "RLIST"))
620 {
7ac48069 621 sq_save_data(sq, (void *)aid);
5eaef520 622 /* get all the list_id's of containing lists */
623 EXEC SQL DECLARE csr107 CURSOR FOR
624 SELECT list_id FROM imembers
625 WHERE member_type = 'LIST' AND member_id = :aid;
626 if (dbms_errno)
627 return mr_errcode;
628 EXEC SQL OPEN csr107;
629 if (dbms_errno)
630 return mr_errcode;
631 while (1)
632 {
633 EXEC SQL FETCH csr107 INTO :listid;
634 if (sqlca.sqlcode)
635 break;
7ac48069 636 sq_save_unique_data(sq, (void *)listid);
0659551f 637 }
5eaef520 638 EXEC SQL CLOSE csr107;
639 /* now process each one */
640 while (sq_get_data(sq, &id))
641 {
642 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
643 found++;
0659551f 644 }
645 }
b070f8a1 646
5eaef520 647 if (!strcmp(atype, "RUSER"))
648 {
649 EXEC SQL DECLARE csr108 CURSOR FOR
650 SELECT list_id FROM imembers
651 WHERE member_type = 'USER' AND member_id = :aid;
652 if (dbms_errno)
653 return mr_errcode;
654 EXEC SQL OPEN csr108;
655 if (dbms_errno)
656 return mr_errcode;
657 while (1)
658 {
659 EXEC SQL FETCH csr108 INTO :listid;
660 if (sqlca.sqlcode)
661 break;
7ac48069 662 sq_save_data(sq, (void *)listid);
0659551f 663 }
5eaef520 664 EXEC SQL CLOSE csr108;
665 /* now process each one */
666 while (sq_get_data(sq, &id))
667 {
668 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
669 found++;
0659551f 670 }
5eaef520 671 if (get_ace_internal("USER", aid, action, actarg) == MR_SUCCESS)
672 found++;
0659551f 673 }
45bf7573 674
5eaef520 675 if (!strcmp(atype, "RKERBEROS"))
676 {
677 EXEC SQL DECLARE csr109 CURSOR FOR
678 SELECT list_id FROM imembers
679 WHERE member_type = 'KERBEROS' AND member_id = :aid;
680 if (dbms_errno)
681 return mr_errcode;
682 EXEC SQL OPEN csr109;
683 if (dbms_errno)
684 return mr_errcode;
685 while (1)
686 {
687 EXEC SQL FETCH csr109 INTO :listid;
688 if (sqlca.sqlcode)
689 break;
7ac48069 690 sq_save_data(sq, (void *)listid);
0659551f 691 }
5eaef520 692 EXEC SQL CLOSE csr109;
693 /* now process each one */
694 while (sq_get_data(sq, &id))
695 {
696 if (get_ace_internal("LIST", id, action, actarg) == MR_SUCCESS)
697 found++;
0659551f 698 }
5eaef520 699 if (get_ace_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
700 found++;
0659551f 701 }
b3ce33fe 702
5eaef520 703 sq_destroy(sq);
704 if (dbms_errno)
705 return mr_errcode;
706 if (!found)
707 return MR_NO_MATCH;
708 return MR_SUCCESS;
5d677c81 709}
b070f8a1 710
711
0659551f 712/* This looks up a single list or user for ace use. atype must be "USER"
713 * or "LIST", and aid is the ID of the corresponding object. This is used
714 * by get_ace_use above.
b070f8a1 715 */
716
7ac48069 717int get_ace_internal(char *atype, int aid,
718 int (*action)(int, char *[], void *), void *actarg)
5d677c81 719{
5eaef520 720 char *rargv[2];
721 int found = 0;
722 EXEC SQL BEGIN DECLARE SECTION;
d3051625 723 char name[MAX_FIELD_WIDTH], *type = atype;
5eaef520 724 int id = aid;
725 EXEC SQL END DECLARE SECTION;
726
727 rargv[1] = name;
728 if (!strcmp(atype, "LIST"))
729 {
730 rargv[0] = "FILESYS";
731 EXEC SQL DECLARE csr110 CURSOR FOR
732 SELECT label FROM filesys
733 WHERE owners = :id;
734 if (dbms_errno)
735 return mr_errcode;
736 EXEC SQL OPEN csr110;
737 if (dbms_errno)
738 return mr_errcode;
739 while (1)
740 {
741 EXEC SQL FETCH csr110 INTO :name;
742 if (sqlca.sqlcode)
743 break;
744 (*action)(2, rargv, actarg);
745 found++;
b070f8a1 746 }
5eaef520 747 EXEC SQL CLOSE csr110;
748
749 rargv[0] = "QUERY";
750 EXEC SQL DECLARE csr111 CURSOR FOR
751 SELECT capability FROM capacls
752 WHERE list_id = :id;
753 if (dbms_errno)
754 return mr_errcode;
755 EXEC SQL OPEN csr111;
756 if (dbms_errno)
757 return mr_errcode;
758 while (1)
759 {
760 EXEC SQL FETCH csr111 INTO :name;
761 if (sqlca.sqlcode)
762 break;
763 (*action)(2, rargv, actarg);
764 found++;
b070f8a1 765 }
5eaef520 766 EXEC SQL CLOSE csr111;
767 }
768 else if (!strcmp(atype, "USER"))
769 {
770 rargv[0] = "FILESYS";
771 EXEC SQL DECLARE csr112 CURSOR FOR
772 SELECT label FROM filesys
773 WHERE owner = :id;
774 if (dbms_errno)
775 return mr_errcode;
776 EXEC SQL OPEN csr112;
777 if (dbms_errno)
778 return mr_errcode;
779 while (1)
780 {
781 EXEC SQL FETCH csr112 INTO :name;
782 if (sqlca.sqlcode)
783 break;
784 (*action)(2, rargv, actarg);
785 found++;
b070f8a1 786 }
5eaef520 787 EXEC SQL CLOSE csr112;
b070f8a1 788 }
0fc7ab46 789
5eaef520 790 rargv[0] = "LIST";
791 EXEC SQL DECLARE csr113 CURSOR FOR
792 SELECT name FROM list
40a6abf9 793 WHERE (acl_type = :type AND acl_id = :id)
794 OR (memacl_type = :type AND memacl_id = :id);
5eaef520 795 if (dbms_errno)
796 return mr_errcode;
797 EXEC SQL OPEN csr113;
798 if (dbms_errno)
799 return mr_errcode;
800 while (1)
801 {
802 EXEC SQL FETCH csr113 INTO :name;
803 if (sqlca.sqlcode)
804 break;
805 (*action)(2, rargv, actarg);
806 found++;
5d677c81 807 }
5eaef520 808 EXEC SQL CLOSE csr113;
809
810 rargv[0] = "SERVICE";
811 EXEC SQL DECLARE csr114 CURSOR FOR
812 SELECT name FROM servers
813 WHERE acl_type = :type AND acl_id = :id;
814 if (dbms_errno)
815 return mr_errcode;
816 EXEC SQL OPEN csr114;
817 if (dbms_errno)
818 return mr_errcode;
819 while (1)
820 {
821 EXEC SQL FETCH csr114 INTO :name;
822 if (sqlca.sqlcode)
823 break;
824 (*action)(2, rargv, actarg);
825 found++;
0fc7ab46 826 }
5eaef520 827 EXEC SQL CLOSE csr114;
b070f8a1 828
5eaef520 829 rargv[0] = "HOSTACCESS";
830 EXEC SQL DECLARE csr115 CURSOR FOR
831 SELECT name FROM machine m, hostaccess ha
832 WHERE m.mach_id = ha.mach_id AND ha.acl_type = :type
833 AND ha.acl_id = :id;
d3051625 834 if (dbms_errno)
835 return mr_errcode;
836 EXEC SQL OPEN csr115;
837 if (dbms_errno)
838 return mr_errcode;
839 while (1)
840 {
841 EXEC SQL FETCH csr115 INTO :name;
842 if (sqlca.sqlcode)
843 break;
844 (*action)(2, rargv, actarg);
845 found++;
846 }
847 EXEC SQL CLOSE csr115;
848
849 rargv[0] = "MACHINE";
850 EXEC SQL DECLARE csr115a CURSOR FOR
851 SELECT name FROM machine m
852 WHERE m.owner_type = :type
853 AND m.owner_id = :id;
854 if (dbms_errno)
855 return mr_errcode;
856 EXEC SQL OPEN csr115a;
857 if (dbms_errno)
858 return mr_errcode;
859 while (1)
860 {
861 EXEC SQL FETCH csr115a INTO :name;
862 if (sqlca.sqlcode)
863 break;
864 (*action)(2, rargv, actarg);
865 found++;
866 }
867 EXEC SQL CLOSE csr115a;
868
869 rargv[0] = "ZEPHYR";
870 EXEC SQL DECLARE csr116 CURSOR FOR
871 SELECT class FROM zephyr z
872 WHERE z.xmt_type = :type AND z.xmt_id = :id
873 OR z.sub_type = :type AND z.sub_id = :id
874 OR z.iws_type = :type AND z.iws_id = :id
34463187 875 OR z.iui_type = :type AND z.iui_id = :id
876 OR z.owner_type = :type AND z.owner_id = :id;
d3051625 877 if (dbms_errno)
878 return mr_errcode;
879 EXEC SQL OPEN csr116;
880 if (dbms_errno)
881 return mr_errcode;
882 while (1)
883 {
884 EXEC SQL FETCH csr116 INTO :name;
885 if (sqlca.sqlcode)
886 break;
887 (*action)(2, rargv, actarg);
888 found++;
889 }
890 EXEC SQL CLOSE csr116;
891
892 if (!found)
893 return MR_NO_MATCH;
894 return MR_SUCCESS;
895}
896
897/* ghbo_internal */
898int ghbo_internal(char *atype, int aid,
899 int (*action)(int, char *[], void *), void *actarg)
900{
901 char *rargv[1];
902 int found = 0;
903 EXEC SQL BEGIN DECLARE SECTION;
904 char name[MACHINE_NAME_SIZE], *type = atype;
905 int id = aid;
906 EXEC SQL END DECLARE SECTION;
907
908 rargv[0] = name;
909 EXEC SQL DECLARE csr115b CURSOR FOR
910 SELECT name FROM machine m
911 WHERE m.owner_type = :type
912 AND m.owner_id = :id;
913 if (dbms_errno)
914 return mr_errcode;
915 EXEC SQL OPEN csr115b;
916 if (dbms_errno)
917 return mr_errcode;
918 while (1)
919 {
920 EXEC SQL FETCH csr115b INTO :name;
921 if (sqlca.sqlcode)
922 break;
923 (*action)(1, rargv, actarg);
924 found++;
925 }
926 EXEC SQL CLOSE csr115b;
927
928 if (!found)
929 return MR_NO_MATCH;
930 return MR_SUCCESS;
931}
932
933/* get_host_by_owner - like gaus but limited to hosts */
934int get_host_by_owner(struct query *q, char *argv[], client *cl,
935 int (*action)(int, char *[], void *), void *actarg)
936{
937 int found = 0;
938 EXEC SQL BEGIN DECLARE SECTION;
939 char *atype;
940 int aid, listid, id;
941 EXEC SQL END DECLARE SECTION;
942 struct save_queue *sq;
943
944 atype = argv[0];
945 aid = *(int *)argv[1];
946 if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
947 !strcmp(atype, "KERBEROS"))
948 return ghbo_internal(atype, aid, action, actarg);
949
950 sq = sq_create();
951 if (!strcmp(atype, "RLIST"))
952 {
953 sq_save_data(sq, (void *)aid);
954 /* get all the list_id's of containing lists */
955 EXEC SQL DECLARE csr107q CURSOR FOR
956 SELECT list_id FROM imembers
957 WHERE member_type = 'LIST' AND member_id = :aid;
958 if (dbms_errno)
959 return mr_errcode;
960 EXEC SQL OPEN csr107q;
961 if (dbms_errno)
962 return mr_errcode;
963 while (1)
964 {
965 EXEC SQL FETCH csr107q INTO :listid;
966 if (sqlca.sqlcode)
967 break;
968 sq_save_unique_data(sq, (void *)listid);
969 }
970 EXEC SQL CLOSE csr107q;
971 /* now process each one */
972 while (sq_get_data(sq, &id))
973 {
974 if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
975 found++;
976 }
977 }
978
979 if (!strcmp(atype, "RUSER"))
980 {
981 EXEC SQL DECLARE csr108q CURSOR FOR
982 SELECT list_id FROM imembers
983 WHERE member_type = 'USER' AND member_id = :aid;
984 if (dbms_errno)
5eaef520 985 return mr_errcode;
d3051625 986 EXEC SQL OPEN csr108q;
987 if (dbms_errno)
5eaef520 988 return mr_errcode;
d3051625 989 while (1)
990 {
991 EXEC SQL FETCH csr108q INTO :listid;
992 if (sqlca.sqlcode)
993 break;
994 sq_save_data(sq, (void *)listid);
995 }
996 EXEC SQL CLOSE csr108q;
997 /* now process each one */
998 while (sq_get_data(sq, &id))
999 {
1000 if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
1001 found++;
1002 }
1003 if (ghbo_internal("USER", aid, action, actarg) == MR_SUCCESS)
0659551f 1004 found++;
d3051625 1005 }
1006
1007 if (!strcmp(atype, "RKERBEROS"))
1008 {
1009 EXEC SQL DECLARE csr109q CURSOR FOR
1010 SELECT list_id FROM imembers
1011 WHERE member_type = 'KERBEROS' AND member_id = :aid;
1012 if (dbms_errno)
1013 return mr_errcode;
1014 EXEC SQL OPEN csr109q;
1015 if (dbms_errno)
1016 return mr_errcode;
1017 while (1)
1018 {
1019 EXEC SQL FETCH csr109q INTO :listid;
1020 if (sqlca.sqlcode)
1021 break;
1022 sq_save_data(sq, (void *)listid);
1023 }
1024 EXEC SQL CLOSE csr109q;
1025 /* now process each one */
1026 while (sq_get_data(sq, &id))
1027 {
1028 if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
1029 found++;
1030 }
1031 if (ghbo_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
0659551f 1032 found++;
d3051625 1033 }
b070f8a1 1034
d3051625 1035 sq_destroy(sq);
1036 if (dbms_errno)
1037 return mr_errcode;
1038 if (!found)
1039 return MR_NO_MATCH;
1040 return MR_SUCCESS;
5d677c81 1041}
b070f8a1 1042
0659551f 1043/* get_lists_of_member - given a type and a name, return the name and flags
1044 * of all of the lists of the given member. The member_type is one of
5f7b0741 1045 * "LIST", "USER", "STRING", "KERBEROS", "MACHINE", "RLIST", "RUSER",
1046 * "RSTRING", "RKERBEROS", or "RMACHINE" in argv[0], and argv[1] will contain
1047 * the ID of the entity in question. The R* types mean to recursively look
1048 * at every containing list, not just when the object in question is a direct
1049 * member.
0659551f 1050 */
b070f8a1 1051
5eaef520 1052int get_lists_of_member(struct query *q, char *argv[], client *cl,
7ac48069 1053 int (*action)(int, char *[], void *), void *actarg)
0659551f 1054{
5eaef520 1055 int found = 0, direct = 1;
1056 char *rargv[6];
1057 EXEC SQL BEGIN DECLARE SECTION;
1058 char *atype;
1059 int aid;
e688520a 1060 char name[LIST_NAME_SIZE];
1061 char active[5], public[5], hidden[5], maillist[5], grouplist[5];
5eaef520 1062 EXEC SQL END DECLARE SECTION;
1063
1064 atype = argv[0];
1065 aid = *(int *)argv[1];
1066 if (!strcmp(atype, "RLIST"))
1067 {
1068 atype = "LIST";
1069 direct = 0;
0659551f 1070 }
5eaef520 1071 if (!strcmp(atype, "RUSER"))
1072 {
1073 atype = "USER";
1074 direct = 0;
0659551f 1075 }
5eaef520 1076 if (!strcmp(atype, "RSTRING"))
1077 {
1078 atype = "STRING";
1079 direct = 0;
0659551f 1080 }
5eaef520 1081 if (!strcmp(atype, "RKERBEROS"))
1082 {
1083 atype = "KERBEROS";
1084 direct = 0;
0659551f 1085 }
5f7b0741 1086 if (!strcmp(atype, "RMACHINE"))
1087 {
1088 atype = "MACHINE";
1089 direct = 0;
1090 }
b070f8a1 1091
5eaef520 1092 rargv[0] = name;
1093 rargv[1] = active;
1094 rargv[2] = public;
1095 rargv[3] = hidden;
1096 rargv[4] = maillist;
1097 rargv[5] = grouplist;
1098 if (direct)
1099 {
1100 EXEC SQL DECLARE csr117a CURSOR FOR
1101 SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1102 FROM list l, imembers im
1103 WHERE l.list_id = im.list_id AND im.direct = 1
1104 AND im.member_type = :atype AND im.member_id = :aid;
1105 if (dbms_errno)
1106 return mr_errcode;
1107 EXEC SQL OPEN csr117a;
1108 if (dbms_errno)
1109 return mr_errcode;
1110 while (1)
1111 {
1112 EXEC SQL FETCH csr117a
1113 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1114 if (sqlca.sqlcode)
1115 break;
1116 (*action)(6, rargv, actarg);
1117 found++;
0659551f 1118 }
5eaef520 1119 EXEC SQL CLOSE csr117a;
1120 }
1121 else
1122 {
1123 EXEC SQL DECLARE csr117b CURSOR FOR
1124 SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1125 FROM list l, imembers im
1126 WHERE l.list_id = im.list_id
1127 AND im.member_type = :atype AND im.member_id = :aid;
1128 if (dbms_errno)
1129 return mr_errcode;
1130 EXEC SQL OPEN csr117b;
1131 if (dbms_errno)
1132 return mr_errcode;
1133 while (1)
1134 {
1135 EXEC SQL FETCH csr117b
1136 INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1137 if (sqlca.sqlcode)
1138 break;
1139 (*action)(6, rargv, actarg);
1140 found++;
0659551f 1141 }
5eaef520 1142 EXEC SQL CLOSE csr117b;
038fd8a4 1143 }
0659551f 1144
5eaef520 1145 if (dbms_errno)
1146 return mr_errcode;
1147 if (!found)
1148 return MR_NO_MATCH;
1149 return MR_SUCCESS;
038fd8a4 1150}
1151
b070f8a1 1152
0659551f 1153/* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
1154 * the five flags associated with each list. It will return the name of
1155 * each list that meets the quailifications. It does this by building a
1156 * where clause based on the arguments, then doing a retrieve.
1157 */
1158
1159static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
b070f8a1 1160
5eaef520 1161int qualified_get_lists(struct query *q, char *argv[], client *cl,
7ac48069 1162 int (*action)(int, char *[], void *), void *actarg)
5d677c81 1163{
5eaef520 1164 return qualified_get(q, argv, action, actarg, "l.list_id != 0",
1165 "l", "name", lflags);
0659551f 1166}
0fc7ab46 1167
5d677c81 1168
5eaef520 1169int get_members_of_list(struct query *q, char *argv[], client *cl,
7ac48069 1170 int (*action)(int, char *[], void *), void *actarg)
0659551f 1171{
5eaef520 1172 EXEC SQL BEGIN DECLARE SECTION;
7ac48069 1173 int list_id, direct;
46b6f1f6 1174 char member_name[MAX_FIELD_WIDTH], tag[STRINGS_STRING_SIZE];
5eaef520 1175 EXEC SQL END DECLARE SECTION;
46b6f1f6 1176 char *targv[3];
1177 int targc;
5eaef520 1178
46b6f1f6 1179 /* For gmol or gtml, only get direct members. For geml, get all. */
1180 if (!strcmp(q->shortname, "geml"))
1181 direct = -1;
1182 else
5eaef520 1183 direct = 0;
46b6f1f6 1184
1185 /* For gmol or geml, only return type and name. For gtml, return tag too. */
1186 if (!strcmp(q->shortname, "gtml"))
1187 targc = 3;
5eaef520 1188 else
46b6f1f6 1189 targc = 2;
5eaef520 1190
1191 list_id = *(int *)argv[0];
1192
1193 targv[1] = member_name;
46b6f1f6 1194 targv[2] = tag;
1195
5eaef520 1196 targv[0] = "USER";
1197 EXEC SQL DECLARE csr119 CURSOR FOR
46b6f1f6 1198 SELECT u.login, s.string FROM users u, imembers im, strings s
5eaef520 1199 WHERE im.list_id = :list_id AND im.member_type = 'USER'
1200 AND im.member_id = u.users_id AND im.direct > :direct
46b6f1f6 1201 AND s.string_id = im.tag ORDER BY 1;
5eaef520 1202 if (dbms_errno)
1203 return mr_errcode;
1204 EXEC SQL OPEN csr119;
1205 if (dbms_errno)
1206 return mr_errcode;
1207 while (1)
1208 {
46b6f1f6 1209 EXEC SQL FETCH csr119 INTO :member_name, :tag;
5eaef520 1210 if (sqlca.sqlcode)
1211 break;
46b6f1f6 1212 (*action)(targc, targv, actarg);
b070f8a1 1213 }
5eaef520 1214 EXEC SQL CLOSE csr119;
1215 if (dbms_errno)
1216 return mr_errcode;
1217
1218 targv[0] = "LIST";
1219 EXEC SQL DECLARE csr120 CURSOR FOR
46b6f1f6 1220 SELECT l.name, s.string FROM list l, imembers im, strings s
5eaef520 1221 WHERE im.list_id = :list_id AND im.member_type = 'LIST'
1222 AND im.member_id = l.list_id AND im.direct > :direct
46b6f1f6 1223 AND s.string_id = im.tag ORDER BY 1;
5eaef520 1224 if (dbms_errno)
1225 return mr_errcode;
1226 EXEC SQL OPEN csr120;
1227 if (dbms_errno)
1228 return mr_errcode;
1229 while (1)
1230 {
46b6f1f6 1231 EXEC SQL FETCH csr120 INTO :member_name, :tag;
5eaef520 1232 if (sqlca.sqlcode)
1233 break;
46b6f1f6 1234 (*action)(targc, targv, actarg);
b070f8a1 1235 }
5eaef520 1236 EXEC SQL CLOSE csr120;
1237 if (dbms_errno)
1238 return mr_errcode;
1239
1240 targv[0] = "STRING";
1241 EXEC SQL DECLARE csr121 CURSOR FOR
46b6f1f6 1242 SELECT str.string, s.string FROM strings str, imembers im, strings s
5eaef520 1243 WHERE im.list_id = :list_id AND im.member_type = 'STRING'
1244 AND im.member_id = str.string_id AND im.direct > :direct
46b6f1f6 1245 AND s.string_id = im.tag ORDER BY 1;
5eaef520 1246 if (dbms_errno)
1247 return mr_errcode;
1248 EXEC SQL OPEN csr121;
1249 if (dbms_errno)
1250 return mr_errcode;
1251 while (1)
1252 {
46b6f1f6 1253 EXEC SQL FETCH csr121 INTO :member_name, :tag;
5eaef520 1254 if (sqlca.sqlcode)
1255 break;
46b6f1f6 1256 (*action)(targc, targv, actarg);
b070f8a1 1257 }
5eaef520 1258 EXEC SQL CLOSE csr121;
1259 if (dbms_errno)
1260 return mr_errcode;
1261
1262 targv[0] = "KERBEROS";
1263 EXEC SQL DECLARE csr122 CURSOR FOR
46b6f1f6 1264 SELECT str.string, s.string FROM strings str, imembers im, strings s
5eaef520 1265 WHERE im.list_id = :list_id AND im.member_type = 'KERBEROS'
46b6f1f6 1266 AND im.member_id = str.string_id AND im.direct > :direct
1267 AND s.string_id = im.tag ORDER BY 1;
5eaef520 1268 if (dbms_errno)
1269 return mr_errcode;
1270 EXEC SQL OPEN csr122;
1271 if (dbms_errno)
1272 return mr_errcode;
1273 while (1)
1274 {
46b6f1f6 1275 EXEC SQL FETCH csr122 INTO :member_name, :tag;
5eaef520 1276 if (sqlca.sqlcode)
1277 break;
46b6f1f6 1278 (*action)(targc, targv, actarg);
0659551f 1279 }
5eaef520 1280 EXEC SQL CLOSE csr122;
1281 if (dbms_errno)
1282 return mr_errcode;
b070f8a1 1283
5f7b0741 1284 targv[0] = "MACHINE";
1285 EXEC SQL DECLARE csr123 CURSOR FOR
1286 SELECT m.name, s.string FROM machine m, imembers im, strings s
1287 WHERE im.list_id = :list_id AND im.member_type = 'MACHINE'
1288 AND im.member_id = m.mach_id AND im.direct > :direct
1289 AND s.string_id = im.tag ORDER BY 1;
1290 if (dbms_errno)
1291 return mr_errcode;
1292 EXEC SQL OPEN csr123;
1293 if (dbms_errno)
1294 return mr_errcode;
1295 while (1)
1296 {
1297 EXEC SQL FETCH csr123 INTO :member_name, :tag;
1298 if (sqlca.sqlcode)
1299 break;
1300 (*action)(targc, targv, actarg);
1301 }
1302 EXEC SQL CLOSE csr123;
1303 if (dbms_errno)
1304 return mr_errcode;
1305
5eaef520 1306 return MR_SUCCESS;
5d677c81 1307}
b070f8a1 1308
1309
0659551f 1310/* count_members_of_list: this is a simple query, but it cannot be done
1311 * through the dispatch table.
1312 */
b070f8a1 1313
5eaef520 1314int count_members_of_list(struct query *q, char *argv[], client *cl,
7ac48069 1315 int (*action)(int, char *[], void *), void *actarg)
5d677c81 1316{
5eaef520 1317 EXEC SQL BEGIN DECLARE SECTION;
1318 int list, ct = 0;
1319 EXEC SQL END DECLARE SECTION;
1320 char *rargv[1], countbuf[5];
1321
1322 list = *(int *)argv[0];
1323 rargv[0] = countbuf;
1324 EXEC SQL SELECT count (*) INTO :ct FROM imembers
1325 WHERE list_id = :list AND direct = 1;
1326 if (dbms_errno)
1327 return mr_errcode;
1328 sprintf(countbuf, "%d", ct);
1329 (*action)(1, rargv, actarg);
1330 return MR_SUCCESS;
5d677c81 1331}
b070f8a1 1332
b070f8a1 1333
0659551f 1334/* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1335 * the three flags associated with each service. It will return the name of
1336 * each service that meets the quailifications. It does this by building a
1337 * where clause based on the arguments, then doing a retrieve.
1338 */
b070f8a1 1339
0659551f 1340static char *sflags[3] = { "enable", "inprogress", "harderror" };
b070f8a1 1341
5eaef520 1342int qualified_get_server(struct query *q, char *argv[], client *cl,
7ac48069 1343 int (*action)(int, char *[], void *), void *actarg)
5d677c81 1344{
5eaef520 1345 return qualified_get(q, argv, action, actarg, "s.name is not null",
1346 "s", "name", sflags);
1347 /* of course, name will never be null, but we need something there
1348 to make qualified_get happy */
5d677c81 1349}
b070f8a1 1350
1351
0659551f 1352/* generic qualified get routine, used by qualified_get_lists,
1353 * qualified_get_server, and qualified_get_serverhost.
1354 * Args:
1355 * start - a simple where clause, must not be empty
1356 * range - the name of the range variable
1357 * field - the field to return
1358 * flags - an array of strings, names of the flag variables
b070f8a1 1359 */
1360
7ac48069 1361int qualified_get(struct query *q, char *argv[],
1362 int (*action)(int, char *[], void *), void *actarg,
5eaef520 1363 char *start, char *range, char *field, char *flags[])
453b99fe 1364{
5eaef520 1365 char qual[256];
1366 int i;
1367 char buf[32];
1368
1369 strcpy(qual, start);
1370 for (i = 0; i < q->argc; i++)
1371 {
1372 if (!strcmp(argv[i], "TRUE"))
1373 {
1374 sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1375 strcat(qual, buf);
1376 }
1377 else if (!strcmp(argv[i], "FALSE"))
1378 {
1379 sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1380 strcat(qual, buf);
0659551f 1381 }
453b99fe 1382 }
1383
5eaef520 1384 sprintf(stmt_buf, "SELECT %s.%s FROM %s %s WHERE %s", range, field,
1385 table_name[q->rtable], range, qual);
1386 return do_for_all_rows(stmt_buf, 1, action, actarg);
453b99fe 1387}
1388
1389
0659551f 1390/* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1391 * the five flags associated with each serverhost. It will return the name of
1392 * each service and host that meets the quailifications. It does this by
1393 * building a where clause based on the arguments, then doing a retrieve.
45bf7573 1394 */
45bf7573 1395
0659551f 1396static char *shflags[6] = { "service", "enable", "override", "success",
1397 "inprogress", "hosterror" };
45bf7573 1398
5eaef520 1399int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
7ac48069 1400 int (*action)(int, char *[], void *),
1401 void *actarg)
45bf7573 1402{
5eaef520 1403 char qual[256], buf[32];
1404 int i;
1405
1406 sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1407 argv[0]);
1408 for (i = 1; i < q->argc; i++)
1409 {
1410 if (!strcmp(argv[i], "TRUE"))
1411 {
1412 sprintf(buf, " AND sh.%s != 0", shflags[i]);
1413 strcat(qual, buf);
1414 }
1415 else if (!strcmp(argv[i], "FALSE"))
1416 {
1417 sprintf(buf, " AND sh.%s = 0", shflags[i]);
1418 strcat(qual, buf);
0659551f 1419 }
1420 }
45bf7573 1421
5eaef520 1422 sprintf(stmt_buf, "SELECT sh.service, m.name FROM serverhosts sh, "
1423 "machine m WHERE %s", qual);
1424 return do_for_all_rows(stmt_buf, 2, action, actarg);
45bf7573 1425}
1426
0659551f 1427
1428/* register_user - change user's login name and allocate a pobox, group,
1429 * filesystem, and quota for them. The user's status must start out as 0,
b4bfb4d1 1430 * and is left as 2. Arguments are: user's UID, new login name, and
edf666a5 1431 * pobox type ("POP" = POP, "IMAP" or numeric = IMAP)
99e09b48 1432 */
0659551f 1433
5eaef520 1434int register_user(struct query *q, char **argv, client *cl)
99e09b48 1435{
5eaef520 1436 EXEC SQL BEGIN DECLARE SECTION;
e688520a 1437 char *login, *entity;
1438 char directory[FILESYS_NAME_SIZE], machname[MACHINE_NAME_SIZE];
b4bfb4d1 1439 char dir[NFSPHYS_DIR_SIZE], *potype;
1440 int who, rowcount, mid, uid, users_id;
1441 int ostatus, nstatus, fsidval, popid;
1442 int npid, tmp;
edf666a5 1443 int po_exists = 0;
b4bfb4d1 1444 static int m_id, def_quota, def_imap_quota, list_id;
5eaef520 1445 EXEC SQL END DECLARE SECTION;
1446 char buffer[256], *aargv[3];
1447
b4bfb4d1 1448 if (!m_id)
1449 {
1450 EXEC SQL SELECT list_id INTO :list_id FROM list
1451 WHERE name = 'wheel';
1452
1453 EXEC SQL SELECT mach_id INTO :m_id FROM machine
1454 WHERE name = 'ATHENA.MIT.EDU';
1455
1456 EXEC SQL SELECT value INTO :def_quota FROM numvalues
1457 WHERE name = 'def_quota';
1458 if (sqlca.sqlerrd[2] != 1)
1459 return MR_NO_QUOTA;
1460
1461 EXEC SQL SELECT value INTO :def_imap_quota FROM numvalues
1462 WHERE name = 'def_imap_quota';
1463 if (sqlca.sqlerrd[2] != 1)
1464 return MR_NO_QUOTA;
1465 }
1466
5eaef520 1467 entity = cl->entity;
1468 who = cl->client_id;
1469
1470 uid = atoi(argv[0]);
1471 login = argv[1];
b4bfb4d1 1472 potype = argv[2];
5eaef520 1473
1474 /* find user */
1475 EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1476 FROM users
1477 WHERE unix_uid = :uid AND (status = 0 OR status = 5 OR status = 6);
1478
1479 if (sqlca.sqlerrd[2] == 0)
1480 return MR_NO_MATCH;
1481 if (sqlca.sqlerrd[2] > 1)
1482 return MR_NOT_UNIQUE;
1483
1484 /* check new login name */
1485 EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1486 WHERE login = :login AND users_id != :users_id;
1487 if (dbms_errno)
1488 return mr_errcode;
1489 if (rowcount > 0)
1490 return MR_IN_USE;
1491 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
ec0b8003 1492 WHERE LOWER(name) = :login;
5eaef520 1493 if (dbms_errno)
1494 return mr_errcode;
1495 if (rowcount > 0)
1496 return MR_IN_USE;
1497 EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
edf666a5 1498 WHERE label = :login;
5eaef520 1499 if (dbms_errno)
1500 return mr_errcode;
1501 if (rowcount > 0)
1502 return MR_IN_USE;
edf666a5 1503 EXEC SQL SELECT COUNT(label) INTO :rowcount
1504 FROM filesys WHERE label = :login || '.po';
1505 if (dbms_errno)
1506 return mr_errcode;
1507 if (rowcount > 0)
1508 {
1509 EXEC SQL SELECT owner INTO :tmp FROM filesys
1510 WHERE label = :login || '.po';
1511 if (dbms_errno)
1512 return mr_errcode;
1513 if ((ostatus == 0) || (tmp != users_id))
1514 return MR_IN_USE;
69e2312e 1515 else
edf666a5 1516 po_exists = 1;
1517 }
5eaef520 1518 EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
b4bfb4d1 1519 WHERE ( name = :login OR name = :login || '.po' )
1520 AND type = 'FILESYS';
5eaef520 1521 if (dbms_errno)
1522 return mr_errcode;
1523 if (rowcount > 0)
1524 return MR_IN_USE;
1525 com_err(whoami, 0, "login name OK");
1526
edf666a5 1527 EXEC SQL SELECT COUNT(potype) INTO :rowcount FROM users WHERE
1528 login = :login AND potype = 'POP';
1529 if (dbms_errno)
1530 return mr_errcode;
1531 if (rowcount > 0)
1532 po_exists = 1;
1533
b4bfb4d1 1534 /* choose type and location for pobox */
edf666a5 1535 if (!po_exists)
5eaef520 1536 {
edf666a5 1537 if (!strcmp(potype, "POP"))
b4bfb4d1 1538 {
edf666a5 1539
1540 EXEC SQL DECLARE csr130 CURSOR FOR
1541 SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1542 WHERE sh.service = 'POP' AND sh.mach_id = m.mach_id
1543 AND sh.value2 - sh.value1 =
1544 (SELECT MAX(value2 - value1) FROM serverhosts WHERE service = 'POP');
b4bfb4d1 1545 if (dbms_errno)
1546 return mr_errcode;
edf666a5 1547 EXEC SQL OPEN csr130;
1548 if (dbms_errno)
1549 return mr_errcode;
1550 EXEC SQL FETCH csr130 INTO :popid, :machname;
1551 if (sqlca.sqlerrd[2] == 0)
1552 {
1553 EXEC SQL CLOSE csr130;
1554 if (dbms_errno)
1555 return mr_errcode;
1556 return MR_NO_POBOX;
1557 }
1558 else
1559 {
1560 EXEC SQL CLOSE csr130;
1561 if (dbms_errno)
1562 return mr_errcode;
1563 }
1564
1565 EXEC SQL UPDATE users SET potype = 'POP', pop_id = :popid
1566 WHERE users_id = :users_id;
1567 com_err(whoami, 0, "pobox set to POP:%s", strtrim(machname));
1568 }
b4bfb4d1 1569 else
1570 {
edf666a5 1571 /* Select all IMAP nfsphys entries in order of increasing
1572 * (allocated - partsize). The partitions will almost always be
1573 * overallocated, but we choose the one that is the least
1574 * overallocated.
1575 */
1576 potype = "IMAP";
1577
1578 EXEC SQL DECLARE csr_rusr_imap CURSOR FOR
1579 SELECT np.allocated - np.partsize, np.nfsphys_id, np.mach_id,
1580 np.dir, m.name FROM serverhosts sh, nfsphys np, machine m
1581 WHERE sh.service = 'POSTOFFICE' AND sh.mach_id = np.mach_id
1582 AND m.mach_id = np.mach_id
1583 ORDER BY 1;
b4bfb4d1 1584 if (dbms_errno)
1585 return mr_errcode;
edf666a5 1586 EXEC SQL OPEN csr_rusr_imap;
1587 if (dbms_errno)
1588 return mr_errcode;
1589 EXEC SQL FETCH csr_rusr_imap INTO :tmp, :npid, :mid, :dir, :machname;
1590 if (sqlca.sqlerrd[2] == 0)
1591 {
1592 EXEC SQL CLOSE csr_rusr_imap;
1593 if (dbms_errno)
1594 return mr_errcode;
1595 return MR_NO_POBOX;
1596 }
1597 else
1598 {
1599 EXEC SQL CLOSE csr_rusr_imap;
1600 if (dbms_errno)
1601 return mr_errcode;
1602 }
b4bfb4d1 1603
edf666a5 1604 /* create filesystem */
1605 if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1606 return MR_NO_ID;
1607 incremental_clear_before();
1608
1609 EXEC SQL SELECT value INTO :popid FROM numvalues
1610 WHERE numvalues.name = 'filsys_id';
1611 EXEC SQL INSERT INTO filesys
1612 (filsys_id, phys_id, label, type, mach_id, name,
1613 mount, rwaccess, comments, owner, owners, createflg,
1614 lockertype, modtime, modby, modwith)
1615 VALUES
1616 (:popid, :npid, :login || '.po', 'IMAP', :mid, :dir,
1617 CHR(0), 'w', 'IMAP box', :users_id, :list_id, 1,
9f3ee749 1618 'USER', SYSDATE, :who, :entity);
b4bfb4d1 1619
b4bfb4d1 1620 if (dbms_errno)
1621 return mr_errcode;
edf666a5 1622 if (sqlca.sqlerrd[2] != 1)
1623 return MR_INTERNAL;
1624 sprintf(buffer, "fs.filsys_id = %d", popid);
1625 incremental_after(FILESYS_TABLE, buffer, 0);
1626
1627 /* set quota */
1628 incremental_clear_before();
1629 EXEC SQL INSERT INTO quota
1630 (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1631 VALUES (:users_id, :popid, 'USER', :def_imap_quota, :npid,
1632 SYSDATE, :who, :entity);
1633 if (dbms_errno)
1634 return mr_errcode;
1635 if (sqlca.sqlerrd[2] != 1)
1636 return MR_INTERNAL;
1637 aargv[0] = login;
1638 aargv[1] = "USER";
1639 aargv[2] = login;
1640 sprintf(buffer, "q.entity_id = %d and q.filsys_id = %d and "
1641 "q.type = 'USER'", users_id, popid);
1642 incremental_after(QUOTA_TABLE, buffer, aargv);
b4bfb4d1 1643 if (dbms_errno)
1644 return mr_errcode;
99e09b48 1645
edf666a5 1646 EXEC SQL UPDATE nfsphys SET allocated = allocated + :def_imap_quota
1647 WHERE nfsphys_id = :npid;
1648
1649 EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :popid
1650 WHERE users_id = :users_id;
1651 com_err(whoami, 0, "pobox set to IMAP:%s:%s", strtrim(machname),
1652 strtrim(dir));
1653 }
b4bfb4d1 1654 }
1655
5eaef520 1656 /* change login name, set pobox */
1657 sprintf(buffer, "u.users_id = %d", users_id);
1658 incremental_before(USERS_TABLE, buffer, 0);
1659 nstatus = 2;
1660 if (ostatus == 5 || ostatus == 6)
1661 nstatus = 1;
1662 EXEC SQL UPDATE users SET login = :login, status = :nstatus,
b4bfb4d1 1663 modtime = SYSDATE, modby = :who, modwith = :entity,
3b634eb3 1664 pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity,
1665 created = SYSDATE, creator = :who
5eaef520 1666 WHERE users_id = :users_id;
1667
1668 if (dbms_errno)
1669 return mr_errcode;
1670 if (sqlca.sqlerrd[2] != 1)
1671 return MR_INTERNAL;
edf666a5 1672
1673 /* Only update usage count if we created a POP pobox. */
1674 if (!strcmp(potype, "POP") && !po_exists)
1675 set_pop_usage(mid, 1);
1676
b4bfb4d1 1677 com_err(whoami, 0, "set login name to %s", login);
5eaef520 1678 incremental_after(USERS_TABLE, buffer, 0);
1679
5eaef520 1680 /* create filesystem */
1681 if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1682 return MR_NO_ID;
1683 incremental_clear_before();
1684 if (islower(login[0]) && islower(login[1]))
1685 {
1686 sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1687 login[0], login[1], login);
0659551f 1688 }
5eaef520 1689 else
1690 sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1691
1692 EXEC SQL SELECT value INTO :fsidval FROM numvalues
1693 WHERE numvalues.name = 'filsys_id';
1694 EXEC SQL INSERT INTO filesys
1695 (filsys_id, phys_id, label, type, mach_id, name,
1696 mount, rwaccess, comments, owner, owners, createflg,
1697 lockertype, modtime, modby, modwith)
1698 VALUES
1699 (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1700 '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1701 'HOMEDIR', SYSDATE, :who, :entity);
1702
1703 if (dbms_errno)
1704 return mr_errcode;
1705 if (sqlca.sqlerrd[2] != 1)
1706 return MR_INTERNAL;
1707 sprintf(buffer, "fs.filsys_id = %d", fsidval);
1708 incremental_after(FILESYS_TABLE, buffer, 0);
1709
1710 /* set quota */
5eaef520 1711 incremental_clear_before();
1712 EXEC SQL INSERT INTO quota
1713 (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1714 VALUES (0, :fsidval, 'ANY', :def_quota, 0, SYSDATE, :who, :entity);
1715 if (dbms_errno)
1716 return mr_errcode;
1717 if (sqlca.sqlerrd[2] != 1)
1718 return MR_INTERNAL;
1719 aargv[0] = login;
1720 aargv[1] = "ANY";
1721 aargv[2] = login;
1722 sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'",
1723 fsidval);
1724 incremental_after(QUOTA_TABLE, buffer, aargv);
1725 com_err(whoami, 0, "quota of %d assigned", def_quota);
1726 if (dbms_errno)
1727 return mr_errcode;
1728
5eaef520 1729 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1730 WHERE table_name = 'users';
1731 EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = SYSDATE
1732 WHERE table_name = 'filesys' OR table_name = 'quota';
1733 if (dbms_errno)
1734 return mr_errcode;
1735 return MR_SUCCESS;
3afb5524 1736}
1737
0659551f 1738/** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1739 **
1740 ** Inputs:
1741 ** id of machine
1742 ** delta (will be +/- 1)
1743 **
1744 ** Description:
1745 ** - incr/decr value field in serverhosts table for pop/mach_id
1746 **
1747 **/
1748
1749int set_pop_usage(id, cnt)
03c05291 1750 int id, cnt;
0659551f 1751{
5eaef520 1752 EXEC SQL BEGIN DECLARE SECTION;
1753 int iid = id, icnt = cnt;
1754 EXEC SQL END DECLARE SECTION;
03c05291 1755
5eaef520 1756 EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1757 WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;
835aabee 1758
5eaef520 1759 if (dbms_errno)
1760 return mr_errcode;
1761 return MR_SUCCESS;
835aabee 1762}
1763
77eb4bdf 1764
1765int do_user_reservation(struct query *q, char *argv[], client *cl)
1766{
1767 EXEC SQL BEGIN DECLARE SECTION;
1768 char resv[USERS_RESERVATIONS_SIZE];
1769 char *trans, name[ALIAS_NAME_SIZE];
1770 int uid;
1771 EXEC SQL END DECLARE SECTION;
1772
1773 uid = *(int *)argv[0];
1774 trans = argv[1];
1775
1776 EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1777 WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1778 if (dbms_errno)
1779 return mr_errcode;
1780 if (sqlca.sqlerrd[2] != 1)
1781 return MR_STRING;
1782 name[1] = '\0';
1783
1784 EXEC SQL SELECT reservations INTO :resv FROM users
1785 WHERE users_id = :uid;
1786 if (dbms_errno)
1787 return mr_errcode;
1788 strtrim(resv);
1789
1790 if (!strcmp(q->shortname, "aurv"))
1791 {
1792 if (strchr(resv, *name))
1793 return MR_EXISTS;
1794 if (strlen(resv) == USERS_RESERVATIONS_SIZE - 1)
1795 return MR_ARG_TOO_LONG;
1796
1797 strcat(resv, name);
1798 }
1799 else
1800 {
1801 char *p = strchr(resv, *name);
1802 if (!p)
1803 return MR_NO_MATCH;
1804 memmove(p, p + 1, strlen(p));
1805 }
1806
1807 EXEC SQL UPDATE users SET reservations = NVL(:resv, CHR(0))
1808 WHERE users_id = :uid;
1809 if (dbms_errno)
1810 return mr_errcode;
1811
1812 EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1813 WHERE table_name = 'users';
1814 return set_modtime_by_id(q, argv, cl);
1815}
1816
1817int get_user_reservations(struct query *q, char **argv, client *cl,
1818 int (*action)(int, char *[], void *), void *actarg)
1819{
1820 EXEC SQL BEGIN DECLARE SECTION;
1821 char resv[USERS_RESERVATIONS_SIZE], *p;
1822 char trans[ALIAS_TRANS_SIZE], name[2] = { 0, 0 };
1823 int uid;
1824 EXEC SQL END DECLARE SECTION;
1825 char *targv[1];
1826
1827 uid = *(int *)argv[0];
1828
1829 EXEC SQL SELECT reservations INTO :resv FROM users
1830 WHERE users_id = :uid;
1831 if (dbms_errno)
1832 return mr_errcode;
1833
1834 targv[0] = trans;
1835 p = resv;
1836 while (*p && !isspace(*p))
1837 {
1838 name[0] = toupper(*p);
1839 EXEC SQL SELECT trans INTO :trans FROM alias
1840 WHERE type = 'RESERVE' AND UPPER(name) = :name;
1841 if (dbms_errno)
1842 return mr_errcode;
1843 if (sqlca.sqlerrd[2] != 1)
1844 sprintf(trans, "Unknown (%s)", name);
1845 (*action)(1, targv, actarg);
1846 p++;
1847 }
1848 return MR_SUCCESS;
1849}
1850
1851int get_user_by_reservation(struct query *q, char **argv, client *cl,
1852 int (*action)(int, char *[], void *), void *actarg)
1853{
1854 EXEC SQL BEGIN DECLARE SECTION;
1855 char resv[USERS_RESERVATIONS_SIZE], login[USERS_LOGIN_SIZE];
1856 char *trans, name[ALIAS_NAME_SIZE];
1857 int uid;
1858 EXEC SQL END DECLARE SECTION;
1859 char *targv[1];
1860
1861 trans = argv[0];
1862
1863 EXEC SQL SELECT UPPER(name) INTO :name FROM alias
1864 WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
1865 if (dbms_errno)
1866 return mr_errcode;
1867 if (sqlca.sqlerrd[2] != 1)
1868 return MR_STRING;
1869 name[1] = '\0';
1870
1871 EXEC SQL DECLARE csr_gubr CURSOR FOR
1872 SELECT login FROM users WHERE reservations LIKE '%' || :name || '%';
1873 EXEC SQL OPEN csr_gubr;
1874 if (dbms_errno)
1875 return mr_errcode;
1876
1877 targv[0] = login;
1878 while (1)
1879 {
1880 EXEC SQL FETCH csr_gubr INTO :login;
1881 if (sqlca.sqlcode)
1882 break;
1883 (*action)(1, targv, actarg);
1884 }
1885 EXEC SQL CLOSE csr_gubr;
1886
1887 return MR_SUCCESS;
1888}
2fb668b0 1889
1890int update_container(struct query *q, char *argv[], client *cl)
1891{
1892 EXEC SQL BEGIN DECLARE SECTION;
1893 int cnt_id, acl_id, memacl_id, who;
1894 char name[CONTAINERS_NAME_SIZE], newchildname[CONTAINERS_NAME_SIZE];
1895 char* newname, *entity, *description, *location, *contact, *acl_type, *memacl_type;
1896 EXEC SQL END DECLARE SECTION;
1897 char* tmpchar;
1898 int cnt, childid;
1899 char childname[CONTAINERS_NAME_SIZE];
bb5c9457 1900 char *qual;
2fb668b0 1901
1902 cnt_id = *(int *)argv[0];
1903 newname = argv[1];
1904 description = argv[2];
1905 location = argv[3];
1906 contact = argv[4];
1907 acl_type = argv[5];
1908 acl_id = *(int *)argv[6];
1909 memacl_type = argv[7];
1910 memacl_id = *(int *)argv[8];
1911 entity = cl->entity;
1912 who = cl->client_id;
1913
1914 EXEC SQL SELECT name INTO :name
1915 FROM containers
1916 WHERE cnt_id = :cnt_id;
1917
1918 /* trim off the trailing spaces */
1919 strcpy(name, strtrim(name));
1920
751f43da 1921 qual = xmalloc(MAX_FIELD_WIDTH);
1922 sprintf(qual, "name = '%s'", name);
1923 incremental_before(CONTAINERS_TABLE, qual, argv);
1924
2fb668b0 1925 /* if we are renaming the container */
1926 if (strcmp(name, newname))
1927 {
1928 /* make sure that only the name part of the name has been changed */
1929 tmpchar = strrchr(name, '/');
1930 /* not a top-level name */
1931 if (tmpchar)
1932 {
1933 cnt = tmpchar - name + 1;
1934 /* the parent part of the old and new name should be identical */
1935 if (strncmp(name, newname, cnt))
1936 return MR_NEW_CONTAINER_NAME;
1937 }
1938 /* top level name, new name should be a top level name too */
1939 else
1940 {
1941 if (strrchr(newname, '/'))
1942 return MR_NEW_CONTAINER_NAME;
1943 }
1944
1945 /* update the name for this container */
1946 EXEC SQL UPDATE containers
1947 SET name = :newname
1948 WHERE cnt_id = :cnt_id;
1949
1950 if (dbms_errno)
1951 return mr_errcode;
1952
1953 /* get names for its child containers */
1954 EXEC SQL DECLARE csr_ucon CURSOR FOR
1955 SELECT name, cnt_id FROM containers WHERE name LIKE :name || '/' || '%';
1956
1957 EXEC SQL OPEN csr_ucon;
1958 if (dbms_errno)
1959 return mr_errcode;
1960
1961 while (1)
1962 {
1963 EXEC SQL FETCH csr_ucon INTO :childname, :childid;
1964 if (sqlca.sqlcode)
1965 break;
d16a0628 1966
1967 strcpy(childname, strtrim(childname));
2fb668b0 1968 /* concatenate the new parent name with the existing sub-container name
1969 * we get the sub-containers new name */
1970 tmpchar = childname + strlen(name);
1971 strcpy(newchildname, newname);
1972 strcat(newchildname, tmpchar);
1973
1974 /* update the name */
1975 EXEC SQL UPDATE containers
1976 SET name = :newchildname, modtime = SYSDATE, modby = :who, modwith = :entity
1977 WHERE cnt_id = :childid;
1978
1979 if (sqlca.sqlcode)
1980 break;
1981 }
1982
1983 EXEC SQL CLOSE csr_ucon;
1984 if (dbms_errno)
1985 return mr_errcode;
1986 }
1987
1988 /* update the remaining fields */
1989 EXEC SQL UPDATE containers
1990 SET description = NVL(:description, CHR(0)), location = NVL(:location, CHR(0)),
1991 contact = NVL(:contact, CHR(0)), acl_type = :acl_type, acl_id = :acl_id,
1992 memacl_type = :memacl_type, memacl_id = :memacl_id,
1993 modtime = SYSDATE, modby = :who, modwith = :entity
1994 WHERE cnt_id = :cnt_id;
1995
1996 if (dbms_errno)
1997 return mr_errcode;
bb5c9457 1998
1999 sprintf(qual, "name = '%s'", newname);
2000 incremental_after(CONTAINERS_TABLE, qual, argv);
2fb668b0 2001
2002 return MR_SUCCESS;
2003}
2004
2005int get_machines_of_container(struct query *q, char *argv[], client *cl,
2006 int (*action)(int, char *[], void *), void *actarg)
2007{
2008 EXEC SQL BEGIN DECLARE SECTION;
2009 int cnt_id, isrecursive;
2010 char machinename[MACHINE_NAME_SIZE], containername[CONTAINERS_NAME_SIZE];
2011 char *qs;
2012 EXEC SQL END DECLARE SECTION;
2013
2014 char querystring[512], tmp [256];
2015 char *rargv[2];
2016 int found = 0;
2017
2018 rargv[0] = machinename;
2019 rargv[1] = containername;
2020
2021 cnt_id = *(int *)argv[0];
faf44ced 2022 isrecursive = atoi(argv[1]);
2fb668b0 2023
2024 /* get the container name */
2025
2026 EXEC SQL SELECT name INTO :containername
2027 FROM containers
2028 WHERE cnt_id = :cnt_id;
2029
2030 /* trim off the trailing spaces */
2031 strcpy(containername, strtrim(containername));
2032
2033 strcpy(querystring, "SELECT a.name, b.name FROM machine a, containers b, mcntmap c ");
2034 strcat(querystring, "WHERE a.mach_id = c.mach_id AND b.cnt_id = c.cnt_id ");
2035
2036 if (!isrecursive)
2037 sprintf(tmp, "AND b.cnt_id = %d ", cnt_id);
2038 else
2039 sprintf(tmp, "AND (b.cnt_id = %d OR b.name LIKE '%s/%%') ", cnt_id, containername);
2040
2041 strcat(querystring, tmp);
2042 strcat(querystring, "ORDER BY b.name, a.name");
2043
2044 qs = querystring;
2045
2046 EXEC SQL PREPARE stmt FROM :qs;
2047 if (sqlca.sqlcode)
2048 return MR_INTERNAL;
2049 EXEC SQL DECLARE curs_gmnm CURSOR FOR stmt;
2050 EXEC SQL OPEN curs_gmnm;
2051
2052 while (1)
2053 {
2054 EXEC SQL FETCH curs_gmnm INTO :machinename, :containername;
2055 if (sqlca.sqlcode)
2056 break;
2057 (*action)(2, rargv, actarg);
2058 found++;
2059 }
2060
2061 EXEC SQL CLOSE curs_gmnm;
2062 if (!found)
2063 return MR_NO_MATCH;
2064 return MR_SUCCESS;
2065}
2066
2067int get_subcontainers_of_container(struct query *q, char *argv[], client *cl,
2068 int (*action)(int, char *[], void *), void *actarg)
2069{
2070 EXEC SQL BEGIN DECLARE SECTION;
2071 int cnt_id, isrecursive;
2072 char containername[CONTAINERS_NAME_SIZE], subcontainername[CONTAINERS_NAME_SIZE];
2073 char *qs;
2074 EXEC SQL END DECLARE SECTION;
2075
8d7d8af0 2076 char querystring[2048], tmp [1024];
2fb668b0 2077 char *rargv[1];
2078 int found = 0;
2079
2080 rargv[0] = subcontainername;
2081
2082 cnt_id = *(int *)argv[0];
faf44ced 2083 isrecursive = atoi(argv[1]);
2fb668b0 2084
2085 /* get the container name */
2086
2087 EXEC SQL SELECT name INTO :containername
2088 FROM containers
2089 WHERE cnt_id = :cnt_id;
2090
2091 /* trim off the trailing spaces */
2092 strcpy(containername, strtrim(containername));
2093
2094 strcpy(querystring, "SELECT name FROM containers ");
2095
2096 if (!isrecursive)
2097 sprintf(tmp, "WHERE name LIKE '%s/%%' and name NOT LIKE '%s/%%/%%' ", containername,
2098 containername);
2099 else
2100 sprintf(tmp, "WHERE name LIKE '%s/%%' ", containername);
2101
2102 strcat(querystring, tmp);
2103 strcat(querystring, "ORDER BY name");
2104
2105 qs = querystring;
2106
2107 EXEC SQL PREPARE stmt FROM :qs;
2108 if (sqlca.sqlcode)
2109 return MR_INTERNAL;
2110 EXEC SQL DECLARE curs_gsoc CURSOR FOR stmt;
2111 EXEC SQL OPEN curs_gsoc;
2112
2113 while (1)
2114 {
2115 EXEC SQL FETCH curs_gsoc INTO :subcontainername;
2116 if (sqlca.sqlcode)
2117 break;
2118 (*action)(1, rargv, actarg);
2119 found++;
2120 }
2121
2122 EXEC SQL CLOSE curs_gsoc;
2123 if (!found)
2124 return MR_NO_MATCH;
2125 return MR_SUCCESS;
2126}
This page took 0.452854 seconds and 5 git commands to generate.