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"
28 static rb_dlink_list privilegeset_list
= {};
31 privilegeset_in_set(struct PrivilegeSet
*set
, const char *priv
)
33 s_assert(set
!= NULL
);
34 s_assert(priv
!= NULL
);
36 return strstr(set
->privs
, priv
) != NULL
;
39 static struct PrivilegeSet
*
40 privilegeset_get_any(const char *name
)
44 s_assert(name
!= NULL
);
46 RB_DLINK_FOREACH(iter
, privilegeset_list
.head
)
48 struct PrivilegeSet
*set
= (struct PrivilegeSet
*) iter
->data
;
50 if (!strcasecmp(set
->name
, name
))
58 privilegeset_set_new(const char *name
, const char *privs
, PrivilegeFlags flags
)
60 struct PrivilegeSet
*set
;
62 set
= privilegeset_get_any(name
);
65 if (!(set
->status
& CONF_ILLEGAL
))
66 ilog(L_MAIN
, "Duplicate privset %s", name
);
67 set
->status
&= ~CONF_ILLEGAL
;
72 set
= rb_malloc(sizeof(struct PrivilegeSet
));
75 set
->name
= rb_strdup(name
);
77 rb_dlinkAdd(set
, &set
->node
, &privilegeset_list
);
79 set
->privs
= rb_strdup(privs
);
86 privilegeset_extend(struct PrivilegeSet
*parent
, const char *name
, const char *privs
, PrivilegeFlags flags
)
88 struct PrivilegeSet
*set
;
90 s_assert(parent
!= NULL
);
91 s_assert(name
!= NULL
);
92 s_assert(privs
!= NULL
);
94 set
= privilegeset_get_any(name
);
97 if (!(set
->status
& CONF_ILLEGAL
))
98 ilog(L_MAIN
, "Duplicate privset %s", name
);
99 set
->status
&= ~CONF_ILLEGAL
;
104 set
= rb_malloc(sizeof(struct PrivilegeSet
));
107 set
->name
= rb_strdup(name
);
109 rb_dlinkAdd(set
, &set
->node
, &privilegeset_list
);
112 set
->privs
= rb_malloc(strlen(parent
->privs
) + 1 + strlen(privs
) + 1);
113 strcpy(set
->privs
, parent
->privs
);
114 strcat(set
->privs
, " ");
115 strcat(set
->privs
, privs
);
120 struct PrivilegeSet
*
121 privilegeset_get(const char *name
)
123 struct PrivilegeSet
*set
;
125 set
= privilegeset_get_any(name
);
126 if (set
!= NULL
&& set
->status
& CONF_ILLEGAL
)
131 struct PrivilegeSet
*
132 privilegeset_ref(struct PrivilegeSet
*set
)
134 s_assert(set
!= NULL
);
142 privilegeset_unref(struct PrivilegeSet
*set
)
144 s_assert(set
!= NULL
);
149 ilog(L_MAIN
, "refs on privset %s is already 0",
151 if (set
->refs
== 0 && set
->status
& CONF_ILLEGAL
)
153 rb_dlinkDelete(&set
->node
, &privilegeset_list
);
162 privilegeset_mark_all_illegal(void)
166 RB_DLINK_FOREACH(iter
, privilegeset_list
.head
)
168 struct PrivilegeSet
*set
= (struct PrivilegeSet
*) iter
->data
;
170 set
->status
|= CONF_ILLEGAL
;
171 /* but do not free it yet */
176 privilegeset_delete_all_illegal(void)
178 rb_dlink_node
*iter
, *next
;
180 RB_DLINK_FOREACH_SAFE(iter
, next
, privilegeset_list
.head
)
182 struct PrivilegeSet
*set
= (struct PrivilegeSet
*) iter
->data
;
184 privilegeset_ref(set
);
185 privilegeset_unref(set
);