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.
25 #include "privilege.h"
27 static rb_dlink_list privilegeset_list
= {};
30 privilegeset_in_set(struct PrivilegeSet
*set
, const char *priv
)
32 s_assert(set
!= NULL
);
33 s_assert(priv
!= NULL
);
35 return strstr(set
->privs
, priv
) != NULL
;
39 privilegeset_set_new(const char *name
, const char *privs
, PrivilegeFlags flags
)
41 struct PrivilegeSet
*set
;
43 s_assert(privilegeset_get(name
) == NULL
);
45 set
= rb_malloc(sizeof(struct PrivilegeSet
));
47 set
->name
= rb_strdup(name
);
48 set
->privs
= rb_strdup(privs
);
51 rb_dlinkAdd(set
, &set
->node
, &privilegeset_list
);
57 privilegeset_extend(struct PrivilegeSet
*parent
, const char *name
, const char *privs
, PrivilegeFlags flags
)
59 struct PrivilegeSet
*set
;
61 s_assert(parent
!= NULL
);
62 s_assert(name
!= NULL
);
63 s_assert(privs
!= NULL
);
64 s_assert(privilegeset_get(name
) == NULL
);
66 set
= rb_malloc(sizeof(struct PrivilegeSet
));
68 set
->name
= rb_strdup(name
);
70 set
->privs
= rb_malloc(strlen(parent
->privs
) + 1 + strlen(privs
) + 1);
71 strcpy(set
->privs
, parent
->privs
);
72 strcat(set
->privs
, " ");
73 strcat(set
->privs
, privs
);
75 rb_dlinkAdd(set
, &set
->node
, &privilegeset_list
);
81 privilegeset_get(const char *name
)
85 s_assert(name
!= NULL
);
87 RB_DLINK_FOREACH(iter
, privilegeset_list
.head
)
89 struct PrivilegeSet
*set
= (struct PrivilegeSet
*) iter
->data
;
91 if (!strcasecmp(set
->name
, name
))
99 privilegeset_ref(struct PrivilegeSet
*set
)
101 s_assert(set
!= NULL
);
107 privilegeset_unref(struct PrivilegeSet
*set
)
109 s_assert(set
!= NULL
);
111 if (--set
->refs
== 0)
113 rb_dlinkDelete(&set
->node
, &privilegeset_list
);