exec.c

Go to the documentation of this file.
00001 /*
00002  *
00003  * $Id: exec.c 4518 2008-07-28 15:39:28Z henningw $
00004  *
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio 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  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History
00025  * --------
00026  * 2003-02-28 scratchpad compatibility abandoned (jiri)
00027  * 2003-01-28 scratchpad removed
00028  * 2004-07-21 rewrite uri done via action() (bogdan)
00029  */
00030 
00031 
00032 #include <stdio.h>
00033 #include <strings.h>
00034 #include <errno.h>
00035 #include <stdlib.h>
00036 #include <sys/types.h>
00037 /* 
00038 #include <sys/resource.h>
00039 */
00040 #include <sys/wait.h>
00041 #include "../../mem/mem.h"
00042 #include "../../error.h"
00043 #include "../../config.h"
00044 #include "../../parser/msg_parser.h"
00045 #include "../../dprint.h"
00046 #include "../../dset.h"
00047 #include "../../action.h"
00048 #include "../../usr_avp.h"
00049 
00050 #include "exec.h"
00051 
00052 int exec_msg(struct sip_msg *msg, char *cmd )
00053 {
00054    FILE *pipe;
00055    int exit_status;
00056    int ret;
00057 
00058    ret=-1; /* pessimist: assume error */
00059    pipe=popen( cmd, "w" );
00060    if (pipe==NULL) {
00061       LM_ERR("cannot open pipe: %s\n", cmd);
00062       ser_error=E_EXEC;
00063       return -1;
00064    }
00065 
00066    if (fwrite(msg->buf, 1, msg->len, pipe)!=msg->len) {
00067       LM_ERR("failed to write to pipe\n");
00068       ser_error=E_EXEC;
00069       goto error01;
00070    }
00071    /* success */
00072    ret=1;
00073 
00074 error01:
00075    if (ferror(pipe)) {
00076       LM_ERR("pipe: %s\n", strerror(errno));
00077       ser_error=E_EXEC;
00078       ret=-1;
00079    }
00080    exit_status=pclose(pipe);
00081    if (WIFEXITED(exit_status)) { /* exited properly .... */
00082       /* return false if script exited with non-zero status */
00083       if (WEXITSTATUS(exit_status)!=0) ret=-1;
00084    } else { /* exited erroneously */
00085       LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n",
00086          cmd, exit_status, errno, strerror(errno) );
00087       ret=-1;
00088    }
00089    return ret;
00090 }
00091 
00092 int exec_str(struct sip_msg *msg, char *cmd, char *param, int param_len) {
00093 
00094    struct action act;
00095    int cmd_len;
00096    FILE *pipe; 
00097    char *cmd_line;
00098    int ret;
00099    int l1;
00100    static char uri_line[MAX_URI_SIZE+1];
00101    int uri_cnt;
00102    str uri;
00103    int exit_status;
00104 
00105    /* pessimist: assume error by default */
00106    ret=-1;
00107    
00108    l1=strlen(cmd);
00109    if(param_len>0)
00110       cmd_len=l1+param_len+4;
00111    else
00112       cmd_len=l1+1;
00113    cmd_line=pkg_malloc(cmd_len);
00114    if (cmd_line==0) {
00115       ret=ser_error=E_OUT_OF_MEM;
00116       LM_ERR("no pkg mem for command\n");
00117       goto error00;
00118    }
00119 
00120    /* 'command parameter \0' */
00121    memcpy(cmd_line, cmd, l1); 
00122    if(param_len>0)
00123    {
00124       cmd_line[l1]=' ';
00125       cmd_line[l1+1]='\'';
00126       memcpy(cmd_line+l1+2, param, param_len);
00127       cmd_line[l1+param_len+2]='\'';
00128       cmd_line[l1+param_len+3]=0;
00129    } else {
00130       cmd_line[l1] = 0;
00131    }
00132    
00133    pipe=popen( cmd_line, "r" );
00134    if (pipe==NULL) {
00135       LM_ERR("cannot open pipe: %s\n", cmd_line);
00136       ser_error=E_EXEC;
00137       goto error01;
00138    }
00139 
00140    /* read now line by line */
00141    uri_cnt=0;
00142    while( fgets(uri_line, MAX_URI_SIZE, pipe)!=NULL){
00143       uri.s = uri_line;
00144       uri.len=strlen(uri.s);
00145       /* trim from right */
00146       while(uri.len && (uri.s[uri.len-1]=='\r' 
00147             || uri.s[uri.len-1]=='\n' 
00148             || uri.s[uri.len-1]=='\t'
00149             || uri.s[uri.len-1]==' ' )) {
00150          LM_DBG("rtrim\n");
00151          uri.len--;
00152       }
00153       /* skip empty line */
00154       if (uri.len==0) continue;
00155       /* ZT */
00156       uri.s[uri.len]=0;
00157       if (uri_cnt==0) {
00158          memset(&act, 0, sizeof(act));
00159          act.type = SET_URI_T;
00160          act.elem[0].type = STRING_ST;
00161          act.elem[0].u.string = uri.s;
00162          if (do_action(&act, msg)<0) {
00163             LM_ERR("the action for has failed\n");
00164             ser_error=E_OUT_OF_MEM;
00165             goto error02;
00166          }
00167       } else {
00168          if (append_branch(msg, &uri, 0, 0, Q_UNSPECIFIED, 0, 0)==-1) {
00169             LM_ERR("append_branch failed; too many or too long URIs?\n");
00170             goto error02;
00171          }
00172       }
00173       uri_cnt++;
00174    }
00175    if (uri_cnt==0) {
00176       LM_ERR("no uri from %s\n", cmd_line );
00177       goto error02;
00178    }
00179    /* success */
00180    ret=1;
00181 
00182 error02:
00183    if (ferror(pipe)) {
00184       LM_ERR("in pipe: %s\n", strerror(errno));
00185       ser_error=E_EXEC;
00186       ret=-1;
00187    }
00188    exit_status=pclose(pipe);
00189    if (WIFEXITED(exit_status)) { /* exited properly .... */
00190       /* return false if script exited with non-zero status */
00191       if (WEXITSTATUS(exit_status)!=0) ret=-1;
00192    } else { /* exited erroneously */
00193       LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n",
00194          cmd, exit_status, errno, strerror(errno) );
00195       ret=-1;
00196    }
00197 error01:
00198    pkg_free(cmd_line);
00199 error00:
00200    return ret;
00201 }
00202 
00203 
00204 int exec_avp(struct sip_msg *msg, char *cmd, pvname_list_p avpl)
00205 {
00206    int_str avp_val;
00207    int_str avp_name;
00208    unsigned short avp_type;
00209    FILE *pipe; 
00210    int ret;
00211    char res_line[MAX_URI_SIZE+1];
00212    str res;
00213    int exit_status;
00214    int i;
00215    pvname_list_t* crt;
00216 
00217    /* pessimist: assume error by default */
00218    ret=-1;
00219    
00220    pipe=popen( cmd, "r" );
00221    if (pipe==NULL) {
00222       LM_ERR("cannot open pipe: %s\n", cmd);
00223       ser_error=E_EXEC;
00224       return ret;
00225    }
00226 
00227    /* read now line by line */
00228    i=0;
00229    crt = avpl;
00230    while( fgets(res_line, MAX_URI_SIZE, pipe)!=NULL){
00231       res.s = res_line;
00232       res.len=strlen(res.s);
00233       /* trim from right */
00234       while(res.len && (res.s[res.len-1]=='\r' 
00235             || res.s[res.len-1]=='\n' 
00236             || res.s[res.len-1]=='\t'
00237             || res.s[res.len-1]==' ' )) {
00238          res.len--;
00239       }
00240       /* skip empty line */
00241       if (res.len==0) continue;
00242       /* ZT */
00243       res.s[res.len]=0;
00244 
00245       avp_type = 0;
00246       if(crt==NULL)
00247       {
00248          avp_name.n = i+1;
00249       } else {
00250          if(pv_get_avp_name(msg, &(crt->sname.pvp), &avp_name, &avp_type)!=0)
00251          {
00252             LM_ERR("can't get item name [%d]\n",i);
00253             goto error;
00254          }
00255       }
00256 
00257       avp_type |= AVP_VAL_STR;
00258       avp_val.s = res;
00259    
00260       if(add_avp(avp_type, avp_name, avp_val)!=0)
00261       {
00262          LM_ERR("unable to add avp\n");
00263          goto error;
00264       }
00265    
00266       if(crt)
00267          crt = crt->next;
00268 
00269       i++;
00270    }
00271    if (i==0)
00272       LM_DBG("no result from %s\n", cmd);
00273    /* success */
00274    ret=1;
00275 
00276 error:
00277    if (ferror(pipe)) {
00278       LM_ERR("pipe: %d/%s\n", errno, strerror(errno));
00279       ser_error=E_EXEC;
00280       ret=-1;
00281    }
00282    exit_status=pclose(pipe);
00283    if (WIFEXITED(exit_status)) { /* exited properly .... */
00284       /* return false if script exited with non-zero status */
00285       if (WEXITSTATUS(exit_status)!=0) ret=-1;
00286    } else { /* exited erroneously */
00287       LM_ERR("cmd %s failed. exit_status=%d, errno=%d: %s\n",
00288          cmd, exit_status, errno, strerror(errno) );
00289       ret=-1;
00290    }
00291    return ret;
00292 }
00293 

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