jabberd2  2.2.16
sm/aci.c
Go to the documentation of this file.
00001 /*
00002  * jabberd - Jabber Open Source Server
00003  * Copyright (c) 2002 Jeremie Miller, Thomas Muldowney,
00004  *                    Ryan Eatmon, Robert Norris
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA02111-1307USA
00019  */
00020 
00021 #include "sm.h"
00022 
00030 xht aci_load(sm_t sm)
00031 {
00032     xht acls;
00033     int aelem, jelem, attr;
00034     char type[33];
00035     jid_t list, jid;
00036 
00037     log_debug(ZONE, "loading aci");
00038 
00039     acls = xhash_new(51);
00040 
00041     if((aelem = nad_find_elem(sm->config->nad, 0, -1, "aci", 1)) < 0)
00042         return acls;
00043 
00044     aelem = nad_find_elem(sm->config->nad, aelem, -1, "acl", 1);
00045     while(aelem >= 0)
00046     {
00047         list = NULL;
00048 
00049         if((attr = nad_find_attr(sm->config->nad, aelem, -1, "type", NULL)) < 0)
00050         {
00051             aelem = nad_find_elem(sm->config->nad, aelem, -1, "acl", 0);
00052             continue;
00053         }
00054 
00055         snprintf(type, 33, "%.*s", NAD_AVAL_L(sm->config->nad, attr), NAD_AVAL(sm->config->nad, attr));
00056 
00057         log_debug(ZONE, "building list for '%s'", type);
00058 
00059         jelem = nad_find_elem(sm->config->nad, aelem, -1, "jid", 1);
00060         while(jelem >= 0)
00061         {
00062             if(NAD_CDATA_L(sm->config->nad, jelem) > 0)
00063             {
00064                 jid = jid_new(NAD_CDATA(sm->config->nad, jelem), NAD_CDATA_L(sm->config->nad, jelem));
00065                 list = jid_append(list, jid);
00066                 
00067                 log_debug(ZONE, "added '%s'", jid_user(jid));
00068 
00069                 jid_free(jid);
00070             }
00071 
00072             jelem = nad_find_elem(sm->config->nad, jelem, -1, "jid", 0);
00073         }
00074 
00075         if(list != NULL) {
00076             xhash_put(acls, pstrdup(xhash_pool(acls), type), (void *) list);
00077         }
00078 
00079         aelem = nad_find_elem(sm->config->nad, aelem, -1, "acl", 0);
00080     }
00081 
00082     return acls;
00083 }
00084 
00086 int aci_check(xht acls, char *type, jid_t jid)
00087 {
00088     jid_t list, dup;
00089 
00090     dup = jid_dup(jid);
00091     if (dup->resource[0]) {
00092         /* resourceless version */
00093         dup->resource[0] = '\0';
00094         dup->dirty = 1;
00095     }
00096 
00097     log_debug(ZONE, "checking for '%s' in acl 'all'", jid_full(jid));
00098     list = (jid_t) xhash_get(acls, "all");
00099     if(jid_search(list, jid)) {
00100         jid_free(dup);
00101         return 1;
00102     }
00103 
00104     log_debug(ZONE, "checking for '%s' in acl 'all'", jid_user(dup));
00105     if(jid_search(list, dup)) {
00106         jid_free(dup);
00107         return 1;
00108     }
00109 
00110     if(type != NULL) {
00111         log_debug(ZONE, "checking for '%s' in acl '%s'", jid_full(jid), type);
00112         list = (jid_t) xhash_get(acls, type);
00113         if(jid_search(list, jid)) {
00114             jid_free(dup);
00115             return 1;
00116         }
00117 
00118         log_debug(ZONE, "checking for '%s' in acl '%s'", jid_user(dup), type);
00119         if(jid_search(list, dup)) {
00120             jid_free(dup);
00121             return 1;
00122         }
00123     }
00124 
00125     jid_free(dup);
00126     return 0;
00127 }
00128 
00129 void aci_unload(xht acls)
00130 { 
00131     jid_t list, jid;
00132 
00133     log_debug(ZONE, "unloading acls");
00134 
00135     if(xhash_iter_first(acls))
00136         do {
00137             xhash_iter_get(acls, NULL, NULL, (void *) &list);
00138             while (list != NULL) {
00139                jid = list;
00140                list = list->next;
00141                jid_free(jid);
00142             }
00143         } while(xhash_iter_next(acls));
00144 
00145   return;
00146 }