From 0fecb0a8fe5446a8d896ebfaac8854ff9880d2b5 Mon Sep 17 00:00:00 2001 From: "Jean-Marc Pigeon (Delson)" Date: Sat, 21 Jun 2025 12:54:58 -0400 Subject: [PATCH] Changeing the mkprepbinding is done --- lib/devsoc.c | 43 +++++++++++++++++++--- lib/devsoc.h | 7 ++-- lib/modrec.c | 64 +++++++++++++++++++++++++------- lib/modrec.h | 2 +- lib/unitls.c | 102 ++++++++++++++------------------------------------- lib/unitls.h | 2 +- 6 files changed, 122 insertions(+), 98 deletions(-) diff --git a/lib/devsoc.c b/lib/devsoc.c index 77ba270..77f0cfe 100644 --- a/lib/devsoc.c +++ b/lib/devsoc.c @@ -566,6 +566,38 @@ return handle; */ /********************************************************/ /* */ +/* Procedure to parse an email protocol */ +/* */ +/********************************************************/ +PUBLIC PROTYP soc_getprotocol(const char *strproto) + +{ +static struct { + PROTYP proto; + const char *voca; + }prolist[]={ + {pro_smtp,""}, + {pro_smtp,"smtp"}, + {pro_smtps,"smtps"}, + {pro_unknwn,(char *)0} + }; +PROTYP proto; + + +proto=pro_unknwn; +for (int i=0;prolist[i].voca!=(char *)0;i++) { + if (strcasecmp(strproto,prolist[i].voca)==0) { + proto=prolist[i].proto; + break; + } + } +return proto; +} +/* + +*/ +/********************************************************/ +/* */ /* Procedure to free memory used by a bind */ /* definition. */ /* */ @@ -595,16 +627,17 @@ return socptr; /* definition. */ /* */ /********************************************************/ -PUBLIC SOCPTR **soc_mkbindinf(SOCPTR **socptr,const LISTYP *bind) +PUBLIC SOCPTR **soc_mkbindinf(SOCPTR **socptr,PROTYP proto, + const char *ip,const char *port,int iteration) { SOCTYP *soc; soc=newsocket(); -soc->proto=bind->proto; -soc->ip=strdup(bind->afn->strnumip); -soc->port=strdup(bind->port); -soc->iteration=bind->iteration; +soc->proto=proto; +soc->ip=strdup(ip); +soc->port=strdup(port); +soc->iteration=iteration; socptr=(SOCPTR **)rou_addlist((void **)socptr,(void *)soc); return socptr; } diff --git a/lib/devsoc.h b/lib/devsoc.h index 7e73072..5158b30 100644 --- a/lib/devsoc.h +++ b/lib/devsoc.h @@ -20,8 +20,8 @@ typedef struct sockaddr SOCKADDR; //reference to a socket definition typedef void SOCPTR; -//reference to a output channel definition -typedef void OUTPTR; +//procedure to parse an email protocol +extern PROTYP soc_getprotocol(const char *strproto); //procedure to free all memory used by a TCP socket //definition (once closed) @@ -29,7 +29,8 @@ extern SOCPTR **soc_freebindinf(SOCPTR **socptr); //procedure to assign memory to be used by a TCP socket //definition -extern SOCPTR **soc_mkbindinf(SOCPTR **socptr,const LISTYP *bind); +extern SOCPTR **soc_mkbindinf(SOCPTR **socptr,PROTYP proto, + const char *ip,const char *port,int iteration); //procedure to open one exchange socket //to connect a remote smtp server diff --git a/lib/modrec.c b/lib/modrec.c index fe57f20..2be48be 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -41,23 +41,61 @@ /* -> smtps protocol,port 465,2 iteration */ /* */ /********************************************************/ -static int prepbinding(SOCPTR ***bindings) +static int prepbinding(SOCPTR ***bindings,int argc,char *argv[]) { -#define OPEP "modrec.c:prepbinding" - -const LISTYP **bind; +#define OPEP "moderec.c:prepbinding" *bindings=(SOCPTR **)0; -if ((bind=tls_get_bind_data())==(const LISTYP **)0) { - (void) rou_alert(0,"%s SMTPPORTS data fully missing (config?, bug?)",OPEP); - (void) exit(-1); - } -while (*bind!=(LISTYP *)0) { - *bindings=soc_mkbindinf(*bindings,*bind); - bind++; +for (int i=0;i",i,argv[i]); + for (int j=0;j<3;j++) { + char *sofar; + + if ((sofar=strchr(ptr,':'))==(char *)0) + break; + *sofar='\000'; + sofar++; + switch (j) { + case 0 : + if ((proto=soc_getprotocol(ptr))==pro_unknwn) { + (void) rou_alert(0,"%s, protocol unknown within config <%s>", + OPEP,argv[i]); + proto=pro_smtp; + } + break; + case 1 : + ipnum=ptr; + break; + case 2 : + if (strlen(ptr)>0) + port=ptr; + if (strlen(sofar)>0) + iteration=atoi(sofar); + break; + default : + (void) rou_alert(0,"%s, Code fault, '%d' unexpected value",OPEP,j); + break; + } + ptr=sofar; + } + *bindings=soc_mkbindinf(*bindings,proto,ipnum,port,iteration); } return rou_nbrlist(*bindings); + #undef OPEP } /* @@ -272,7 +310,7 @@ while (proceed==true) { /* Waiting and handling smtp request */ /* */ /********************************************************/ -PUBLIC void rec_handlesmtp() +PUBLIC void rec_handlesmtp(int argc,char *argv[]) { #define OPEP "modrec.c:rec_handlesmtp" @@ -295,7 +333,7 @@ while (proceed==true) { //(void) rou_alert(0,"JMPDBG %s phase='%d'",OPEP,phase); switch (phase) { case 0 : //preparing iteration - if ((nbrbind=prepbinding(&bindings))==0) { + if ((nbrbind=prepbinding(&bindings,argc,argv))==0) { (void) rou_alert(0,"%s, No listening IP found (config?)",OPEP); phase=999; } diff --git a/lib/modrec.h b/lib/modrec.h index 01be0b7..e13e747 100644 --- a/lib/modrec.h +++ b/lib/modrec.h @@ -10,6 +10,6 @@ #include //procedure to receive email from outside -extern void rec_handlesmtp(); +extern void rec_handlesmtp(int argc,char *argv[]); #endif diff --git a/lib/unitls.c b/lib/unitls.c index 04ec588..f2b75e9 100644 --- a/lib/unitls.c +++ b/lib/unitls.c @@ -23,28 +23,8 @@ //alternate define SSL_CIPHER_LIST "ALL:!LOW" #define SSL_CIPHER_LIST "DEFAULT" -static _Bool modopen; //module open/close status -static const LISTYP **binds; //Binding information -static AFNTYP **afns; //Binding information as AFN -/* -^L -*/ -/********************************************************/ -/* */ -/* Procedure to free LISTYP memory */ -/* */ -/********************************************************/ -static LISTYP *freelisten(LISTYP *listen) - -{ -if (listen!=(LISTYP *)0) { - listen->afn=afn_freeipnum(listen->afn); - listen->port=rou_freestr(listen->port); - (void) free(listen); - listen=(LISTYP *)0; - } -return listen; -} +static _Bool modopen; //module open/close status +static AFNTYP **afns; //Binding information as AFN /* ^L */ @@ -418,10 +398,10 @@ return proto; /* and build the LISTYP structure. */ /* */ /********************************************************/ -static void buildbinds() +static void buildafns() { -#define OPEP "unitls.c,buildbinds," +#define OPEP "unitls.c,buildafns," #define DIP "0.0.0.0" //listening on all PORT #define DPORT "25" //Default SMTP port @@ -440,21 +420,14 @@ while (proceed==true) { data=strdup(data); break; case 1 : //scanning sequence + char *ips; char *seq; + ips=strdup(""); seq=data; while (seq!=(char *)0) { - LISTYP *list; - PROTYP proto; - AFNTYP **locafns; - char *port; - int iteration; char *ptr; - proto=pro_smtp; - locafns=(AFNTYP **)0; - port=DPORT; - iteration=2; if ((ptr=strchr(seq,','))!=(char *)0) { *ptr='\000'; ptr++; @@ -468,42 +441,35 @@ while (proceed==true) { } switch (j) { case 0 : //extracting protocol - if ((proto=tls_getprotocol(seq))==pro_unknwn) { - (void) rou_alert(0,"%s, protocol unknown within config <%s>", - OPEP,seq); - proto=pro_smtp; - } + //forget about protocol break; case 1 : - if (strlen(seq)==0) - seq=DIP; - if ((locafns=afn_getipnum(seq))==(AFNTYP **)0) - (void) rou_alert(0,"%s, Not valid IP <%s> (config?)",OPEP,seq); + int taille; + char *ip; + char *newips; + + ip=seq; + if (strlen(ip)==0) + ip=DIP; + taille=strlen(ips)+strlen(ip)+2; + newips=(char *)calloc(taille,sizeof(char)); + (void) strcpy(newips,ips); + ips=rou_freestr(ips); + ips=newips; + if (strlen(ip)>0) + (void) strcat(ips,","); + (void) strcat(ips,ip); break; case 2 : - if (strlen(seq)>0) - port=seq; - if ((sofar!=(char *)0)&&(strlen(sofar)>0)) - iteration=atoi(sofar); + //forget about port and itreations break; } seq=sofar; } - if (locafns!=(AFNTYP **)0) { - for (int i=0;locafns[i]!=(AFNTYP *)0;i++) { - list=(LISTYP *)calloc(1,sizeof(LISTYP)); - list->proto=proto; - list->port=strdup(port);; - list->iteration=iteration; - list->afn=locafns[i]; - (void) rou_alert(0,"%s JMDPBG adding <%s>",OPEP,list->afn->strnumip); - binds=(const LISTYP **)rou_addlist((void **)binds,(void *)list); - afns=(AFNTYP **)rou_addlist((void **)afns,(void *)locafns[i]); - } - (void) free(locafns); - } seq=ptr; } + afns=afn_getipnum(ips); + ips=rou_freestr(ips); break; default : //SAFE Guard (void) free(data); @@ -1288,19 +1254,6 @@ return ssl; */ /********************************************************/ /* */ -/* Procedure to return the list of binding data */ -/* */ -/********************************************************/ -PUBLIC const LISTYP **tls_get_bind_data() - -{ -return binds; -} -/* -^L -*/ -/********************************************************/ -/* */ /* Procedure to return the AFN list of binding data*/ /* */ /********************************************************/ @@ -1333,11 +1286,10 @@ if (mode!=modopen) { case true : (void) SSL_library_init(); (void) ERR_clear_error(); - (void) buildbinds(); + (void) buildafns(); break; case false : - (void) free(afns); - (void) rou_freelist((void **)binds,(genfree_t)freelisten); + afns=(AFNTYP **)rou_freelist((void **)afns,(genfree_t)afn_freeipnum); break; default : (void) fprintf(stderr,"Calling %s with wrong mode='%d' (Bug?!):", diff --git a/lib/unitls.h b/lib/unitls.h index d4356ca..44d0539 100644 --- a/lib/unitls.h +++ b/lib/unitls.h @@ -78,7 +78,7 @@ extern int tls_check_peer(TLSTYP *tls); extern SSL *tls_setsocket(int handle,SSL_CTX *ctx); //getting binding information -extern const LISTYP **tls_get_bind_data(); +//extern const LISTYP **tls_get_bind_data(); //getting binding information as AFN structure extern AFNTYP **tls_get_bind_afn(); -- 2.47.3