/src/tmux/cmd-server-access.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD$ */ |
2 | | |
3 | | /* |
4 | | * Copyright (c) 2021 Dallas Lyons <dallasdlyons@gmail.com> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER |
15 | | * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
16 | | * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <sys/stat.h> |
20 | | #include <sys/types.h> |
21 | | |
22 | | #include <pwd.h> |
23 | | #include <stdio.h> |
24 | | #include <string.h> |
25 | | #include <stdlib.h> |
26 | | #include <unistd.h> |
27 | | |
28 | | #include "tmux.h" |
29 | | |
30 | | /* |
31 | | * Controls access to session. |
32 | | */ |
33 | | |
34 | | static enum cmd_retval cmd_server_access_exec(struct cmd *, struct cmdq_item *); |
35 | | |
36 | | const struct cmd_entry cmd_server_access_entry = { |
37 | | .name = "server-access", |
38 | | .alias = NULL, |
39 | | |
40 | | .args = { "adlrw", 0, 1, NULL }, |
41 | | .usage = "[-adlrw] " CMD_TARGET_PANE_USAGE " [user]", |
42 | | |
43 | | .flags = CMD_CLIENT_CANFAIL, |
44 | | .exec = cmd_server_access_exec |
45 | | }; |
46 | | |
47 | | static enum cmd_retval |
48 | | cmd_server_access_deny(struct cmdq_item *item, struct passwd *pw) |
49 | 0 | { |
50 | 0 | struct client *loop; |
51 | 0 | struct server_acl_user *user; |
52 | 0 | uid_t uid; |
53 | |
|
54 | 0 | if ((user = server_acl_user_find(pw->pw_uid)) == NULL) { |
55 | 0 | cmdq_error(item, "user %s not found", pw->pw_name); |
56 | 0 | return (CMD_RETURN_ERROR); |
57 | 0 | } |
58 | 0 | TAILQ_FOREACH(loop, &clients, entry) { |
59 | 0 | uid = proc_get_peer_uid(loop->peer); |
60 | 0 | if (uid == server_acl_get_uid(user)) { |
61 | 0 | loop->exit_message = xstrdup("access not allowed"); |
62 | 0 | loop->flags |= CLIENT_EXIT; |
63 | 0 | } |
64 | 0 | } |
65 | 0 | server_acl_user_deny(pw->pw_uid); |
66 | |
|
67 | 0 | return (CMD_RETURN_NORMAL); |
68 | 0 | } |
69 | | |
70 | | static enum cmd_retval |
71 | | cmd_server_access_exec(struct cmd *self, struct cmdq_item *item) |
72 | 0 | { |
73 | |
|
74 | 0 | struct args *args = cmd_get_args(self); |
75 | 0 | struct client *c = cmdq_get_target_client(item); |
76 | 0 | char *name; |
77 | 0 | struct passwd *pw = NULL; |
78 | |
|
79 | 0 | if (args_has(args, 'l')) { |
80 | 0 | server_acl_display(item); |
81 | 0 | return (CMD_RETURN_NORMAL); |
82 | 0 | } |
83 | 0 | if (args_count(args) == 0) { |
84 | 0 | cmdq_error(item, "missing user argument"); |
85 | 0 | return (CMD_RETURN_ERROR); |
86 | 0 | } |
87 | | |
88 | 0 | name = format_single(item, args_string(args, 0), c, NULL, NULL, NULL); |
89 | 0 | if (*name != '\0') |
90 | 0 | pw = getpwnam(name); |
91 | 0 | if (pw == NULL) { |
92 | 0 | cmdq_error(item, "unknown user: %s", name); |
93 | 0 | return (CMD_RETURN_ERROR); |
94 | 0 | } |
95 | 0 | free(name); |
96 | |
|
97 | 0 | if (pw->pw_uid == 0 || pw->pw_uid == getuid()) { |
98 | 0 | cmdq_error(item, "%s owns the server, can't change access", |
99 | 0 | pw->pw_name); |
100 | 0 | return (CMD_RETURN_ERROR); |
101 | 0 | } |
102 | | |
103 | 0 | if (args_has(args, 'a') && args_has(args, 'd')) { |
104 | 0 | cmdq_error(item, "-a and -d cannot be used together"); |
105 | 0 | return (CMD_RETURN_ERROR); |
106 | 0 | } |
107 | 0 | if (args_has(args, 'w') && args_has(args, 'r')) { |
108 | 0 | cmdq_error(item, "-r and -w cannot be used together"); |
109 | 0 | return (CMD_RETURN_ERROR); |
110 | 0 | } |
111 | | |
112 | 0 | if (args_has(args, 'd')) |
113 | 0 | return (cmd_server_access_deny(item, pw)); |
114 | 0 | if (args_has(args, 'a')) { |
115 | 0 | if (server_acl_user_find(pw->pw_uid) != NULL) { |
116 | 0 | cmdq_error(item, "user %s is already added", |
117 | 0 | pw->pw_name); |
118 | 0 | return (CMD_RETURN_ERROR); |
119 | 0 | } |
120 | 0 | server_acl_user_allow(pw->pw_uid); |
121 | | /* Do not return - allow -r or -w with -a. */ |
122 | 0 | } else if (args_has(args, 'r') || args_has(args, 'w')) { |
123 | | /* -r or -w implies -a if user does not exist. */ |
124 | 0 | if (server_acl_user_find(pw->pw_uid) == NULL) |
125 | 0 | server_acl_user_allow(pw->pw_uid); |
126 | 0 | } |
127 | | |
128 | 0 | if (args_has(args, 'w')) { |
129 | 0 | if (server_acl_user_find(pw->pw_uid) == NULL) { |
130 | 0 | cmdq_error(item, "user %s not found", pw->pw_name); |
131 | 0 | return (CMD_RETURN_ERROR); |
132 | 0 | } |
133 | 0 | server_acl_user_allow_write(pw->pw_uid); |
134 | 0 | return (CMD_RETURN_NORMAL); |
135 | 0 | } |
136 | | |
137 | 0 | if (args_has(args, 'r')) { |
138 | 0 | if (server_acl_user_find(pw->pw_uid) == NULL) { |
139 | 0 | cmdq_error(item, "user %s not found", pw->pw_name); |
140 | 0 | return (CMD_RETURN_ERROR); |
141 | 0 | } |
142 | 0 | server_acl_user_deny_write(pw->pw_uid); |
143 | 0 | return (CMD_RETURN_NORMAL); |
144 | 0 | } |
145 | | |
146 | 0 | return (CMD_RETURN_NORMAL); |
147 | 0 | } |