6 * Copyright (C) 1987 by the Massachusetts Institute of Technology
7 * For copying and distribution information, please see the file
13 static char *rcsid_qsupport_dc = "$Header$";
16 #include <mit-copyright.h>
17 #include "mr_server.h"
20 EXEC SQL INCLUDE sqlca;
24 extern int dbms_errno, mr_errcode;
26 EXEC SQL BEGIN DECLARE SECTION;
27 extern char stmt_buf[];
28 EXEC SQL END DECLARE SECTION;
30 EXEC SQL WHENEVER SQLERROR DO dbmserr();
33 /* Specialized Access Routines */
35 /* access_user - verify that client name equals specified login name
37 * - since field validation routines are called first, a users_id is
38 * now in argv[0] instead of the login name.
41 int access_user(q, argv, cl)
46 if (cl->users_id != *(int *)argv[0])
54 /* access_login - verify that client name equals specified login name
56 * argv[0...n] contain search info. q->
59 int access_login(q, argv, cl)
64 EXEC SQL BEGIN DECLARE SECTION;
66 EXEC SQL END DECLARE SECTION;
68 if(q->argc != 1) return MR_ARGS;
70 if(!strcmp(q->shortname, "gual")) {
71 EXEC SQL SELECT users_id INTO :id FROM users u, strings str
72 WHERE u.login LIKE :argv[0] AND u.users_id != 0
73 AND u.comments = str.string_id;
74 } else if (!strcmp(q->shortname, "gubl")) {
75 EXEC SQL SELECT users_id INTO :id FROM users u
76 WHERE u.login LIKE :argv[0] AND u.users_id != 0;
77 } else if (!strcmp(q->shortname, "guau")) {
78 EXEC SQL SELECT users_id INTO :id FROM users u, strings str
79 WHERE u.unix_uid = :argv[0] AND u.users_id != 0
80 AND u.comments = str.string_id;
81 } else if(!strcmp(q->shortname, "gubu")) {
82 EXEC SQL SELECT users_id INTO :id FROM users u
83 WHERE u.unix_uid = :argv[0] AND u.users_id != 0;
86 if (sqlca.sqlcode == SQL_NO_MATCH)
87 return(MR_NO_MATCH); /* ought to be MR_USER, but this is what
88 gual returns, so we have to be consistent */
89 else if (sqlca.sqlerrd[2] != 1 || id != cl->users_id)
97 /* access_list - check access for most list operations
99 * Inputs: argv[0] - list_id
101 * argv[2] - member ID (only for queries "amtl" and "dmfl")
102 * argv[7] - group IID (only for query "ulis")
105 * - check that client is a member of the access control list
106 * - OR, if the query is add_member_to_list or delete_member_from_list
107 * and the list is public, allow access if client = member
110 int access_list(q, argv, cl)
115 EXEC SQL BEGIN DECLARE SECTION;
116 int list_id, acl_id, flags, gid, users_id;
117 char acl_type[9], *newname;
118 EXEC SQL END DECLARE SECTION;
121 list_id = *(int *)argv[0];
122 EXEC SQL SELECT acl_id, acl_type, gid, publicflg
123 INTO :acl_id, :acl_type, :gid, :flags
125 WHERE list_id = :list_id;
127 if (sqlca.sqlerrd[2] != 1)
130 /* if amtl or dmfl and list is public allow client to add or delete self */
131 if (((!strcmp("amtl", q->shortname) && flags) ||
132 (!strcmp("dmfl", q->shortname)))) {
133 if (!strcmp("USER", argv[1]) && *(int *)argv[2] == cl->users_id)
135 if (!strcmp("KERBEROS", argv[1]) && *(int *)argv[2] == -cl->client_id)
137 /* if update_list, don't allow them to change the GID or rename to
138 a username other than their own */
139 } else if (!strcmp("ulis", q->shortname)) {
140 if (!strcmp(argv[7], UNIQUE_GID)) {
141 if(gid != -1) return MR_PERM;
143 if(gid != atoi(argv[7])) return MR_PERM;
146 EXEC SQL SELECT users_id INTO :users_id FROM users
147 WHERE login=:newname;
148 if ((sqlca.sqlcode != SQL_NO_MATCH) && (users_id != cl->users_id))
152 /* check for client in access control list */
153 status = find_member(acl_type, acl_id, cl);
154 if (!status) return(MR_PERM);
160 /* access_visible_list - allow access to list only if it is not hidden,
161 * or if the client is on the ACL
163 * Inputs: argv[0] - list_id
164 * cl - client identifier
167 int access_visible_list(q, argv, cl)
172 EXEC SQL BEGIN DECLARE SECTION;
173 int list_id, acl_id, flags ;
175 EXEC SQL END DECLARE SECTION;
178 list_id = *(int *)argv[0];
179 EXEC SQL SELECT hidden, acl_id, acl_type
180 INTO :flags, :acl_id, :acl_type
182 WHERE list_id = :list_id;
183 if (sqlca.sqlerrd[2] != 1)
188 /* check for client in access control list */
189 status = find_member(acl_type, acl_id, cl);
197 /* access_vis_list_by_name - allow access to list only if it is not hidden,
198 * or if the client is on the ACL
200 * Inputs: argv[0] - list name
201 * cl - client identifier
204 int access_vis_list_by_name(q, argv, cl)
209 EXEC SQL BEGIN DECLARE SECTION;
210 int acl_id, flags, rowcount;
211 char acl_type[9], *listname;
212 EXEC SQL END DECLARE SECTION;
216 EXEC SQL SELECT hidden, acl_id, acl_type INTO :flags, :acl_id, :acl_type
217 FROM list WHERE name = :listname;
219 rowcount=sqlca.sqlerrd[2];
227 /* check for client in access control list */
228 status = find_member(acl_type, acl_id, cl);
236 /* access_member - allow user to access member of type "USER" and name matches
237 * username, or to access member of type "KERBEROS" and the principal matches
238 * the user, or to access member of type "LIST" and list is one that user is
239 * on the acl of, or the list is visible.
242 int access_member(q, argv, cl)
247 if (!strcmp(argv[0], "LIST") || !strcmp(argv[0], "RLIST"))
248 return(access_visible_list(q, &argv[1], cl));
250 if (!strcmp(argv[0], "USER") || !strcmp(argv[0], "RUSER")) {
251 if (cl->users_id == *(int *)argv[1])
255 if (!strcmp(argv[0], "KERBEROS") || !strcmp(argv[0], "RKERBEROS")) {
256 if (cl->client_id == -*(int *)argv[1])
264 /* access_qgli - special access routine for Qualified_get_lists. Allows
265 * access iff argv[0] == "TRUE" and argv[2] == "FALSE".
268 int access_qgli(q, argv, cl)
273 if (!strcmp(argv[0], "TRUE") && !strcmp(argv[2], "FALSE"))
279 /* access_service - allow access if user is on ACL of service. Don't
280 * allow access if a wildcard is used.
283 int access_service(q, argv, cl)
288 EXEC SQL BEGIN DECLARE SECTION;
290 char *name, acl_type[9];
291 EXEC SQL END DECLARE SECTION;
296 for(c=name;*c;c++) if(islower(*c)) *c = toupper(*c); /* uppercasify */
297 EXEC SQL SELECT acl_id, acl_type INTO :acl_id, :acl_type FROM servers
299 if (sqlca.sqlerrd[2] > 1)
302 /* check for client in access control list */
303 status = find_member(acl_type, acl_id, cl);
304 if (!status) return(MR_PERM);
310 /* access_filesys - verify that client is owner or on owners list of filesystem
314 int access_filesys(q, argv, cl)
319 EXEC SQL BEGIN DECLARE SECTION;
320 int users_id, list_id;
322 EXEC SQL END DECLARE SECTION;
326 EXEC SQL SELECT owner, owners INTO :users_id, :list_id FROM filesys
329 if (sqlca.sqlerrd[2] != 1)
331 if (users_id == cl->users_id)
333 status = find_member("LIST", list_id, cl);
341 /* access_host - successful if owner of host, or subnet containing host
344 int host_access_level = 0; /* 1 for network, 2 for host */
346 int access_host(q, argv, cl)
351 EXEC SQL BEGIN DECLARE SECTION;
353 char mtype[9], stype[9];
354 EXEC SQL END DECLARE SECTION;
357 if (q->type == APPEND) {
358 id = *(int *)argv[8];
359 EXEC SQL SELECT s.owner_type, s.owner_id
360 INTO :stype, :sid FROM subnet s
363 } else if (q->type == RETRIEVE) {
364 if (strcmp(argv[0], "*") || strcmp(argv[1], "*") ||
365 strcmp(argv[2], "*") || strcmp(argv[3], "*"))
370 id = *(int *)argv[0];
371 EXEC SQL SELECT m.owner_type, m.owner_id, s.owner_type, s.owner_id
372 INTO :mtype, :mid, :stype, :sid FROM machine m, subnet s
373 WHERE m.mach_id=:id and s.snet_id=m.snet_id;
375 if (sqlca.sqlerrd[2] != 1)
378 status = find_member(stype, sid, cl);
380 host_access_level = 1;
383 status = find_member(mtype, mid, cl);
385 host_access_level = 2;
392 /* access_ahal - check for adding a host alias.
393 * successful if host has less then 2 aliases and (client is owner of
395 * If deleting an alias, any owner will do.
398 int access_ahal(q, argv, cl)
403 EXEC SQL BEGIN DECLARE SECTION;
404 int cnt, id, mid, sid;
405 char mtype[256], stype[256];
406 EXEC SQL END DECLARE SECTION;
409 if (q->type == RETRIEVE)
412 id = *(int *)argv[1];
414 EXEC SQL SELECT count(name) INTO :cnt from hostalias WHERE mach_id = :id;
415 if (dbms_errno) return(mr_errcode);
416 /* if the type is APPEND, this is ahal and we need to make sure there
417 * will be no more than 2 aliases. If it's not, it must be dhal and
420 if (q->type == APPEND && cnt >= 2)
422 EXEC SQL SELECT m.owner_type, m.owner_id, s.owner_type, s.owner_id
423 INTO :mtype, :mid, :stype, :sid FROM machine m, subnet s
424 WHERE m.mach_id=:id and s.snet_id=m.snet_id;
425 status = find_member(mtype, mid, cl);
428 status = find_member(stype, sid, cl);
437 /* access_snt - check for retrieving network structure
440 int access_snt(q, argv, cl)
445 if(q->type == RETRIEVE)