2 * charybdis: an advanced ircd.
3 * privilege.c: Dynamic privileges API.
5 * Copyright (c) 2008 William Pitcock <nenolod@dereferenced.org>
7 * Permission to use, copy, modify, and/or 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 is present in all copies.
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
26 #include "privilege.h"
32 static rb_dlink_list privilegeset_list
= {};
35 privilegeset_in_set(struct PrivilegeSet
*set
, const char *priv
)
37 s_assert(set
!= NULL
);
38 s_assert(priv
!= NULL
);
40 return strstr(set
->privs
, priv
) != NULL
;
43 static struct PrivilegeSet
*
44 privilegeset_get_any(const char *name
)
48 s_assert(name
!= NULL
);
50 RB_DLINK_FOREACH(iter
, privilegeset_list
.head
)
52 struct PrivilegeSet
*set
= (struct PrivilegeSet
*) iter
->data
;
54 if (!strcasecmp(set
->name
, name
))
62 privilegeset_set_new(const char *name
, const char *privs
, PrivilegeFlags flags
)
64 struct PrivilegeSet
*set
;
66 set
= privilegeset_get_any(name
);
69 if (!(set
->status
& CONF_ILLEGAL
))
70 ilog(L_MAIN
, "Duplicate privset %s", name
);
71 set
->status
&= ~CONF_ILLEGAL
;
76 set
= rb_malloc(sizeof(struct PrivilegeSet
));
79 set
->name
= rb_strdup(name
);
81 rb_dlinkAdd(set
, &set
->node
, &privilegeset_list
);
83 set
->privs
= rb_strdup(privs
);
90 privilegeset_extend(struct PrivilegeSet
*parent
, const char *name
, const char *privs
, PrivilegeFlags flags
)
92 struct PrivilegeSet
*set
;
94 s_assert(parent
!= NULL
);
95 s_assert(name
!= NULL
);
96 s_assert(privs
!= NULL
);
98 set
= privilegeset_get_any(name
);
101 if (!(set
->status
& CONF_ILLEGAL
))
102 ilog(L_MAIN
, "Duplicate privset %s", name
);
103 set
->status
&= ~CONF_ILLEGAL
;
108 set
= rb_malloc(sizeof(struct PrivilegeSet
));
111 set
->name
= rb_strdup(name
);
113 rb_dlinkAdd(set
, &set
->node
, &privilegeset_list
);
116 set
->privs
= rb_malloc(strlen(parent
->privs
) + 1 + strlen(privs
) + 1);
117 strcpy(set
->privs
, parent
->privs
);
118 strcat(set
->privs
, " ");
119 strcat(set
->privs
, privs
);
124 struct PrivilegeSet
*
125 privilegeset_get(const char *name
)
127 struct PrivilegeSet
*set
;
129 set
= privilegeset_get_any(name
);
130 if (set
!= NULL
&& set
->status
& CONF_ILLEGAL
)
135 struct PrivilegeSet
*
136 privilegeset_ref(struct PrivilegeSet
*set
)
138 s_assert(set
!= NULL
);
146 privilegeset_unref(struct PrivilegeSet
*set
)
148 s_assert(set
!= NULL
);
153 ilog(L_MAIN
, "refs on privset %s is already 0",
155 if (set
->refs
== 0 && set
->status
& CONF_ILLEGAL
)
157 rb_dlinkDelete(&set
->node
, &privilegeset_list
);
166 privilegeset_mark_all_illegal(void)
170 RB_DLINK_FOREACH(iter
, privilegeset_list
.head
)
172 struct PrivilegeSet
*set
= (struct PrivilegeSet
*) iter
->data
;
174 /* the "default" privset is special and must remain available */
175 if (!strcmp(set
->name
, "default"))
178 set
->status
|= CONF_ILLEGAL
;
180 set
->privs
= rb_strdup("");
181 /* but do not free it yet */
186 privilegeset_delete_all_illegal(void)
188 rb_dlink_node
*iter
, *next
;
190 RB_DLINK_FOREACH_SAFE(iter
, next
, privilegeset_list
.head
)
192 struct PrivilegeSet
*set
= (struct PrivilegeSet
*) iter
->data
;
194 privilegeset_ref(set
);
195 privilegeset_unref(set
);
200 privilegeset_report(struct Client
*source_p
)
204 RB_DLINK_FOREACH(ptr
, privilegeset_list
.head
)
206 struct PrivilegeSet
*set
= ptr
->data
;
208 /* use RPL_STATSDEBUG for now -- jilles */
209 sendto_one_numeric(source_p
, RPL_STATSDEBUG
,