]> andersk Git - moira.git/blob - server/qsupport.qc
sq_save_unique_data and sq_save_unique_string didn't used to
[moira.git] / server / qsupport.qc
1 /*
2  *      $Source$
3  *      $Author$
4  *      $Header$
5  *
6  *      Copyright (C) 1987 by the Massachusetts Institute of Technology
7  *
8  *      $Log$
9  *      Revision 1.18  1988-01-14 12:00:38  mar
10  *      separate access check from setup routine.
11  *      make get_query_need be more paranoid
12  *
13 Revision 1.17  88/01/04  12:09:00  mar
14 fixup pobox access checking & make id assignment more paranoid (wesommer)
15
16 Revision 1.16  87/11/12  18:16:56  wesommer
17 Add support for delete_user_by_uid.
18 Fix access_pop.
19 Check a few return codes.
20
21 Revision 1.15  87/09/11  15:31:58  wesommer
22 add_user_group modified to side-effect the time for tbs for groups as 
23 well as the other things.
24
25 Revision 1.15  87/09/10  22:44:49  wesommer
26 One more table needs to be modified when adding the user-group.
27
28 Revision 1.14  87/09/04  17:50:03  mike
29 Renamed delete_list_members to followup_delete_list.  New routine removes
30 list from maillists and groups tables in addition to the members table.
31
32 Added routines validate_label and validate_label_2 used in validating
33 filesys labels for get_filesys and delete_filesys respectively.
34
35 Revision 1.13  87/09/01  16:24:01  wesommer
36 A list should be considered modified if its acl changes.
37
38 Revision 1.12  87/08/29  00:05:54  mike
39 Fixed bug in check_nfs.
40 Added code to upcasify machine names in validate_id.
41
42 Revision 1.11  87/08/28  14:55:45  mike
43 Modified delete_locker to require only one argument, the login name.
44 Modified get_groups_of_all_users and get_all_poboxes to disable INGRES
45 table locking.
46
47 Revision 1.10  87/08/25  15:56:54  mike
48 - Fixed bugs in get_groups_of_all_users
49 - Added tblstats updates to add_user_group
50 - Added routines: add_hostaccess (ashi), delete_hostaccess (dshi),
51   followup_ushp, and followup_usha
52
53 Revision 1.9  87/08/22  17:41:34  wesommer
54 More of Mike's changes. 
55
56 Revision 1.8  87/08/18  15:05:20  wesommer
57 Fixed definition of add_locker.
58
59 Revision 1.7  87/08/04  01:49:41  wesommer
60 Rearranged messages.
61
62 Revision 1.6  87/08/04  01:10:02  wesommer
63 Changes by mike; checked in prior to my hacking.
64
65 Revision 1.5  87/07/30  14:54:13  wesommer
66 Added debugging code in an attempt to catch a flakey problem.
67
68 Revision 1.4  87/07/30  00:30:21  wesommer
69 replaced appends = appends+1 with appends = tbs.appends+1
70
71 Revision 1.3  87/07/30  00:26:11  wesommer
72 Changes by mike prior to "appends" fix.
73
74 Revision 1.2  87/07/29  16:00:39  wesommer
75 Fixed add_locker.
76
77 Revision 1.1  87/07/29  15:13:57  wesommer
78 Initial revision
79
80  */
81
82 #ifndef lint
83 static char *rcsid_qsupport_qc = "$Header$";
84 #endif lint
85
86 #include "query.h"
87 #include "sms_server.h"
88 #include <ctype.h>
89
90 #define SMS_SUCCESS 0
91
92 extern char *whoami;
93
94 /* Specialized Access Routines */
95
96 /**
97  ** access_user - verify that client name equals specified login name
98  **
99  ** Used by: update_user_shell
100  **          update_finger_by_login
101  **
102  **  - since field validation routines are called first, a users_id is
103  **    now in argv[0] instead of the login name.  Therefore, we must 
104  **    convert the client name to a users_id.
105  **/
106
107 access_user(q, argv, cl)
108     struct query *q;
109     char *argv[];
110     client *cl;
111 ##{
112     register struct krbname *krb;
113 ##  int client_id;
114 ##  char *client_name;
115 ##  int rowcount;
116
117     client_name = cl->kname.name;
118 ##  repeat retrieve (client_id = users.users_id) 
119 ##         where users.login = @client_name
120 ##  inquire_equel (rowcount = "rowcount")
121     if (rowcount != 1) return(SMS_PERM);
122     if (client_id != *(int *)argv[0]) return(SMS_PERM);
123
124     return(SMS_SUCCESS);
125 ##}
126     
127 /**
128  ** access_pop - same as access_user plus verifies that a user has only one
129  **              mailbox of type "POP"
130  **
131  ** Inputs:
132  **   argv[0] - users_id
133  **   argv[1] - type
134  **   argv[2] - mach_id
135  **   argv[3] - box
136  **
137  ** Description:
138  **   - if q->name = "add_pobox" and type = "POP", 
139  **        verify that no POP box already exists for user
140  **   - call access_user
141  **
142  **/
143
144 access_pop(q, argv, cl)
145     struct query *q;
146     char *argv[];
147     client *cl;
148 ##{
149 ##  int users_id;
150 ##  int mach_id;
151 ##  char *box;
152 ##  int exists;
153
154     if (!bcmp(q->name, "add_pobox", 10) && !bcmp(argv[1], "POP", 4)) {
155         box = argv[3];
156         if (strcmp(cl->kname.name, box) != 0) return SMS_PERM;
157         
158         users_id = *(int *)argv[0];
159         mach_id = *(int *)argv[2];
160             
161 ##      range of p is pobox
162 ##      repeat retrieve (exists = any(p.#box where p.#users_id = @users_id
163 ##                                    and p.type = "POP"))
164         if (exists) return(SMS_EXISTS);
165     }
166
167     return(access_user(q, argv, cl));
168 ##}
169
170 /**
171  ** access_list - check access for adding or deleting list members
172  **
173  ** Inputs: argv[0] - list_id
174  **         cl->krb.name - client name
175  **
176  ** - check that client is a member of the access control list
177  ** - OR, if q->shortname == {amtl | dfml} and
178  **       if list.flags & LF_PUBLIC, allow access if client = member
179  **
180  **/ 
181
182 access_list(q, argv, cl)
183     struct query *q;
184     char *argv[];
185     client *cl;
186 ##{
187 ##  int list_id;
188 ##  int acl_id;
189 ##  int flags;
190     int member_id;
191     char *client_type;
192     int client_id;
193     int status;
194     int exists;
195
196     list_id = *(int *)argv[0];
197 ##  repeat retrieve (acl_id = list.#acl_id, flags = list.#flags) 
198 ##         where list.#list_id = @list_id
199
200     /* parse client structure */
201     status = get_client(cl, &client_type, &client_id);
202     if (status != SMS_SUCCESS) return(status);
203
204     /* if amtl or dmfl and list is public allow client to add or delete self */
205     if (!bcmp("amtl", q->shortname, 4) || !bcmp("dmfl", q->shortname, 4)) {
206         if ((flags & LF_PUBLIC) && !bcmp("USER", argv[1], 4)) {
207             member_id = *(int *)argv[2];
208             if (member_id == client_id) return(SMS_SUCCESS);
209         }
210     }
211
212     /* check for client in access control list */
213     exists = find_member(acl_id, client_type, client_id, 0);
214     if (!exists) return(SMS_PERM);
215
216     return(SMS_SUCCESS);
217 ##}
218
219 /**
220  ** access_maillist - access_list + disallow adding user-group to maillists
221  **
222  ** Inputs:
223  **   argv[0] - list_id
224  **
225  **/
226
227 access_maillist(q, argv, cl)
228     struct query *q;
229     char *argv[];
230     client *cl;
231 ##{
232 ##  int list_id;
233 ##  int exists;
234 ##  char list_name[32];
235     int status;
236
237     status = access_list(q, argv, cl);
238     if (status != SMS_SUCCESS) return(status);
239     if (bcmp(q->name, "add_maillist", 12)) return(status);
240
241     list_id = *(int *)argv[0];
242 ##  range of g is groups
243 ##  repeat retrieve (exists = any(g.#list_id where g.#list_id = @list_id))
244     if (!exists) return(SMS_SUCCESS);
245 ##  repeat retrieve (list_name = list.name) where list.#list_id = @list_id
246 ##  repeat retrieve (exists = any(users.login where users.login = @list_name))
247     return ((exists) ? SMS_USER_GROUP : SMS_SUCCESS);
248 ##}
249
250 /**
251  ** Setup routine for add_group
252  **
253  ** Inputs: none
254  **
255  ** Description: allocate next gid and store in values table
256  **
257  **/
258
259 setup_add_group(q, argv, cl)
260     struct query *q;
261     char *argv[];
262     client *cl;
263 ##{
264 ##  int ngid;
265 ##  int exists;
266 ##  int rowcount;
267        int status;
268
269 ##  range of g is groups
270 ##  range of v is values
271 ##  repeat retrieve (ngid = v.value) where v.name = "gid"
272     exists = 1;
273     while (exists) {
274         ngid++;
275 ##      repeat retrieve (exists = any(g.#gid where g.#gid = @ngid))
276     }
277
278 ##  repeat replace v (value = @ngid) where v.name = "gid"
279 ##  inquire_equel (rowcount = "rowcount")
280     if (rowcount != 1) return SMS_INGRES_ERR;
281     else return(SMS_SUCCESS);
282 ##}
283 \f
284 /**
285  ** Setup routine for add_user
286  **
287  ** Inputs: argv[0] - login
288  **         argv[1] - uid
289  **
290  ** Description:
291  **
292  ** - if argv[1] == "#" then set argv[1] = next(uid)
293  ** - if argv[0] == "#" then set argv[0] = "#<uid>"
294  **
295  **/
296
297 setup_add_user(q, argv, cl)
298     struct query *q;
299     register char *argv[];
300     client *cl;
301 ##{
302 ##  int nuid;
303 ##  int exists;
304
305     if (!bcmp(argv[1], "#", 2)) {
306 ##      range of u is users
307 ##      range of v is values
308 ##      repeat retrieve (nuid = v.value) where v.name = "uid"
309         exists = 1;
310         while (exists) {
311             nuid++;
312 ##          repeat retrieve (exists = any(u.#uid where u.#uid = @nuid))
313         }
314 ##      repeat replace v (value = @nuid) where v.name = "uid"
315         sprintf(argv[1], "%d", nuid);
316     }
317
318     if (!bcmp(argv[0], "#", 2)) {
319         sprintf(argv[0], "#%s", argv[1]);
320     }
321
322     return(SMS_SUCCESS);
323 ##}
324
325 /**
326  ** followup_add_user - add finger entry, set_user_modtime
327  ** followup_delete_user - delete finger entry
328  **
329  ** Inputs:
330  **   argv[0] - login (add_user)
331  **   argv[0] - users_id (delete_user)
332  **
333  **/
334
335 followup_add_user(q, argv)
336     struct query *q;
337     char *argv[];
338 ##{
339 ##  char *login;
340 ##  int users_id;
341 ##  char first[33];
342 ##  char middle[33];
343 ##  char last[33];
344 ##  char fullname[128];
345     register char *cp1;
346     register char *cp2;
347
348     login = argv[0];
349
350     /* get user information */
351 ##  range of u is users
352 ##  repeat retrieve (users_id = u.#users_id, last = u.#last,
353 ##                   first = u.#first, middle = u.#middle) 
354 ##         where u.#login = @login
355
356     /* build fullname */
357     cp2 = fullname;
358     cp1 = first;
359     while (*cp1) *cp2++ = *cp1++;
360     *cp2++ = ' ';
361     cp1 = middle;
362     if (*cp1 == 0) cp2--;
363     while (*cp1) *cp2++ = *cp1++;
364     *cp2++ = ' ';
365     cp1 = last;
366     while (*cp2++ = *cp1++) ;
367
368     /* create a finger entry */
369 ##  repeat append finger (#users_id = @users_id, #fullname = @fullname)
370
371     /* set modtime (creation time) on user */
372 ##  repeat replace u (modtime = "now") where u.#users_id = @users_id
373
374     return(SMS_SUCCESS);
375 ##}
376
377 followup_delete_user(q, argv)
378     struct query *q;
379     char *argv[];
380 ##{
381 ##  int users_id;
382
383     users_id = *(int *)argv[0];
384 ##  repeat delete finger where finger.#users_id = @users_id
385     return(SMS_SUCCESS);
386 ##}
387 \f
388 /**
389  ** setup_add_filesys - verify existance of referenced file systems
390  ** setup_update_filesys - same, except argv[1..5] --> argv[2..6]
391  **
392  ** Inputs:     Add         Update
393  **   argv[0] - label       label
394  **   argv[1] - type        new label
395  **   argv[2] - mach_id     type
396  **   argv[3] - name        mach_id
397  **   argv[4] - mount       name
398  **   argv[5] - access      mount
399  **   argv[6] -             access
400  **
401  ** Description:
402  **   - for type = RVD:
403  **        * verify mach_id/name in rvdvirt
404  **        * verify access in {r, x, R, X}
405  **   - for type = NFS:
406  **        * extract directory prefix from name
407  **        * verify mach_id/dir in nfsphys
408  **        * verify access in {r, w, R, W}
409  **
410  ** Errors:
411  **   SMS_RVD - no such rvd
412  **   SMS_NFS - specified directory not exported
413  **   SMS_FILESYS_ACCESS - invalid filesys access
414  **
415  **/
416
417 setup_add_filesys(q, argv)
418     struct query *q;
419     char *argv[];
420 {
421     char *type;
422     int mach_id;
423     char *name;
424     char *access;  
425
426     type = argv[1];
427     mach_id = *(int *)argv[2];
428     name = argv[3];
429     access = argv[5];
430
431     if (!bcmp(type, "RVD", 3))
432         return (check_rvd(mach_id, name, access));
433     else if (!bcmp(type, "NFS", 3))
434         return (check_nfs(mach_id, name, access));
435     else
436         return(SMS_SUCCESS);
437 }
438
439 setup_update_filesys(q, argv)
440     struct query *q;
441     char *argv[];
442 {
443     char *type;
444     int mach_id;
445     char *name;
446     char *access;  
447
448     type = argv[2];
449     mach_id = *(int *)argv[3];
450     name = argv[4];
451     access = argv[6];
452
453     if (!bcmp(type, "RVD", 3))
454         return (check_rvd(mach_id, name, access));
455     else if (!bcmp(type, "NFS", 3))
456         return (check_nfs(mach_id, name, access));
457     else
458         return(SMS_SUCCESS);
459 }
460
461 ##check_rvd(mach_id, name, access)
462 ##  int mach_id;
463 ##  char *name;
464     char *access;
465 ##{
466 ##  int rowcount;
467     char caccess;
468
469 ##  range of rv is rvdvirt
470 ##  retrieve (rowcount = any(rv.#name where rv.#mach_id = mach_id and
471 ##                           rv.#name = name))
472     if (rowcount == 0) return(SMS_RVD);
473
474     caccess = (isupper(*access)) ? tolower(*access) : *access;
475     if (caccess != 'r' && caccess != 'x') return(SMS_FILESYS_ACCESS);
476
477     return(SMS_SUCCESS);
478 ##}
479
480 ##check_nfs(mach_id, name, access)
481 ##  int mach_id;
482     char *name;
483     char *access;
484 ##{
485 ##  int rowcount;
486 ##  char dir[32];
487     char caccess;
488     register int status;
489     register char *cp1;
490     register char *cp2;
491
492     caccess = (isupper(*access)) ? tolower(*access) : *access;
493     if (caccess != 'r' && caccess != 'w') return(SMS_FILESYS_ACCESS);
494
495     status = SMS_NFS;
496 ##  range of np is nfsphys
497 ##  retrieve (dir = np.#dir) where np.#mach_id = mach_id
498 ##  {
499          cp1 = name;
500          cp2 = dir;
501          while (*cp2) {
502              if (*cp1++ != *cp2) break;
503              cp2++;
504          }
505          if (*cp2 == 0) {
506              status = SMS_SUCCESS;
507 ##           endretrieve
508          }
509 ##  }
510
511     return(status);
512 ##}
513 \f
514 /* Followup Routines */
515
516 set_user_modtime(q, argv)
517     struct query *q;
518     char *argv[];
519 ##{
520 ##  char *login;
521
522     login = argv[0];
523 ##  repeat replace u (modtime = "now") where u.#login = @login
524     return(SMS_SUCCESS);
525 ##}
526
527 set_user_modtime_by_id(q, argv)
528     struct query *q;
529     char *argv[];
530 ##{
531 ##  int users_id;
532
533     users_id = *(int *)argv[0];
534 ##  repeat replace users (modtime = "now") where users.#users_id = @users_id
535     return(SMS_SUCCESS);
536 ##}
537
538 set_list_modtime(q, argv)
539     struct query *q;
540     char *argv[];
541 ##{
542 ##  char *list_name;
543
544     list_name = argv[0];
545 ##  repeat replace list (modtime = "now") where list.name = @list_name
546     return(SMS_SUCCESS);
547 ##}
548
549 set_list_modtime_by_id(q, argv)
550     struct query *q;
551     char *argv[];
552 ##{
553 ##  int list_id;
554
555     list_id = *(int *)argv[0];
556 ##  repeat replace list (modtime = "now") where list.#list_id = @list_id
557 ##  repeat replace list (modtime = "now") where list.#acl_id = @list_id
558     return(SMS_SUCCESS);
559 ##}
560
561 set_finger_modtime(q, argv)
562     struct query *q;
563     char *argv[];
564 ##{
565 ##  int users_id;
566
567     users_id = *(int *)argv[0];
568 ##  repeat replace f (modtime = "now") where f.#users_id = @users_id
569     return(SMS_SUCCESS);
570 ##}
571 \f
572 /**
573  ** followup_amtl - followup for amtl and dmfl; when adding a list 
574  **                 member to a maillist, add list to maillist table,
575  **                 unless list is a user-group.
576  **                 Then set_list_modtime_by_id.
577  **
578  ** Inputs:
579  **   argv[0] - list_id
580  **   argv[1] - member_type
581  **   argv[2] - member_id
582  **
583  **/
584
585 followup_amtl(q, argv)
586     struct query *q;
587     char *argv[];
588 ##{
589 ##  int list_id;
590 ##  int member_id;
591 ##  int exists;
592 ##  char list_name[33];
593
594     list_id = *(int *)argv[0];
595
596 ##  repeat replace list (modtime = "now") where list.#list_id = @list_id
597 ##  repeat replace list (modtime = "now") where list.#acl_id = @list_id
598
599     /* if query is not amtl or if member_type is not LIST then return */
600     if (bcmp(q->shortname, "amtl", 4) || bcmp(argv[1], "LIST", 4)) 
601         return(SMS_SUCCESS);
602
603     member_id = *(int *)argv[2];
604 ##  range of l is list
605 ##  range of ml is maillists
606 ##  range of g is groups
607
608     /* is parent list a mailing list? */
609 ##  repeat retrieve (exists = any(ml.#list_id where ml.#list_id=@list_id))
610
611     /* if not then return */
612     if (!exists) return(SMS_SUCCESS);
613
614     /* is member_list a user-group? */
615     /* is it a group? */
616 ##  repeat retrieve (exists = any(g.#list_id where g.#list_id = @member_id))
617     if (exists) {
618         /* get list_name */
619 ##      repeat retrieve (list_name = l.#name) where l.#list_id = @member_id
620         /* is list_name a username? */
621 ##      repeat retrieve (exists = any(users.login 
622 ##                                    where users.login = @list_name))
623         /* yes, return error */
624         if (exists) return(SMS_USER_GROUP);
625     }
626
627     /* list is not a user-group; add list to maillist table */
628 ##  repeat append maillists (#list_id = @member_id, ltid = l.tid)
629 ##         where l.#list_id = @member_id
630
631     return(SMS_SUCCESS);
632 ##}
633 \f
634 /**
635  ** followup_add_pobox
636  ** followup_delete_pobox - followup routines for pobox queries
637  **
638  ** Description:
639  **   add_pobox: set pobox creation time
640  **              increment pop usage in serverhosts
641  **
642  **   delete_pobox: decrement pop usage in serverhosts
643  **
644  **/
645
646 followup_add_pobox(q, argv)
647     struct query *q;
648     char *argv[];
649 {
650     set_pobox_creation(q, argv);
651     set_pop_usage(q, argv, 1);
652     return(SMS_SUCCESS);
653 }
654
655 followup_delete_pobox(q, argv)
656     struct query *q;
657     char *argv[];
658 {
659     set_pop_usage(q, argv, -1);
660     return(SMS_SUCCESS);
661 }
662
663 set_pobox_creation(q, argv)
664     struct query *q;
665     char *argv[];
666 ##{
667 ##  int users_id;
668 ##  int mach_id;
669 ##  char *type;
670 ##  char *box;
671
672     users_id = *(int *)argv[0];
673     type = argv[1];
674     mach_id = *(int *)argv[2];
675     box = argv[3];
676
677 ##  range of p is pobox
678 ##  repeat replace p (created = "now") 
679 ##         where p.#users_id = @users_id and p.#type = @type and
680 ##               p.#mach_id = @mach_id and p.#box = @box
681
682     return (SMS_SUCCESS);
683 ##}
684
685 /**
686  ** set_pop_usage - incr/decr usage count for pop server in serverhosts talbe
687  **
688  ** Inputs:
689  **   q->name - "add_pobox" or "delete_pobox"
690  **   argv[1] - type
691  **   argv[2] - mach_id
692  **
693  ** Description:
694  **   - incr/decr value field in serverhosts table for pop/mach_id
695  **
696  **/
697
698 set_pop_usage(q, argv, count)
699     struct query *q;
700     char *argv[];
701 ##{
702 ##  int mach_id;
703 ##  int n;
704
705     if (bcmp(argv[1], "POP", 3)) return(SMS_SUCCESS);
706
707     mach_id = *(int *)argv[2];
708     n = count;
709
710 ##  range of sh is serverhosts
711 ##  repeat replace sh (value1 = sh.value1 + @n)
712 ##         where sh.service = "pop" and sh.#mach_id = @mach_id
713
714     return(SMS_SUCCESS);
715 ##}
716
717 /**
718  ** delete_user_poboxes - delete all poboxes for a user
719  **
720  ** Inputs:
721  **   argv[0] - users_id
722  **
723  **/
724
725 delete_user_poboxes(q, argv)
726     struct query *q;
727     char *argv[];
728 ##{
729 ##  int users_id;
730 ##  int n;
731 ##  int mach_id;
732     register int i;
733     int mach_ids[10];
734
735     users_id = *(int *)argv[0];
736
737     /* get machine ids for pop server(s) on which the user currently exists */
738 ##  range of p is pobox
739     i = 0;
740 ##  repeat retrieve (mach_id = p.#mach_id) 
741 ##         where p.#users_id = @users_id and p.type = "POP"
742 ##  {
743          mach_ids[i++] = mach_id;
744          if (i == 10) {
745 ##           endretrieve
746          }
747 ##  }
748
749     /* decrement counts on serverhost entries */
750 ##  range of sh is serverhosts
751     while (--i >= 0) {
752         mach_id = mach_ids[i];
753 ##      repeat replace sh (value1 = sh.value1 - 1)
754 ##             where sh.service = "pop" and sh.#mach_id = @mach_id
755     }
756
757     /* delete user's poboxes */
758 ##  repeat delete p where p.#users_id = @users_id
759
760     return(SMS_SUCCESS);
761 ##}
762
763 \f
764 /**
765  ** add_new_quota
766  ** delete_current_quota - adjust nfsphys values on xxx_quota queries.
767  **
768  ** Inputs:
769  **   argv[0] - mach_id
770  **   argv[1] - device
771  **   argv[2] - users_id
772  **   argv[3] - quota (add_new_quota only)
773  **
774  ** Description:
775  **   delete_current_quota:
776  **     - find nfsquota entry
777  **     - decrement nfsphys.allocated by nfsquota.quota
778  **   add_new_quota
779  **     - increment nfsphys.allocated by quota
780  **
781  **/
782
783 add_new_quota(q, argv)
784     struct query *q;
785     register char *argv[];
786 ##{
787 ##  int mach_id;
788 ##  char *device;
789 ##  int quota;
790
791     mach_id = *(int*)argv[0];
792     device = argv[1];
793     quota = *(int *)argv[3];
794
795 ##  range of np is nfsphys
796 ##  repeat replace np (allocated = np.allocated + @quota)
797 ##      where np.#mach_id = @mach_id and np.#device = @device
798
799     return(SMS_SUCCESS);
800 ##}
801
802 delete_current_quota(q, argv, cl)
803     struct query *q;
804     register char *argv[];
805     client *cl;
806 ##{
807 ##  int mach_id;
808 ##  int users_id;
809 ##  char *device;
810 ##  int quota;
811
812     mach_id = *(int *)argv[0];
813     device = argv[1];
814     users_id = *(int *)argv[2];
815
816 ##  range of np is nfsphys
817 ##  range of nq is nfsquota
818 ##  repeat retrieve (quota = nq.#quota) 
819 ##      where nq.#mach_id = @mach_id and nq.#device = @device and
820 ##            nq.#users_id = @users_id
821 ##  repeat replace np (allocated = np.allocated - @quota)
822 ##      where np.#mach_id = @mach_id and np.#device = @device
823  
824     return(SMS_SUCCESS);
825 ##}
826 \f
827 /**
828  ** add_hostaccess - create entry in hostaccess table upon adding a new
829  **                  machine to the serverhosts table where service =
830  **                  "hostaccess".
831  **
832  ** Inputs:
833  **   argv[0] - service
834  **   argv[1] - mach_id
835  **
836  **/
837
838 add_hostaccess(q, argv)
839     struct query *q;
840     char *argv[];
841 ##{
842 ##  int mach_id;
843
844     /* only work with service = "hostaccess" */
845     if (bcmp(argv[0], "hostaccess", 10)) return(SMS_SUCCESS);
846
847     mach_id = *(int *)argv[1];
848 ##  repeat append hostaccess (#mach_id = @mach_id, status = 0)
849 ##  repeat replace tblstats (modtime = "now", appends = tblstats.appends + 1)
850 ##         where tblstats.table = "hostaccess"
851     return(SMS_SUCCESS);
852 ##}
853
854 /* followup to delete_server_host_info */
855
856 delete_hostaccess(q, argv)
857     struct query *q;
858     char *argv[];
859 ##{
860 ##  int mach_id;
861
862     /* only work with service = "hostaccess" */
863     if (bcmp(argv[0], "hostaccess", 10)) return(SMS_SUCCESS);
864
865     mach_id = *(int *)argv[1];
866 ##  repeat delete hostaccess where hostaccess.#mach_id = @mach_id
867 ##  repeat replace tblstats (modtime = "now", deletes = tblstats.deletes + 1)
868 ##         where tblstats.table = "hostaccess"
869     return(SMS_SUCCESS);
870 ##}
871
872 followup_ushp(q, argv)
873     struct query *q;
874     char *argv[];
875 ##{
876 ##  int mach_id;
877 ##  int status;
878
879     mach_id = *(int *)argv[0];
880 ##  range of ha is hostaccess
881 ##  repeat retrieve (status = ha.#status) where ha.#mach_id = @mach_id
882     status |= 1;
883 ##  repeat replace ha (#status = @status) where ha.#mach_id = @mach_id
884     return(SMS_SUCCESS);
885 ##}
886
887 followup_usha(q, argv)
888     struct query *q;
889     char *argv[];
890 ##{
891 ##  int mach_id;
892 ##  int status;
893
894     mach_id = *(int *)argv[0];
895 ##  range of ha is hostaccess
896 ##  repeat retrieve (status = ha.#status) where ha.#mach_id = @mach_id
897     status |= 2;
898 ##  repeat replace ha (#status = @status) where ha.#mach_id = @mach_id
899     return(SMS_SUCCESS);
900 ##}
901 \f
902 /**
903  ** followup_delete_list - called after the delete_list query to clean up
904  **                        members table; also deletes list from maillist
905  **                        and groups tables.
906  **
907  ** Inputs: argv[0] - list_id
908  **
909  ** Description:
910  **   - foreach string member: decr string refc; ifzero, delete string
911  **   - delete all members entries for this list_id
912  **
913  **/
914
915 followup_delete_list(q, argv)
916     struct query *q;
917     register char *argv[];
918 ##{
919 ##  int list_id;
920 ##  int string_id;
921 ##  int refc;
922 ##  int rowcount;
923     struct save_queue *sq;
924     struct save_queue *sq_create();
925
926     list_id = *(int *)argv[0];
927     sq = sq_create();
928
929 ##  range of m is members
930 ##  repeat retrieve (string_id = m.member_id)
931 ##         where m.#list_id = @list_id and m.member_type = "STRING"
932 ##  {
933          sq_save_data(sq, string_id);
934 ##  }
935
936     while (sq_get_data(sq, &string_id)) {
937 ##      range of s is strings
938 ##      repeat retrieve (refc = s.#refc) where s.#string_id = @string_id
939 ##      inquire_equel (rowcount = "rowcount")
940         if (rowcount == 0) continue;
941         if (--refc == 0) {
942 ##          repeat delete s where s.#string_id = @string_id
943         } else {
944 ##          repeat replace s (#refc = @refc) where s.#string_id = @string_id
945         }
946     }
947     sq_destroy(sq);
948
949 ##  repeat delete m where m.#list_id = @list_id
950 ##  repeat delete maillists where maillists.#list_id = @list_id
951 ##  repeat delete groups where groups.#list_id = @list_id
952
953     return(SMS_SUCCESS);
954 ##}
955 \f
956 /**
957  ** followup_grvd - Support routine for get_rvd_servers query
958  **
959  ** Inputs:
960  **    q     - grvd query structure
961  **    sq    - save_queue struture: contains list of {machine, oper_acl_id,
962  **            admin_acl_id, shutdown_acl_id} records.
963  **    v     - validate structure (not used)
964  **    action - action routine
965  **    actarg - action routine argument
966  **
967  ** Description:
968  **   - translate acl_ids to list names
969  **
970  **/
971
972 followup_grvd(q, sq, v, action, actarg)
973     struct query *q;
974     struct save_queue *sq;
975     struct validate *v;
976     int (*action)();
977     int actarg;
978 ##{
979     char **argv;
980     char *targv[4];
981 ##  char oper[33];
982 ##  char admin[33];
983 ##  char shutdown[33];
984 ##  int list_id;
985
986     targv[0] = oper;
987     targv[1] = admin;
988     targv[2] = shutdown;
989
990 ##  range of l is list
991
992     while (sq_get_data(sq, &argv)) {
993         sscanf(argv[0], "%d", &list_id);
994 ##      repeat retrieve (oper = l.name) where l.#list_id = @list_id
995         sscanf(argv[1], "%d", &list_id);
996 ##      repeat retrieve (admin = l.name) where l.#list_id = @list_id
997         sscanf(argv[2], "%d", &list_id);
998 ##      repeat retrieve (shutdown = l.name) where l.#list_id = @list_id
999         
1000         (*action)(3, targv, actarg);
1001         free(argv[0]);
1002         free(argv[1]);
1003         free(argv[2]);
1004     }
1005
1006     sq_destroy(sq);
1007     return(SMS_SUCCESS);
1008 ##}
1009
1010 followup_gars(q, sq, v, action, actarg)
1011     struct query *q;
1012     struct save_queue *sq;
1013     struct validate *v;
1014     int (*action)();
1015     int actarg;
1016 ##{
1017     char **argv;
1018     char *targv[4];
1019 ##  char oper[33];
1020 ##  char admin[33];
1021 ##  char shutdown[33];
1022 ##  int list_id;
1023
1024     targv[1] = oper;
1025     targv[2] = admin;
1026     targv[3] = shutdown;
1027
1028 ##  range of l is list
1029
1030     while (sq_get_data(sq, &argv)) {
1031         sscanf(argv[1], "%d", &list_id);
1032 ##      repeat retrieve (oper = l.name) where l.#list_id = @list_id
1033         sscanf(argv[2], "%d", &list_id);
1034 ##      repeat retrieve (admin = l.name) where l.#list_id = @list_id
1035         sscanf(argv[3], "%d", &list_id);
1036 ##      repeat retrieve (shutdown = l.name) where l.#list_id = @list_id
1037         
1038         targv[0] = argv[0];
1039         (*action)(4, targv, actarg);
1040         free(argv[0]);
1041         free(argv[1]);
1042         free(argv[2]);
1043         free(argv[3]);
1044     }
1045
1046     sq_destroy(sq);
1047     return(SMS_SUCCESS);
1048 ##}
1049 \f
1050 /**
1051  ** set_next_object_id - set next object id in values table
1052  **
1053  ** Inputs: object - object name in values table
1054  **
1055  ** - called before an APPEND operation to set the next object id to
1056  **   be used for the new record
1057  **
1058  **/
1059
1060 set_next_object_id(object)
1061     char *object;
1062 ##{
1063 ##  char *name;
1064 ##  int rowcount;
1065
1066        name = object;
1067 ##  range of v is values
1068 ##  repeat replace v (value = v.value + 1) where v.#name = @name
1069 ##      inquire_equel (rowcount = "rowcount")
1070     if (rowcount != 1)
1071         return SMS_INGRES_ERR;
1072     else return(SMS_SUCCESS);
1073 ##}
1074
1075 /**
1076  ** get_query_need - check modtime of query's associated table against given
1077  **                  time and return true if greater (false if not)
1078  **
1079  ** Inputs:
1080  **   argv[0] - query name
1081  **   argv[1] - time to compare against
1082  **
1083  **/
1084
1085 get_query_need(q, argv, action, actarg)
1086     struct query *q;
1087     register char *argv[];
1088     int (*action)();
1089 ##{
1090     struct query *q1;
1091 ##  char *last_get_time;
1092 ##  char *table;
1093 ##  int need;
1094     char *result;
1095     struct query *get_query_by_name();
1096
1097     q1 = get_query_by_name(argv[0]);
1098
1099     last_get_time = argv[1];
1100     table = q1->rtable;
1101
1102     if (q1 == NULL || q1->type != RETRIEVE || table == NULL)
1103         return(SMS_NO_MATCH);
1104
1105 ##  range of tbs is tblstats
1106 ##  repeat retrieve (need = any(tbs.modtime where tbs.#table = @table and
1107 ##                              tbs.modtime > @last_get_time))
1108     
1109     result = (need) ? "true" : "false";
1110     (*action)(1, &result, actarg);
1111     return(SMS_SUCCESS);
1112 ##}
1113
1114 /**
1115  ** get_list_is_group
1116  ** get_list_is_maillist
1117  **
1118  ** Inputs:
1119  **   argv[0] - list_id
1120  **
1121  ** Returns:
1122  **   {true | false}
1123  **
1124  **/
1125
1126 get_list_is_group(q, argv, action, actarg)
1127     struct query *q;
1128     char *argv[];
1129     int (*action)();
1130     int actarg;
1131 ##{
1132 ##  int exists;
1133 ##  int list_id;
1134     char *result;
1135
1136     list_id = *(int *)argv[0];
1137
1138 ##  range of g is groups
1139 ##  repeat retrieve (exists = any(g.#list_id where g.#list_id = @list_id))
1140
1141     result = (exists) ? "true" : "false";
1142     (*action)(1, &result, actarg);
1143     return(SMS_SUCCESS);
1144 ##}
1145
1146 get_list_is_maillist(q, argv, action, actarg)
1147     struct query *q;
1148     char *argv[];
1149     int (*action)();
1150     int actarg;
1151 ##{
1152 ##  int exists;
1153 ##  int list_id;
1154     char *result;
1155
1156     list_id = *(int *)argv[0];
1157
1158 ##  range of ml is maillists
1159 ##  repeat retrieve (exists = any(ml.#list_id where ml.#list_id = @list_id))
1160
1161     result = (exists) ? "true" : "false";
1162     (*action)(1, &result, actarg);
1163     return(SMS_SUCCESS);
1164 ##}
1165
1166 \f
1167 /**
1168  ** add_locker - special query routine for creating a user locker
1169  **
1170  ** Inputs:
1171  **   argv[0] - users_id
1172  **   argv[1] - machine_id
1173  **   argv[2] - device
1174  **   argv[3] - initial quota
1175  ** 
1176  ** Description:
1177  **   - get prefix directory (dir) for mount point on specified machine/device
1178  **   - create filesys entry (label=<login>, type=NFS, machine=<machine>,
1179  **     mount=<dir>/<login>, access=w, acl=dbadmin)
1180  **   - increment allocated in nfsphys by quota
1181  **   - create nfsquota entry
1182  **
1183  ** Errors:
1184  **   - SMS_NFSPHYS - machine/device does not exist in nfsphys
1185  **   - SMS_FILESYS_EXISTS - file system already exists
1186  ** 
1187  **/
1188
1189 add_locker(q, argv)
1190     register struct query *q;
1191     char *argv[];
1192 ##{
1193 ##  int users_id;
1194 ##  int mach_id;
1195 ##  char *device;
1196 ##  int quota;
1197 ##  int rowcount;
1198 ##  char login[9];
1199 ##  char dir[32];
1200 ##  int allocated;
1201 ##  char locker[64];
1202 ##  char mount[64];
1203 ##  int user_acl;
1204
1205     /* copy arguments */
1206     users_id = *(int *)argv[0];
1207     mach_id = *(int *)argv[1];
1208     device = argv[2];
1209     sscanf(argv[3], "%d", &quota);
1210
1211 ##  range of u is users
1212 ##  range of f is filesys
1213 ##  range of np is nfsphys
1214 ##  range of tbs is tblstats
1215
1216     /* get login name */
1217 ##  repeat retrieve (login = u.#login) where u.#users_id = @users_id
1218
1219     /* get user's acl id */
1220 ##  repeat retrieve (user_acl = list.list_id) where list.name = @login
1221
1222     /* get filesystem directory prefix; give error if machine/device
1223        pair not in nfsphys table */
1224     printf("np.mach_id = %d and np.device = %s\n", mach_id, device);
1225     
1226 ##  repeat retrieve (dir = np.#dir, allocated = np.#allocated) 
1227 ##         where np.#mach_id = @mach_id and np.#device = @device
1228 ##  inquire_equel (rowcount = "rowcount")
1229     if (rowcount == 0) return(SMS_NFSPHYS);
1230
1231     /* make sure a filesys with user's name does not already exist */
1232 ##  repeat retrieve (rowcount = any(f.label where f.label = @login))
1233     if (rowcount != 0) return(SMS_FILESYS_EXISTS);
1234
1235     /* create a new filesys */
1236     sprintf(locker, "%s/%s", dir, login);
1237     sprintf(mount, "/mit/%s", login);
1238 ##  repeat append filesys 
1239 ##             (#label = @login, type = "NFS", #mach_id = @mach_id,
1240 ##              name = @locker, access = "w", order = 1, #mount = @mount,
1241 ##              acl_id = @user_acl)
1242 ##  repeat replace tbs (appends = tbs.appends + 1, modtime = "now")
1243 ##             where tbs.table = "filesys"
1244
1245     /* increment usage count in nfsphys table */
1246     allocated += quota;
1247 ##  replace np (#allocated = allocated) 
1248 ##          where np.#mach_id = mach_id and np.#device = device
1249 ##  repeat replace tbs (updates = tbs.updates + 1, modtime = "now")
1250 ##             where tbs.table = "nfsphys"
1251
1252     /* create nfsquota entry */
1253 ##  append nfsquota (#users_id = users_id, #mach_id = mach_id,
1254 ##                   #device = device, #quota = quota)
1255 ##  repeat replace tbs (appends = tbs.appends + 1, modtime = "now")
1256 ##             where tbs.table = "nfsquota"
1257
1258     return(SMS_SUCCESS);
1259 ##}
1260
1261 /**
1262  ** delete_locker - special query routine for deleting a user locker
1263  **
1264  ** Inputs:
1265  **   argv[0] - users_id
1266  ** 
1267  ** Description:
1268  **   - get login name from users_id
1269  **   - get filesys entry from login
1270  **   - use filesys.mach_id and filesys.name to determine machine/device
1271  **     pair for nfsphys and nfsquota
1272  **   - delete filesys entry (label=<login>)
1273  **   - decrement allocated in nfsphys by quota
1274  **   - delete nfsquota entry
1275  **
1276  ** Errors:
1277  **   - SMS_FILESYS - no filesys exists for user
1278  ** 
1279  **/
1280
1281 delete_locker(q, argv)
1282     register struct query *q;
1283     register char *argv[];
1284 ##{
1285 ##  int users_id;
1286 ##  int mach_id;
1287 ##  int quota;
1288 ##  int rowcount;
1289 ##  char login[9];
1290 ##  char lname[64];
1291 ##  char ndev[32];
1292     register char *c;
1293
1294     /* copy arguments */
1295     users_id = *(int *)argv[0];
1296
1297 ##  range of u is users
1298 ##  range of f is filesys
1299 ##  range of np is nfsphys
1300 ##  range of nq is nfsquota
1301 ##  range of tbs is tblstats
1302
1303     /* get login name */
1304 ##  repeat retrieve (login = u.#login) where u.#users_id = @users_id
1305
1306     /* get mach_id and locker name from filesys entry; then delete it */
1307 ##  repeat retrieve (mach_id = f.#mach_id, lname = f.#name)
1308 ##         where f.#label = @login
1309 ##  inquire_equel (rowcount = "rowcount")
1310     if (rowcount == 0) return(SMS_FILESYS);
1311 ##  repeat delete f where f.#label = @login
1312
1313     /* get prefix directory */
1314     c = (char *)rindex(lname, '/');
1315     *c = 0;
1316
1317     /* get nfs device */
1318 ##  repeat retrieve (ndev = np.device) 
1319 ##         where np.#mach_id = @mach_id and np.dir = @lname
1320
1321     /* get quota from nfsquota entry; then delete entry */
1322 ##  repeat retrieve (quota = nq.#quota)
1323 ##         where nq.#mach_id = @mach_id and nq.#device = @ndev and
1324 ##               nq.#users_id = @users_id
1325 ##  repeat delete nq where nq.#mach_id = @mach_id and nq.#device = @ndev and
1326 ##                  nq.#users_id = @users_id
1327
1328     /* decrement nfsphys.allocated */
1329 ##  repeat replace np (allocated = np.allocated - @quota)
1330 ##         where np.#mach_id = @mach_id and np.#device = @ndev
1331
1332     /* adjust table statistics */
1333 ##  repeat replace tbs (deletes = tbs.deletes + 1, modtime = "now")
1334 ##             where tbs.table = "filesys"
1335 ##  repeat replace tbs (updates = tbs.updates + 1, modtime = "now")
1336 ##             where tbs.table = "nfsphys"
1337 ##  repeat replace tbs (deletes = tbs.deletes + 1, modtime = "now")
1338 ##             where tbs.table = "nfsquota"
1339
1340     return(SMS_SUCCESS);
1341 ##}
1342 \f
1343 /**
1344  ** add_user_group - create a group for a user and add user to group
1345  **
1346  ** Inputs:
1347  **   argv[0] - login
1348  **
1349  ** Description:
1350  **   - verify specified user exists
1351  **   - create a list of same name as user
1352  **   - add user as a member of the list
1353  **
1354  **/
1355
1356 add_user_group(q, argv)
1357     struct query *q;
1358     char *argv[];
1359 ##{
1360 ##  char *login;
1361 ##  int exists;
1362 ##  int users_id;
1363 ##  int list_id;
1364 ##  int gid;
1365
1366     login = argv[0];
1367
1368     /* verify user exists */
1369 ##  repeat retrieve (users_id = users.#users_id) where users.#login = @login
1370 ##  inquire_equel (exists = "rowcount")
1371     if (exists != 1) return(SMS_USER);
1372
1373     /* verify list does not exist */
1374 ##  repeat retrieve (exists = any(list.name where list.name = @login))
1375     if (exists) return(SMS_LIST);
1376
1377     /* get new list_id */
1378 ##  repeat retrieve (list_id = values.value) where values.name = "list_id"
1379     list_id++;
1380 ##  repeat replace values (value = @list_id) where values.name = "list_id"
1381
1382     /* create the list */
1383 ##  range of tbs is tblstats
1384 ##  repeat append list (name = @login, #list_id = @list_id, flags = 1,
1385 ##                      desc = "User Group", acl_id = @list_id,
1386 ##                      expdate = "today" + "5 years", modtime = "now")
1387 ##  repeat replace tbs (modtime = "now", appends = tbs.appends + 1)
1388 ##         where tbs.table = "list"
1389
1390     /* add user to list */
1391 ##  repeat append members (#list_id = @list_id, member_type = "USER",
1392 ##                         member_id = @users_id)
1393 ##  repeat replace tbs (modtime = "now", appends = tbs.appends + 1)
1394 ##         where tbs.table = "members"
1395
1396     /* get new gid */
1397 ##  range of g is groups
1398 ##  range of v is values
1399 ##  repeat retrieve (gid = v.value) where v.name = "gid"
1400     exists = 1;
1401     while (exists) {
1402         gid++;
1403 ##      repeat retrieve (exists = any(g.#gid where g.#gid = @gid))
1404     }
1405 ##  repeat replace v (value = @gid) where v.name = "gid"
1406     /* add list to group table */
1407 ##  repeat append groups (#list_id = @list_id, ltid = list.tid, #gid = @gid)
1408 ##      where list.#list_id = @list_id
1409
1410 ##  repeat replace tbs (modtime = "now", appends = tbs.appends + 1)
1411 ##              where tbs.table = "groups"
1412
1413 ##  repeat replace tbs (modtime = "now", appends = tbs.appends + 1)
1414 ##         where tbs.table = "members"
1415
1416     /* and we're done */
1417     return(SMS_SUCCESS);
1418 ##}
1419
1420 \f
1421 /**
1422  ** get_members_of_list - optimized query for retrieval of list members
1423  **
1424  ** Inputs:
1425  **   argv[0] - list_id
1426  **
1427  ** Description:
1428  **   - retrieve USER members, then LIST members, then STRING members
1429  **
1430  **/
1431
1432 get_members_of_list(q, argv, action, actarg)
1433     struct query *q;
1434     char *argv[];
1435     int (*action)();
1436     int actarg;
1437 ##{
1438 ##  int list_id;
1439 ##  char member_name[129];
1440     char *targv[2];
1441
1442     list_id = *(int *)argv[0];
1443     targv[0] = "USER";
1444     targv[1] = member_name;
1445
1446 ##  range of m is members
1447 ##  repeat retrieve (member_name = users.login)
1448 ##             where m.#list_id = @list_id and m.member_type = "USER"
1449 ##                   and m.member_id = users.users_id
1450 ##             sort by #member_name
1451 ##  {
1452          (*action)(2, targv, actarg);
1453 ##  }
1454
1455     targv[0] = "LIST";
1456 ##  repeat retrieve (member_name = list.name)
1457 ##             where m.#list_id = @list_id and m.member_type = "LIST"
1458 ##                   and m.member_id = list.#list_id
1459 ##             sort by #member_name
1460 ##  {
1461          (*action)(2, targv, actarg);
1462 ##  }
1463
1464     targv[0] = "STRING";
1465 ##  repeat retrieve (member_name = strings.string)
1466 ##             where m.#list_id = @list_id and m.member_type = "STRING"
1467 ##                   and m.member_id = strings.string_id
1468 ##             sort by #member_name
1469 ##  {
1470          (*action)(2, targv, actarg);
1471 ##  }
1472
1473     return(SMS_SUCCESS);
1474 ##}
1475
1476 /**
1477  ** get_groups_of_user - optimized query for retrieval of all groups to
1478  **                      which a user belongs
1479  **
1480  **/
1481
1482 get_groups_of_user(q, argv, action, actarg)
1483     struct query *q;
1484     char *argv[];
1485     int (*action)();
1486     int actarg;
1487 ##{
1488 ##  int users_id;
1489 ##  char list_name[33];
1490 ##  char gid[11];
1491 ##  int rowcount;
1492     char *targv[2];
1493
1494     users_id = *(int *)argv[0];
1495     targv[0] = list_name;
1496     targv[1] = gid;
1497
1498 ##  range of m is members
1499
1500 ##  repeat retrieve (list_name = list.name, gid = text(groups.#gid))
1501 ##         where m.member_id = @users_id and m.member_type = "USER" and
1502 ##               m.list_id = groups.list_id and groups.ltid = list.tid
1503 ##         sort by #list_name
1504 ##  {
1505          (*action)(2, targv, actarg);
1506 ##  }
1507 ##  inquire_equel (rowcount = "rowcount")
1508
1509     return ((rowcount = 0) ? SMS_NO_MATCH : SMS_SUCCESS);
1510 ##}
1511
1512 get_groups_of_all_users(q, argv, action, actarg)
1513     struct query *q;
1514     char *argv[];
1515     int (*action)();
1516     int actarg;
1517 ##{
1518 ##  char login[9];
1519 ##  char group[33];
1520 ##  char gid[11];
1521     char *targv[3];
1522 ##  int errorno;
1523
1524     targv[0] = login;
1525     targv[1] = group;
1526     targv[2] = gid;
1527
1528 ##  range of u is users
1529 ##  range of l is list
1530 ##  range of m is members
1531 ##  range of g is groups
1532
1533 ##  set lockmode session where readlock = nolock
1534
1535 ##  repeat retrieve (login = u.#login, group = l.name, gid = text(g.#gid))
1536 ##         where m.member_type = "USER" and m.member_id = u.users_id and
1537 ##               u.status != 0 and m.list_id = g.list_id and
1538 ##               g.ltid = l.tid
1539 ##         sort by #login, #group
1540 ##  {
1541          (*action)(3, targv, actarg);
1542 ##  }
1543
1544 ##  inquire_equel (errorno = "errorno")
1545 ##  set lockmode session where readlock = system
1546
1547     return((errorno) ? SMS_INGRES_ERR : SMS_SUCCESS);
1548 ##}
1549 \f
1550 /**
1551  ** get_all_poboxes - optimized query for retrieval of all poboxes
1552  **
1553  ** Description:
1554  **   - retrieve LOCAL boxes, then POP boxes, then FOREIGN boxes
1555  **
1556  **/
1557
1558 get_all_poboxes(q, argv, action, actarg)
1559     struct query *q;
1560     char *argv[];
1561     int (*action)();
1562     int actarg;
1563 ##{
1564 ##  char login[9];
1565 ##  char machine[129];
1566 ##  char box[129];
1567     char *targv[4];
1568
1569     targv[0] = login;
1570     targv[2] = machine;
1571     targv[3] = box;
1572
1573     targv[1] = "LOCAL";
1574
1575 ##  set lockmode session where readlock = nolock
1576 ##  range of p is pobox
1577 ##  repeat retrieve (login=users.#login, machine = #machine.name, box=p.#box)
1578 ##             where p.type = "LOCAL" and p.users_id = users.users_id
1579 ##                   and p.mach_id = #machine.mach_id
1580 ##  {
1581          (*action)(4, targv, actarg);
1582 ##  }
1583
1584     targv[1] = "POP";
1585 ##  repeat retrieve (login=users.#login, machine = #machine.name, box=p.#box)
1586 ##             where p.type = "POP" and p.users_id = users.users_id
1587 ##                   and p.mach_id = #machine.mach_id
1588 ##  {
1589          (*action)(4, targv, actarg);
1590 ##  }
1591
1592     targv[1] = "FOREIGN";
1593 ##  repeat retrieve (login=users.#login, machine=strings.string, box=p.#box)
1594 ##             where p.type = "FOREIGN" and p.users_id = users.users_id
1595 ##                   and p.mach_id = strings.string_id
1596 ##  {
1597          (*action)(4, targv, actarg);
1598 ##  }
1599 ##  set lockmode session where readlock = system
1600
1601     return(SMS_SUCCESS);
1602 ##}
1603
1604 get_new_poboxes(q, argv, action, actarg)
1605     struct query *q;
1606     char *argv[];
1607     int (*action)();
1608     int actarg;
1609 ##{
1610 ##  char *created;
1611 ##  char login[9];
1612 ##  char machine[129];
1613 ##  char box[129];
1614     char *targv[4];
1615
1616     created = argv[0];
1617
1618     targv[0] = login;
1619     targv[2] = machine;
1620     targv[3] = box;
1621
1622     targv[1] = "LOCAL";
1623 ##  range of p is pobox
1624 ##  repeat retrieve (login=users.#login, machine = #machine.name, box=p.#box)
1625 ##             where p.type = "LOCAL" and p.users_id = users.users_id
1626 ##                   and p.mach_id = #machine.mach_id and
1627 ##                   p.#created > @created
1628 ##  {
1629          (*action)(4, targv, actarg);
1630 ##  }
1631
1632     targv[1] = "POP";
1633 ##  repeat retrieve (login=users.#login, machine = #machine.name, box=p.#box)
1634 ##             where p.type = "POP" and p.users_id = users.users_id
1635 ##                   and p.mach_id = #machine.mach_id and
1636 ##                   p.#created > @created
1637 ##  {
1638          (*action)(4, targv, actarg);
1639 ##  }
1640
1641     targv[1] = "FOREIGN";
1642 ##  repeat retrieve (login=users.#login, machine=strings.string, box=p.#box)
1643 ##             where p.type = "FOREIGN" and p.users_id = users.users_id
1644 ##                   and p.mach_id = strings.string_id and
1645 ##                   p.#created > @created
1646 ##  {
1647          (*action)(4, targv, actarg);
1648 ##  }
1649
1650     return(SMS_SUCCESS);
1651 ##}
1652 \f
1653 /* Validation Routines */
1654
1655 validate_row(q, argv, v)
1656     register struct query *q;
1657     char *argv[];
1658     register struct validate *v;
1659 ##{
1660 ##  char *rvar;
1661 ##  char *table;
1662 ##  char *name;
1663 ##  char qual[128];
1664 ##  int rowcount;
1665
1666     /* build where clause */
1667     build_qual(v->qual, v->argc, argv, qual);
1668
1669     /* setup ingres variables */
1670     rvar = q->rvar;
1671     table = q->rtable;
1672     name = v->field;
1673
1674     if (log_flags & LOG_RES)
1675         /* tell the logfile what we're doing */
1676         com_err(whoami, 0, "validating row: %s", qual);
1677     
1678     /* look for the record */
1679 ##  range of rvar is table
1680 ##  retrieve (rowcount = count(rvar.name where qual))
1681     if (rowcount == 0) return(SMS_NO_MATCH);
1682     if (rowcount > 1) return(SMS_NOT_UNIQUE);
1683     return(SMS_EXISTS);
1684 ##}
1685
1686 validate_fields(q, argv, vo, n)
1687     struct query *q;
1688     register char *argv[];
1689     register struct valobj *vo;
1690     register int n;
1691 {
1692     register int status;
1693
1694     while (--n >= 0) {
1695         switch (vo->type) {
1696         case V_NAME:
1697             if (log_flags & LOG_RES)
1698                 com_err(whoami, 0, "validating %s in %s: %s", 
1699                     vo->namefield, vo->table, argv[vo->index]);
1700             status = validate_name(argv, vo);
1701             break;
1702
1703         case V_ID:
1704             if (log_flags & LOG_RES)
1705                 com_err(whoami, 0, "validating %s in %s: %s", 
1706                     vo->idfield, vo->table, argv[vo->index]);
1707             status = validate_id(argv, vo);
1708             break;
1709
1710         case V_DATE:
1711             if (log_flags & LOG_RES)
1712                 com_err(whoami, 0, "validating date: %s", argv[vo->index]);
1713             status = validate_date(argv, vo);
1714             break;
1715
1716         case V_TYPE:
1717             if (log_flags & LOG_RES)
1718                 com_err(whoami, 0, "validating %s type: %s",
1719                     vo->table, argv[vo->index]);
1720             status = validate_type(argv, vo);
1721             break;
1722
1723         case V_TYPEDATA:
1724             if (log_flags & LOG_RES)
1725                 com_err(whoami, 0, "validating typed data (%s): %s",
1726                     argv[vo->index - 1], argv[vo->index]);
1727             status = validate_typedata(q, argv, vo);
1728             break;
1729
1730         case V_FOLLOWUP:
1731             status = SMS_EXISTS;
1732             break;
1733
1734         case V_SORT:
1735             status = SMS_EXISTS;
1736             break;
1737
1738         }
1739
1740         if (status != SMS_EXISTS) return(status);
1741         vo++;
1742     }
1743
1744     return(SMS_SUCCESS);
1745 }
1746
1747 validate_id(argv, vo)
1748     char *argv[];
1749     register struct valobj *vo;
1750 ##{
1751 ##  char *name;
1752 ##  char *table;
1753 ##  char *namefield;
1754 ##  char *idfield;
1755 ##  int id;
1756 ##  int rowcount;
1757     register char *c;
1758
1759     name = argv[vo->index];
1760     table = vo->table;
1761     /* minor kludge to upcasify machine names */
1762     if (!bcmp(table, "machine", 7))
1763         for (c = name; *c; c++) if (islower(*c)) *c = toupper(*c);
1764     namefield = vo->namefield;
1765     idfield = vo->idfield;
1766     if (!bcmp(namefield, "uid", 4)) {
1767 ##    retrieve (id = table.idfield) where table.namefield = int4(name)
1768 ##    inquire_equel (rowcount = "rowcount")
1769     } else {
1770 ##    retrieve (id = table.idfield) where table.namefield = name
1771 ##    inquire_equel (rowcount = "rowcount")
1772     }
1773     if (rowcount != 1) return(vo->error);
1774     *(int *)argv[vo->index] = id;
1775     return(SMS_EXISTS);
1776 ##}
1777
1778 validate_name(argv, vo)
1779     char *argv[];
1780     register struct valobj *vo;
1781 ##{
1782 ##  char *name;
1783 ##  char *table;
1784 ##  char *namefield;
1785 ##  int rowcount;
1786
1787     name = argv[vo->index];
1788     table = vo->table;
1789     namefield = vo->namefield;
1790 ##  retrieve (rowcount = countu(table.namefield 
1791 ##            where table.namefield = name))
1792     return ((rowcount == 1) ? SMS_EXISTS : vo->error);
1793 ##}
1794
1795 /* Special validation routine for get_filesys */
1796 validate_label(q, argv)
1797     struct query *q;
1798     char *argv[];
1799 {
1800     register char *label;
1801     register char *c;
1802
1803     label = argv[0];
1804     c = (char *)index(label, '*');
1805     if (c == label) return(SMS_NOT_UNIQUE);
1806     return(SMS_SUCCESS);
1807 }
1808
1809 /* Special validation routine for delete_filesys */
1810 validate_label_2(q, argv)
1811     struct query *q;
1812     char *argv[];
1813 {
1814     if (index(argv[0], '*') != 0) return(SMS_NOT_UNIQUE);
1815     return(SMS_SUCCESS);
1816 }
1817
1818 validate_date(argv, vo)
1819     char *argv[];
1820     struct valobj *vo;
1821 ##{
1822 ##  char *idate;
1823 ##  double dd;
1824 ##  int errorno;
1825
1826     idate = argv[vo->index];
1827
1828 ##  retrieve (dd = interval("years", date(idate) - date("today")))
1829 ##  inquire_equel (errorno = "errorno")
1830     if (errorno != 0 || dd > 5.0) return(SMS_DATE);
1831     return(SMS_SUCCESS);
1832 ##}
1833
1834 validate_type(argv, vo)
1835     char *argv[];
1836     register struct valobj *vo;
1837 ##{
1838 ##  char *typename;
1839 ##  char *value;
1840 ##  int rowcount;
1841     register char *c;
1842
1843     typename = vo->table;
1844     value = argv[vo->index];
1845
1846     /* uppercase type fields */
1847     for (c = value; *c; c++) if (islower(*c)) *c = toupper(*c);
1848
1849 ##  range of a is alias
1850 ##  repeat retrieve (rowcount = count(a.trans where a.name = @typename and
1851 ##                                    a.type = "TYPE" and
1852 ##                                    a.trans = @value))
1853     return ((rowcount == 1) ? SMS_EXISTS : vo->error);
1854 ##}
1855
1856 /* validate member or type-specific data field */
1857
1858 validate_typedata(q, argv, vo)
1859     register struct query *q;
1860     register char *argv[];
1861     register struct valobj *vo;
1862 ##{
1863 ##  char *name;
1864 ##  char *field_type;
1865 ##  char data_type[17];
1866 ##  int id;
1867 ##  int refc;
1868 ##  int rowcount;
1869     register char *c;
1870
1871     /* get named object */
1872     name = argv[vo->index];
1873
1874     /* get field type string (known to be at index-1) */
1875     field_type = argv[vo->index-1];
1876
1877     /* get corresponding data type associated with field type name */
1878 ##  repeat retrieve (data_type = alias.trans) 
1879 ##         where alias.#name = @field_type and alias.type = "TYPEDATA"
1880 ##  inquire_equel (rowcount = "rowcount")
1881     if (rowcount != 1) return(SMS_TYPE);
1882
1883     /* now retrieve the record id corresponding to the named object */
1884
1885     if (!strcmp(data_type, "user")) {
1886         /* USER */
1887 ##      repeat retrieve (id = users.users_id) where users.login = @name
1888 ##      inquire_equel (rowcount = "rowcount")
1889         if (rowcount != 1) return(SMS_USER);
1890
1891     } else if (!strcmp(data_type, "list")) {
1892         /* LIST */
1893 ##      repeat retrieve (id = list.list_id) where list.#name = @name
1894 ##      inquire_equel (rowcount = "rowcount")
1895         if (rowcount != 1) return(SMS_LIST);
1896
1897     } else if (!strcmp(data_type, "machine")) {
1898         /* MACHINE */
1899         for (c = name; *c; c++) if (islower(*c)) *c = toupper(*c);
1900 ##      repeat retrieve (id = machine.mach_id) where machine.#name = @name
1901 ##      inquire_equel (rowcount = "rowcount")
1902         if (rowcount != 1) return(SMS_MACHINE);
1903
1904     } else if (!strcmp(data_type, "string")) {
1905         /* STRING */
1906 ##      range of s is strings
1907 ##      repeat retrieve (id = s.string_id, refc = s.#refc) 
1908 ##             where s.string = @name
1909 ##      inquire_equel (rowcount = "rowcount")
1910         if (rowcount == 0) {
1911             if (q->type != APPEND) return(SMS_STRING);
1912 ##          range of v is values
1913 ##          retrieve (id = v.value) where v.#name = "strings_id"
1914             id++;
1915 ##          replace v (value = id) where v.#name = "strings_id"
1916 ##          append to strings (string_id = id, string = name, #refc = 1)
1917         } else if (rowcount == 1) {
1918             if (q->type == APPEND || q->type == DELETE) {
1919                 refc += (q->type == APPEND) ? 1 : -1;
1920                 if (refc > 0) {
1921 ##                  replace s (#refc = refc) where s.string_id = id
1922                 } else {
1923 ##                  delete s where s.string_id = id
1924                 }
1925             }
1926         }
1927     } else {
1928         return(SMS_TYPE);
1929     }
1930
1931     /* now set value in argv */
1932     *(int *)argv[vo->index] = id;
1933     
1934     return (SMS_EXISTS);
1935 ##}
1936
1937 \f
1938 translate_ids(q, sq, v, action, actarg)
1939     register struct query *q;
1940     register struct save_queue *sq;
1941     register struct validate *v;
1942     register int (*action)();
1943     int actarg;
1944 ##{
1945 ##  char *name;
1946 ##  char *field_type;
1947 ##  char data_type[17];
1948 ##  int id;
1949 ##  int rowcount;
1950     register int i;
1951     struct valobj *vo;
1952     char **argv;
1953
1954     for (i = 0; i < v->objcnt; i++) {
1955         vo = &v->valobj[i];
1956         if (vo->type == V_FOLLOWUP) break;
1957     }
1958
1959     /* for each row */
1960     while (sq_get_data(sq, &argv)) {
1961
1962         /* get object id */
1963         i = vo->index;
1964         sscanf(argv[i], "%d", &id);
1965         free(argv[i]);
1966         name = (char *)malloc(129);
1967         argv[i] = name;
1968
1969         /* get field type string (known to be at index-1) */
1970         field_type = argv[vo->index-1];
1971
1972         /* get corresponding data type associated with field type name */
1973 ##      repeat retrieve (data_type = alias.trans) 
1974 ##             where alias.#name = @field_type and alias.type = "TYPEDATA"
1975 ##      inquire_equel (rowcount = "rowcount")
1976         if (rowcount != 1) {
1977             sprintf(name, "%d", id);
1978             (*action)(q->vcnt, argv, actarg);
1979             continue;
1980         }
1981
1982         /* retrieve object name */
1983
1984         if (!strcmp(data_type, "user")) {
1985             /* USER */
1986 ##          repeat retrieve (name = users.login) where users.users_id = @id
1987 ##          inquire_equel (rowcount = "rowcount")
1988
1989         } else if (!strcmp(data_type, "list")) {
1990             /* LIST */
1991 ##          repeat retrieve (name = list.#name) where list.list_id = @id
1992 ##          inquire_equel (rowcount = "rowcount")
1993
1994         } else if (!strcmp(data_type, "machine")) {
1995             /* MACHINE */
1996 ##          repeat retrieve (name = machine.#name) where machine.mach_id = @id
1997 ##          inquire_equel (rowcount = "rowcount")
1998
1999         } else if (!strcmp(data_type, "string")) {
2000             /* STRING */
2001 ##          repeat retrieve (name = strings.string) 
2002 ##                 where strings.string_id = @id
2003 ##          inquire_equel (rowcount = "rowcount")
2004
2005         } else {
2006             rowcount = 0;
2007         }
2008
2009         /* if there wasn't a corresponding object name, then use the id */
2010         if (rowcount != 1) sprintf(name, "%d", id);
2011
2012         /* send the data */
2013         (*action)(q->vcnt, argv, actarg);
2014
2015         /* free saved data */
2016         for (i = 0; i < q->vcnt; i++) 
2017             free(argv[i]);
2018         free(argv);
2019     }
2020
2021     sq_destroy(sq);
2022     return (SMS_SUCCESS);
2023 ##}
2024 \f
2025 /*
2026  * Local Variables:
2027  * mode: c
2028  * c-indent-level: 4
2029  * c-continued-statement-offset: 4
2030  * c-brace-offset: -4
2031  * c-argdecl-indent: 4
2032  * c-label-offset: -4
2033  * End:
2034  */
This page took 0.198212 seconds and 5 git commands to generate.