jabberd2  2.7.0
aci.c
Go to the documentation of this file.
1 /*
2  * jabberd - Jabber Open Source Server
3  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
4  * Ryan Eatmon, Robert Norris
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
19  */
20 
21 #include "router.h"
22 
25 typedef struct aci_user_st *aci_user_t;
26 struct aci_user_st {
27  char *name;
29 };
30 
32  xht aci;
33  int aelem, uelem, attr;
34  char type[33];
35  aci_user_t list_head, list_tail, user;
36 
37  log_debug(ZONE, "loading aci");
38 
39  aci = xhash_new(51);
40 
41  if((aelem = nad_find_elem(r->config->nad, 0, -1, "aci", 1)) < 0)
42  return aci;
43 
44  aelem = nad_find_elem(r->config->nad, aelem, -1, "acl", 1);
45  while(aelem >= 0) {
46  if((attr = nad_find_attr(r->config->nad, aelem, -1, "type", NULL)) < 0) {
47  aelem = nad_find_elem(r->config->nad, aelem, -1, "acl", 0);
48  continue;
49  }
50 
51  list_head = NULL;
52  list_tail = NULL;
53 
54  snprintf(type, 33, "%.*s", NAD_AVAL_L(r->config->nad, attr), NAD_AVAL(r->config->nad, attr));
55 
56  log_debug(ZONE, "building list for '%s'", type);
57 
58  uelem = nad_find_elem(r->config->nad, aelem, -1, "user", 1);
59  while(uelem >= 0) {
60  if(NAD_CDATA_L(r->config->nad, uelem) > 0) {
61  user = (aci_user_t) calloc(1, sizeof(struct aci_user_st));
62 
63  user->name = (char *) malloc(sizeof(char) * (NAD_CDATA_L(r->config->nad, uelem) + 1));
64  sprintf(user->name, "%.*s", NAD_CDATA_L(r->config->nad, uelem), NAD_CDATA(r->config->nad, uelem));
65 
66  if(list_tail != NULL) {
67  list_tail->next = user;
68  list_tail = user;
69  }
70 
71  /* record the head of the list */
72  if(list_head == NULL) {
73  list_head = user;
74  list_tail = user;
75  }
76 
77  log_debug(ZONE, "added '%s'", user->name);
78  }
79 
80  uelem = nad_find_elem(r->config->nad, uelem, -1, "user", 0);
81  }
82 
83  if(list_head != NULL)
84  xhash_put(aci, pstrdup(xhash_pool(aci), type), (void *) list_head);
85 
86  aelem = nad_find_elem(r->config->nad, aelem, -1, "acl", 0);
87  }
88 
89  return aci;
90 }
91 
93 int aci_check(xht aci, const char *type, const char *name) {
94  aci_user_t list, scan;
95 
96  log_debug(ZONE, "checking for '%s' in acl 'all'", name);
97  list = (aci_user_t) xhash_get(aci, "all");
98  for(scan = list; scan != NULL; scan = scan->next)
99  if(strcmp(scan->name, name) == 0)
100  return 1;
101 
102  if(type != NULL) {
103  log_debug(ZONE, "checking for '%s' in acl '%s'", name, type);
104  list = (aci_user_t) xhash_get(aci, type);
105  for(scan = list; scan != NULL; scan = scan->next)
106  if(strcmp(scan->name, name) == 0)
107  return 1;
108  }
109 
110  return 0;
111 }
112 
114 void aci_unload(xht aci) {
115  aci_user_t list, user;
116 
117  /* free list of users for each acl*/
118  if(xhash_iter_first(aci))
119  do {
120  xhash_iter_get(aci, NULL, NULL, (void *) &list);
121  while (list != NULL) {
122  user = list;
123  list = list->next;
124  free(user->name);
125  free(user);
126  }
127  } while(xhash_iter_next(aci));
128 
129  xhash_free(aci);
130  return;
131 }
#define NAD_CDATA_L(N, E)
Definition: nad.h:186
char * name
Definition: aci.c:27
void xhash_free(xht h)
Definition: xhash.c:241
xht aci_load(router_t r)
Definition: aci.c:31
int nad_find_attr(nad_t nad, unsigned int elem, int ns, const char *name, const char *val)
get a matching attr on this elem, both name and optional val
Definition: nad.c:237
int xhash_iter_next(xht h)
Definition: xhash.c:320
nad_t nad
Definition: util.h:203
struct aci_user_st * aci_user_t
aci manager
Definition: aci.c:25
int aci_check(xht aci, const char *type, const char *name)
see if a username is in an acl
Definition: aci.c:93
void xhash_put(xht h, const char *key, void *val)
Definition: xhash.c:163
int xhash_iter_get(xht h, const char **key, int *keylen, void **val)
Definition: xhash.c:374
#define NAD_AVAL_L(N, A)
Definition: nad.h:190
#define log_debug(...)
Definition: log.h:65
#define NAD_AVAL(N, A)
Definition: nad.h:189
int nad_find_elem(nad_t nad, unsigned int elem, int ns, const char *name, int depth)
locate the next elem at a given depth with an optional matching name
Definition: nad.c:206
int xhash_iter_first(xht h)
iteration
Definition: xhash.c:311
pool_t xhash_pool(xht h)
get our pool
Definition: xhash.c:305
char * pstrdup(pool_t p, const char *src)
XXX efficient: move this to const char * and then loop throug the existing heaps to see if src is wit...
Definition: pool.c:191
#define NAD_CDATA(N, E)
Definition: nad.h:185
aci_user_t next
Definition: aci.c:28
void * xhash_get(xht h, const char *key)
Definition: xhash.c:184
#define ZONE
Definition: mio_impl.h:76
xht xhash_new(int prime)
Definition: xhash.c:96
config_t config
config
Definition: router.h:74
void aci_unload(xht aci)
unload aci table
Definition: aci.c:114