]> jfr.im git - solanum.git/blob - modules/m_privs.c
Merge pull request #279 from edk0/operhide
[solanum.git] / modules / m_privs.c
1 /*
2 * m_privs.c: Shows effective operator privileges
3 *
4 * Copyright (C) 2008 Jilles Tjoelker
5 * Copyright (C) 2008 charybdis development team
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * 1.Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * 2.Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3.The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include "stdinc.h"
33 #include "client.h"
34 #include "numeric.h"
35 #include "send.h"
36 #include "msg.h"
37 #include "parse.h"
38 #include "modules.h"
39 #include "s_conf.h"
40 #include "s_newconf.h"
41
42 static const char privs_desc[] = "Provides the PRIVS command to inspect an operator's privileges";
43
44 static void m_privs(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
45 static void me_privs(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
46 static void mo_privs(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[]);
47
48 struct Message privs_msgtab = {
49 "PRIVS", 0, 0, 0, 0,
50 {mg_unreg, {m_privs, 0}, mg_ignore, mg_ignore, {me_privs, 0}, {mo_privs, 0}}
51 };
52
53 mapi_clist_av1 privs_clist[] = {
54 &privs_msgtab,
55 NULL
56 };
57
58 /* XXX this is a copy, not so nice
59 *
60 * Sort of... it's int in newconf.c since oper confs don't need 64-bit wide flags.
61 * --Elizafox
62 */
63 struct mode_table
64 {
65 const char *name;
66 uint64_t mode;
67 };
68
69 /* there is no such table like this anywhere else */
70 static struct mode_table auth_client_table[] = {
71 {"resv_exempt", FLAGS_EXEMPTRESV },
72 {"kline_exempt", FLAGS_EXEMPTKLINE },
73 {"flood_exempt", FLAGS_EXEMPTFLOOD },
74 {"spambot_exempt", FLAGS_EXEMPTSPAMBOT },
75 {"shide_exempt", FLAGS_EXEMPTSHIDE },
76 {"jupe_exempt", FLAGS_EXEMPTJUPE },
77 {"extend_chans", FLAGS_EXTENDCHANS },
78 {NULL, 0}
79 };
80
81 DECLARE_MODULE_AV2(privs, NULL, NULL, privs_clist, NULL, NULL, NULL, NULL, privs_desc);
82
83 static void show_privs(struct Client *source_p, struct Client *target_p)
84 {
85 char buf[512];
86 struct mode_table *p;
87
88 buf[0] = '\0';
89 if (target_p->localClient->privset)
90 rb_strlcat(buf, target_p->localClient->privset->privs, sizeof buf);
91 if (IsOper(target_p))
92 {
93 if (buf[0] != '\0')
94 rb_strlcat(buf, " ", sizeof buf);
95 rb_strlcat(buf, "operator:", sizeof buf);
96 rb_strlcat(buf, target_p->localClient->opername, sizeof buf);
97
98 if (target_p->localClient->privset)
99 {
100 if (buf[0] != '\0')
101 rb_strlcat(buf, " ", sizeof buf);
102 rb_strlcat(buf, "privset:", sizeof buf);
103 rb_strlcat(buf, target_p->localClient->privset->name, sizeof buf);
104 }
105 }
106 p = &auth_client_table[0];
107 while (p->name != NULL)
108 {
109 if (target_p->flags & p->mode)
110 {
111 if (buf[0] != '\0')
112 rb_strlcat(buf, " ", sizeof buf);
113 rb_strlcat(buf, p->name, sizeof buf);
114 }
115 p++;
116 }
117 sendto_one_numeric(source_p, RPL_PRIVS, form_str(RPL_PRIVS),
118 target_p->name, buf);
119 }
120
121 static void
122 me_privs(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
123 {
124 struct Client *target_p;
125
126 if (!IsOper(source_p) || parc < 2 || EmptyString(parv[1]))
127 return;
128
129 /* we cannot show privs for remote clients */
130 if((target_p = find_person(parv[1])) && MyClient(target_p))
131 show_privs(source_p, target_p);
132 }
133
134 static void
135 mo_privs(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
136 {
137 struct Client *target_p;
138
139 if (parc < 2 || EmptyString(parv[1]))
140 target_p = source_p;
141 else
142 {
143 target_p = find_named_person(parv[1]);
144 if (target_p == NULL)
145 {
146 sendto_one_numeric(source_p, ERR_NOSUCHNICK,
147 form_str(ERR_NOSUCHNICK), parv[1]);
148 return;
149 }
150 }
151
152 if (MyClient(target_p))
153 show_privs(source_p, target_p);
154 else
155 sendto_one(target_p, ":%s ENCAP %s PRIVS %s",
156 get_id(source_p, target_p),
157 target_p->servptr->name,
158 use_id(target_p));
159 }
160
161 static void
162 m_privs(struct MsgBuf *msgbuf_p, struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
163 {
164 if (parc >= 2 && !EmptyString(parv[1]) &&
165 irccmp(parv[1], source_p->name)) {
166 sendto_one_numeric(source_p, ERR_NOPRIVILEGES,
167 form_str(ERR_NOPRIVILEGES));
168 return;
169 }
170
171 show_privs(source_p, source_p);
172 }