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