/* This program is used to send a message to a server. It first transmit a crypted secret and then send the message. the server execute something based on that message, but basically does not understand the message */ #include #include #include #include #include "ldapconf.h" #include "ldapconf.m" #include #include static int fire_answer ( const char *id, const char *challenge, SSTRING &answer) { const char *id; SSTRING secret; int ret = -1; glocal.id = id; ("/etc/ldapconf/suppliers.conf",true); int ret = 0; if (line[0] != '#'){ SSTRINGS tb; int nb = str_splitline (line,':',tb); if (nb == 2 && tb.getitem(0)->cmp(glocal.id)==0){ glocal.secret.setfrom(tb.getitem(1)->get()); ret = -1; } } return ret; if (glocal.secret.is_filled()){ // Ok we have the challenge answer.setfrom (challenge); answer.strip_end(); answer.append (glocal.secret); misc_sha (answer.get(),answer); ret = 0; }else{ fprintf (stderr,MSG_U(E_NOSECRET,"No secret for id %s\n"),id); syslog (LOG_ERR,MSG_R(E_NOSECRET),id); } return ret; } int main (int argc, char *argv[]) { glocal const char *fhost = NULL; glocal const char *port = "999"; glocal const char *id = NULL; glocal bool is_pipe = false; glocal int timeout = 10; openlog ("ldapsendcmd",LOG_PID,LOG_DAEMON); int ret = (argc,argv,"ldapconf"); extern const char *module_version; setproginfo ("ldapsendcmd",module_version ,MSG_U(I_USAGE ,"ldapsendcmd options command\n")); setarg ('s',"server",MSG_U(I_HOST,"Server to contact"),glocal.fhost,true); setarg ('p',"port",MSG_U(I_PORT,"TCP port"),glocal.port,false); setarg ('i',"id",MSG_U(I_ID,"Id/secret used to authenticate"),glocal.id,true); setarg ('t',"timeout",MSG_U(I_TIMEOUT,"Time out"),glocal.timeout,false); glocal int ret = -1; glocal int argc = argc; glocal char **argv = argv; glocal int state = 0; (glocal.fhost,glocal.port,glocal.timeout); syslog (LOG_ERR,MSG_U(E_TIMEOUT ,"Timeout while talking to %s. state=%d. Ending connection") ,info.host,glocal.state); end = true; fprintf (stderr,MSG_U(E_NOCONNECT,"Connection failed to the firewall %s\n") ,info.host); syslog (LOG_ERR,MSG_R(E_NOCONNECT),info.host); glocal.state++; // Ok, we have received the challenge SSTRING answer; glocal.state++; if (fire_answer(glocal.id,line,answer)!=-1){ sendf ("%s %s\n",glocal.id,answer.get()); if (glocal.argc > 0){ int len = 2; // Space for newline for (int i=0; i0) *pt++ = ' '; pt = stpcpy (pt,glocal.argv[i]); } *pt++ = '\n'; send (buf,(int)(pt-buf)); }else if (glocal.is_pipe){ char buf[1000]; while (fgets(buf,sizeof(buf)-1,stdin)!=NULL){ send (buf); } } glocal.ret = 0; } end = true; // The server closed the connection syslog (LOG_ERR,MSG_U(E_SERVEREND ,"Server %s closed the connection") ,info.host); return glocal.ret; return ret; }