From a98e5e9fc7ec8cfb0cdc28cfd2baa1d9dc3ad013 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Tue, 18 Mar 2025 10:02:49 -0400 Subject: [PATCH] check mailfrom originator format --- lib/gestcp.c | 141 ------------------------------------------ lib/gestcp.h | 21 ------- lib/lvleml.c | 170 +++++++++++++++++++++++++++++++++++++++++++++++++-- lib/lvleml.h | 21 +++++++ lib/modrec.c | 4 +- 5 files changed, 187 insertions(+), 170 deletions(-) diff --git a/lib/gestcp.c b/lib/gestcp.c index ce68f34..2c34b41 100644 --- a/lib/gestcp.c +++ b/lib/gestcp.c @@ -20,34 +20,6 @@ #include "uniprc.h" #include "gestcp.h" -/* -^L -*/ -/********************************************************/ -/* */ -/* Procedure to free memory used by contact */ -/* */ -/********************************************************/ -static CONTYP *freecontact(CONTYP *contact) - -{ -#define OPEP "gestcp.c:freecontact" - -if (contact!=(CONTYP *)0) { - contact->logptr=log_closelog(contact->logptr); - contact->cursesid=rou_freestr(contact->cursesid); - contact->mainsesid=rou_freestr(contact->mainsesid); - contact->fqdn=rou_freestr(contact->fqdn); - contact->peername=rou_freestr(contact->peername); - contact->peerip=rou_freestr(contact->peerip); - contact->locname=rou_freestr(contact->locname); - contact->locserv=rou_freestr(contact->locserv); - (void) free(contact); - contact=(CONTYP *)0; - } -return contact; -#undef OPEP -} /* ^L */ @@ -128,116 +100,3 @@ if (socptr!=(SOCPTR *)0) sent=soc_writebuffer(socptr,buffer,tosend); return sent; } -/* -^L -*/ -/********************************************************/ -/* */ -/* Procedure to wait for a remote client. */ -/* return all reference to the established contact.*/ -/* */ -/********************************************************/ -PUBLIC CONTYP *tcp_getcontact(SOCPTR *socptr,int pos) - -{ -#define OPEP "gestcp.c:tcp_getcontact" -#define MXCARIN 200 //maximun number of char - //within carpile - -CONTYP *contact; -int phase; -_Bool proceed; - -contact=(CONTYP *)0; -phase=0; -proceed=true; -while (proceed==true) { - switch (phase){ - case 0 : //check for binding - if (socptr==(SOCPTR *)0) { - (void) rou_alert(0,"%s socket pointer is NULL (Bug!?)",OPEP); - phase=999; //not going further - } - break; - case 1 : //waiting from contact - contact=calloc(1,sizeof(CONTYP)); - if ((contact->socptr=soc_accept(socptr,pos))==(SOCPTR *)0) { - (void) rou_alert(0,"%s Unable to open contact",OPEP); - contact=freecontact(contact); - phase=999; //no contact - } - break; - case 2 : //Preparing contact - contact->mainsesid=eml_getmainsesid(); - contact->cursesid=eml_getcursesid(contact->mainsesid,contact->numreset); - contact->locname=soc_getaddrinfo(contact->socptr,true,true); - contact->locserv=soc_getaddrinfo(contact->socptr,true,false); - contact->peername=soc_getaddrinfo(contact->socptr,false,true); - contact->peerip=soc_getaddrinfo(contact->socptr,false,false); - contact->logptr=log_openlog(contact->mainsesid,true); - break; - case 3 : //check contact validity - if ((contact->locname==(char *)0)||(contact->peerip==(char *)0)) { - (void) rou_alert(0,"%s Unable to establish contact entities",OPEP); - contact=freecontact(contact); - phase=999; //no identity - } - break; - case 4 : //contact is good, then sending a signon - (void) log_fprintlog(contact->logptr,false,"SID: %s -> Contact open", - contact->mainsesid); - break; - default : //SAFE guard - proceed=false; - break; - } - phase++; - } -return contact; -#undef MXCARIN -#undef OPEP -} -/* -^L -*/ -/********************************************************/ -/* */ -/* Procedure to wait for a remote client. */ -/* return all reference to contact. */ -/* */ -/********************************************************/ -PUBLIC CONTYP *tcp_dropcontact(CONTYP *contact) - -{ -#define OPEP "gestcp.c:tcp_dropcontact" - -int phase; -_Bool proceed; - -phase=0; -proceed=true; -while (proceed==true) { - switch (phase){ - case 0 : //check for binding - if (contact==(CONTYP *)0) { - (void) rou_alert(0,"%s Contact pointer is NULL (Bug!?)",OPEP); - phase=999; //not going further - } - break; - case 1 : //properly closing remote contact - (void) rou_alert(0,"Contact from peer <%s> on port <%s> Terminated", - contact->peerip,contact->locserv); - contact->socptr=soc_release(contact->socptr); - break; - case 2 : //freeing contact memory - contact=freecontact(contact); - break; - default : //SAFE guard - proceed=false; - break; - } - phase++; - } -return contact; -#undef OPEP -} diff --git a/lib/gestcp.h b/lib/gestcp.h index 30c1ab3..82c7ba3 100644 --- a/lib/gestcp.h +++ b/lib/gestcp.h @@ -13,31 +13,10 @@ #include "devlog.h" #include "devsoc.h" -typedef struct { - SOCPTR *socptr; //established contact context - _Bool tlsok; //link is in crypted mode - char *fqdn; //fully qualified domain from peer - char *locname; //socket local hostname - char *locserv; //local service port - char *peerip; //socket remote peer IP - char *peername; //socket remote peer FQDN - int numreset; //number of SMTP reset received - char *mainsesid;//session main ID - char *cursesid; //current session ID - char *mailfrom; //current mail from originator - LOGPTR *logptr; //reference to session log - }CONTYP; - //read a line from contact up to CRLF extern int tcp_getline(SOCPTR *socptr,TIMESPEC *attend,char **lineptr); //Transmit formated data to the contact channel extern int tcp_write(SOCPTR *socptr,char *buffer,int tosend); -//wait for an incoming contact -extern CONTYP *tcp_getcontact(SOCPTR *socptr,int pos); - -//drop contact established by remote -extern CONTYP *tcp_dropcontact(CONTYP *contact); - #endif diff --git a/lib/lvleml.c b/lib/lvleml.c index 5205cb9..824b66e 100644 --- a/lib/lvleml.c +++ b/lib/lvleml.c @@ -6,6 +6,7 @@ /* */ /********************************************************/ #include +#include #include #include @@ -15,6 +16,34 @@ #include "lvleml.h" +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to free memory used by contact */ +/* */ +/********************************************************/ +static CONTYP *freecontact(CONTYP *contact) + +{ +#define OPEP "gestcp.c:freecontact" + +if (contact!=(CONTYP *)0) { + contact->logptr=log_closelog(contact->logptr); + contact->cursesid=rou_freestr(contact->cursesid); + contact->mainsesid=rou_freestr(contact->mainsesid); + contact->fqdn=rou_freestr(contact->fqdn); + contact->peername=rou_freestr(contact->peername); + contact->peerip=rou_freestr(contact->peerip); + contact->locname=rou_freestr(contact->locname); + contact->locserv=rou_freestr(contact->locserv); + (void) free(contact); + contact=(CONTYP *)0; + } +return contact; +#undef OPEP +} /* ^L */ @@ -203,18 +232,34 @@ proceed=true; phase=0; while (proceed==true) { switch (phase) { - case 0 : //do we have already a from + case 0 : //do we have an originator + if ((mailfrom==(char *)0)||(strlen(mailfrom)<3)) { + (void) transmit(contact,"%d 5.5.0 %s %s originator not specified", + BADPAR,MAILF,mailfrom); + phase=999; //no need to go further + } + break; + case 1 : //do we have already a from if (contact->mailfrom!=(char *)0) { - (void) transmit(contact,"%d 5.5.1 %s %s already specified as originator", - BADPAR,MAILF,contact->mailfrom); + (void) transmit(contact,"%d 5.5.1 %s '%s' %s", + BADPAR,MAILF,contact->mailfrom, + "was previously defined as originator" + ); phase=999; //no need to go further } break; - case 1 : //clean mailfrom + case 2 : //check from format + if ((mailfrom[0]!='<')||(mailfrom[strlen(mailfrom)-1]!='>')) { + (void) transmit(contact,"%d 5.5.2 %s %s Format error", + BADPAR,MAILF,mailfrom); + phase=999; //no need to go further + } + mailfrom[strlen(mailfrom)-1]='\000'; + (void) memmove(mailfrom,mailfrom+1,strlen(mailfrom)); break; - case 2 : //everything ok + case 3 : //everything ok contact->mailfrom=strdup(mailfrom); - (void) transmit(contact,"%d 2.1.0 %s.. sender ok", + (void) transmit(contact,"%d 2.1.3 %s.. sender ok", CMDOK,contact->mailfrom); break; default : //SAFE guard @@ -323,3 +368,116 @@ while (proceed==true) { return status; #undef OPEP } +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to wait for a remote client. */ +/* return all reference to the established contact.*/ +/* */ +/********************************************************/ +PUBLIC CONTYP *eml_getcontact(SOCPTR *socptr,int pos) + +{ +#define OPEP "lvleml.c:eml_getcontact" +#define MXCARIN 200 //maximun number of char + //within carpile + +CONTYP *contact; +int phase; +_Bool proceed; + +contact=(CONTYP *)0; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase){ + case 0 : //check for binding + if (socptr==(SOCPTR *)0) { + (void) rou_alert(0,"%s socket pointer is NULL (Bug!?)",OPEP); + phase=999; //not going further + } + break; + case 1 : //waiting from contact + contact=calloc(1,sizeof(CONTYP)); + if ((contact->socptr=soc_accept(socptr,pos))==(SOCPTR *)0) { + (void) rou_alert(0,"%s Unable to open contact",OPEP); + contact=freecontact(contact); + phase=999; //no contact + } + break; + case 2 : //Preparing contact + contact->mainsesid=eml_getmainsesid(); + contact->cursesid=eml_getcursesid(contact->mainsesid,contact->numreset); + contact->locname=soc_getaddrinfo(contact->socptr,true,true); + contact->locserv=soc_getaddrinfo(contact->socptr,true,false); + contact->peername=soc_getaddrinfo(contact->socptr,false,true); + contact->peerip=soc_getaddrinfo(contact->socptr,false,false); + contact->logptr=log_openlog(contact->mainsesid,true); + break; + case 3 : //check contact validity + if ((contact->locname==(char *)0)||(contact->peerip==(char *)0)) { + (void) rou_alert(0,"%s Unable to establish contact entities",OPEP); + contact=freecontact(contact); + phase=999; //no identity + } + break; + case 4 : //contact is good, then sending a signon + (void) log_fprintlog(contact->logptr,false,"SID: %s -> Contact open", + contact->mainsesid); + break; + default : //SAFE guard + proceed=false; + break; + } + phase++; + } +return contact; +#undef MXCARIN +#undef OPEP +} +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to wait for a remote client. */ +/* return all reference to contact. */ +/* */ +/********************************************************/ +PUBLIC CONTYP *eml_dropcontact(CONTYP *contact) + +{ +#define OPEP "lvleml.c:eml_dropcontact" + +int phase; +_Bool proceed; + +phase=0; +proceed=true; +while (proceed==true) { + switch (phase){ + case 0 : //check for binding + if (contact==(CONTYP *)0) { + (void) rou_alert(0,"%s Contact pointer is NULL (Bug!?)",OPEP); + phase=999; //not going further + } + break; + case 1 : //properly closing remote contact + (void) rou_alert(0,"Contact from peer <%s> on port <%s> Terminated", + contact->peerip,contact->locserv); + contact->socptr=soc_release(contact->socptr); + break; + case 2 : //freeing contact memory + contact=freecontact(contact); + break; + default : //SAFE guard + proceed=false; + break; + } + phase++; + } +return contact; +#undef OPEP +} diff --git a/lib/lvleml.h b/lib/lvleml.h index 12f51e3..5bde91b 100644 --- a/lib/lvleml.h +++ b/lib/lvleml.h @@ -10,7 +10,28 @@ #include "gestcp.h" +typedef struct { + SOCPTR *socptr; //established contact context + _Bool tlsok; //link is in crypted mode + char *fqdn; //fully qualified domain from peer + char *locname; //socket local hostname + char *locserv; //local service port + char *peerip; //socket remote peer IP + char *peername; //socket remote peer FQDN + int numreset; //number of SMTP reset received + char *mainsesid;//session main ID + char *cursesid; //current session ID + char *mailfrom; //current mail from originator + LOGPTR *logptr; //reference to session log + }CONTYP; + + //procedure to extract line and proceed with peer contact extern int eml_docontact(CONTYP *contact); +//wait for an incoming contact +extern CONTYP *eml_getcontact(SOCPTR *socptr,int pos); + +//drop contact established by remote +extern CONTYP *eml_dropcontact(CONTYP *contact); #endif diff --git a/lib/modrec.c b/lib/modrec.c index b23bba8..ba1022d 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -123,7 +123,7 @@ proceed=true; while (proceed==true) { switch (phase) { case 0 : //waiting contact - if ((contact=tcp_getcontact(socptr,pos))==(CONTYP *)0) + if ((contact=eml_getcontact(socptr,pos))==(CONTYP *)0) phase=999; //No contact! break; case 1 : //within forked process @@ -146,7 +146,7 @@ while (proceed==true) { } break; case 3 : //connection terminated - contact=tcp_dropcontact(contact); + contact=eml_dropcontact(contact); (void) rou_checkleak(false); break; default : //SAFE guard -- 2.47.3