From 9291987bba2e61795ad1603541641caae0591676 Mon Sep 17 00:00:00 2001 From: William Pitcock Date: Sun, 17 Aug 2008 06:27:59 -0500 Subject: [PATCH] Add basic PrivilegeSet API. --- include/privilege.h | 47 +++++++++++++++++++++++ src/Makefile.in | 1 + src/privilege.c | 93 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 include/privilege.h create mode 100644 src/privilege.c diff --git a/include/privilege.h b/include/privilege.h new file mode 100644 index 0000000..b033839 --- /dev/null +++ b/include/privilege.h @@ -0,0 +1,47 @@ +/* + * charybdis: an advanced ircd. + * privilege.h: Dynamic privileges API. + * + * Copyright (c) 2008 William Pitcock + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __CHARYBDIS_PRIVILEGE_H +#define __CHARYBDIS_PRIVILEGE_H + +#include "stdinc.h" + +typedef enum { + PRIV_NEEDOPER = 1 +} PrivilegeFlags; + +struct PrivilegeSet { + int refs; + char *name; + char *privs; + PrivilegeFlags flags; + rb_dlink_node node; +}; + +int privilegeset_in_set(struct PrivilegeSet *set, const char *priv); +struct PrivilegeSet *privilegeset_set_new(const char *name, const char *privs, PrivilegeFlags flags); +struct PrivilegeSet *privilegeset_get(const char *name); +void privilegeset_ref(struct PrivilegeSet *set); +void privilegeset_unref(struct PrivilegeSet *set); + +#endif diff --git a/src/Makefile.in b/src/Makefile.in index 5322b5a..be06853 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -78,6 +78,7 @@ SRCS = \ numeric.c \ packet.c \ parse.c \ + privilege.c \ res.c \ reslib.c \ reject.c \ diff --git a/src/privilege.c b/src/privilege.c new file mode 100644 index 0000000..91cc39d --- /dev/null +++ b/src/privilege.c @@ -0,0 +1,93 @@ +/* + * charybdis: an advanced ircd. + * privilege.c: Dynamic privileges API. + * + * Copyright (c) 2008 William Pitcock + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice is present in all copies. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING + * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include "privilege.h" + +static rb_dlink_list privilegeset_list = {}; + +int +privilegeset_in_set(struct PrivilegeSet *set, const char *priv) +{ + s_assert(set != NULL); + s_assert(priv != NULL); + + return strstr(set->privs, priv) != NULL; +} + +struct PrivilegeSet * +privilegeset_set_new(const char *name, const char *privs, PrivilegeFlags flags) +{ + struct PrivilegeSet *set; + + set = rb_malloc(sizeof(struct PrivilegeSet)); + set->refs = 1; + set->name = rb_strdup(name); + set->privs = rb_strdup(privs); + set->flags = flags; + + rb_dlinkAdd(set, &set->node, &privilegeset_list); + + return set; +} + +struct PrivilegeSet * +privilegeset_get(const char *name) +{ + rb_dlink_node *iter; + + s_assert(name != NULL); + + RB_DLINK_FOREACH(iter, privilegeset_list.head) + { + struct PrivilegeSet *set = (struct PrivilegeSet *) iter->data; + + if (!strcasecmp(set->name, name)) + return set; + } + + return NULL; +} + +void +privilegeset_ref(struct PrivilegeSet *set) +{ + s_assert(set != NULL); + + set->refs++; +} + +void +privilegeset_unref(struct PrivilegeSet *set) +{ + s_assert(set != NULL); + + if (--set->refs == 0) + { + rb_dlinkDelete(&set->node, &privilegeset_list); + + rb_free(set->name); + rb_free(set->privs); + rb_free(set); + } +}