00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "../../comp_defs.h"
00038
00039 #include <stdio.h>
00040 #include <strings.h>
00041 #include <errno.h>
00042 #include <stdlib.h>
00043 #include <sys/types.h>
00044
00045
00046
00047 #include <sys/wait.h>
00048 #include "../../mem/mem.h"
00049 #include "../../error.h"
00050 #include "../../config.h"
00051 #include "../../parser/msg_parser.h"
00052 #include "../../dprint.h"
00053 #include "../../dset.h"
00054 #include "../../action.h"
00055 #include "../../ut.h"
00056 #include "config.h"
00057
00058 int exec_msg(struct sip_msg *msg, str* cmd )
00059 {
00060 FILE *pipe;
00061 int exit_status;
00062 int ret;
00063 char* c;
00064
00065 ret=-1;
00066
00067 c = as_asciiz(cmd);
00068 if (!c) {
00069 ERR("No memory left\n");
00070 return -1;
00071 }
00072
00073 pipe=popen( c, "w" );
00074 pkg_free(c);
00075 if (pipe==NULL) {
00076 LOG(L_ERR, "ERROR: exec_msg: cannot open pipe: %.*s\n",
00077 cmd->len, ZSW(cmd->s));
00078 ser_error=E_EXEC;
00079 return -1;
00080 }
00081
00082 if (fwrite(msg->buf, 1, msg->len, pipe)!=msg->len) {
00083 LOG(L_ERR, "ERROR: exec_msg: error writing to pipe\n");
00084 ser_error=E_EXEC;
00085 goto error01;
00086 }
00087
00088 ret=1;
00089
00090 error01:
00091 if (ferror(pipe)) {
00092 LOG(L_ERR, "ERROR: exec_str: error in pipe: %s\n",
00093 strerror(errno));
00094 ser_error=E_EXEC;
00095 ret=-1;
00096 }
00097 exit_status=pclose(pipe);
00098 if (WIFEXITED(exit_status)) {
00099
00100 if (WEXITSTATUS(exit_status)!=0) ret=-1;
00101 } else {
00102 LOG(L_ERR, "ERROR: exec_msg: cmd %.*s failed. "
00103 "exit_status=%d, errno=%d: %s\n",
00104 cmd->len, ZSW(cmd->s), exit_status, errno, strerror(errno) );
00105 ret=-1;
00106 }
00107 return ret;
00108 }
00109
00110 int exec_str(struct sip_msg *msg, str* cmd, char *param, int param_len) {
00111
00112 struct action act;
00113 int cmd_len;
00114 FILE *pipe;
00115 char *cmd_line;
00116 int ret;
00117 char uri_line[MAX_URI_SIZE+1];
00118 int uri_cnt;
00119 int uri_len;
00120 int exit_status;
00121 struct run_act_ctx ra_ctx;
00122
00123
00124 ret=-1;
00125
00126 cmd_len=cmd->len+param_len+2;
00127 cmd_line=pkg_malloc(cmd_len);
00128 if (cmd_line==0) {
00129 ret=ser_error=E_OUT_OF_MEM;
00130 LOG(L_ERR, "ERROR: exec_str: no mem for command\n");
00131 goto error00;
00132 }
00133
00134
00135 memcpy(cmd_line, cmd->s, cmd->len); cmd_line[cmd->len]=' ';
00136 memcpy(cmd_line+cmd->len+1, param, param_len);cmd_line[cmd->len+param_len+1]=0;
00137
00138 pipe=popen( cmd_line, "r" );
00139 if (pipe==NULL) {
00140 LOG(L_ERR, "ERROR: exec_str: cannot open pipe: %s\n",
00141 cmd_line);
00142 ser_error=E_EXEC;
00143 goto error01;
00144 }
00145
00146
00147 uri_cnt=0;
00148 while( fgets(uri_line, MAX_URI_SIZE, pipe)!=NULL){
00149 uri_len=strlen(uri_line);
00150
00151 while(uri_len && (uri_line[uri_len-1]=='\r'
00152 || uri_line[uri_len-1]=='\n'
00153 || uri_line[uri_len-1]=='\t'
00154 || uri_line[uri_len-1]==' ' )) {
00155 DBG("exec_str: rtrim\n");
00156 uri_len--;
00157 }
00158
00159 if (uri_len==0) continue;
00160
00161 uri_line[uri_len]=0;
00162 if (uri_cnt==0) {
00163 memset(&act, 0, sizeof(act));
00164 act.type = SET_URI_T;
00165 act.val[0].type = STRING_ST;
00166 act.val[0].u.string = uri_line;
00167 init_run_actions_ctx(&ra_ctx);
00168 if (do_action(&ra_ctx, &act, msg)<0) {
00169 LOG(L_ERR,"ERROR:exec_str : SET_URI_T action failed\n");
00170 ser_error=E_OUT_OF_MEM;
00171 goto error02;
00172 }
00173 } else {
00174 if (append_branch(msg, uri_line, uri_len, 0, 0, Q_UNSPECIFIED, 0)==-1) {
00175 LOG(L_ERR, "ERROR: exec_str: append_branch failed;"
00176 " too many or too long URIs?\n");
00177 goto error02;
00178 }
00179 }
00180 uri_cnt++;
00181 }
00182 if (uri_cnt==0) {
00183 LOG(L_ERR, "ERROR:exec_str: no uri from %s\n", cmd_line );
00184 goto error02;
00185 }
00186
00187 ret=1;
00188
00189 error02:
00190 if (ferror(pipe)) {
00191 LOG(L_ERR, "ERROR: exec_str: error in pipe: %s\n",
00192 strerror(errno));
00193 ser_error=E_EXEC;
00194 ret=-1;
00195 }
00196 exit_status=pclose(pipe);
00197 if (WIFEXITED(exit_status)) {
00198
00199 if (WEXITSTATUS(exit_status)!=0) ret=-1;
00200 } else {
00201 LOG(L_ERR, "ERROR: exec_str: cmd %.*s failed. "
00202 "exit_status=%d, errno=%d: %s\n",
00203 cmd->len, ZSW(cmd->s), exit_status, errno, strerror(errno) );
00204 ret=-1;
00205 }
00206 error01:
00207 pkg_free(cmd_line);
00208 error00:
00209 return ret;
00210 }