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;
131 char acl_type[LIST_ACL_TYPE_SIZE], name[LIST_NAME_SIZE], *newname;
132 EXEC SQL END DECLARE SECTION;
135 list_id = *(int *)argv[0];
136 EXEC SQL SELECT acl_id, acl_type, gid, publicflg, name
137 INTO :acl_id, :acl_type, :gid, :flags, :name
139 WHERE list_id = :list_id;
141 if (sqlca.sqlerrd[2] != 1)
144 /* Allow client to add self to public list or delete self from any
147 if ((((!strcmp("amtl", q->shortname) ||
148 !strcmp("atml", q->shortname)) && flags) ||
149 (!strcmp("dmfl", q->shortname))))
151 if (!strcmp("USER", argv[1]) && *(int *)argv[2] == cl->users_id)
153 if (!strcmp("KERBEROS", argv[1]) && *(int *)argv[2] == -cl->client_id)
155 } /* if update_list, don't allow them to change the GID or rename to
156 a username other than their own */
157 else if (!strcmp("ulis", q->shortname))
159 if (!strcmp(argv[7], UNIQUE_GID))
166 if (gid != atoi(argv[7]))
170 EXEC SQL SELECT users_id INTO :users_id FROM users
171 WHERE login = :newname;
172 if ((sqlca.sqlcode != SQL_NO_MATCH) && strcmp(strtrim(name), newname) &&
173 (users_id != cl->users_id))
177 /* check for client in access control list */
178 status = find_member(acl_type, acl_id, cl);
186 /* access_visible_list - allow access to list only if it is not hidden,
187 * or if the client is on the ACL
189 * Inputs: argv[0] - list_id
190 * cl - client identifier
193 int access_visible_list(struct query *q, char *argv[], client *cl)
195 EXEC SQL BEGIN DECLARE SECTION;
196 int list_id, acl_id, flags ;
197 char acl_type[LIST_ACL_TYPE_SIZE];
198 EXEC SQL END DECLARE SECTION;
201 list_id = *(int *)argv[0];
202 EXEC SQL SELECT hidden, acl_id, acl_type
203 INTO :flags, :acl_id, :acl_type
205 WHERE list_id = :list_id;
206 if (sqlca.sqlerrd[2] != 1)
211 /* check for client in access control list */
212 status = find_member(acl_type, acl_id, cl);
220 /* access_vis_list_by_name - allow access to list only if it is not hidden,
221 * or if the client is on the ACL
223 * Inputs: argv[0] - list name
224 * cl - client identifier
227 int access_vis_list_by_name(struct query *q, char *argv[], client *cl)
229 EXEC SQL BEGIN DECLARE SECTION;
230 int acl_id, flags, rowcount;
231 char acl_type[LIST_ACL_TYPE_SIZE], *listname;
232 EXEC SQL END DECLARE SECTION;
236 EXEC SQL SELECT hidden, acl_id, acl_type INTO :flags, :acl_id, :acl_type
237 FROM list WHERE name = :listname;
239 rowcount = sqlca.sqlerrd[2];
247 /* check for client in access control list */
248 status = find_member(acl_type, acl_id, cl);
256 /* access_member - allow user to access member of type "USER" and name matches
257 * username, or to access member of type "KERBEROS" and the principal matches
258 * the user, or to access member of type "LIST" and list is one that user is
259 * on the acl of, or the list is visible.
262 int access_member(struct query *q, char *argv[], client *cl)
264 if (!strcmp(argv[0], "LIST") || !strcmp(argv[0], "RLIST"))
265 return access_visible_list(q, &argv[1], cl);
267 if (!strcmp(argv[0], "USER") || !strcmp(argv[0], "RUSER"))
269 if (cl->users_id == *(int *)argv[1])
273 if (!strcmp(argv[0], "KERBEROS") || !strcmp(argv[0], "RKERBEROS"))
275 if (cl->client_id == -*(int *)argv[1])
283 /* access_qgli - special access routine for Qualified_get_lists. Allows
284 * access iff argv[0] == "TRUE" and argv[2] == "FALSE".
287 int access_qgli(struct query *q, char *argv[], client *cl)
289 if (!strcmp(argv[0], "TRUE") && !strcmp(argv[2], "FALSE"))
295 /* access_service - allow access if user is on ACL of service. Don't
296 * allow access if a wildcard is used.
299 int access_service(struct query *q, char *argv[], client *cl)
301 EXEC SQL BEGIN DECLARE SECTION;
303 char *name, acl_type[LIST_ACL_TYPE_SIZE];
304 EXEC SQL END DECLARE SECTION;
309 for (c = name; *c; c++)
314 EXEC SQL SELECT acl_id, acl_type INTO :acl_id, :acl_type FROM servers
316 if (sqlca.sqlerrd[2] > 1)
319 /* check for client in access control list */
320 status = find_member(acl_type, acl_id, cl);
328 /* access_filesys - verify that client is owner or on owners list of filesystem
332 int access_filesys(struct query *q, char *argv[], client *cl)
334 EXEC SQL BEGIN DECLARE SECTION;
335 int users_id, list_id;
337 EXEC SQL END DECLARE SECTION;
341 EXEC SQL SELECT owner, owners INTO :users_id, :list_id FROM filesys
344 if (sqlca.sqlerrd[2] != 1)
346 if (users_id == cl->users_id)
348 status = find_member("LIST", list_id, cl);
356 /* access_host - successful if owner of host, or subnet containing host
359 int access_host(struct query *q, char *argv[], client *cl)
361 EXEC SQL BEGIN DECLARE SECTION;
363 char mtype[MACHINE_OWNER_TYPE_SIZE], stype[SUBNET_OWNER_TYPE_SIZE];
364 EXEC SQL END DECLARE SECTION;
367 if (q->type == RETRIEVE)
369 if (strcmp(argv[0], "*") || strcmp(argv[1], "*") ||
370 strcmp(argv[2], "*") || strcmp(argv[3], "*"))
376 if (q->type == APPEND)
378 /* Non-query owner must set use to zero */
379 if (atoi(argv[6]) != 0)
382 /* ... and start the hostname with a letter */
383 if (isdigit(argv[0][0]))
386 id = *(int *)argv[8];
387 EXEC SQL SELECT s.owner_type, s.owner_id
388 INTO :stype, :sid FROM subnet s
389 WHERE s.snet_id = :id;
392 if (find_member(stype, sid, cl))
397 else /* q-type == UPDATE */
399 EXEC SQL BEGIN DECLARE SECTION;
400 int status, acomment, use, ocomment, snid;
401 char contact[MACHINE_CONTACT_SIZE], address[MACHINE_ADDRESS_SIZE];
402 char name[MACHINE_NAME_SIZE];
403 EXEC SQL END DECLARE SECTION;
405 id = *(int *)argv[0];
406 EXEC SQL SELECT m.name, m.use, m.contact, m.status, m.address,
407 m.owner_type, m.owner_id, m.acomment, m.ocomment, m.snet_id,
408 s.owner_type, s.owner_id INTO :name, :use, :contact, :status,
409 :address, :mtype, :mid, :acomment, :ocomment, :snid, :stype, :sid
410 FROM machine m, subnet s
411 WHERE m.mach_id = :id AND s.snet_id = m.snet_id;
415 /* non-query-owner cannot change use or ocomment */
416 if ((use != atoi(argv[7])) || (ocomment != *(int *)argv[14]))
419 /* or rename to start with digit */
420 if (isdigit(argv[1][0]) && strcmp(strtrim(name), argv[1]))
423 if (!find_member(stype, sid, cl))
425 if (find_member(mtype, mid, cl))
427 /* host owner also cannot change contact, status, address,
428 owner, or acomment */
429 if (strcmp(argv[6], strtrim(contact)) ||
430 (status != atoi(argv[8])) ||
431 strcmp(argv[10], strtrim(address)) ||
432 strcmp(argv[11], strtrim(mtype)) ||
433 (mid != *(int *)argv[12]) || (acomment != *(int *)argv[13]))
440 /* If moving to a new subnet, make sure user is on acl there */
441 id = *(int *)argv[9];
444 EXEC SQL SELECT owner_type, owner_id INTO :stype, :sid
445 FROM subnet WHERE snet_id=:id;
446 if (!find_member(stype, sid, cl))
455 /* access_ahal - check for adding a host alias.
456 * successful if host has less then 2 aliases and (client is owner of
458 * If deleting an alias, any owner will do.
461 int access_ahal(struct query *q, char *argv[], client *cl)
463 EXEC SQL BEGIN DECLARE SECTION;
464 int cnt, id, mid, sid;
465 char mtype[MACHINE_OWNER_TYPE_SIZE], stype[SUBNET_OWNER_TYPE_SIZE];
466 EXEC SQL END DECLARE SECTION;
469 if (q->type == RETRIEVE)
472 id = *(int *)argv[1];
474 if (q->type == APPEND && isdigit(argv[0][0]))
477 EXEC SQL SELECT count(name) INTO :cnt from hostalias WHERE mach_id = :id;
480 /* if the type is APPEND, this is ahal and we need to make sure there
481 * will be no more than 2 aliases. If it's not, it must be dhal and
484 if (q->type == APPEND && cnt >= 2)
486 EXEC SQL SELECT m.owner_type, m.owner_id, s.owner_type, s.owner_id
487 INTO :mtype, :mid, :stype, :sid FROM machine m, subnet s
488 WHERE m.mach_id = :id and s.snet_id = m.snet_id;
489 status = find_member(mtype, mid, cl);
492 status = find_member(stype, sid, cl);
500 /* access_snt - check for retrieving network structure
503 int access_snt(struct query *q, char *argv[], client *cl)
505 if (q->type == RETRIEVE)
513 int access_printer(struct query *q, char *argv[], client *cl)
515 EXEC SQL BEGIN DECLARE SECTION;
516 char type[PRINTSERVERS_OWNER_TYPE_SIZE];
518 EXEC SQL END DECLARE SECTION;
521 mach_id = *(int *)argv[PRN_RM];
522 EXEC SQL SELECT owner_type, owner_id INTO :type, :id
523 FROM printservers WHERE mach_id = :mach_id;
527 status = find_member(type, id, cl);