3 * Check access to queries
5 * Copyright (C) 1987-1998 by the Massachusetts Institute of Technology
6 * For copying and distribution information, please see the file
10 #include <mit-copyright.h>
11 #include "mr_server.h"
18 EXEC SQL INCLUDE sqlca;
23 extern int dbms_errno, mr_errcode;
25 EXEC SQL WHENEVER SQLERROR DO dbmserr();
28 /* Specialized Access Routines */
30 /* access_user - verify that client name equals specified login name
32 * - since field validation routines are called first, a users_id is
33 * now in argv[0] instead of the login name.
36 int access_user(struct query *q, char *argv[], client *cl)
38 if (cl->users_id != *(int *)argv[0])
46 /* access_login - verify that client name equals specified login name
48 * argv[0...n] contain search info. q->
51 int access_login(struct query *q, char *argv[], client *cl)
53 EXEC SQL BEGIN DECLARE SECTION;
55 EXEC SQL END DECLARE SECTION;
60 if (!strcmp(q->shortname, "gual"))
62 EXEC SQL SELECT users_id INTO :id FROM users
63 WHERE login = :argv[0] AND users_id != 0;
65 else if (!strcmp(q->shortname, "gubl"))
67 EXEC SQL SELECT users_id INTO :id FROM users u
68 WHERE u.login = :argv[0] AND u.users_id != 0;
70 else if (!strcmp(q->shortname, "guau"))
72 EXEC SQL SELECT users_id INTO :id FROM users
73 WHERE unix_uid = :argv[0] AND users_id != 0;
75 else if (!strcmp(q->shortname, "gubu"))
77 EXEC SQL SELECT users_id INTO :id FROM users u
78 WHERE u.unix_uid = :argv[0] AND u.users_id != 0;
81 if (sqlca.sqlcode == SQL_NO_MATCH)
82 return MR_NO_MATCH; /* ought to be MR_USER, but this is what
83 gual returns, so we have to be consistent */
84 else if (sqlca.sqlerrd[2] != 1 || id != cl->users_id)
91 /* access_spob - check access for set_pobox */
93 int access_spob(struct query *q, char *argv[], client *cl)
95 EXEC SQL BEGIN DECLARE SECTION;
97 EXEC SQL END DECLARE SECTION;
99 if (!strcmp(argv[1], "IMAP"))
101 EXEC SQL SELECT owner INTO :id FROM filesys f
102 WHERE f.label = :argv[2] AND f.type = 'IMAP' AND
103 f.lockertype = 'USER';
104 if (cl->users_id != id)
107 if (cl->users_id != *(int *)argv[0])
114 /* access_list - check access for most list operations
116 * Inputs: argv[0] - list_id
118 * argv[2] - member ID (only for queries "amtl" and "dmfl")
119 * argv[7] - group ID (only for query "ulis")
122 * - check that client is a member of the access control list
123 * - OR, if the query is add_member_to_list or delete_member_from_list
124 * and the list is public, allow access if client = member
127 int access_list(struct query *q, char *argv[], client *cl)
129 EXEC SQL BEGIN DECLARE SECTION;
130 int list_id, acl_id, flags, gid, users_id, member_id, member_acl_id;
132 char acl_type[LIST_ACL_TYPE_SIZE], name[LIST_NAME_SIZE], *newname;
133 char member_acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
134 EXEC SQL END DECLARE SECTION;
137 list_id = *(int *)argv[0];
138 member_id = *(int *)argv[2];
139 EXEC SQL SELECT acl_id, acl_type, memacl_id, memacl_type,
141 INTO :acl_id, :acl_type, :memacl_id, :memacl_type,
144 WHERE list_id = :list_id;
146 if (sqlca.sqlerrd[2] != 1)
149 /* if update_list, don't allow them to change the GID or rename to a
150 username other than their own */
151 if (!strcmp("ulis", q->shortname))
153 if (!strcmp(argv[7], UNIQUE_GID))
160 if (gid != atoi(argv[7]))
166 if (!strcmp("ulis", q->shortname))
168 /* Check that it doesn't conflict with the Grouper namespace. */
169 if (strlen(newname) > 4 && isdigit(newname[2]) &&
170 isdigit(newname[3]) && newname[4] == '-')
172 if (!strncasecmp(newname, "fa", 2) ||
173 !strncasecmp(newname, "sp", 2) ||
174 !strncasecmp(newname, "su", 2) ||
175 !strncasecmp(newname, "ja", 2))
179 /* Don't let anyone take owner-foo list names. They interact
180 * weirdly with the aliases automatically generated by
183 if (!strncasecmp(newname, "owner-", 6))
187 EXEC SQL SELECT users_id INTO :users_id FROM users
188 WHERE login = :newname;
189 if ((sqlca.sqlcode != SQL_NO_MATCH) && strcmp(strtrim(name), newname) &&
190 (users_id != cl->users_id))
194 /* check for client in access control list and return success right
195 * away if it's there. */
196 if (find_member(acl_type, acl_id, cl))
199 /* If not amtl, atml, or dmfl, we lose. */
200 if (strcmp(q->shortname, "amtl") && strcmp(q->shortname, "atml") &&
201 strcmp(q->shortname, "dmfl") && strcmp(q->shortname, "tmol"))
204 if (find_member(memacl_type, memacl_id, cl))
207 if (flags || q->type == DELETE)
209 if (!strcmp("USER", argv[1]) && *(int *)argv[2] == cl->users_id)
211 if (!strcmp("KERBEROS", argv[1]) && *(int *)argv[2] == -cl->client_id)
213 if (!strcmp("LIST", argv[1]) && !strcmp("dmfl", q->shortname))
215 EXEC SQL SELECT acl_id, acl_type INTO :member_acl_id,
218 WHERE list_id = :member_id;
220 if (find_member(member_acl_type, member_acl_id, cl))
225 /* Otherwise fail. */
230 /* access_visible_list - allow access to list only if it is not hidden,
231 * or if the client is on the ACL
233 * Inputs: argv[0] - list_id
234 * cl - client identifier
237 int access_visible_list(struct query *q, char *argv[], client *cl)
239 EXEC SQL BEGIN DECLARE SECTION;
240 int list_id, acl_id, memacl_id, flags ;
241 char acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
242 EXEC SQL END DECLARE SECTION;
245 list_id = *(int *)argv[0];
246 EXEC SQL SELECT hidden, acl_id, acl_type, memacl_id, memacl_type
247 INTO :flags, :acl_id, :acl_type, :memacl_id, :memacl_type
249 WHERE list_id = :list_id;
250 if (sqlca.sqlerrd[2] != 1)
255 /* check for client in access control list */
256 status = find_member(acl_type, acl_id, cl);
259 status = find_member(memacl_type, memacl_id, cl);
267 /* access_vis_list_by_name - allow access to list only if it is not hidden,
268 * or if the client is on the ACL
270 * Inputs: argv[0] - list name
271 * cl - client identifier
274 int access_vis_list_by_name(struct query *q, char *argv[], client *cl)
276 EXEC SQL BEGIN DECLARE SECTION;
277 int acl_id, memacl_id, flags, rowcount;
278 char acl_type[LIST_ACL_TYPE_SIZE], memacl_type[LIST_ACL_TYPE_SIZE];
280 EXEC SQL END DECLARE SECTION;
284 EXEC SQL SELECT hidden, acl_id, acl_type, memacl_id, memacl_type
285 INTO :flags, :acl_id, :acl_type, :memacl_id, :memacl_type
287 WHERE name = :listname;
289 rowcount = sqlca.sqlerrd[2];
297 /* check for client in access control list */
298 status = find_member(acl_type, acl_id, cl);
301 status = find_member(memacl_type, memacl_id, cl);
309 /* access_member - allow user to access member of type "USER" and name matches
310 * username, or to access member of type "KERBEROS" and the principal matches
311 * the user, or to access member of type "LIST" and list is one that user is
312 * on the acl of, or the list is visible.
315 int access_member(struct query *q, char *argv[], client *cl)
317 if (!strcmp(argv[0], "LIST") || !strcmp(argv[0], "RLIST"))
318 return access_visible_list(q, &argv[1], cl);
320 if (!strcmp(argv[0], "USER") || !strcmp(argv[0], "RUSER"))
322 if (cl->users_id == *(int *)argv[1])
326 if (!strcmp(argv[0], "KERBEROS") || !strcmp(argv[0], "RKERBEROS"))
328 if (cl->client_id == -*(int *)argv[1])
336 /* access_qgli - special access routine for Qualified_get_lists. Allows
337 * access iff argv[0] == "TRUE" and argv[2] == "FALSE".
340 int access_qgli(struct query *q, char *argv[], client *cl)
342 if (!strcmp(argv[0], "TRUE") && !strcmp(argv[2], "FALSE"))
348 /* access_service - allow access if user is on ACL of service. Don't
349 * allow access if a wildcard is used.
352 int access_service(struct query *q, char *argv[], client *cl)
354 EXEC SQL BEGIN DECLARE SECTION;
356 char *name, acl_type[LIST_ACL_TYPE_SIZE];
357 EXEC SQL END DECLARE SECTION;
362 for (c = name; *c; c++)
367 EXEC SQL SELECT acl_id, acl_type INTO :acl_id, :acl_type FROM servers
369 if (sqlca.sqlerrd[2] > 1)
372 /* check for client in access control list */
373 status = find_member(acl_type, acl_id, cl);
381 /* access_filesys - verify that client is owner or on owners list of filesystem
385 int access_filesys(struct query *q, char *argv[], client *cl)
387 EXEC SQL BEGIN DECLARE SECTION;
388 int users_id, list_id;
390 EXEC SQL END DECLARE SECTION;
394 EXEC SQL SELECT owner, owners INTO :users_id, :list_id FROM filesys
397 if (sqlca.sqlerrd[2] != 1)
399 if (users_id == cl->users_id)
401 status = find_member("LIST", list_id, cl);
409 /* access_host - successful if owner of host, or subnet containing host
412 int access_host(struct query *q, char *argv[], client *cl)
414 EXEC SQL BEGIN DECLARE SECTION;
416 char mtype[MACHINE_OWNER_TYPE_SIZE], stype[SUBNET_OWNER_TYPE_SIZE];
417 EXEC SQL END DECLARE SECTION;
425 if (q->type == RETRIEVE)
427 if (strcmp(argv[0], "*") || strcmp(argv[1], "*") ||
428 strcmp(argv[2], "*") || strcmp(argv[3], "*"))
434 if (q->type == APPEND)
436 /* Non-query owner must set use to zero */
437 if (atoi(argv[6 + idx]) != 0)
440 /* ... and start the hostname with a letter */
441 if (isdigit(argv[0][0]))
444 id = *(int *)argv[8 + idx];
445 EXEC SQL SELECT s.owner_type, s.owner_id
446 INTO :stype, :sid FROM subnet s
447 WHERE s.snet_id = :id;
450 if (find_member(stype, sid, cl))
455 else /* q-type == UPDATE */
457 EXEC SQL BEGIN DECLARE SECTION;
458 int status, acomment, use, ocomment, snid;
459 char contact[MACHINE_CONTACT_SIZE], address[MACHINE_ADDRESS_SIZE];
460 char name[MACHINE_NAME_SIZE];
461 char billing_contact[MACHINE_BILLING_CONTACT_SIZE];
462 EXEC SQL END DECLARE SECTION;
464 id = *(int *)argv[0];
465 EXEC SQL SELECT m.name, m.use, m.contact, m.billing_contact, m.status,
466 m.address, m.owner_type, m.owner_id, m.acomment, m.ocomment, m.snet_id,
467 s.owner_type, s.owner_id INTO :name, :use, :contact, :billing_contact,
468 :status, :address, :mtype, :mid, :acomment, :ocomment, :snid, :stype,
470 FROM machine m, subnet s
471 WHERE m.mach_id = :id AND s.snet_id = m.snet_id;
475 /* non-query-owner cannot change use or ocomment */
476 if ((use != atoi(argv[7 + idx])) || (ocomment != *(int *)argv[14 + idx]))
479 /* or rename to start with digit */
480 if (isdigit(argv[1][0]) && strcmp(strtrim(name), argv[1]))
483 if (!find_member(stype, sid, cl))
485 if (find_member(mtype, mid, cl))
487 /* host owner also cannot change contact, status, address,
488 owner, or acomment */
489 if (strcmp(argv[6], strtrim(contact)) ||
490 (status != atoi(argv[8 + idx])) ||
491 strcmp(argv[10 + idx], strtrim(address)) ||
492 strcmp(argv[11 + idx], strtrim(mtype)) ||
493 (mid != *(int *)argv[12 + idx]) ||
494 (acomment != *(int *)argv[13 + idx]))
496 /* Billing contact field didn't appear until version 6 */
498 if (strcmp(argv[7], strtrim(billing_contact)))
505 /* If moving to a new subnet, make sure user is on acl there */
506 id = *(int *)argv[9 + idx];
509 EXEC SQL SELECT owner_type, owner_id INTO :stype, :sid
510 FROM subnet WHERE snet_id=:id;
511 if (!find_member(stype, sid, cl))
520 /* access_ahal - check for adding a host alias.
521 * successful if host has less then 2 aliases and (client is owner of
523 * If deleting an alias, any owner will do.
526 int access_ahal(struct query *q, char *argv[], client *cl)
528 EXEC SQL BEGIN DECLARE SECTION;
529 int cnt, id, mid, sid;
530 char mtype[MACHINE_OWNER_TYPE_SIZE], stype[SUBNET_OWNER_TYPE_SIZE];
531 EXEC SQL END DECLARE SECTION;
534 if (q->type == RETRIEVE)
537 id = *(int *)argv[1];
539 if (q->type == APPEND && isdigit(argv[0][0]))
542 EXEC SQL SELECT count(name) INTO :cnt from hostalias WHERE mach_id = :id;
545 /* if the type is APPEND, this is ahal and we need to make sure there
546 * will be no more than 2 aliases. If it's not, it must be dhal and
549 if (q->type == APPEND && cnt >= 2)
551 EXEC SQL SELECT m.owner_type, m.owner_id, s.owner_type, s.owner_id
552 INTO :mtype, :mid, :stype, :sid FROM machine m, subnet s
553 WHERE m.mach_id = :id and s.snet_id = m.snet_id;
554 status = find_member(mtype, mid, cl);
557 status = find_member(stype, sid, cl);
565 /* access_snt - check for retrieving network structure
568 int access_snt(struct query *q, char *argv[], client *cl)
570 if (q->type == RETRIEVE)
578 int access_printer(struct query *q, char *argv[], client *cl)
580 EXEC SQL BEGIN DECLARE SECTION;
581 char type[PRINTSERVERS_OWNER_TYPE_SIZE];
583 EXEC SQL END DECLARE SECTION;
586 mach_id = *(int *)argv[PRN_RM];
587 EXEC SQL SELECT owner_type, owner_id INTO :type, :id
588 FROM printservers WHERE mach_id = :mach_id;
592 status = find_member(type, id, cl);
600 int access_zephyr(struct query *q, char *argv[], client *cl)
602 EXEC SQL BEGIN DECLARE SECTION;
603 char type[ZEPHYR_OWNER_TYPE_SIZE];
606 EXEC SQL END DECLARE SECTION;
609 class = argv[ZA_CLASS];
610 EXEC SQL SELECT owner_type, owner_id INTO :type, :id
611 FROM zephyr WHERE class = :class;
615 status = find_member(type, id, cl);