Check admin or operator privileges

This commit is contained in:
Andreas Neue 2016-08-07 19:41:07 +02:00
parent 5f47deeb94
commit bae871a524
2 changed files with 34 additions and 18 deletions

View File

@ -88,20 +88,17 @@ func handleCmdJoin(sv *Server, msg *irc.Message) {
sv.sendMsg(msg) sv.sendMsg(msg)
sv.sendReply(msg.Pre, RPL_TOPIC, msg.Args[0], sv.chTopics[msg.Args[0]]) sv.sendReply(msg.Pre, RPL_TOPIC, msg.Args[0], sv.chTopics[msg.Args[0]])
sv.channelNames(msg.Pre, msg.Args[0]) sv.channelNames(msg.Pre, msg.Args[0])
_, isadm := sv.admins[clid] m, isoper := sv.opers[clid]
if isadm { if isoper {
mode := "o " + clid mode := "o " + clid
sv.chModes[chid][mode] = true sv.chModes[chid][mode] = true
sv.sendMsg(irc.M(sv.host, "MODE", chid+" +o "+clid, "")) sv.sendMsg(irc.M(sv.host, "MODE", chid+" +o "+clid, ""))
if m == "a" {
sv.sendMsg(irc.M(sv.host, "PRIVMSG", chid, clid+" is a server administrator")) sv.sendMsg(irc.M(sv.host, "PRIVMSG", chid, clid+" is a server administrator"))
return
} }
_, isop := sv.opers[clid] if m == "o" {
if isop {
mode := "o " + clid
sv.chModes[chid][mode] = true
sv.sendMsg(irc.M(sv.host, "MODE", chid+" +o "+clid, ""))
sv.sendMsg(irc.M(sv.host, "PRIVMSG", chid, clid+" is a server operator")) sv.sendMsg(irc.M(sv.host, "PRIVMSG", chid, clid+" is a server operator"))
}
return return
} }
if first { if first {
@ -258,7 +255,7 @@ func handleCmdPing(sv *Server, msg *irc.Message) {
func handleCmdRehash(sv *Server, msg *irc.Message) { func handleCmdRehash(sv *Server, msg *irc.Message) {
sv.loadConfig() sv.loadConfig()
sv.sendReply(msg.Pre, RPL_REHASHING, "", "Rehashing.") sv.sendReply(msg.Pre, RPL_REHASHING, "", "Rehashing")
xlog.Info("Rehashing") xlog.Info("Rehashing")
} }

View File

@ -40,8 +40,7 @@ type Server struct {
created string created string
motd string motd string
admins map[string]bool opers map[string]string
opers map[string]bool
clients map[string]Client clients map[string]Client
clModes map[string]string clModes map[string]string
@ -70,8 +69,7 @@ func NewServer(configPath, software, version string) *Server {
sv.addq = make(chan Client, 128) sv.addq = make(chan Client, 128)
sv.delq = make(chan Client, 128) sv.delq = make(chan Client, 128)
sv.admins = make(map[string]bool) sv.opers = make(map[string]string)
sv.opers = make(map[string]bool)
sv.clients = make(map[string]Client) sv.clients = make(map[string]Client)
sv.clModes = make(map[string]string) sv.clModes = make(map[string]string)
@ -92,11 +90,11 @@ func NewServer(configPath, software, version string) *Server {
nicks, _ := sv.config.GetString("control", "a") nicks, _ := sv.config.GetString("control", "a")
for _, nick := range strings.Split(nicks, ",") { for _, nick := range strings.Split(nicks, ",") {
sv.admins[nick] = true sv.opers[nick] = "a"
} }
nicks, _ = sv.config.GetString("control", "o") nicks, _ = sv.config.GetString("control", "o")
for _, nick := range strings.Split(nicks, ",") { for _, nick := range strings.Split(nicks, ",") {
sv.opers[nick] = true sv.opers[nick] = "o"
} }
sv.packetsTransferred = 0 sv.packetsTransferred = 0
@ -268,11 +266,24 @@ func (sv *Server) delClient(cl Client) {
func (sv *Server) recvMsg(msg *irc.Message) { func (sv *Server) recvMsg(msg *irc.Message) {
cmd := msg.Cmd cmd := msg.Cmd
clid := strings.ToLower(msg.Pre)
hook, exists := svCommandHooks[cmd] hook, exists := svCommandHooks[cmd]
if !exists { if !exists {
sv.sendReply(msg.Pre, ERR_UNKNOWNCOMMAND, cmd, "Unknown command") sv.sendReply(msg.Pre, ERR_UNKNOWNCOMMAND, cmd, "Unknown command")
return return
} }
nm := hook.NeedMode
if nm != "" {
if m, isoper := sv.opers[clid]; !isoper {
sv.sendReply(msg.Pre, ERR_NOPRIVILEGES, cmd, "Permission denied: You're not a server operator")
return
} else {
if strings.Index(nm, m) == -1 {
sv.sendReply(msg.Pre, ERR_NOPRIVILEGES, cmd, "Permission denied: You're not a server admin")
return
}
}
}
argc := len(msg.Args) argc := len(msg.Args)
if argc < hook.MinArgs { if argc < hook.MinArgs {
sv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters") sv.sendReply(msg.Pre, ERR_NEEDMOREPARAMS, cmd, "Not enough parameters")
@ -342,6 +353,14 @@ func (sv *Server) sendLogon(nick string) {
sv.sendReply(nick, RPL_MOTD, "", fmt.Sprintf("- %s", line)) sv.sendReply(nick, RPL_MOTD, "", fmt.Sprintf("- %s", line))
} }
sv.sendReply(nick, RPL_ENDOFMOTD, "", "End of MOTD command") sv.sendReply(nick, RPL_ENDOFMOTD, "", "End of MOTD command")
if m, isoper := sv.opers[strings.ToLower(nick)]; isoper {
switch m {
case "a":
sv.sendReply(nick, RPL_YOUREOPER, "", "You are now a server admin")
case "o":
sv.sendReply(nick, RPL_YOUREOPER, "", "You are now a server operator")
}
}
} }
// Send channel name list to client // Send channel name list to client