]> SAFE projects GIT repository - jmp/mailleur/commitdiff
SSL chanell no working anymore
authorJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 9 Aug 2024 15:44:59 +0000 (11:44 -0400)
committerJean-Marc Pigeon (Delson) <jmp@safe.ca>
Fri, 9 Aug 2024 15:44:59 +0000 (11:44 -0400)
lib/devsoc.c
lib/devsoc.h
lib/gestcp.c
lib/gestcp.h
lib/lvleml.c
lib/subrou.c
lib/unieml.c
lib/unieml.h
lib/unitls.c

index 548087e9a6eb687437e60da099d3dbcb77b007ee..523b163acc982be4a84cbbffdc054bc95b5da431 100644 (file)
@@ -480,6 +480,7 @@ int iterations;
 SOCTYP *soc;
 
 soc=(SOCTYP *)socptr;
+iterations=0;
 if (soc!=(SOCTYP *)0)
   iterations=soc->iteration;
 return iterations;
@@ -953,7 +954,7 @@ while (proceed==true) {
         phase=999;      //never reached
         }
       break;
-    case 1      :       //shuting dow the TCP link
+    case 1      :       //shutting down the TCP link
       switch (soc->proto) {
         case pro_smtp           :       //plain socket
         case pro_starttls       :       //plain socket + STARTTLS
@@ -1027,3 +1028,28 @@ if (mode!=modopen) {
 return status;
 #undef  OPEP
 }
+/*
+^L
+*/
+/********************************************************/
+/*                                                     */
+/*      Procedure to switch a plain socket channel to   */
+/*      crypted channel, return true is successful.     */
+/*                                                     */
+/********************************************************/
+_Bool soc_starttls(SOCPTR *socptr,_Bool server)
+
+{
+_Bool ok;
+SOCTYP *soc;
+
+ok=false;
+soc=(SOCTYP *)socptr;
+if ((soc!=(SOCTYP *)0)&&(soc->modtls==false)) {
+  if ((soc->tls=tls_opentls(soc->handle,server))!=(TLSTYP *)0) {
+    soc->modtls=true;
+    ok=true;
+    }
+  }
+return ok;
+}
index 62cd75d49b54d294b98f754dde1ed1e777af2bc1..3915caf84f9c39dceb5c292d8d02642964e4e5b7 100644 (file)
@@ -65,6 +65,9 @@ extern char *soc_getaddrinfo(SOCPTR *socptr,_Bool local,_Bool ip);
 //procedure to release/clsoe socket
 extern SOCPTR *soc_release(SOCPTR *socptr);
 
+//procedure to initiate crypted mode on plain channel
+extern _Bool soc_starttls(SOCPTR *socptr,_Bool server);
+
 //homework to be done before starting/stopping module.
 extern int soc_modedevsoc(_Bool mode);
 
index b6a4d160b252392ec8f5d217eef9ab16b83dcf3a..65d692b78d0a43fcd4a45ea284c5a5cdd486f59c 100644 (file)
@@ -35,6 +35,7 @@ static CONTYP *freecontact(CONTYP *contact)
 #define OPEP    "gesttcp.c:freecontact"
 
 if (contact!=(CONTYP *)0) {
+  contact->fqdn=rou_freestr(contact->fqdn);
   contact->peerip=rou_freestr(contact->peerip);
   contact->locname=rou_freestr(contact->locname);
   contact->locserv=rou_freestr(contact->locserv);
@@ -89,8 +90,8 @@ while (proceed==true) {
           break;                //no need to read line
         default         :       //char available
           phase=0;              //check for new line
-          if (soc_receive(contact->socptr)==0) {
-            got=0;
+          if (soc_receive(contact->socptr)<0) {
+            got=-1;
             phase=999;          //remote disconnected?
             }
           break;
@@ -129,6 +130,39 @@ return sent;
 */
 /********************************************************/
 /*                                                      */
+/*     Procedure to send a signon on the current       */
+/*      channel (either plain of crypted).              */
+/*      Return the number of char transmitted.          */
+/*                                                      */
+/********************************************************/
+PUBLIC int tcp_signon(CONTYP *contact)
+
+{
+#define OPEP    "gestcp.c:tcp_signon"
+#define FMT     "%d %s ESMTP %s-%s; %s%s"
+
+int sent;
+
+sent=0;
+if (contact!=(CONTYP *)0) {
+  char signon[100];
+
+  (void) snprintf(signon,sizeof(signon),FMT,
+                                        SIGNON,contact->locname,
+                                        appname,rou_getversion(),
+                                        rou_ascsysstamp(time((time_t *)0)),CRLF);
+  if ((sent=tcp_write(contact,signon,strlen(signon)))<0) 
+    (void) rou_alert(0,"%s Unable to send signon to remote",OPEP);
+  }
+#undef  FMT
+#undef  OPEP
+return sent;
+}
+/*
+^L
+*/
+/********************************************************/
+/*                                                      */
 /*     Procedure to wait for a remote client.          */
 /*      return all reference to the established contact.*/
 /*                                                      */
@@ -174,24 +208,9 @@ while (proceed==true) {
         }
       break;
     case 3      :       //contact is good sending signon
-      if (contact!=(CONTYP *)0) {       //always
-        #define FMT     "%d %s ESMTP %s-%s; %s%s"
-
-        char signon[100];
-
-        (void) prc_settitle("Contact from peer '%s' started at %s",
-                             contact->peerip, 
-                             rou_ascsysstamp(time((time_t *)0)));
-        (void) snprintf(signon,sizeof(signon),FMT,
-                                      SIGNON,contact->locname,
-                                      appname,rou_getversion(),
-                                      rou_ascsysstamp(time((time_t *)0)),CRLF);
-        if (tcp_write(contact,signon,strlen(signon))<0) {
-          (void) rou_alert(0,"%s Unable to send signon to remote",OPEP);
-          contact=freecontact(contact);
-          phase=999;      //no contact 
-          }
-        #undef  FMT
+      if (tcp_signon(contact)<=0) {
+        contact=freecontact(contact);
+        phase=999;      //no contact 
         }
       break;
     default     :       //SAFE guard
index 971f476cbb895a205253510d51dd85c91a6049fa..8655c5957a44a2a13250d35a1b7d37aa14a8ffde 100644 (file)
@@ -14,6 +14,8 @@
 
 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
@@ -25,6 +27,9 @@ extern int tcp_getline(CONTYP *contact,TIMESPEC *attend,char **lineptr);
 //Transmit formated data to the contact channel
 extern int tcp_write(CONTYP *contact,char *buffer,int tosend);
 
+//send signon on channel
+extern int tcp_signon(CONTYP *contact);
+
 //wait for an incoming contact
 extern CONTYP *tcp_getcontact(SOCPTR *socptr,int pos);
 
index 3f441596aa703727df748c462d8ee9245b57b7c0..d6f6f9726b1e0cb8ed4f8573611079d4d6388eaa 100644 (file)
@@ -37,6 +37,72 @@ va_start(args,fmt);
 va_end(args);
 }
 /*
+\f
+*/
+/************************************************/
+/*                                             */
+/*     Procedure to send an "HELO" message     */
+/*     if EHLO message is not accepted.        */
+/*                                             */
+/************************************************/
+static _Bool doehlo(CONTYP *contact,char *line)
+
+{
+static char *ehlostr[]= {
+        "-8BITMIME",
+        "-STARTTLS",
+        "-ENHANCEDSTATUSCODES",
+        "-AUTH PLAIN LOGIN",
+        " HELP",
+        (char *)0
+        };
+
+#define        OPEP    "unieml.c:doehlo"
+#define DETAIL  "syntax error (domain part missing), disconnecting"
+
+_Bool done;
+char *fqdn;
+int phase;
+_Bool proceed;
+
+done=false;
+phase=0;
+proceed=true;
+while (proceed==true) {
+  switch (phase) {
+    case 0      :       //Do we have 
+      fqdn=strchr(line,' ');
+      if (fqdn!=(char *)0) {
+        while ((*fqdn==' ')||(*fqdn=='\t'))
+          fqdn++;
+        }
+      if ((fqdn==(char *)0)||(strlen(fqdn)==0)) {
+        (void) transmit(contact,"%d 5.5.4 %s",BADPAR,DETAIL);
+        phase=999;      //Trouble trouble
+        }
+      break;
+    case 1      :       //thereis an FQDN
+      contact->fqdn=rou_freestr(contact->fqdn);
+      contact->fqdn=strdup(fqdn);
+      (void) transmit(contact,"%d-%s ready, your IP=[%s]",
+                              CMDOK,contact->locname,contact->peerip);
+      (void) transmit(contact,"%d-SIZE %ld",CMDOK,MXMSIZE);
+      for (int i=0;ehlostr[i]!=(char *)0;i++) {
+        (void) transmit(contact,"%d%s",CMDOK,ehlostr[i]);
+        }
+      done=true;
+      break;
+    default     :       //SAFE guard
+      proceed=false;
+      break;
+    }
+  phase++;
+  }
+return done;
+#undef  DETAIL
+#undef  OPEP
+}
+/*
 ^L
 */
 /********************************************************/
@@ -66,13 +132,32 @@ while (proceed==true) {
   status=tcp_getline(contact,&attend,&line);
   if (status<=0)        //timeout or trouble?
     break;              //no need to go further
+  (void) printf("JMPDBG getline got <%s>\n",line);
   switch (eml_getcode(line)) {
-    case c_quit         :       //quit SMTP protocol
-      (void) transmit(contact,"%d closing connection",QUITOK);
+    case c_ehlo         :       //EHLO SMTP protocol
+      proceed=doehlo(contact,line);
+      break;
+    case c_quit         :       //QUIT SMTP protocol
+      (void) transmit(contact,"%d 2.0.0 Bye, closing connection",QUITOK);
       proceed=false;
       break;
+    case c_starttls     :       //EHLO start encryptel link
+      switch (soc_starttls(contact->socptr,true)) {
+        case true       :       //link now in TLS crypted mode
+          (void) tcp_signon(contact);
+          break;
+        case false      :       //unable to establish link
+          (void) transmit(contact,"%d 5.3.3 command starttls not successful",
+                                   CMDBAD);
+          status=-1;
+          proceed=false;
+          break;
+        }
+      break;
     case c_unknown      :       //uknown keyword
-      (void) transmit(contact,"%d command <%s> is unknown",CMDBAD,line);
+      (void) rou_alert(0,"Command <%s> from [%s] is unknown (config?)",
+                         line,contact->peerip);
+      (void) transmit(contact,"%d 5.5.1 command <%s> is unknown",CMDBAD,line);
       break;
     default             : 
       (void) rou_alert(0,"Unable to find keyword for <%s> (Bug?)",OPEP,line);
index 9a7aacf46b5cc78f6f70041cd913e62b64776221..e1e3496fb28a042d7b68661337c1b74f592e51aa 100644 (file)
@@ -20,7 +20,7 @@
 
 //version definition 
 #define VERSION "0.3"
-#define RELEASE "36"
+#define RELEASE "37"
 
 //Public variables
 PUBLIC  int debug=0;            //debug level
@@ -50,7 +50,10 @@ static char *get_dropzone()
 char *dropzone;
 char command[100];
 
-dropzone=rou_apppath("/tmp/"APPNAME"-crash");
+(void) strcpy(command,"/tmp/");
+(void) strcat(command,APPNAME);
+(void) strcat(command,"-crash");
+dropzone=rou_apppath(command);
 (void) snprintf(command,sizeof(command)-2,"mkdir -p %s",dropzone);
 (void) system(command);
 return dropzone;
@@ -237,7 +240,7 @@ PUBLIC char *rou_apppath(const char *path)
 char *root;
 char *newpath;
 int taille;
-char loc[3];
+char loc[300];
 
 root="";
 newpath=(char *)0;
index b0df33266729979c873929cdfb55d2a654cbe127..4f6c17ca5c2877e727fa5bfef2a0e919523b61e0 100644 (file)
@@ -21,7 +21,9 @@ typedef struct  {
 
 //this list order by key length
 static VOCTYP vocsmtp[]={
+                {c_ehlo,"EHLO"},
                 {c_quit,"QUIT"},
+                {c_starttls,"STARTTLS"},
                 {c_unknown,(const char *)0}
                 };
 /*
@@ -40,7 +42,7 @@ VOCTYP *ptr;
 
 code=c_unknown;
 for (ptr=vocsmtp;ptr->code!=c_unknown;ptr++) {
-  if (strcasecmp(ptr->key,keyword)==0) {
+  if (strncasecmp(ptr->key,keyword,strlen(ptr->key))==0) {
     code=ptr->code;
     break;
     }
index e63b16b06c35ef09c8bf155c9fa4846e0d83b5ac..004d605a9dde5c75b5cd5bf7d3ebff61deb28ddd 100644 (file)
@@ -8,14 +8,19 @@
 #ifndef        UNIEML
 #define UNIEML
 
+#define MXMSIZE 52428800        //52 Megabytes
 #define CRLF    "\r\n"          //EOL within SMTP protocol
 #define        SIGNON  220             //signon information
 #define        QUITOK  221             //status on quit
+#define        CMDOK   250             //Everything OK
+#define BADPAR  501             //error in parameters
 #define CMDBAD  502             //command not implemented
 
 //list of keyword
 typedef enum    {               //list of SMTP protocol keyword
+                c_ehlo,         //EHLO command
                 c_quit,         //quit exchange
+                c_starttls,     //Starting a TLS crypted link
                 c_unknown       //key word unknown
                 }CODTYP;
 
index f2c3261020303c236c92e5b4f72ff387806be0b8..5e4eef44c34c981798576a44aa07464b4b8a76c5 100644 (file)
@@ -378,6 +378,7 @@ ready=false;
 phase=0;
 proceed=true;
 while (proceed==true) {
+  (void) printf("JMPDBG opentls phase='%d'\n",phase);
   switch (phase) {
     case 0      :       //prepare the structure first;
       tls=(TLSTYP *)calloc(1,sizeof(TLSTYP));
@@ -491,6 +492,7 @@ if (tls!=(TLSTYP *)0) {
   int sofar;
 
   proceed=true;
+  (void) printf("JMPDBG sending <%s>\n",buffer);
   while (proceed==true) {
     proceed=false;
     sofar=SSL_write(tls->ssl,buffer,tosend);
@@ -553,8 +555,10 @@ int got;
 
 got=-1;
 if (tls!=(TLSTYP *)0) {
-  if ((got=SSL_read(tls->ssl,buffer,maxread))<=0)
+  if ((got=SSL_read(tls->ssl,buffer,maxread))<=0) {
     (void) showtlserror(tls,got,"Trouble to read data");
+    got=-1;
+    }
   }
 return got;
 }