| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
#include "ggtrans.h" |
|---|
| 21 |
#include "conf.h" |
|---|
| 22 |
#include "jid.h" |
|---|
| 23 |
#include <fnmatch.h> |
|---|
| 24 |
#include "debug.h" |
|---|
| 25 |
|
|---|
| 26 |
struct acl_s { |
|---|
| 27 |
int allow; |
|---|
| 28 |
char *what; |
|---|
| 29 |
char *who; |
|---|
| 30 |
}; |
|---|
| 31 |
|
|---|
| 32 |
static GList *acl=NULL; |
|---|
| 33 |
|
|---|
| 34 |
int acl_init(){ |
|---|
| 35 |
xmlnode node; |
|---|
| 36 |
char *str,*what,*who; |
|---|
| 37 |
int allow=0; |
|---|
| 38 |
struct acl_s *acl_e; |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
node=xmlnode_get_tag(config,"acl"); |
|---|
| 42 |
if (!node) return 0; |
|---|
| 43 |
|
|---|
| 44 |
for(node=xmlnode_get_firstchild(node);node;node=xmlnode_get_nextsibling(node)){ |
|---|
| 45 |
str=xmlnode_get_name(node); |
|---|
| 46 |
if (!str) continue; |
|---|
| 47 |
if (!strcmp(str,"allow")) allow=1; |
|---|
| 48 |
else if (!strcmp(str,"deny")) allow=0; |
|---|
| 49 |
else g_error(L_("Invalid tag <%s/> in config section <acl/>"),str); |
|---|
| 50 |
what=xmlnode_get_attrib(node,"what"); |
|---|
| 51 |
if (!what || what[0]=='\000' || !strcmp(what,"*")) what=NULL; |
|---|
| 52 |
who=xmlnode_get_attrib(node,"who"); |
|---|
| 53 |
if (!who || who[0]=='\000' || !strcmp(who,"*")) who=NULL; |
|---|
| 54 |
acl_e=g_new0(struct acl_s,1); |
|---|
| 55 |
if (what) acl_e->what=g_strdup(what); |
|---|
| 56 |
if (who) acl_e->who=g_strdup(who); |
|---|
| 57 |
acl_e->allow=allow; |
|---|
| 58 |
acl=g_list_append(acl,acl_e); |
|---|
| 59 |
} |
|---|
| 60 |
return 0; |
|---|
| 61 |
} |
|---|
| 62 |
|
|---|
| 63 |
void acl_done(){ |
|---|
| 64 |
GList *it; |
|---|
| 65 |
struct acl_s *acl_e; |
|---|
| 66 |
|
|---|
| 67 |
for(it=g_list_first(acl);it;it=g_list_next(it)){ |
|---|
| 68 |
acl_e=(struct acl_s*)it->data; |
|---|
| 69 |
g_free(acl_e->who); |
|---|
| 70 |
g_free(acl_e->what); |
|---|
| 71 |
g_free(acl_e); |
|---|
| 72 |
} |
|---|
| 73 |
g_list_free(acl); |
|---|
| 74 |
} |
|---|
| 75 |
|
|---|
| 76 |
int acl_is_allowed(const char *from,xmlnode node){ |
|---|
| 77 |
GList *it; |
|---|
| 78 |
struct acl_s *acl_e; |
|---|
| 79 |
char *jid; |
|---|
| 80 |
xmlnode x; |
|---|
| 81 |
int result=0; |
|---|
| 82 |
|
|---|
| 83 |
if (from){ |
|---|
| 84 |
jid=jid_normalized(from,0); |
|---|
| 85 |
if (jid==NULL){ |
|---|
| 86 |
debug(L_("Not Allowed - bad 'from'")); |
|---|
| 87 |
return 0; |
|---|
| 88 |
} |
|---|
| 89 |
} |
|---|
| 90 |
else jid=NULL; |
|---|
| 91 |
x=xmlnode_new_tag("x"); |
|---|
| 92 |
xmlnode_insert_tag_node(x,node); |
|---|
| 93 |
for(it=g_list_first(acl);it;it=g_list_next(it)){ |
|---|
| 94 |
acl_e=(struct acl_s*)it->data; |
|---|
| 95 |
if (acl_e->who && jid){ |
|---|
| 96 |
if (fnmatch(acl_e->who,jid,0)) continue; |
|---|
| 97 |
} |
|---|
| 98 |
if (acl_e && acl_e->what && !xmlnode_get_tag(x,acl_e->what)) continue; |
|---|
| 99 |
result=acl_e->allow; |
|---|
| 100 |
break; |
|---|
| 101 |
} |
|---|
| 102 |
if (it==NULL) result=1; |
|---|
| 103 |
xmlnode_free(x); |
|---|
| 104 |
g_free(jid); |
|---|
| 105 |
if (result) debug(L_("Allowed")); |
|---|
| 106 |
else debug(L_("Denied")); |
|---|
| 107 |
return result; |
|---|
| 108 |
} |
|---|