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

Revision 24250, 61.4 KB checked in by broder, 14 years ago (diff)
New Moira snapshot from subversion. Sorry for the large diff - looks like all of the keywords changed in the SVN import process.
Line 
1/* $Id: qsupport.pc,v 2.54 2009-12-29 17:29:33 zacheiss Exp $
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("$Header: /afs/.athena.mit.edu/astaff/project/moiradev/repository/moira/server/qsupport.pc,v 2.54 2009-12-29 17:29:33 zacheiss Exp $");
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    OR (memacl_type = :type AND memacl_id = :id);
829  if (dbms_errno)
830    return mr_errcode;
831  EXEC SQL OPEN csr113;
832  if (dbms_errno)
833    return mr_errcode;
834  while (1)
835    {
836      EXEC SQL FETCH csr113 INTO :name;
837      if (sqlca.sqlcode)
838        break;
839      (*action)(2, rargv, actarg);
840      found++;
841    }
842  EXEC SQL CLOSE csr113;
843
844  rargv[0] = "SERVICE";
845  EXEC SQL DECLARE csr114 CURSOR FOR
846    SELECT name FROM servers
847    WHERE acl_type = :type AND acl_id = :id;
848  if (dbms_errno)
849    return mr_errcode;
850  EXEC SQL OPEN csr114;
851  if (dbms_errno)
852    return mr_errcode;
853  while (1)
854    {
855      EXEC SQL FETCH csr114 INTO :name;
856      if (sqlca.sqlcode)
857        break;
858      (*action)(2, rargv, actarg);
859      found++;
860    }
861  EXEC SQL CLOSE csr114;
862
863  rargv[0] = "HOSTACCESS";
864  EXEC SQL DECLARE csr115 CURSOR FOR
865    SELECT name FROM machine m, hostaccess ha
866    WHERE m.mach_id = ha.mach_id AND ha.acl_type = :type
867    AND ha.acl_id = :id;
868  if (dbms_errno)
869    return mr_errcode;
870  EXEC SQL OPEN csr115;
871  if (dbms_errno)
872    return mr_errcode;
873  while (1)
874    {
875      EXEC SQL FETCH csr115 INTO :name;
876      if (sqlca.sqlcode)
877        break;
878      (*action)(2, rargv, actarg);
879      found++;
880    }
881  EXEC SQL CLOSE csr115;
882
883  rargv[0] = "MACHINE";
884  EXEC SQL DECLARE csr115a CURSOR FOR
885    SELECT name FROM machine m
886    WHERE m.owner_type = :type
887    AND m.owner_id = :id;
888  if (dbms_errno)
889    return mr_errcode;
890  EXEC SQL OPEN csr115a;
891  if (dbms_errno)
892    return mr_errcode;
893  while (1)
894    {
895      EXEC SQL FETCH csr115a INTO :name;
896      if (sqlca.sqlcode)
897        break;
898      (*action)(2, rargv, actarg);
899      found++;
900    }
901  EXEC SQL CLOSE csr115a;
902
903  rargv[0] = "ZEPHYR";
904  EXEC SQL DECLARE csr116 CURSOR FOR
905    SELECT class FROM zephyr z
906    WHERE z.xmt_type = :type AND z.xmt_id = :id
907    OR z.sub_type = :type AND z.sub_id = :id
908    OR z.iws_type = :type AND z.iws_id = :id
909    OR z.iui_type = :type AND z.iui_id = :id
910    OR z.owner_type = :type AND z.owner_id = :id;
911  if (dbms_errno)
912    return mr_errcode;
913  EXEC SQL OPEN csr116;
914  if (dbms_errno)
915    return mr_errcode;
916  while (1)
917    {
918      EXEC SQL FETCH csr116 INTO :name;
919      if (sqlca.sqlcode)
920        break;
921      (*action)(2, rargv, actarg);
922      found++;
923    }
924  EXEC SQL CLOSE csr116;
925
926  rargv[0] = "CONTAINER";
927  EXEC SQL DECLARE csr117c CURSOR FOR
928    SELECT name FROM containers c
929    WHERE c.acl_type = :type AND c.acl_id = :id;
930  if (dbms_errno)
931    return mr_errcode;
932  EXEC SQL OPEN csr117c;
933  while (1)
934    {
935      EXEC SQL FETCH csr117c INTO :name;
936      if (sqlca.sqlcode)
937        break;
938      (*action)(2, rargv, actarg);
939      found++;
940    }
941  EXEC SQL CLOSE csr117c;
942
943  rargv[0] = "CONTAINER-MEMACL";
944  EXEC SQL DECLARE csr117d CURSOR FOR
945    SELECT name FROM containers c
946    WHERE c.memacl_type = :type AND c.memacl_id = :id;
947  if (dbms_errno)
948    return mr_errcode;
949  EXEC SQL OPEN csr117d;
950  while (1)
951    {
952      EXEC SQL FETCH csr117d INTO :name;
953      if (sqlca.sqlcode)
954        break;
955      (*action)(2, rargv, actarg);
956      found++;
957    }
958  EXEC SQL CLOSE csr117d;
959
960  if (!found)
961    return MR_NO_MATCH;
962  return MR_SUCCESS;
963}
964
965/* ghbo_internal */
966int ghbo_internal(char *atype, int aid,
967                  int (*action)(int, char *[], void *), void *actarg)
968{
969  char *rargv[1];
970  int found = 0;
971  EXEC SQL BEGIN DECLARE SECTION;
972  char name[MACHINE_NAME_SIZE], *type = atype;
973  int id = aid;
974  EXEC SQL END DECLARE SECTION;
975
976  rargv[0] = name;
977  EXEC SQL DECLARE csr115b CURSOR FOR
978    SELECT name FROM machine m
979    WHERE m.owner_type = :type
980    AND m.owner_id = :id;
981  if (dbms_errno)
982    return mr_errcode;
983  EXEC SQL OPEN csr115b;
984  if (dbms_errno)
985    return mr_errcode;
986  while (1)
987    {
988      EXEC SQL FETCH csr115b INTO :name;
989      if (sqlca.sqlcode)
990        break;
991      (*action)(1, rargv, actarg);
992      found++;
993    }
994  EXEC SQL CLOSE csr115b;
995 
996  if (!found)
997    return MR_NO_MATCH;
998  return MR_SUCCESS;
999}
1000
1001/* get_host_by_owner - like gaus but limited to hosts */
1002int get_host_by_owner(struct query *q, char *argv[], client *cl,
1003                      int (*action)(int, char *[], void *), void *actarg)
1004{
1005  int found = 0;
1006  EXEC SQL BEGIN DECLARE SECTION;
1007  char *atype;
1008  long aid, listid, id;
1009  EXEC SQL END DECLARE SECTION;
1010  struct save_queue *sq;
1011
1012  atype = argv[0];
1013  aid = *(int *)argv[1];
1014  if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
1015      !strcmp(atype, "KERBEROS"))
1016    return ghbo_internal(atype, aid, action, actarg);
1017
1018  sq = sq_create();
1019  if (!strcmp(atype, "RLIST"))
1020    {
1021      sq_save_data(sq, (void *)aid);
1022      /* get all the list_id's of containing lists */
1023      EXEC SQL DECLARE csr107q CURSOR FOR
1024        SELECT list_id FROM imembers
1025        WHERE member_type = 'LIST' AND member_id = :aid;
1026      if (dbms_errno)
1027        return mr_errcode;
1028      EXEC SQL OPEN csr107q;
1029      if (dbms_errno)
1030        return mr_errcode;
1031      while (1)
1032        {
1033          EXEC SQL FETCH csr107q INTO :listid;
1034          if (sqlca.sqlcode)
1035            break;
1036          sq_save_unique_data(sq, (void *)listid);
1037        }
1038      EXEC SQL CLOSE csr107q;
1039      /* now process each one */
1040      while (sq_get_data(sq, &id))
1041        {
1042          if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
1043            found++;
1044        }
1045    }
1046
1047  if (!strcmp(atype, "RUSER"))
1048    {
1049      EXEC SQL DECLARE csr108q CURSOR FOR
1050        SELECT list_id FROM imembers
1051        WHERE member_type = 'USER' AND member_id = :aid;
1052      if (dbms_errno)
1053        return mr_errcode;
1054      EXEC SQL OPEN csr108q;
1055      if (dbms_errno)
1056        return mr_errcode;
1057      while (1)
1058        {
1059          EXEC SQL FETCH csr108q INTO :listid;
1060          if (sqlca.sqlcode)
1061            break;
1062          sq_save_data(sq, (void *)listid);
1063        }
1064      EXEC SQL CLOSE csr108q;
1065      /* now process each one */
1066      while (sq_get_data(sq, &id))
1067        {
1068          if (ghbo_internal("LIST", id, action, actarg) == MR_SUCCESS)
1069            found++;
1070        }
1071      if (ghbo_internal("USER", aid, action, actarg) == MR_SUCCESS)
1072        found++;
1073    }
1074
1075  if (!strcmp(atype, "RKERBEROS"))
1076    {
1077      EXEC SQL DECLARE csr109q CURSOR FOR
1078        SELECT list_id FROM imembers
1079        WHERE member_type = 'KERBEROS' AND member_id = :aid;
1080      if (dbms_errno)
1081        return mr_errcode;
1082      EXEC SQL OPEN csr109q;
1083      if (dbms_errno)
1084        return mr_errcode;
1085      while (1)
1086        {
1087          EXEC SQL FETCH csr109q INTO :listid;
1088          if (sqlca.sqlcode)
1089            break;
1090          sq_save_data(sq, (void *)listid);
1091        }
1092      EXEC SQL CLOSE csr109q;
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("KERBEROS", aid, action, actarg) == MR_SUCCESS)
1100        found++;
1101    }
1102
1103  sq_destroy(sq);
1104  if (dbms_errno)
1105    return mr_errcode;
1106  if (!found)
1107    return MR_NO_MATCH;
1108  return MR_SUCCESS;
1109}
1110
1111int guas_internal(char *atype, int aid,
1112                  int (*action)(int, char *[], void *), void *actarg)
1113{
1114  char *rargv[1];
1115  int found = 0;
1116  EXEC SQL BEGIN DECLARE SECTION;
1117  char login[USERS_LOGIN_SIZE], *type = atype;
1118  int id = aid;
1119  EXEC SQL END DECLARE SECTION;
1120
1121  rargv[0] = login;
1122  EXEC SQL DECLARE csr115sp CURSOR FOR
1123    SELECT login FROM users u
1124    WHERE u.sponsor_type = :type
1125    AND u.sponsor_id = :id;
1126  if (dbms_errno)
1127    return mr_errcode;
1128  EXEC SQL OPEN csr115sp;
1129  if (dbms_errno)
1130    return mr_errcode;
1131  while (1)
1132    {
1133      EXEC SQL FETCH csr115sp INTO :login;
1134      if (sqlca.sqlcode)
1135        break;
1136      (*action)(1, rargv, actarg);
1137      found++;
1138    }
1139  EXEC SQL CLOSE csr115sp;
1140 
1141  if (!found)
1142    return MR_NO_MATCH;
1143  return MR_SUCCESS;
1144}
1145
1146/* get_user_account_by_sponsor - like gaus but limited to user accounts */
1147int get_user_account_by_sponsor(struct query *q, char *argv[], client *cl,
1148                                int (*action)(int, char *[], void *),
1149                                void *actarg)
1150{
1151  int found = 0;
1152  EXEC SQL BEGIN DECLARE SECTION;
1153  char *atype;
1154  long aid, listid, id;
1155  EXEC SQL END DECLARE SECTION;
1156  struct save_queue *sq;
1157
1158  atype = argv[0];
1159  aid = *(int *)argv[1];
1160  if (!strcmp(atype, "LIST") || !strcmp(atype, "USER") ||
1161      !strcmp(atype, "KERBEROS"))
1162    return guas_internal(atype, aid, action, actarg);
1163
1164  sq = sq_create();
1165  if (!strcmp(atype, "RLIST"))
1166    {
1167      sq_save_data(sq, (void *)aid);
1168      /* get all the list_id's of containing lists */
1169      EXEC SQL DECLARE csr107sp CURSOR FOR
1170        SELECT list_id FROM imembers
1171        WHERE member_type = 'LIST' AND member_id = :aid;
1172      if (dbms_errno)
1173        return mr_errcode;
1174      EXEC SQL OPEN csr107sp;
1175      if (dbms_errno)
1176        return mr_errcode;
1177      while (1)
1178        {
1179          EXEC SQL FETCH csr107sp INTO :listid;
1180          if (sqlca.sqlcode)
1181            break;
1182          sq_save_unique_data(sq, (void *)listid);
1183        }
1184      EXEC SQL CLOSE csr107sp;
1185      /* now process each one */
1186      while (sq_get_data(sq, &id))
1187        {
1188          if (guas_internal("LIST", id, action, actarg) == MR_SUCCESS)
1189            found++;
1190        }
1191    }
1192
1193  if (!strcmp(atype, "RUSER"))
1194    {
1195      EXEC SQL DECLARE csr108sp CURSOR FOR
1196        SELECT list_id FROM imembers
1197        WHERE member_type = 'USER' AND member_id = :aid;
1198      if (dbms_errno)
1199        return mr_errcode;
1200      EXEC SQL OPEN csr108sp;
1201      if (dbms_errno)
1202        return mr_errcode;
1203      while (1)
1204        {
1205          EXEC SQL FETCH csr108sp INTO :listid;
1206          if (sqlca.sqlcode)
1207            break;
1208          sq_save_data(sq, (void *)listid);
1209        }
1210      EXEC SQL CLOSE csr108sp;
1211      /* now process each one */
1212      while (sq_get_data(sq, &id))
1213        {
1214          if (guas_internal("LIST", id, action, actarg) == MR_SUCCESS)
1215            found++;
1216        }
1217      if (guas_internal("USER", aid, action, actarg) == MR_SUCCESS)
1218        found++;
1219    }
1220
1221  if (!strcmp(atype, "RKERBEROS"))
1222    {
1223      EXEC SQL DECLARE csr109sp CURSOR FOR
1224        SELECT list_id FROM imembers
1225        WHERE member_type = 'KERBEROS' AND member_id = :aid;
1226      if (dbms_errno)
1227        return mr_errcode;
1228      EXEC SQL OPEN csr109sp;
1229      if (dbms_errno)
1230        return mr_errcode;
1231      while (1)
1232        {
1233          EXEC SQL FETCH csr109sp INTO :listid;
1234          if (sqlca.sqlcode)
1235            break;
1236          sq_save_data(sq, (void *)listid);
1237        }
1238      EXEC SQL CLOSE csr109sp;
1239      /* now process each one */
1240      while (sq_get_data(sq, &id))
1241        {
1242          if (guas_internal("LIST", id, action, actarg) == MR_SUCCESS)
1243            found++;
1244        }
1245      if (guas_internal("KERBEROS", aid, action, actarg) == MR_SUCCESS)
1246        found++;
1247    }
1248
1249  sq_destroy(sq);
1250  if (dbms_errno)
1251    return mr_errcode;
1252  if (!found)
1253    return MR_NO_MATCH;
1254  return MR_SUCCESS;
1255}
1256
1257/* get_lists_of_member - given a type and a name, return the name and flags
1258 * of all of the lists of the given member.  The member_type is one of
1259 * "LIST", "USER", "STRING", "KERBEROS", "MACHINE", "RLIST", "RUSER",
1260 * "RSTRING", "RKERBEROS", or "RMACHINE" in argv[0], and argv[1] will contain
1261 * the ID of the entity in question.  The R* types mean to recursively look
1262 * at every containing list, not just when the object in question is a direct
1263 * member.
1264 */
1265
1266int get_lists_of_member(struct query *q, char *argv[], client *cl,
1267                        int (*action)(int, char *[], void *), void *actarg)
1268{
1269  int found = 0, direct = 1;
1270  char *rargv[6];
1271  EXEC SQL BEGIN DECLARE SECTION;
1272  char *atype;
1273  int aid;
1274  char name[LIST_NAME_SIZE];
1275  char active[5], public[5], hidden[5], maillist[5], grouplist[5];
1276  EXEC SQL END DECLARE SECTION;
1277
1278  atype = argv[0];
1279  aid = *(int *)argv[1];
1280  if (!strcmp(atype, "RLIST"))
1281    {
1282      atype = "LIST";
1283      direct = 0;
1284    }
1285  if (!strcmp(atype, "RUSER"))
1286    {
1287      atype = "USER";
1288      direct = 0;
1289    }
1290  if (!strcmp(atype, "RSTRING"))
1291    {
1292      atype = "STRING";
1293      direct = 0;
1294    }
1295  if (!strcmp(atype, "RKERBEROS"))
1296    {
1297      atype = "KERBEROS";
1298      direct = 0;
1299    }
1300  if (!strcmp(atype, "RMACHINE"))
1301    {
1302      atype = "MACHINE";
1303      direct = 0;
1304    }
1305
1306  rargv[0] = name;
1307  rargv[1] = active;
1308  rargv[2] = public;
1309  rargv[3] = hidden;
1310  rargv[4] = maillist;
1311  rargv[5] = grouplist;
1312  if (direct)
1313    {
1314      EXEC SQL DECLARE csr117a CURSOR FOR
1315        SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1316        FROM list l, imembers im
1317        WHERE l.list_id = im.list_id AND im.direct = 1
1318        AND im.member_type = :atype AND im.member_id = :aid;
1319      if (dbms_errno)
1320        return mr_errcode;
1321      EXEC SQL OPEN csr117a;
1322      if (dbms_errno)
1323        return mr_errcode;
1324      while (1)
1325        {
1326          EXEC SQL FETCH csr117a
1327            INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1328          if (sqlca.sqlcode)
1329            break;
1330          (*action)(6, rargv, actarg);
1331          found++;
1332        }
1333      EXEC SQL CLOSE csr117a;
1334    }
1335  else
1336    {
1337      EXEC SQL DECLARE csr117b CURSOR FOR
1338        SELECT l.name, l.active, l.publicflg, l.hidden, l.maillist, l.grouplist
1339        FROM list l, imembers im
1340        WHERE l.list_id = im.list_id
1341        AND im.member_type = :atype AND im.member_id = :aid;
1342      if (dbms_errno)
1343        return mr_errcode;
1344      EXEC SQL OPEN csr117b;
1345      if (dbms_errno)
1346        return mr_errcode;
1347      while (1)
1348        {
1349          EXEC SQL FETCH csr117b
1350            INTO :name, :active, :public, :hidden, :maillist, :grouplist;
1351          if (sqlca.sqlcode)
1352            break;
1353          (*action)(6, rargv, actarg);
1354          found++;
1355        }
1356      EXEC SQL CLOSE csr117b;
1357    }
1358
1359  if (dbms_errno)
1360    return mr_errcode;
1361  if (!found)
1362    return MR_NO_MATCH;
1363  return MR_SUCCESS;
1364}
1365
1366
1367/* qualified_get_lists: passed "TRUE", "FALSE", or "DONTCARE" for each of
1368 * the five flags associated with each list.  It will return the name of
1369 * each list that meets the quailifications.  It does this by building a
1370 * where clause based on the arguments, then doing a retrieve.
1371 */
1372
1373static char *lflags[5] = { "active", "publicflg", "hidden", "maillist", "grouplist" };
1374
1375int qualified_get_lists(struct query *q, char *argv[], client *cl,
1376                        int (*action)(int, char *[], void *), void *actarg)
1377{
1378  return qualified_get(q, argv, action, actarg, "l.list_id != 0",
1379                       "l", "name", lflags);
1380}
1381
1382
1383int get_members_of_list(struct query *q, char *argv[], client *cl,
1384                        int (*action)(int, char *[], void *), void *actarg)
1385{
1386  EXEC SQL BEGIN DECLARE SECTION;
1387  int list_id, direct;
1388  char member_name[MAX_FIELD_WIDTH], tag[STRINGS_STRING_SIZE];
1389  EXEC SQL END DECLARE SECTION;
1390  char *targv[3];
1391  int targc;
1392
1393  /* For gmol or gtml, only get direct members. For geml, get all. */
1394  if (!strcmp(q->shortname, "geml"))
1395    direct = -1;
1396  else
1397    direct = 0;
1398
1399  /* For gmol or geml, only return type and name. For gtml, return tag too. */
1400  if (!strcmp(q->shortname, "gtml"))
1401    targc = 3;
1402  else
1403    targc = 2;
1404
1405  list_id = *(int *)argv[0];
1406
1407  targv[1] = member_name;
1408  targv[2] = tag;
1409
1410  targv[0] = "USER";
1411  EXEC SQL DECLARE csr119 CURSOR FOR
1412    SELECT u.login, s.string FROM users u, imembers im, strings s
1413    WHERE im.list_id = :list_id AND im.member_type = 'USER'
1414    AND im.member_id = u.users_id AND im.direct > :direct
1415    AND s.string_id = im.tag ORDER BY 1;
1416  if (dbms_errno)
1417    return mr_errcode;
1418  EXEC SQL OPEN csr119;
1419  if (dbms_errno)
1420    return mr_errcode;
1421  while (1)
1422    {
1423      EXEC SQL FETCH csr119 INTO :member_name, :tag;
1424      if (sqlca.sqlcode)
1425        break;
1426      (*action)(targc, targv, actarg);
1427    }
1428  EXEC SQL CLOSE csr119;
1429  if (dbms_errno)
1430    return mr_errcode;
1431
1432  targv[0] = "LIST";
1433  EXEC SQL DECLARE csr120 CURSOR FOR
1434    SELECT l.name, s.string FROM list l, imembers im, strings s
1435    WHERE im.list_id = :list_id AND im.member_type = 'LIST'
1436    AND im.member_id = l.list_id AND im.direct > :direct
1437    AND s.string_id = im.tag ORDER BY 1;
1438  if (dbms_errno)
1439    return mr_errcode;
1440  EXEC SQL OPEN csr120;
1441  if (dbms_errno)
1442    return mr_errcode;
1443  while (1)
1444    {
1445      EXEC SQL FETCH csr120 INTO :member_name, :tag;
1446      if (sqlca.sqlcode)
1447        break;
1448      (*action)(targc, targv, actarg);
1449    }
1450  EXEC SQL CLOSE csr120;
1451  if (dbms_errno)
1452    return mr_errcode;
1453
1454  targv[0] = "STRING";
1455  EXEC SQL DECLARE csr121 CURSOR FOR
1456    SELECT str.string, s.string FROM strings str, imembers im, strings s
1457    WHERE im.list_id = :list_id AND im.member_type = 'STRING'
1458    AND im.member_id = str.string_id AND im.direct > :direct
1459    AND s.string_id = im.tag ORDER BY 1;
1460  if (dbms_errno)
1461    return mr_errcode;
1462  EXEC SQL OPEN csr121;
1463  if (dbms_errno)
1464    return mr_errcode;
1465  while (1)
1466    {
1467      EXEC SQL FETCH csr121 INTO :member_name, :tag;
1468      if (sqlca.sqlcode)
1469        break;
1470      (*action)(targc, targv, actarg);
1471    }
1472  EXEC SQL CLOSE csr121;
1473  if (dbms_errno)
1474    return mr_errcode;
1475
1476  targv[0] = "KERBEROS";
1477  EXEC SQL DECLARE csr122 CURSOR FOR
1478    SELECT str.string, s.string FROM strings str, imembers im, strings s
1479    WHERE im.list_id = :list_id AND im.member_type = 'KERBEROS'
1480    AND im.member_id = str.string_id AND im.direct > :direct
1481    AND s.string_id = im.tag ORDER BY 1;
1482  if (dbms_errno)
1483    return mr_errcode;
1484  EXEC SQL OPEN csr122;
1485  if (dbms_errno)
1486    return mr_errcode;
1487  while (1)
1488    {
1489      EXEC SQL FETCH csr122 INTO :member_name, :tag;
1490      if (sqlca.sqlcode)
1491        break;
1492      (*action)(targc, targv, actarg);
1493    }
1494  EXEC SQL CLOSE csr122;
1495  if (dbms_errno)
1496    return mr_errcode;
1497
1498  targv[0] = "MACHINE";
1499  EXEC SQL DECLARE csr123 CURSOR FOR
1500    SELECT m.name, s.string FROM machine m, imembers im, strings s
1501    WHERE im.list_id = :list_id AND im.member_type = 'MACHINE'
1502    AND im.member_id = m.mach_id AND im.direct > :direct
1503    AND s.string_id = im.tag ORDER BY 1;
1504  if (dbms_errno)
1505    return mr_errcode;
1506  EXEC SQL OPEN csr123;
1507  if (dbms_errno)
1508    return mr_errcode;
1509  while (1)
1510    {
1511      EXEC SQL FETCH csr123 INTO :member_name, :tag;
1512      if (sqlca.sqlcode)
1513        break;
1514      (*action)(targc, targv, actarg);
1515    }
1516  EXEC SQL CLOSE csr123;
1517  if (dbms_errno)
1518    return mr_errcode;
1519
1520  return MR_SUCCESS;
1521}
1522
1523
1524/* count_members_of_list: this is a simple query, but it cannot be done
1525 * through the dispatch table.
1526 */
1527
1528int count_members_of_list(struct query *q, char *argv[], client *cl,
1529                          int (*action)(int, char *[], void *), void *actarg)
1530{
1531  EXEC SQL BEGIN DECLARE SECTION;
1532  int  list, ct = 0;
1533  EXEC SQL END DECLARE SECTION;
1534  char *rargv[1], countbuf[5];
1535
1536  list = *(int *)argv[0];
1537  rargv[0] = countbuf;
1538  EXEC SQL SELECT count (*) INTO :ct FROM imembers
1539    WHERE list_id = :list AND direct = 1;
1540  if (dbms_errno)
1541    return mr_errcode;
1542  sprintf(countbuf, "%d", ct);
1543  (*action)(1, rargv, actarg);
1544  return MR_SUCCESS;
1545}
1546
1547
1548/* qualified_get_server: passed "TRUE", "FALSE", or "DONTCARE" for each of
1549 * the three flags associated with each service.  It will return the name of
1550 * each service that meets the quailifications.  It does this by building a
1551 * where clause based on the arguments, then doing a retrieve.
1552 */
1553
1554static char *sflags[3] = { "enable", "inprogress", "harderror" };
1555
1556int qualified_get_server(struct query *q, char *argv[], client *cl,
1557                         int (*action)(int, char *[], void *), void *actarg)
1558{
1559  return qualified_get(q, argv, action, actarg, "s.name is not null",
1560                       "s", "name", sflags);
1561  /* of course, name will never be null, but we need something there
1562     to make qualified_get happy */
1563}
1564
1565
1566/* generic qualified get routine, used by qualified_get_lists,
1567 * qualified_get_server, and qualified_get_serverhost.
1568 *   Args:
1569 *      start - a simple where clause, must not be empty
1570 *      range - the name of the range variable
1571 *      field - the field to return
1572 *      flags - an array of strings, names of the flag variables
1573 */
1574
1575int qualified_get(struct query *q, char *argv[],
1576                  int (*action)(int, char *[], void *), void *actarg,
1577                  char *start, char *range, char *field, char *flags[])
1578{
1579  char qual[256];
1580  int i;
1581  char buf[32];
1582
1583  strcpy(qual, start);
1584  for (i = 0; i < q->argc; i++)
1585    {
1586      if (!strcmp(argv[i], "TRUE"))
1587        {
1588          sprintf(buf, " AND %s.%s != 0", range, flags[i]);
1589          strcat(qual, buf);
1590        }
1591      else if (!strcmp(argv[i], "FALSE"))
1592        {
1593          sprintf(buf, " AND %s.%s = 0", range, flags[i]);
1594          strcat(qual, buf);
1595        }
1596    }
1597
1598  sprintf(stmt_buf, "SELECT %s.%s FROM %s %s WHERE %s", range, field,
1599          table_name[q->rtable], range, qual);
1600  return do_for_all_rows(stmt_buf, 1, action, actarg);
1601}
1602
1603
1604/* qualified_get_serverhost: passed "TRUE", "FALSE", or "DONTCARE" for each of
1605 * the five flags associated with each serverhost.  It will return the name of
1606 * each service and host that meets the quailifications.  It does this by
1607 * building a where clause based on the arguments, then doing a retrieve.
1608 */
1609
1610static char *shflags[6] = { "service", "enable", "override", "success",
1611                            "inprogress", "hosterror" };
1612
1613int qualified_get_serverhost(struct query *q, char *argv[], client *cl,
1614                             int (*action)(int, char *[], void *),
1615                             void *actarg)
1616{
1617  char qual[256], buf[32];
1618  int i;
1619
1620  sprintf(qual, "m.mach_id = sh.mach_id AND sh.service = UPPER('%s')",
1621          argv[0]);
1622  for (i = 1; i < q->argc; i++)
1623    {
1624      if (!strcmp(argv[i], "TRUE"))
1625        {
1626          sprintf(buf, " AND sh.%s != 0", shflags[i]);
1627          strcat(qual, buf);
1628        }
1629      else if (!strcmp(argv[i], "FALSE"))
1630        {
1631          sprintf(buf, " AND sh.%s = 0", shflags[i]);
1632          strcat(qual, buf);
1633        }
1634    }
1635
1636  sprintf(stmt_buf, "SELECT sh.service, m.name FROM serverhosts sh, "
1637          "machine m WHERE %s", qual);
1638  return do_for_all_rows(stmt_buf, 2, action, actarg);
1639}
1640
1641
1642/* register_user - change user's login name and allocate a pobox, group,
1643 * filesystem, and quota for them.  The user's status must start out as 0,
1644 * and is left as 2.  Arguments are: user's UID, new login name, and
1645 * pobox type ("POP" = POP, "IMAP" or numeric = IMAP, "EXCHANGE" = EXCHANGE)
1646 */
1647
1648int register_user(struct query *q, char **argv, client *cl)
1649{
1650  EXEC SQL BEGIN DECLARE SECTION;
1651  char *login, *entity;
1652  char directory[FILESYS_NAME_SIZE], machname[MACHINE_NAME_SIZE];
1653  char dir[NFSPHYS_DIR_SIZE], *potype;
1654  int who, rowcount, mid, uid, users_id;
1655  int ostatus, nstatus, fsidval, popid;
1656  int npid, tmp;
1657  int po_exists = 0;
1658  static int m_id, def_quota, def_imap_quota, list_id, exchange_id;
1659  EXEC SQL END DECLARE SECTION;
1660  char buffer[256], *aargv[3];
1661
1662  if (!m_id)
1663    {
1664      EXEC SQL SELECT list_id INTO :list_id FROM list
1665        WHERE name = 'wheel';
1666
1667      EXEC SQL SELECT mach_id INTO :m_id FROM machine
1668        WHERE name = 'ATHENA.MIT.EDU';
1669
1670      EXEC SQL SELECT mach_id INTO :exchange_id FROM machine
1671        WHERE name = 'EXCHANGE.MIT.EDU';
1672    }
1673
1674  EXEC SQL SELECT value INTO :def_quota FROM numvalues
1675    WHERE name = 'def_quota';
1676  if (sqlca.sqlerrd[2] != 1)
1677    return MR_NO_QUOTA;
1678
1679  EXEC SQL SELECT value INTO :def_imap_quota FROM numvalues
1680    WHERE name = 'def_imap_quota';
1681  if (sqlca.sqlerrd[2] != 1)
1682    return MR_NO_QUOTA;
1683
1684  entity = cl->entity;
1685  who = cl->client_id;
1686
1687  uid = atoi(argv[0]);
1688  login = argv[1];
1689  potype = argv[2];
1690
1691  /* find user */
1692  EXEC SQL SELECT users_id, status INTO :users_id, :ostatus
1693    FROM users
1694    WHERE unix_uid = :uid AND
1695    (status = 0 OR status = 5 OR status = 6 OR status = 9);
1696
1697  if (sqlca.sqlerrd[2] == 0)
1698    return MR_NO_MATCH;
1699  if (sqlca.sqlerrd[2] > 1)
1700    return MR_NOT_UNIQUE;
1701
1702  /* check new login name */
1703  EXEC SQL SELECT COUNT(login) INTO :rowcount FROM users
1704    WHERE login = :login AND users_id != :users_id;
1705  if (dbms_errno)
1706    return mr_errcode;
1707  if (rowcount > 0)
1708    return MR_IN_USE;
1709
1710  /* Remove this check when we're allowing username reuse again. */
1711  EXEC SQL SELECT COUNT(login) INTO :rowcount FROM userhistory
1712    WHERE login = :login;
1713  if (dbms_errno)
1714    return mr_errcode;
1715  if (rowcount > 0)
1716    return MR_IN_USE;
1717
1718  EXEC SQL SELECT COUNT(name) INTO :rowcount FROM list
1719    WHERE LOWER(name) = :login;
1720  if (dbms_errno)
1721    return mr_errcode;
1722  if (rowcount > 0)
1723    return MR_IN_USE;
1724  EXEC SQL SELECT COUNT(label) INTO :rowcount FROM filesys
1725    WHERE label = :login;
1726  if (dbms_errno)
1727    return mr_errcode;
1728  if (rowcount > 0)
1729    return MR_IN_USE;
1730  EXEC SQL SELECT COUNT(label) INTO :rowcount 
1731    FROM filesys WHERE label = :login || '.po';
1732  if (dbms_errno)
1733    return mr_errcode;
1734  if (rowcount > 0)
1735    {
1736      EXEC SQL SELECT owner INTO :tmp FROM filesys
1737        WHERE label = :login || '.po';
1738      if (dbms_errno)
1739        return mr_errcode;
1740      if ((ostatus == 0 || ostatus == 9) || (tmp != users_id))
1741        return MR_IN_USE;
1742      else
1743        po_exists = 1;
1744    }
1745  EXEC SQL SELECT COUNT(name) INTO :rowcount FROM alias
1746    WHERE ( name = :login OR name = :login || '.po' )
1747    AND type = 'FILESYS';
1748  if (dbms_errno)
1749    return mr_errcode;
1750  if (rowcount > 0)
1751    return MR_IN_USE;
1752  com_err(whoami, 0, "login name OK");
1753
1754  EXEC SQL SELECT COUNT(potype) INTO :rowcount FROM users WHERE
1755    login = :login AND potype = 'POP';
1756  if (dbms_errno)
1757    return mr_errcode;
1758  if (rowcount > 0)
1759    po_exists = 1;
1760
1761  /* choose type and location for pobox */
1762  if (!po_exists)
1763    {
1764      if (!strcmp(potype, "POP"))
1765        {
1766
1767          EXEC SQL DECLARE csr130 CURSOR FOR
1768            SELECT sh.mach_id, m.name FROM serverhosts sh, machine m
1769            WHERE sh.service = 'POP' AND sh.mach_id = m.mach_id
1770            AND sh.value2 - sh.value1 =
1771            (SELECT MAX(value2 - value1) FROM serverhosts WHERE service = 'POP');
1772          if (dbms_errno)
1773            return mr_errcode;
1774          EXEC SQL OPEN csr130;
1775          if (dbms_errno)
1776            return mr_errcode;
1777          EXEC SQL FETCH csr130 INTO :popid, :machname;
1778          if (sqlca.sqlerrd[2] == 0)
1779            {
1780              EXEC SQL CLOSE csr130;
1781              if (dbms_errno)
1782                return mr_errcode;
1783              return MR_NO_POBOX;
1784            }
1785          else
1786            {
1787              EXEC SQL CLOSE csr130;
1788              if (dbms_errno)
1789                return mr_errcode;
1790            }
1791     
1792          EXEC SQL UPDATE users SET potype = 'POP', pop_id = :popid
1793            WHERE users_id = :users_id;
1794          com_err(whoami, 0, "pobox set to POP:%s", strtrim(machname));
1795        }         
1796      else if (!strcmp(potype, "EXCHANGE"))
1797        {
1798          EXEC SQL UPDATE users SET potype = 'EXCHANGE',
1799            exchange_id = :exchange_id
1800            WHERE users_id = :users_id;
1801          com_err(whoami, 0, "pobox set to EXCHANGE:EXCHANGE.MIT.EDU");
1802        }
1803      else
1804        {
1805      /* Select all IMAP nfsphys entries in order of increasing
1806       * (allocated - partsize).  The partitions will almost always be
1807       * overallocated, but we choose the one that is the least
1808       * overallocated.
1809       */
1810          potype = "IMAP";
1811         
1812          EXEC SQL DECLARE csr_rusr_imap CURSOR FOR
1813            SELECT np.allocated - np.partsize, np.nfsphys_id, np.mach_id,
1814            np.dir, m.name FROM serverhosts sh, nfsphys np, machine m
1815            WHERE sh.service = 'POSTOFFICE' AND sh.mach_id = np.mach_id
1816            AND m.mach_id = np.mach_id
1817            ORDER BY 1;
1818          if (dbms_errno)
1819            return mr_errcode;
1820          EXEC SQL OPEN csr_rusr_imap;
1821          if (dbms_errno)
1822            return mr_errcode;
1823          EXEC SQL FETCH csr_rusr_imap INTO :tmp, :npid, :mid, :dir, :machname;
1824          if (sqlca.sqlerrd[2] == 0)
1825            {
1826              EXEC SQL CLOSE csr_rusr_imap;
1827              if (dbms_errno)
1828                return mr_errcode;
1829              return MR_NO_POBOX;
1830            }
1831          else
1832            {
1833              EXEC SQL CLOSE csr_rusr_imap;
1834              if (dbms_errno)
1835                return mr_errcode;
1836            }
1837
1838          /* create filesystem */
1839          if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1840            return MR_NO_ID;
1841          incremental_clear_before();
1842
1843          EXEC SQL SELECT value INTO :popid FROM numvalues
1844            WHERE numvalues.name = 'filsys_id';
1845          EXEC SQL INSERT INTO filesys
1846            (filsys_id, phys_id, label, type, mach_id, name,
1847             mount, rwaccess, comments, owner, owners, createflg,
1848             lockertype, modtime, modby, modwith)
1849            VALUES
1850            (:popid, :npid, :login || '.po', 'IMAP', :mid, :dir,
1851             CHR(0), 'w', 'IMAP box', :users_id, :list_id, 1,
1852             'USER', SYSDATE, :who, :entity);
1853
1854          if (dbms_errno)
1855            return mr_errcode;
1856          if (sqlca.sqlerrd[2] != 1)
1857            return MR_INTERNAL;
1858          sprintf(buffer, "fs.filsys_id = %d", popid);
1859          incremental_after(FILESYS_TABLE, buffer, 0);
1860
1861          /* set quota */
1862          incremental_clear_before();
1863          EXEC SQL INSERT INTO quota
1864            (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1865            VALUES (:users_id, :popid, 'USER', :def_imap_quota, :npid,
1866                    SYSDATE, :who, :entity);
1867          if (dbms_errno)
1868            return mr_errcode;
1869          if (sqlca.sqlerrd[2] != 1)
1870            return MR_INTERNAL;
1871          aargv[0] = login;
1872          aargv[1] = "USER";
1873          aargv[2] = login;
1874          sprintf(buffer, "q.entity_id = %d and q.filsys_id = %d and "
1875                  "q.type = 'USER'", users_id, popid);
1876          incremental_after(QUOTA_TABLE, buffer, aargv);
1877          if (dbms_errno)
1878            return mr_errcode;
1879
1880          EXEC SQL UPDATE nfsphys SET allocated = allocated + :def_imap_quota
1881            WHERE nfsphys_id = :npid;
1882
1883          EXEC SQL UPDATE users SET potype = 'IMAP', imap_id = :popid
1884            WHERE users_id = :users_id;
1885          com_err(whoami, 0, "pobox set to IMAP:%s:%s", strtrim(machname),
1886                  strtrim(dir));
1887        }
1888    }
1889 
1890  /* change login name, set pobox */
1891  sprintf(buffer, "u.users_id = %d", users_id);
1892  incremental_before(USERS_TABLE, buffer, 0);
1893  nstatus = 2;
1894  if (ostatus == 5 || ostatus == 6 || ostatus == 9)
1895    nstatus = 1;
1896  EXEC SQL UPDATE users SET login = :login, status = :nstatus,
1897    modtime = SYSDATE, modby = :who, modwith = :entity,
1898    pmodtime = SYSDATE, pmodby = :who, pmodwith = :entity,
1899    created = SYSDATE, creator = :who
1900    WHERE users_id = :users_id;
1901
1902  if (dbms_errno)
1903    return mr_errcode;
1904  if (sqlca.sqlerrd[2] != 1)
1905    return MR_INTERNAL;
1906 
1907  /* Only update usage count if we created a POP pobox. */
1908  if (!strcmp(potype, "POP") && !po_exists)
1909    set_pop_usage(mid, 1);
1910 
1911  com_err(whoami, 0, "set login name to %s", login);
1912  incremental_after(USERS_TABLE, buffer, 0);
1913
1914  /* create filesystem */
1915  if (set_next_object_id("filsys_id", FILESYS_TABLE, 0))
1916    return MR_NO_ID;
1917  incremental_clear_before();
1918  if (islower(login[0]) && islower(login[1]))
1919    {
1920      sprintf(directory, "/afs/athena.mit.edu/user/%c/%c/%s",
1921              login[0], login[1], login);
1922    }
1923  else
1924    sprintf(directory, "/afs/athena.mit.edu/user/other/%s", login);
1925
1926  EXEC SQL SELECT value INTO :fsidval FROM numvalues
1927    WHERE numvalues.name = 'filsys_id';
1928  EXEC SQL INSERT INTO filesys
1929    (filsys_id, phys_id, label, type, mach_id, name,
1930     mount, rwaccess, comments, owner, owners, createflg,
1931     lockertype, modtime, modby, modwith)
1932    VALUES
1933    (:fsidval, 0, :login, 'AFS', :m_id, :directory,
1934     '/mit/' || :login, 'w', 'User Locker', :users_id, :list_id, 1,
1935     'HOMEDIR', SYSDATE, :who, :entity);
1936
1937  if (dbms_errno)
1938    return mr_errcode;
1939  if (sqlca.sqlerrd[2] != 1)
1940    return MR_INTERNAL;
1941  sprintf(buffer, "fs.filsys_id = %d", fsidval);
1942  incremental_after(FILESYS_TABLE, buffer, 0);
1943
1944  /* set quota */
1945  incremental_clear_before();
1946  EXEC SQL INSERT INTO quota
1947    (entity_id, filsys_id, type, quota, phys_id, modtime, modby, modwith)
1948    VALUES (0, :fsidval, 'ANY', :def_quota, 0, SYSDATE, :who, :entity);
1949  if (dbms_errno)
1950    return mr_errcode;
1951  if (sqlca.sqlerrd[2] != 1)
1952    return MR_INTERNAL;
1953  aargv[0] = login;
1954  aargv[1] = "ANY";
1955  aargv[2] = login;
1956  sprintf(buffer, "q.entity_id = 0 and q.filsys_id = %d and q.type = 'ANY'",
1957          fsidval);
1958  incremental_after(QUOTA_TABLE, buffer, aargv);
1959  com_err(whoami, 0, "quota of %d assigned", def_quota);
1960  if (dbms_errno)
1961    return mr_errcode;
1962
1963  EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
1964    WHERE table_name = 'users';
1965  EXEC SQL UPDATE tblstats SET appends = appends + 1, modtime = SYSDATE
1966    WHERE table_name = 'filesys' OR table_name = 'quota';
1967  if (dbms_errno)
1968    return mr_errcode;
1969  return MR_SUCCESS;
1970}
1971
1972/** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
1973 **
1974 ** Inputs:
1975 **   id of machine
1976 **   delta (will be +/- 1)
1977 **
1978 ** Description:
1979 **   - incr/decr value field in serverhosts table for pop/mach_id
1980 **
1981 **/
1982
1983int set_pop_usage(id, cnt)
1984    int id, cnt;
1985{
1986  EXEC SQL BEGIN DECLARE SECTION;
1987  int iid = id, icnt = cnt;
1988  EXEC SQL END DECLARE SECTION;
1989
1990  EXEC SQL UPDATE serverhosts SET value1 = value1 + :icnt
1991    WHERE serverhosts.service = 'POP' AND serverhosts.mach_id = :iid;
1992
1993  if (dbms_errno)
1994    return mr_errcode;
1995  return MR_SUCCESS;
1996}
1997
1998
1999int do_user_reservation(struct query *q, char *argv[], client *cl)
2000{
2001  EXEC SQL BEGIN DECLARE SECTION;
2002  char resv[USERS_RESERVATIONS_SIZE];
2003  char *trans, name[ALIAS_NAME_SIZE];
2004  int uid;
2005  EXEC SQL END DECLARE SECTION;
2006
2007  uid = *(int *)argv[0];
2008  trans = argv[1];
2009
2010  EXEC SQL SELECT UPPER(name) INTO :name FROM alias
2011    WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
2012  if (dbms_errno)
2013    return mr_errcode;
2014  if (sqlca.sqlerrd[2] != 1)
2015    return MR_STRING;
2016  name[1] = '\0';
2017
2018  EXEC SQL SELECT reservations INTO :resv FROM users
2019    WHERE users_id = :uid;
2020  if (dbms_errno)
2021    return mr_errcode;
2022  strtrim(resv);
2023
2024  if (!strcmp(q->shortname, "aurv"))
2025    {
2026      if (strchr(resv, *name))
2027        return MR_EXISTS;
2028      if (strlen(resv) == USERS_RESERVATIONS_SIZE - 1)
2029        return MR_ARG_TOO_LONG;
2030
2031      strcat(resv, name);
2032    }
2033  else
2034    {
2035      char *p = strchr(resv, *name);
2036      if (!p)
2037        return MR_NO_MATCH;
2038      memmove(p, p + 1, strlen(p));
2039    }
2040
2041  EXEC SQL UPDATE users SET reservations = NVL(:resv, CHR(0))
2042    WHERE users_id = :uid;
2043  if (dbms_errno)
2044    return mr_errcode;
2045
2046  EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
2047    WHERE table_name = 'users';
2048  return set_modtime_by_id(q, argv, cl);
2049}
2050
2051int get_user_reservations(struct query *q, char **argv, client *cl,
2052                          int (*action)(int, char *[], void *), void *actarg)
2053{
2054  EXEC SQL BEGIN DECLARE SECTION;
2055  char resv[USERS_RESERVATIONS_SIZE], *p;
2056  char trans[ALIAS_TRANS_SIZE], name[2] = { 0, 0 };
2057  int uid;
2058  EXEC SQL END DECLARE SECTION;
2059  char *targv[1];
2060
2061  uid = *(int *)argv[0];
2062
2063  EXEC SQL SELECT reservations INTO :resv FROM users
2064    WHERE users_id = :uid;
2065  if (dbms_errno)
2066    return mr_errcode;
2067
2068  targv[0] = trans;
2069  p = resv;
2070  while (*p && !isspace(*p))
2071    {
2072      name[0] = toupper(*p);
2073      EXEC SQL SELECT trans INTO :trans FROM alias
2074        WHERE type = 'RESERVE' AND UPPER(name) = :name;
2075      if (dbms_errno)
2076        return mr_errcode;
2077      if (sqlca.sqlerrd[2] != 1)
2078        sprintf(trans, "Unknown (%s)", name);
2079      (*action)(1, targv, actarg);
2080      p++;
2081    }
2082  return MR_SUCCESS;
2083}
2084
2085int get_user_by_reservation(struct query *q, char **argv, client *cl,
2086                            int (*action)(int, char *[], void *), void *actarg)
2087{
2088  EXEC SQL BEGIN DECLARE SECTION;
2089  char resv[USERS_RESERVATIONS_SIZE], login[USERS_LOGIN_SIZE];
2090  char *trans, name[ALIAS_NAME_SIZE];
2091  int uid;
2092  EXEC SQL END DECLARE SECTION;
2093  char *targv[1];
2094
2095  trans = argv[0];
2096
2097  EXEC SQL SELECT UPPER(name) INTO :name FROM alias
2098    WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
2099  if (dbms_errno)
2100    return mr_errcode;
2101  if (sqlca.sqlerrd[2] != 1)
2102    return MR_STRING;
2103  name[1] = '\0';
2104
2105  EXEC SQL DECLARE csr_gubr CURSOR FOR
2106    SELECT login FROM users WHERE reservations LIKE '%' || :name || '%';
2107  EXEC SQL OPEN csr_gubr;
2108  if (dbms_errno)
2109    return mr_errcode;
2110
2111  targv[0] = login;
2112  while (1)
2113    {
2114      EXEC SQL FETCH csr_gubr INTO :login;
2115      if (sqlca.sqlcode)
2116        break;
2117      (*action)(1, targv, actarg);
2118    }
2119  EXEC SQL CLOSE csr_gubr;
2120
2121  return MR_SUCCESS;
2122}
2123
2124int update_container(struct query *q, char *argv[], client *cl)
2125{
2126  EXEC SQL BEGIN DECLARE SECTION;
2127  int cnt_id, acl_id, memacl_id, who, flag;
2128  char name[CONTAINERS_NAME_SIZE], newchildname[CONTAINERS_NAME_SIZE];
2129  char* newname, *entity, *description, *location, *contact, *acl_type, *memacl_type;
2130  EXEC SQL END DECLARE SECTION;
2131  char* tmpchar;
2132  int cnt, childid;
2133  char childname[CONTAINERS_NAME_SIZE];
2134  char *qual;
2135  int index = 0;
2136
2137  cnt_id = *(int *)argv[index++];
2138  newname = argv[index++];
2139
2140  if (q->version >= 9)
2141    flag = atoi(argv[index++]);
2142
2143  description = argv[index++];
2144  location = argv[index++];
2145  contact = argv[index++];
2146  acl_type = argv[index++];
2147  acl_id = *(int *)argv[index++];
2148  memacl_type = argv[index++];
2149  memacl_id = *(int *)argv[index++];
2150  entity = cl->entity;
2151  who = cl->client_id;
2152
2153  EXEC SQL SELECT name INTO :name
2154    FROM containers
2155    WHERE cnt_id = :cnt_id;
2156
2157  /* trim off the trailing spaces */
2158   strcpy(name, strtrim(name));
2159
2160   qual = xmalloc(MAX_FIELD_WIDTH);     
2161   sprintf(qual, "name = '%s'", name);
2162   incremental_before(CONTAINERS_TABLE, qual, argv);
2163
2164  /* if we are renaming the container */
2165  if (strcmp(name, newname))
2166  {
2167    /* make sure that only the name part of the name has been changed */
2168    tmpchar = strrchr(name, '/');
2169    /* not a top-level name */
2170    if (tmpchar)
2171    {
2172      cnt = tmpchar - name + 1;
2173      /* the parent part of the old and new name should be identical */
2174      if (strncmp(name, newname, cnt))
2175        return MR_NEW_CONTAINER_NAME;
2176    }
2177    /* top level name, new name should be a top level name too */
2178    else
2179    {
2180      if (strrchr(newname, '/'))
2181        return MR_NEW_CONTAINER_NAME;
2182    }
2183
2184    /* update the name for this container */
2185    EXEC SQL UPDATE containers
2186      SET name = :newname
2187    WHERE cnt_id = :cnt_id;
2188
2189    if (dbms_errno)
2190      return mr_errcode;
2191
2192    /* get names for its child containers */
2193    EXEC SQL DECLARE csr_ucon CURSOR FOR
2194      SELECT name, cnt_id FROM containers WHERE name LIKE :name || '/' || '%';
2195 
2196    EXEC SQL OPEN csr_ucon;
2197    if (dbms_errno)
2198      return mr_errcode;
2199
2200    while (1)
2201    {
2202      EXEC SQL FETCH csr_ucon INTO :childname, :childid;
2203      if (sqlca.sqlcode)
2204              break;
2205
2206      strcpy(childname, strtrim(childname));
2207      /* concatenate the new parent name with the existing sub-container name
2208       * we get the sub-containers new name */
2209      tmpchar = childname + strlen(name);
2210      strcpy(newchildname, newname);
2211      strcat(newchildname, tmpchar);
2212
2213      /* update the name */
2214      EXEC SQL UPDATE containers
2215        SET name = :newchildname, modtime = SYSDATE, modby = :who, modwith = :entity
2216      WHERE cnt_id = :childid;
2217
2218      if (sqlca.sqlcode)
2219        break;
2220    }
2221 
2222    EXEC SQL CLOSE csr_ucon;
2223    if (dbms_errno)
2224      return mr_errcode;
2225  }
2226
2227  /* update the remaining fields */
2228  if (q->version >= 9)
2229  {
2230        EXEC SQL UPDATE containers
2231                SET publicflg= :flag, description = NVL(:description, CHR(0)), location = NVL(:location, CHR(0)),
2232                        contact = NVL(:contact, CHR(0)), acl_type = :acl_type, acl_id = :acl_id,
2233                        memacl_type = :memacl_type, memacl_id = :memacl_id,
2234                        modtime = SYSDATE, modby = :who, modwith = :entity
2235                WHERE cnt_id = :cnt_id;
2236  }
2237  else
2238  {
2239    EXEC SQL UPDATE containers
2240                SET publicflg= :flag, description = NVL(:description, CHR(0)), location = NVL(:location, CHR(0)),
2241                        contact = NVL(:contact, CHR(0)), acl_type = :acl_type, acl_id = :acl_id,
2242                        memacl_type = :memacl_type, memacl_id = :memacl_id,
2243                        modtime = SYSDATE, modby = :who, modwith = :entity
2244                WHERE cnt_id = :cnt_id;
2245  }
2246
2247  if (dbms_errno)
2248    return mr_errcode;
2249
2250  sprintf(qual, "name = '%s'", newname);
2251  incremental_after(CONTAINERS_TABLE, qual, argv);
2252   
2253  return MR_SUCCESS;
2254}
2255
2256int get_machines_of_container(struct query *q, char *argv[], client *cl,
2257                        int (*action)(int, char *[], void *), void *actarg)
2258{
2259  EXEC SQL BEGIN DECLARE SECTION;
2260  int cnt_id, isrecursive;
2261  char machinename[MACHINE_NAME_SIZE], containername[CONTAINERS_NAME_SIZE];
2262  char *qs;
2263  EXEC SQL END DECLARE SECTION;
2264
2265  char querystring[512], tmp [256];
2266  char *rargv[2];
2267  int found = 0;
2268 
2269  rargv[0] = machinename;
2270  rargv[1] = containername;
2271
2272  cnt_id = *(int *)argv[0];
2273  isrecursive = atoi(argv[1]);
2274
2275  /* get the container name */
2276 
2277  EXEC SQL SELECT name INTO :containername
2278    FROM containers
2279    WHERE cnt_id = :cnt_id;
2280
2281  /* trim off the trailing spaces */
2282  strcpy(containername, strtrim(containername));
2283
2284  strcpy(querystring, "SELECT a.name, b.name FROM machine a, containers b, mcntmap c ");
2285  strcat(querystring, "WHERE a.mach_id = c.mach_id AND b.cnt_id = c.cnt_id ");
2286 
2287  if (!isrecursive)
2288    sprintf(tmp, "AND b.cnt_id = %d ", cnt_id);
2289  else
2290    sprintf(tmp, "AND (b.cnt_id = %d OR LOWER(b.name) LIKE LOWER('%s/%%')) ",
2291            cnt_id, containername);
2292
2293  strcat(querystring, tmp);
2294  strcat(querystring, "ORDER BY b.name, a.name");
2295
2296  qs = querystring;
2297
2298  EXEC SQL PREPARE stmt FROM :qs;
2299  if (sqlca.sqlcode)
2300    return MR_INTERNAL;
2301  EXEC SQL DECLARE curs_gmnm CURSOR FOR stmt;
2302  EXEC SQL OPEN curs_gmnm;
2303 
2304   while (1)
2305        {
2306          EXEC SQL FETCH curs_gmnm INTO :machinename, :containername;
2307          if (sqlca.sqlcode)
2308            break;
2309          (*action)(2, rargv, actarg);
2310          found++;
2311        }
2312
2313  EXEC SQL CLOSE curs_gmnm;
2314  if (!found)
2315    return MR_NO_MATCH;
2316  return MR_SUCCESS;
2317}
2318
2319int get_subcontainers_of_container(struct query *q, char *argv[], client *cl,
2320                        int (*action)(int, char *[], void *), void *actarg)
2321{
2322  EXEC SQL BEGIN DECLARE SECTION;
2323  int cnt_id, isrecursive;
2324  char containername[CONTAINERS_NAME_SIZE], subcontainername[CONTAINERS_NAME_SIZE];
2325  char *qs;
2326  EXEC SQL END DECLARE SECTION;
2327
2328  char querystring[2048], tmp [1024];
2329  char *rargv[1];
2330  int found = 0;
2331 
2332  rargv[0] = subcontainername;
2333
2334  cnt_id = *(int *)argv[0];
2335  isrecursive = atoi(argv[1]);
2336
2337  /* get the container name */
2338 
2339  EXEC SQL SELECT name INTO :containername
2340    FROM containers
2341    WHERE cnt_id = :cnt_id;
2342
2343  /* trim off the trailing spaces */
2344  strcpy(containername, strtrim(containername));
2345
2346  strcpy(querystring, "SELECT name FROM containers ");
2347 
2348  if (!isrecursive)
2349    sprintf(tmp, "WHERE LOWER(name) LIKE LOWER('%s/%%') and LOWER(name) NOT LIKE LOWER('%s/%%/%%') ",
2350            containername, containername);
2351  else
2352    sprintf(tmp, "WHERE LOWER(name) LIKE LOWER('%s/%%') ", containername);
2353
2354  strcat(querystring, tmp);
2355  strcat(querystring, "ORDER BY name");
2356
2357  qs = querystring;
2358
2359  EXEC SQL PREPARE stmt FROM :qs;
2360  if (sqlca.sqlcode)
2361    return MR_INTERNAL;
2362  EXEC SQL DECLARE curs_gsoc CURSOR FOR stmt;
2363  EXEC SQL OPEN curs_gsoc;
2364 
2365   while (1)
2366        {
2367          EXEC SQL FETCH curs_gsoc INTO :subcontainername;
2368          if (sqlca.sqlcode)
2369            break;
2370          (*action)(1, rargv, actarg);
2371          found++;
2372        }
2373
2374  EXEC SQL CLOSE curs_gsoc;
2375  if (!found)
2376    return MR_NO_MATCH;
2377  return MR_SUCCESS;
2378}
2379
2380int set_container_list(struct query *q, char *argv[], client *cl)
2381{
2382  EXEC SQL BEGIN DECLARE SECTION;
2383  int cnt_id, list_id;
2384  EXEC SQL END DECLARE SECTION;
2385
2386  cnt_id = *(int *)argv[0];
2387  list_id = *(int *)argv[1];
2388
2389  EXEC SQL UPDATE containers SET list_id = :list_id WHERE cnt_id = :cnt_id;
2390  if (dbms_errno)
2391    return mr_errcode;
2392
2393  return MR_SUCCESS;
2394}
Note: See TracBrowser for help on using the repository browser.