modules/permissions/mi.c

Go to the documentation of this file.
00001 /*
00002  *
00003  * Permissions MI functions
00004  *
00005  * Copyright (C) 2006 Juha Heinanen
00006  *
00007  * This file is part of Kamailio, a free SIP server.
00008  *
00009  * Kamailio is free software; you can redistribute it and/or modify
00010  * it under the terms of the GNU General Public License as published by
00011  * the Free Software Foundation; either version 2 of the License, or
00012  * (at your option) any later version
00013  *
00014  * Kamailio is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  * GNU General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU General Public License 
00020  * along with this program; if not, write to the Free Software 
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  * History:
00024  * --------
00025  *  2006-10-16  created (juhe)
00026  */
00027 
00028 
00029 #include "../../dprint.h"
00030 #include "address.h"
00031 #include "trusted.h"
00032 #include "hash.h"
00033 #include "mi.h"
00034 #include "permissions.h"
00035 
00036 
00037 /*
00038  * MI function to reload trusted table
00039  */
00040 struct mi_root* mi_trusted_reload(struct mi_root *cmd_tree, void *param)
00041 {
00042    if (hash_table==NULL)
00043       return init_mi_tree( 200, MI_SSTR(MI_OK));
00044 
00045     if (reload_trusted_table () == 1) {
00046    return init_mi_tree( 200, MI_SSTR(MI_OK));
00047     } else {
00048    return init_mi_tree( 400, MI_SSTR("Trusted table reload failed"));
00049     }
00050 }
00051 
00052 
00053 /*
00054  * MI function to print trusted entries from current hash table
00055  */
00056 struct mi_root* mi_trusted_dump(struct mi_root *cmd_tree, void *param)
00057 {
00058    struct mi_root* rpl_tree;
00059 
00060    if (hash_table==NULL)
00061       return init_mi_tree( 500, MI_SSTR("Trusted-module not in use"));
00062 
00063    rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
00064    if (rpl_tree==NULL) return 0;
00065 
00066    if(hash_table_mi_print(*hash_table, &rpl_tree->node)< 0) {
00067       LM_ERR("failed to add a node\n");
00068       free_mi_tree(rpl_tree);
00069       return 0;
00070    }
00071 
00072    return rpl_tree;
00073 }
00074 
00075 
00076 /*
00077  * MI function to reload address table
00078  */
00079 struct mi_root* mi_address_reload(struct mi_root *cmd_tree, void *param)
00080 {
00081     if (reload_address_table () == 1) {
00082    return init_mi_tree( 200, MI_SSTR(MI_OK));
00083     } else {
00084    return init_mi_tree( 400, MI_SSTR("Address table reload failed"));
00085     }
00086 }
00087 
00088 
00089 /*
00090  * MI function to print address entries from current hash table
00091  */
00092 struct mi_root* mi_address_dump(struct mi_root *cmd_tree, void *param)
00093 {
00094     struct mi_root* rpl_tree;
00095     
00096     rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
00097     if (rpl_tree==NULL) return 0;
00098     
00099     if(addr_hash_table_mi_print(*addr_hash_table, &rpl_tree->node) <  0) {
00100    LM_ERR("failed to add a node\n");
00101    free_mi_tree(rpl_tree);
00102    return 0;
00103     }
00104 
00105     return rpl_tree;
00106 }
00107 
00108 
00109 /*
00110  * MI function to print subnets from current subnet table
00111  */
00112 struct mi_root* mi_subnet_dump(struct mi_root *cmd_tree, void *param)
00113 {
00114     struct mi_root* rpl_tree;
00115     
00116     rpl_tree = init_mi_tree( 200, MI_SSTR(MI_OK));
00117     if (rpl_tree==NULL) return 0;
00118     
00119     if(subnet_table_mi_print(*subnet_table, &rpl_tree->node) <  0) {
00120    LM_ERR("failed to add a node\n");
00121    free_mi_tree(rpl_tree);
00122    return 0;
00123     }
00124 
00125     return rpl_tree;
00126 }
00127 
00128 #define MAX_FILE_LEN 128
00129 
00130 /*
00131  * MI function to make allow_uri query.
00132  */
00133 struct mi_root* mi_allow_uri(struct mi_root *cmd, void *param)
00134 {
00135     struct mi_node *node;
00136     str *basenamep, *urip, *contactp;
00137     char basename[MAX_FILE_LEN + 1];
00138     char uri[MAX_URI_SIZE + 1], contact[MAX_URI_SIZE + 1]; 
00139     unsigned int allow_suffix_len;
00140 
00141     node = cmd->node.kids;
00142     if (node == NULL || node->next == NULL || node->next->next == NULL ||
00143    node->next->next->next != NULL)
00144    return init_mi_tree(400, MI_SSTR(MI_MISSING_PARM));
00145     
00146     /* look for base name */
00147     basenamep = &node->value;
00148     if (basenamep == NULL)
00149    return init_mi_tree(404, MI_SSTR("Basename is NULL"));
00150     allow_suffix_len = strlen(allow_suffix);
00151     if (basenamep->len + allow_suffix_len + 1 > MAX_FILE_LEN)
00152    return init_mi_tree(404, MI_SSTR("Basename is too long"));
00153     memcpy(basename, basenamep->s, basenamep->len);
00154     memcpy(basename + basenamep->len, allow_suffix, allow_suffix_len);
00155     basename[basenamep->len + allow_suffix_len] = 0;
00156 
00157     /* look for uri */
00158     urip = &node->next->value;
00159     if (urip == NULL)
00160    return init_mi_tree(404, MI_SSTR("URI is NULL"));
00161     if (urip->len > MAX_URI_SIZE)
00162    return init_mi_tree(404, MI_SSTR("URI is too long"));
00163     memcpy(uri, urip->s, urip->len);
00164     uri[urip->len] = 0;
00165 
00166     /* look for contact */
00167     contactp = &node->next->next->value;
00168     if (contactp == NULL)
00169    return init_mi_tree(404, MI_SSTR("Contact is NULL"));
00170     if (contactp->len > MAX_URI_SIZE)
00171    return init_mi_tree(404, MI_SSTR("Contact is too long"));
00172     memcpy(contact, contactp->s, contactp->len);
00173     contact[contactp->len] = 0;
00174 
00175     if (allow_test(basename, uri, contact) == 1) {
00176    return init_mi_tree(200, MI_SSTR(MI_OK));
00177     } else {
00178    return init_mi_tree(403, MI_SSTR("Forbidden"));
00179     }
00180 }

Generated on Wed May 23 20:00:27 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6