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

Revision 25198, 61.4 KB checked in by jdreed, 13 years ago (diff)
In moira: * Snapshot moira@r4042 (6/28/11) * Update version number to include moira revision number
Line 
1/* $Id: qsupport.pc 4017 2010-08-26 19:09:55Z 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 4017 2010-08-26 19:09:55Z 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    3072
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 (!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  long 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)
1052        return mr_errcode;
1053      EXEC SQL OPEN csr108q;
1054      if (dbms_errno)
1055        return mr_errcode;
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)
1071        found++;
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)
1099        found++;
1100    }
1101
1102  sq_destroy(sq);
1103  if (dbms_errno)
1104    return mr_errcode;
1105  if (!found)
1106    return MR_NO_MATCH;
1107  return MR_SUCCESS;
1108}
1109
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  long 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
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
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.
1263 */
1264
1265int get_lists_of_member(struct query *q, char *argv[], client *cl,
1266                        int (*action)(int, char *[], void *), void *actarg)
1267{
1268  int found = 0, direct = 1;
1269  char *rargv[6];
1270  EXEC SQL BEGIN DECLARE SECTION;
1271  char *atype;
1272  int aid;
1273  char name[LIST_NAME_SIZE];
1274  char active[5], public[5], hidden[5], maillist[5], grouplist[5];
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;
1283    }
1284  if (!strcmp(atype, "RUSER"))
1285    {
1286      atype = "USER";
1287      direct = 0;
1288    }
1289  if (!strcmp(atype, "RSTRING"))
1290    {
1291      atype = "STRING";
1292      direct = 0;
1293    }
1294  if (!strcmp(atype, "RKERBEROS"))
1295    {
1296      atype = "KERBEROS";
1297      direct = 0;
1298    }
1299  if (!strcmp(atype, "RMACHINE"))
1300    {
1301      atype = "MACHINE";
1302      direct = 0;
1303    }
1304
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++;
1331        }
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++;
1354        }
1355      EXEC SQL CLOSE csr117b;
1356    }
1357
1358  if (dbms_errno)
1359    return mr_errcode;
1360  if (!found)
1361    return MR_NO_MATCH;
1362  return MR_SUCCESS;
1363}
1364
1365
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" };
1373
1374int qualified_get_lists(struct query *q, char *argv[], client *cl,
1375                        int (*action)(int, char *[], void *), void *actarg)
1376{
1377  return qualified_get(q, argv, action, actarg, "l.list_id != 0",
1378                       "l", "name", lflags);
1379}
1380
1381
1382int get_members_of_list(struct query *q, char *argv[], client *cl,
1383                        int (*action)(int, char *[], void *), void *actarg)
1384{
1385  EXEC SQL BEGIN DECLARE SECTION;
1386  int list_id, direct;
1387  char member_name[MAX_FIELD_WIDTH], tag[STRINGS_STRING_SIZE];
1388  EXEC SQL END DECLARE SECTION;
1389  char *targv[3];
1390  int targc;
1391
1392  /* For gmol or gtml, only get direct members. For geml, get all. */
1393  if (!strcmp(q->shortname, "geml"))
1394    direct = -1;
1395  else
1396    direct = 0;
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;
1401  else
1402    targc = 2;
1403
1404  list_id = *(int *)argv[0];
1405
1406  targv[1] = member_name;
1407  targv[2] = tag;
1408
1409  targv[0] = "USER";
1410  EXEC SQL DECLARE csr119 CURSOR FOR
1411    SELECT u.login, s.string FROM users u, imembers im, strings s
1412    WHERE im.list_id = :list_id AND im.member_type = 'USER'
1413    AND im.member_id = u.users_id AND im.direct > :direct
1414    AND s.string_id = im.tag ORDER BY 1;
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    {
1422      EXEC SQL FETCH csr119 INTO :member_name, :tag;
1423      if (sqlca.sqlcode)
1424        break;
1425      (*action)(targc, targv, actarg);
1426    }
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
1433    SELECT l.name, s.string FROM list l, imembers im, strings s
1434    WHERE im.list_id = :list_id AND im.member_type = 'LIST'
1435    AND im.member_id = l.list_id AND im.direct > :direct
1436    AND s.string_id = im.tag ORDER BY 1;
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    {
1444      EXEC SQL FETCH csr120 INTO :member_name, :tag;
1445      if (sqlca.sqlcode)
1446        break;
1447      (*action)(targc, targv, actarg);
1448    }
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
1455    SELECT str.string, s.string FROM strings str, imembers im, strings s
1456    WHERE im.list_id = :list_id AND im.member_type = 'STRING'
1457    AND im.member_id = str.string_id AND im.direct > :direct
1458    AND s.string_id = im.tag ORDER BY 1;
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    {
1466      EXEC SQL FETCH csr121 INTO :member_name, :tag;
1467      if (sqlca.sqlcode)
1468        break;
1469      (*action)(targc, targv, actarg);
1470    }
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
1477    SELECT str.string, s.string FROM strings str, imembers im, strings s
1478    WHERE im.list_id = :list_id AND im.member_type = 'KERBEROS'
1479    AND im.member_id = str.string_id AND im.direct > :direct
1480    AND s.string_id = im.tag ORDER BY 1;
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    {
1488      EXEC SQL FETCH csr122 INTO :member_name, :tag;
1489      if (sqlca.sqlcode)
1490        break;
1491      (*action)(targc, targv, actarg);
1492    }
1493  EXEC SQL CLOSE csr122;
1494  if (dbms_errno)
1495    return mr_errcode;
1496
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
1519  return MR_SUCCESS;
1520}
1521
1522
1523/* count_members_of_list: this is a simple query, but it cannot be done
1524 * through the dispatch table.
1525 */
1526
1527int count_members_of_list(struct query *q, char *argv[], client *cl,
1528                          int (*action)(int, char *[], void *), void *actarg)
1529{
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;
1544}
1545
1546
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 */
1552
1553static char *sflags[3] = { "enable", "inprogress", "harderror" };
1554
1555int qualified_get_server(struct query *q, char *argv[], client *cl,
1556                         int (*action)(int, char *[], void *), void *actarg)
1557{
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 */
1562}
1563
1564
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
1572 */
1573
1574int qualified_get(struct query *q, char *argv[],
1575                  int (*action)(int, char *[], void *), void *actarg,
1576                  char *start, char *range, char *field, char *flags[])
1577{
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);
1594        }
1595    }
1596
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);
1600}
1601
1602
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.
1607 */
1608
1609static char *shflags[6] = { "service", "enable", "override", "success",
1610                            "inprogress", "hosterror" };
1611
1612int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
1613                             int (*action)(int, char *[], void *),
1614                             void *actarg)
1615{
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);
1632        }
1633    }
1634
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);
1638}
1639
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,
1643 * and is left as 2.  Arguments are: user's UID, new login name, and
1644 * pobox type ("POP" = POP, "IMAP" or numeric = IMAP, "EXCHANGE" = EXCHANGE)
1645 */
1646
1647int register_user(struct query *q, char **argv, client *cl)
1648{
1649  EXEC SQL BEGIN DECLARE SECTION;
1650  char *login, *entity;
1651  char directory[FILESYS_NAME_SIZE], machname[MACHINE_NAME_SIZE];
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;
1656  int po_exists = 0;
1657  static int m_id, def_quota, def_imap_quota, list_id, exchange_id;
1658  EXEC SQL END DECLARE SECTION;
1659  char buffer[256], *aargv[3];
1660
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';
1668
1669      EXEC SQL SELECT mach_id INTO :exchange_id FROM machine
1670        WHERE name = 'EXCHANGE.MIT.EDU';
1671    }
1672
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;
1677
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;
1682
1683  entity = cl->entity;
1684  who = cl->client_id;
1685
1686  uid = atoi(argv[0]);
1687  login = argv[1];
1688  potype = argv[2];
1689
1690  /* find user */
1691  EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1692    FROM users
1693    WHERE unix_uid = :uid AND
1694    (status = 0 OR status = 5 OR status = 6 OR status = 9);
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;
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
1717  EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1718    WHERE LOWER(name) = :login;
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
1724    WHERE label = :login;
1725  if (dbms_errno)
1726    return mr_errcode;
1727  if (rowcount > 0)
1728    return MR_IN_USE;
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;
1739      if ((ostatus == 0 || ostatus == 9) || (tmp != users_id))
1740        return MR_IN_USE;
1741      else
1742        po_exists = 1;
1743    }
1744  EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1745    WHERE ( name = :login OR name = :login || '.po' )
1746    AND type = 'FILESYS';
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
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
1760  /* choose type and location for pobox */
1761  if (!po_exists)
1762    {
1763      if (!strcmp(potype, "POP"))
1764        {
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');
1771          if (dbms_errno)
1772            return mr_errcode;
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        }         
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        }
1802      else
1803        {
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;
1817          if (dbms_errno)
1818            return mr_errcode;
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            }
1836
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,
1851             'USER', SYSDATE, :who, :entity);
1852
1853          if (dbms_errno)
1854            return mr_errcode;
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);
1876          if (dbms_errno)
1877            return mr_errcode;
1878
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        }
1887    }
1888 
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;
1893  if (ostatus == 5 || ostatus == 6 || ostatus == 9)
1894    nstatus = 1;
1895  EXEC SQL UPDATE users SET login = :login, status = :nstatus,
1896    modtime = SYSDATE, modby = :who, modwith = :entity,
1897    pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity,
1898    created = SYSDATE, creator = :who
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;
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 
1910  com_err(whoami, 0, "set login name to %s", login);
1911  incremental_after(USERS_TABLE, buffer, 0);
1912
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);
1921    }
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 */
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
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;
1969}
1970
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)
1983    int id, cnt;
1984{
1985  EXEC SQL BEGIN DECLARE SECTION;
1986  int iid = id, icnt = cnt;
1987  EXEC SQL END DECLARE SECTION;
1988
1989  EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1990    WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;
1991
1992  if (dbms_errno)
1993    return mr_errcode;
1994  return MR_SUCCESS;
1995}
1996
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}
2122
2123int update_container(struct query *q, char *argv[], client *cl)
2124{
2125  EXEC SQL BEGIN DECLARE SECTION;
2126  int cnt_id, acl_id, memacl_id, who, flag;
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];
2133  char *qual;
2134  int index = 0;
2135
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++];
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
2159   qual = xmalloc(MAX_FIELD_WIDTH);     
2160   sprintf(qual, "name = '%s'", name);
2161   incremental_before(CONTAINERS_TABLE, qual, argv);
2162
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;
2204
2205      strcpy(childname, strtrim(childname));
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 */
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  }
2245
2246  if (dbms_errno)
2247    return mr_errcode;
2248
2249  sprintf(qual, "name = '%s'", newname);
2250  incremental_after(CONTAINERS_TABLE, qual, argv);
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];
2272  isrecursive = atoi(argv[1]);
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
2289    sprintf(tmp, "AND (b.cnt_id = %d OR LOWER(b.name) LIKE LOWER('%s/%%')) ",
2290            cnt_id, containername);
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
2327  char querystring[2048], tmp [1024];
2328  char *rargv[1];
2329  int found = 0;
2330 
2331  rargv[0] = subcontainername;
2332
2333  cnt_id = *(int *)argv[0];
2334  isrecursive = atoi(argv[1]);
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)
2348    sprintf(tmp, "WHERE LOWER(name) LIKE LOWER('%s/%%') and LOWER(name) NOT LIKE LOWER('%s/%%/%%') ",
2349            containername, containername);
2350  else
2351    sprintf(tmp, "WHERE LOWER(name) LIKE LOWER('%s/%%') ", containername);
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}
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}
Note: See TracBrowser for help on using the repository browser.