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