root/trunk/src/acl.c

Revision 680, 2.7 kB (checked in by jajcus, 3 years ago)

- dates in the copyright header updated

Line 
1 /* $Id: acl.c,v 1.6 2004/04/13 17:44:07 jajcus Exp $ */
2
3 /*
4  *  (C) Copyright 2002-2006 Jacek Konieczny [jajcus(a)jajcus,net]
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 Version 2 as
8  *  published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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; /* no match */
97                 }
98                 if (acl_e && acl_e->what && !xmlnode_get_tag(x,acl_e->what)) continue; /* no match */
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 }
Note: See TracBrowser for help on using the browser.