Browse Source

opers: Enforce oper class permissions

tags/v0.4.0
Daniel Oaks 7 years ago
parent
commit
8e2a8cb1b3
2 changed files with 22 additions and 0 deletions
  1. 15
    0
      irc/client.go
  2. 7
    0
      irc/commands.go

+ 15
- 0
irc/client.go View File

@@ -247,6 +247,21 @@ func (client *Client) HasUsername() bool {
247 247
 	return client.username != "" && client.username != "*"
248 248
 }
249 249
 
250
+// HasCapabs returns true if client has the given (role) capabilities.
251
+func (client *Client) HasCapabs(capabs ...string) bool {
252
+	if client.class == nil {
253
+		return false
254
+	}
255
+
256
+	for _, capab := range capabs {
257
+		if !client.class.Capabilities[capab] {
258
+			return false
259
+		}
260
+	}
261
+
262
+	return true
263
+}
264
+
250 265
 // <mode>
251 266
 func (c *Client) ModeString() (str string) {
252 267
 	str = "+"

+ 7
- 0
irc/commands.go View File

@@ -15,6 +15,7 @@ type Command struct {
15 15
 	leaveClientActive bool // if true, leaves the client active time alone. reversed because we can't default a struct element to True
16 16
 	leaveClientIdle   bool
17 17
 	minParams         int
18
+	capabs            []string
18 19
 }
19 20
 
20 21
 // Run runs this command with the given client/message.
@@ -27,6 +28,10 @@ func (cmd *Command) Run(server *Server, client *Client, msg ircmsg.IrcMessage) b
27 28
 		client.Send(nil, server.name, ERR_NOPRIVILEGES, client.nick, "Permission Denied - You're not an IRC operator")
28 29
 		return false
29 30
 	}
31
+	if len(cmd.capabs) > 0 && !client.HasCapabs(cmd.capabs...) {
32
+		client.Send(nil, server.name, ERR_NOPRIVILEGES, client.nick, "Permission Denied")
33
+		return false
34
+	}
30 35
 	if len(msg.Params) < cmd.minParams {
31 36
 		client.Send(nil, server.name, ERR_NEEDMOREPARAMS, client.nick, msg.Command, "Not enough parameters")
32 37
 		return false
@@ -91,6 +96,7 @@ var Commands = map[string]Command{
91 96
 		handler:   killHandler,
92 97
 		minParams: 1,
93 98
 		oper:      true,
99
+		capabs:    []string{"oper:local_kill"}, //TODO(dan): when we have S2S, this will be checked in the command handler itself
94 100
 	},
95 101
 	"LIST": {
96 102
 		handler:   listHandler,
@@ -168,6 +174,7 @@ var Commands = map[string]Command{
168 174
 		handler:   rehashHandler,
169 175
 		minParams: 0,
170 176
 		oper:      true,
177
+		capabs:    []string{"oper:rehash"},
171 178
 	},
172 179
 	"TIME": {
173 180
 		handler:   timeHandler,

Loading…
Cancel
Save