From: Jean-Marc Pigeon (Delson) Date: Wed, 11 Jun 2025 11:16:14 +0000 (-0400) Subject: Working on binding configuration (SMTPPORTS) X-Git-Tag: tag-0.8~30 X-Git-Url: https://jmp-git.ovh.safe.ca/?a=commitdiff_plain;h=52d6d483c03aa3c4799b654ec34602d74ae8a310;p=jmp%2Fmailleur Working on binding configuration (SMTPPORTS) --- diff --git a/app/Makefile b/app/Makefile index 5cf54de..c1f5913 100644 --- a/app/Makefile +++ b/app/Makefile @@ -72,6 +72,7 @@ chkspf.o: chkspf.c \ emlrec.o: emlrec.c \ ../lib/modrec.h \ + ../lib/unitls.h \ ../lib/unisig.h \ ../lib/uniprc.h \ ../lib/unipar.h \ diff --git a/app/emlrcvr.c b/app/emlrcvr.c index bcfd6d2..b87e9bd 100644 --- a/app/emlrcvr.c +++ b/app/emlrcvr.c @@ -14,6 +14,7 @@ #include "unipar.h" #include "uniprc.h" #include "unisig.h" +#include "unitls.h" #include "modrec.h" #define RECNAME "emlrcvr" @@ -58,6 +59,7 @@ while (proceed==true) { (void) rou_modesubrou(true); (void) prc_modeuniprc(true); (void) sig_modeunisig(true); + (void) tls_modeunitls(true); (void) sig_trapsignal(true,sig_alrm); break; case 2 : //sett lock @@ -81,6 +83,7 @@ while (proceed==true) { (void) prc_cleantitle(); params=par_freeparams(params); (void) sig_trapsignal(false,sig_alrm); + (void) tls_modeunitls(false); (void) sig_modeunisig(false); (void) prc_modeuniprc(false); (void) rou_modesubrou(false); diff --git a/app/sorter.c b/app/sorter.c index ac0e592..7363a83 100644 --- a/app/sorter.c +++ b/app/sorter.c @@ -123,7 +123,6 @@ while (proceed==true) { (void) sig_modeunisig(true); (void) sig_trapsignal(true,sig_alrm); (void) rou_setappname(SORTER); - (void) dns_set_local_smtp(); (void) rou_loadconfig(config,true); (void) openlog(appname,LOG_NDELAY|LOG_PID,LOG_DAEMON); if (foreground==false) { @@ -154,7 +153,6 @@ while (proceed==true) { break; default : //end of task params=par_freeparams(params); - (void) dns_drop_local_smtp(); (void) rou_loadconfig(config,false); (void) sig_trapsignal(false,sig_alrm); (void) sig_modeunisig(false); diff --git a/conf/mailleur.conf.dvl b/conf/mailleur.conf.dvl index 7a9c9ff..8b6ab88 100644 --- a/conf/mailleur.conf.dvl +++ b/conf/mailleur.conf.dvl @@ -1,6 +1,14 @@ #file used to set environment configuration #Used for developpement purpose ONLY #------------------------------------------------ +#list of listening port to do SMTP protocole +#format +#protocol:ipnum:port:iteration[,protocol:ipnum:port:iteration]++ +#example +#:ipnum::: -> smtp:ipnum:25:1 +#stmtps:ipnum:465:2 -> smtps protocol,port 465,2 iteration +#SMTPPORTS = "::,:127.127.10.25:1025:,smtps:127.127.10.25:1026:1" +#------------------------------------------------ #Defining SERVER mode Certificate data CA_ROOT_SRV = "./certs/root-safe_CA.pem" CA_CERT_SRV = "./certs/mailleur_server-chain-cert_x509.pem" diff --git a/lib/Makefile b/lib/Makefile index 1810d86..e4589fd 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -65,7 +65,7 @@ devlog.o: \ devsoc.o: \ subafn.h subrou.h \ - unieml.h uniprc.h unitls.h \ + unieml.h uniprc.h \ devsoc.h devsoc.c devsql.o: \ @@ -118,6 +118,8 @@ gestcp.h: \ gesspf.h: \ subafn.h +devsoc.h: \ + unitls.h uniprc.h: \ subrou.h diff --git a/lib/devsoc.c b/lib/devsoc.c index 2a3ba49..47ee946 100644 --- a/lib/devsoc.c +++ b/lib/devsoc.c @@ -566,38 +566,6 @@ 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. */ /* */ diff --git a/lib/devsoc.h b/lib/devsoc.h index 7e6eb22..d8df075 100644 --- a/lib/devsoc.h +++ b/lib/devsoc.h @@ -12,29 +12,17 @@ #include #include +#include "unitls.h" + //convenient adress structure typedef struct sockaddr SOCKADDR; -//defining email protocol value. -typedef enum { - pro_smtp, //text SMTP protocol, in clear mode - pro_starttls, //Text SMTP protocol, encrypted upon request - pro_smtps, //Text SMTP protocol, text encrypted from start - pro_unknwn //Protcole undefined - }PROTYP; - -//default and debugging certificate for server mode -extern const char *srvr_certs[3]; - //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) extern SOCPTR **soc_freebindinf(SOCPTR **socptr); diff --git a/lib/modrec.c b/lib/modrec.c index d0236f7..7c9d6ee 100644 --- a/lib/modrec.c +++ b/lib/modrec.c @@ -71,7 +71,7 @@ for (int i=0;i", OPEP,argv[i]); proto=pro_smtp; diff --git a/lib/unidns.c b/lib/unidns.c index ef9d65c..ad398ab 100644 --- a/lib/unidns.c +++ b/lib/unidns.c @@ -468,89 +468,3 @@ else { return match; #undef OPEP } -/* - -*/ -/********************************************************/ -/* */ -/* Procedure to collect IP binded to listen SMTP */ -/* port. */ -/* */ -/********************************************************/ -PUBLIC void dns_set_local_smtp() - -{ -#define OPEP "unidns.c:dns_set_local_smtp," -#define MXIP 20 -#define TCP "/proc/net/tcp" - -int numip; -FILE *fichier; -int phase; -_Bool proceed; - -numip=0; -fichier=(FILE *)0; -phase=0; -proceed=true; -while (proceed==true) { - switch (phase) { - case 0 : //Opening the netstat directory - if ((fichier=fopen(TCP,"r"))==(FILE *)0) { - (void) rou_alert(0,"%s Unable to open <%s> (error=<%s>)", - OPEP,strerror(errno)); - (void) rou_alert(0,"%s (not an linux system? exiting!)",OPEP); - (void) exit(-1);//BIG BIG trouble - } - break; - case 1 : //scanning netstat file - int numline; - char line[200]; - - numline=0; - local_ipv4=calloc(MXIP,sizeof(in_addr_t)); - while (fgets(line,sizeof(line),fichier)!=(char *)0) { - int seq; - int addr; - int port; - - numline++; - if (numline==1) - continue; //discarding first line - if (sscanf(line,"%d: %8x:%4x",&seq,&addr,&port)==3) { - (void) rou_alert(0,"JMPDBG seq=%d addr=%x port=%d\n",seq,addr,port); - numip++; - } - } - break; - case 2 : //closing file - (void) fclose(fichier); - break; - default : //SAFE Guard - proceed=false; - break; - } - phase++; - } - -#undef TCP -#undef MXIP -#undef OPEP -} -/* - -*/ -/********************************************************/ -/* */ -/* Procedure to free memory used by SMTP local IP */ -/* */ -/********************************************************/ -PUBLIC void dns_drop_local_smtp() - -{ -if (local_ipv4!=(in_addr_t *)0) { - (void) free(local_ipv4); - num_ipv4=0; - local_ipv4=(in_addr_t *)0; - } -} diff --git a/lib/unidns.h b/lib/unidns.h index 0ddf7a8..74b3306 100644 --- a/lib/unidns.h +++ b/lib/unidns.h @@ -31,14 +31,6 @@ extern MXTYP **dns_getmx(const char *domain); //of domain A record list extern _Bool dns_matchiprec(char *hostname,AFNTYP *afnnum,int mask); -//procedure to create the list of local IP used to listen -//the smtp protocol -extern void dns_set_local_smtp(); - -//procedure to free the list of local IP used to listen -//the smtp protocol -extern void dns_drop_local_smtp(); - //procedure to detect if a domain is served by a local //smtp server extern _Bool dns_is_domain_local(const char *domain); diff --git a/lib/unitls.c b/lib/unitls.c index b12514c..4077e57 100644 --- a/lib/unitls.c +++ b/lib/unitls.c @@ -23,7 +23,25 @@ #define SSL_CIPHER_LIST "DEFAULT" static _Bool modopen; //module open/close status +static LISTYP **binds; //Binding information +/* +^L +*/ +/********************************************************/ +/* */ +/* Procedure to free LISTYP memory */ +/* */ +/********************************************************/ +static LISTYP *freelisten(LISTYP *listen) +{ +if (listen!=(LISTYP *)0) { + listen->ipnum=rou_freestr(listen->ipnum); + (void) free(listen); + listen=(LISTYP *)0; + } +return listen; +} /* ^L */ @@ -357,6 +375,132 @@ while (proceed==true) { #undef OPEP } /* + +*/ +/********************************************************/ +/* */ +/* Procedure to parse an email protocol */ +/* */ +/********************************************************/ +PUBLIC PROTYP tls_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 parse an the SMTPPORTS env variable*/ +/* and build the LISTYP structure. */ +/* */ +/********************************************************/ +static void buildbinds() + +{ +#define OPEP "unitls.c,buildbinds," +#define DIP "0.0.0.0" //listening on all PORT +#define DPORT "25" //Default SMTP port + +char *data; +int phase; +_Bool proceed; + +data=(char *)0; +phase=0; +proceed=true; +while (proceed==true) { + switch (phase) { + case 0 : //Getting the SMTPPORTS variable + if ((data=getenv("SMTPPORTS"))==(char *)0) + data="smtp:0.0.0.0:25:2"; //default configuration + data=strdup(data); + break; + case 1 : //scanning sequence + char *seq; + + seq=data; + while (seq!=(char *)0) { + PROTYP proto; + char *ipnum; + char *port; + int iter; + char *ptr; + + ipnum=DIP; + port=DPORT; + iter=10; + if ((ptr=strchr(seq,','))!=(char *)0) { + *ptr='\000'; + ptr++; + } + (void) rou_alert(0,"%s JMPDBG seq=<%s>",OPEP,seq); + for (int j=0;j<3;j++) { + char *sofar; + + if ((sofar=strchr(seq,':'))==(char *)0) + break; + *sofar='\000'; + sofar++; + 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; + } + break; + case 1 : + if (strlen(seq)>0) + ipnum=seq; + break; + case 2 : + if (strlen(seq)>0) + port=seq; + if (strlen(sofar)>0) + iter=atoi(sofar); + break; + } + seq=sofar; + } + (void) rou_alert(0,"%s JMP ipnum=<%s> port=<%s> iter='%d'", + OPEP,ipnum,port,iter); + seq=ptr; + } + break; + default : //SAFE Guard + (void) free(data); + proceed=false; + break; + } + phase++; + } + +#undef DPORT +#undef DIP +#undef OPEP +} +/* ^L */ /********************************************************/ @@ -1140,8 +1284,10 @@ if (mode!=modopen) { case true : (void) SSL_library_init(); (void) ERR_clear_error(); + (void) buildbinds(); break; case false : + (void) rou_freelist((void **)binds,(genfree_t)freelisten); break; default : (void) fprintf(stderr,"Calling %s with wrong mode='%d' (Bug?!):", diff --git a/lib/unitls.h b/lib/unitls.h index 5a05281..48c57c7 100644 --- a/lib/unitls.h +++ b/lib/unitls.h @@ -11,6 +11,20 @@ #include #include +//defining email protocol value. +typedef enum { + pro_smtp, //text SMTP protocol, in clear mode + pro_starttls, //Text SMTP protocol, encrypted upon request + pro_smtps, //Text SMTP protocol, text encrypted from start + pro_unknwn //Protcole undefined + }PROTYP; + +typedef struct { //structure about listening port + PROTYP proto; //Protocol to be used + char *ipnum; //binding IP number + int iter; //Number of binding iteration + }LISTYP; + typedef struct { _Bool server; //SSL server/client mode _Bool checkpeer;//Check peer certificate @@ -60,6 +74,9 @@ extern int tls_check_peer(TLSTYP *tls); //set socket as an tls socket extern SSL *tls_setsocket(int handle,SSL_CTX *ctx); +//getting binding information +extern LISTYP **tls_get_bind_data(); + //homework to be done before starting/stopping module. extern int tls_modeunitls(_Bool mode);