source: trunk/third/moira/server/qsupport.pc @ 26024

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