4 * Copyright (c) 2021 Holland Schutte, Jayson Morberg
5 * Copyright (c) 2021 Dallas Lyons <dallasdlyons@gmail.com>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
16 * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
17 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 #include <sys/types.h>
22 #include <sys/socket.h>
32 struct server_acl_user
{
36 #define SERVER_ACL_READONLY 0x1
38 RB_ENTRY(server_acl_user
) entry
;
42 server_acl_cmp(struct server_acl_user
*user1
, struct server_acl_user
*user2
)
44 if (user1
->uid
< user2
->uid
)
46 return (user1
->uid
> user2
->uid
);
49 RB_HEAD(server_acl_entries
, server_acl_user
) server_acl_entries
;
50 RB_GENERATE_STATIC(server_acl_entries
, server_acl_user
, entry
, server_acl_cmp
);
52 /* Initialize server_acl tree. */
56 RB_INIT(&server_acl_entries
);
59 server_acl_user_allow(0);
60 server_acl_user_allow(getuid());
63 /* Find user entry. */
64 struct server_acl_user
*
65 server_acl_user_find(uid_t uid
)
67 struct server_acl_user find
= { .uid
= uid
};
69 return (RB_FIND(server_acl_entries
, &server_acl_entries
, &find
));
72 /* Display the tree. */
74 server_acl_display(struct cmdq_item
*item
)
76 struct server_acl_user
*loop
;
80 RB_FOREACH(loop
, server_acl_entries
, &server_acl_entries
) {
83 if ((pw
= getpwuid(loop
->uid
)) != NULL
)
87 if (loop
->flags
== SERVER_ACL_READONLY
)
88 cmdq_print(item
, "%s (R)", name
);
90 cmdq_print(item
, "%s (W)", name
);
96 server_acl_user_allow(uid_t uid
)
98 struct server_acl_user
*user
;
100 user
= server_acl_user_find(uid
);
102 user
= xcalloc(1, sizeof *user
);
104 RB_INSERT(server_acl_entries
, &server_acl_entries
, user
);
108 /* Deny a user (remove from the tree). */
110 server_acl_user_deny(uid_t uid
)
112 struct server_acl_user
*user
;
114 user
= server_acl_user_find(uid
);
116 RB_REMOVE(server_acl_entries
, &server_acl_entries
, user
);
121 /* Allow this user write access. */
123 server_acl_user_allow_write(uid_t uid
)
125 struct server_acl_user
*user
;
128 user
= server_acl_user_find(uid
);
131 user
->flags
&= ~SERVER_ACL_READONLY
;
133 TAILQ_FOREACH(c
, &clients
, entry
) {
134 uid
= proc_get_peer_uid(c
->peer
);
135 if (uid
!= (uid_t
)-1 && uid
== user
->uid
)
136 c
->flags
&= ~CLIENT_READONLY
;
140 /* Deny this user write access. */
142 server_acl_user_deny_write(uid_t uid
)
144 struct server_acl_user
*user
;
147 user
= server_acl_user_find(uid
);
150 user
->flags
|= SERVER_ACL_READONLY
;
152 TAILQ_FOREACH(c
, &clients
, entry
) {
153 uid
= proc_get_peer_uid(c
->peer
);
154 if (uid
!= (uid_t
)-1 && uid
== user
->uid
)
155 c
->flags
|= CLIENT_READONLY
;
160 * Check if the client's UID exists in the ACL list and if so, set as read only
161 * if needed. Return false if the user does not exist.
164 server_acl_join(struct client
*c
)
166 struct server_acl_user
*user
;
169 uid
= proc_get_peer_uid(c
->peer
);
170 if (uid
== (uid_t
)-1)
173 user
= server_acl_user_find(uid
);
176 if (user
->flags
& SERVER_ACL_READONLY
)
177 c
->flags
|= CLIENT_READONLY
;
181 /* Get UID for user entry. */
183 server_acl_get_uid(struct server_acl_user
*user
)