modules_s/exec/exec_mod.c

00001 /*
00002  * execution module
00003  *
00004  * $Id$
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of ser, a free SIP server.
00009  *
00010  * ser is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * For a license to use the ser software under conditions
00016  * other than those described here, or to purchase support for this
00017  * software, please contact iptel.org by e-mail at the following addresses:
00018  *    info@iptel.org
00019  *
00020  * ser is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00028  *
00029  * History:
00030  * -------
00031  * 2003-03-11: New module interface (janakj)
00032  * 2003-03-16: flags export parameter added (janakj)
00033  */
00034 
00035 
00036 #include <stdio.h>
00037 
00038 #include "../../parser/msg_parser.h"
00039 #include "../../str.h"
00040 #include "../../sr_module.h"
00041 #include "../../dprint.h"
00042 #include "../../parser/parse_uri.h"
00043 
00044 #include "exec.h"
00045 #include "kill.h"
00046 #include "exec_hf.h"
00047 
00048 MODULE_VERSION
00049 
00050 unsigned int time_to_kill=0;
00051 
00052 static int mod_init( void );
00053 
00054 inline static int w_exec_dset(struct sip_msg* msg, char* cmd, char* foo);
00055 inline static int w_exec_msg(struct sip_msg* msg, char* cmd, char* foo);
00056 
00057 inline static void exec_shutdown();
00058 
00059 /*
00060  * Exported functions
00061  */
00062 static cmd_export_t cmds[] = {
00063         {"exec_dset", w_exec_dset, 1, fixup_var_str_1, REQUEST_ROUTE | FAILURE_ROUTE},
00064         {"exec_msg",  w_exec_msg,  1, fixup_var_str_1, REQUEST_ROUTE | FAILURE_ROUTE},
00065         {0, 0, 0, 0, 0}
00066 };
00067 
00068 
00069 /*
00070  * Exported parameters
00071  */
00072 static param_export_t params[] = {
00073         {"time_to_kill", PARAM_INT, &time_to_kill},
00074         {"setvars",      PARAM_INT, &setvars     },
00075         {0, 0, 0}
00076 };
00077 
00078 
00079 #ifdef STATIC_EXEC
00080 struct module_exports exec_exports = {
00081 #else
00082 struct module_exports exports= {
00083 #endif
00084         "exec",
00085         cmds,           /* Exported functions */
00086         0,              /* RPC methods */
00087         params,         /* Exported parameters */
00088         mod_init,       /* initialization module */
00089         0,              /* response function */
00090         exec_shutdown,  /* destroy function */
00091         0,              /* oncancel function */
00092         0               /* per-child init function */
00093 };
00094 
00095 void exec_shutdown()
00096 {
00097         if (time_to_kill) destroy_kill();
00098 }
00099 
00100 
00101 static int mod_init( void )
00102 {
00103         fprintf( stderr, "exec - initializing\n");
00104         if (time_to_kill) initialize_kill();
00105         return 0;
00106 }
00107 
00108 inline static int w_exec_dset(struct sip_msg* msg, char* p1, char* foo)
00109 {
00110         str cmd;
00111         str *uri;
00112         environment_t *backup;
00113         int ret;
00114 
00115         if (get_str_fparam(&cmd, msg, (fparam_t*)p1) < 0) {
00116             ERR("Error while obtaining command name\n");
00117             return -1;
00118         }
00119 
00120         backup=0;
00121         if (setvars) {
00122                 backup=set_env(msg);
00123                 if (!backup) {
00124                         LOG(L_ERR, "ERROR: w_exec_msg: no env created\n");
00125                         return -1;
00126                 }
00127         }
00128 
00129         if (msg->new_uri.s && msg->new_uri.len)
00130                 uri=&msg->new_uri;
00131         else
00132                 uri=&msg->first_line.u.request.uri;
00133 
00134         ret=exec_str(msg, &cmd, uri->s, uri->len);
00135         if (setvars) {
00136                 unset_env(backup);
00137         }
00138         return ret;
00139 }
00140 
00141 
00142 inline static int w_exec_msg(struct sip_msg* msg, char* p1, char* foo)
00143 {
00144         str cmd;
00145         environment_t *backup;
00146         int ret;
00147 
00148         if (get_str_fparam(&cmd, msg, (fparam_t*)p1) < 0) {
00149             ERR("Error while obtaining command name\n");
00150             return -1;
00151         }
00152 
00153         backup=0;
00154         if (setvars) {
00155                 backup=set_env(msg);
00156                 if (!backup) {
00157                         LOG(L_ERR, "ERROR: w_exec_msg: no env created\n");
00158                         return -1;
00159                 }
00160         }
00161         ret=exec_msg(msg, &cmd);
00162         if (setvars) {
00163                 unset_env(backup);
00164         }
00165         return ret;
00166 }