jabberd2  2.3.6
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 "sm.h"
22 
31 {
32  xht acls;
33  int aelem, jelem, attr;
34  char type[33];
35  jid_t list, jid;
36 
37  log_debug(ZONE, "loading aci");
38 
39  acls = xhash_new(51);
40 
41  if((aelem = nad_find_elem(sm->config->nad, 0, -1, "aci", 1)) < 0)
42  return acls;
43 
44  aelem = nad_find_elem(sm->config->nad, aelem, -1, "acl", 1);
45  while(aelem >= 0)
46  {
47  list = NULL;
48 
49  if((attr = nad_find_attr(sm->config->nad, aelem, -1, "type", NULL)) < 0)
50  {
51  aelem = nad_find_elem(sm->config->nad, aelem, -1, "acl", 0);
52  continue;
53  }
54 
55  snprintf(type, 33, "%.*s", NAD_AVAL_L(sm->config->nad, attr), NAD_AVAL(sm->config->nad, attr));
56 
57  log_debug(ZONE, "building list for '%s'", type);
58 
59  jelem = nad_find_elem(sm->config->nad, aelem, -1, "jid", 1);
60  while(jelem >= 0)
61  {
62  if(NAD_CDATA_L(sm->config->nad, jelem) > 0)
63  {
64  jid = jid_new(NAD_CDATA(sm->config->nad, jelem), NAD_CDATA_L(sm->config->nad, jelem));
65  list = jid_append(list, jid);
66 
67  log_debug(ZONE, "added '%s'", jid_user(jid));
68 
69  jid_free(jid);
70  }
71 
72  jelem = nad_find_elem(sm->config->nad, jelem, -1, "jid", 0);
73  }
74 
75  if(list != NULL) {
76  xhash_put(acls, pstrdup(xhash_pool(acls), type), (void *) list);
77  }
78 
79  aelem = nad_find_elem(sm->config->nad, aelem, -1, "acl", 0);
80  }
81 
82  return acls;
83 }
84 
86 int aci_check(xht acls, const char *type, jid_t jid)
87 {
88  jid_t list, dup;
89 
90  dup = jid_dup(jid);
91  if (dup->resource[0]) {
92  /* resourceless version */
93  dup->resource[0] = '\0';
94  dup->dirty = 1;
95  }
96 
97  log_debug(ZONE, "checking for '%s' in acl 'all'", jid_full(jid));
98  list = (jid_t) xhash_get(acls, "all");
99  if(jid_search(list, jid)) {
100  jid_free(dup);
101  return 1;
102  }
103 
104  log_debug(ZONE, "checking for '%s' in acl 'all'", jid_user(dup));
105  if(jid_search(list, dup)) {
106  jid_free(dup);
107  return 1;
108  }
109 
110  if(type != NULL) {
111  log_debug(ZONE, "checking for '%s' in acl '%s'", jid_full(jid), type);
112  list = (jid_t) xhash_get(acls, type);
113  if(jid_search(list, jid)) {
114  jid_free(dup);
115  return 1;
116  }
117 
118  log_debug(ZONE, "checking for '%s' in acl '%s'", jid_user(dup), type);
119  if(jid_search(list, dup)) {
120  jid_free(dup);
121  return 1;
122  }
123  }
124 
125  jid_free(dup);
126  return 0;
127 }
128 
129 void aci_unload(xht acls)
130 {
131  jid_t list, jid;
132 
133  log_debug(ZONE, "unloading acls");
134 
135  if(xhash_iter_first(acls))
136  do {
137  xhash_iter_get(acls, NULL, NULL, (void *) &list);
138  while (list != NULL) {
139  jid = list;
140  list = list->next;
141  jid_free(jid);
142  }
143  } while(xhash_iter_next(acls));
144 
145  return;
146 }
struct jid_st * jid_t
static sm_t sm
Definition: main.c:33
data structures and prototypes for the session manager
#define NAD_CDATA_L(N, E)
Definition: nad.h:186
xht aci_load(router_t r)
Definition: aci.c:31
const char * jid_user(jid_t jid)
expand and return the user
Definition: jid.c:339
const char * jid_full(jid_t jid)
expand and return the full
Definition: jid.c:347
jid_t jid_new(const char *id, int len)
make a new jid
Definition: jid.c:81
int nad_find_elem(nad_t nad, 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:204
int dirty
Definition: jid.h:64
config_t config
config context
Definition: sm.h:198
char * resource
Definition: jid.h:46
int xhash_iter_next(xht h)
Definition: xhash.c:320
nad_t nad
Definition: util.h:203
int jid_search(jid_t list, jid_t jid)
util to search through jids
Definition: jid.c:413
session manager global context
Definition: sm.h:167
void jid_free(jid_t jid)
free a jid
Definition: jid.c:286
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
Definition: jid.h:42
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
jid_t jid_append(jid_t list, jid_t jid)
make a copy of jid, link into list (avoiding dups)
Definition: jid.c:464
#define NAD_AVAL(N, A)
Definition: nad.h:189
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
jid_t jid_dup(jid_t jid)
duplicate a jid
Definition: jid.c:373
void * xhash_get(xht h, const char *key)
Definition: xhash.c:184
#define ZONE
Definition: mio_impl.h:76
struct jid_st * next
Definition: jid.h:67
xht xhash_new(int prime)
Definition: xhash.c:96
int nad_find_attr(nad_t nad, 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:235
void aci_unload(xht aci)
unload aci table
Definition: aci.c:114