Browse Source

first commit

Peter Siegrist 3 years ago
commit
7efecd0bb8
4 changed files with 273 additions and 0 deletions
  1. 154 0
      Mail-AddressCheck.c
  2. 45 0
      Mail-AddressCheck.pl
  3. 74 0
      Mail-AddressCheck.py
  4. 0 0
      README.md

+ 154 - 0
Mail-AddressCheck.c

@@ -0,0 +1,154 @@
+/*-----------------------------------------------------------------------------
+ mail_addr_check.c (c) 2015 by Siegrist(SystemLoesungen) <PSS @ ZweierNet.ch>
+ 
+# http://pss.ZweierNet.ch
+#
+# This program is absolutely free software. 
+# Use it as you like but don't point the finger of blame to me ;)
+#
+ 
+ 
+ 
+ params:    [-v]    verbose to stdout
+            mailserverr to connect
+            port on server to connect
+            email-adress to check
+ 
+ returns:   true(1)     if mailaddress exists
+            false(0)    else
+ 
+ Each command sent to the server is represented in the commands[] array ascending from commands[0] to commands[n].
+ Fit it to your own taste.
+ Don't forget the trailing "\r\n", sometimes this might be "\n".
+ 
+ Compile with 'gcc -o mail_addr_check mail_addr_check.c'
+ ------------------------------------------------------------------------------
+*/
+ 
+#include <stdlib.h>
+#include <stdio.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h> 
+#include <string.h>
+#include <ctype.h>
+ 
+#define VERSION     "V0.5"
+#define BUFLEN      1024
+#define TRUE        1
+#define FALSE       0
+#define NUMELEMS(a) (sizeof(a) / sizeof((a)[0]))
+ 
+int verbose = TRUE;
+static char *commands[] = { "HELO xy\r\n",
+                            "RSET\r\n",
+                            "MAIL FROM: mailverification@zweiernet.ch\r\n",
+                            "RCPT TO: <__mail_to_check__>\r\n" };
+ 
+ 
+/*-- Subs -------------------------------------- */
+ 
+void error(char *msg) {
+    perror(msg);
+    exit(0);
+}
+ 
+void usage(char *prog) {
+    fprintf(stderr,"usage %s [-v] < mailserver-to-check-for > < port-on-server-25-or-587 > < email-adress-to-check >\n", prog);
+    exit(0);
+}
+ 
+ 
+char *replace_str(char *str, char *orig, char *rep) {
+    static char bu[BUFLEN];
+    char *p;
+ 
+    if(!(p = strstr(str, orig)))
+        return str;
+ 
+    strncpy(bu, str, p-str);
+    bu[p-str] = '\0';
+    sprintf(bu+(p-str), "%s%s", rep, p+strlen(orig));
+ 
+    return bu;
+}
+ 
+/*-- Main -------------------------------------- */
+ 
+main(int argc, char *argv[]) {
+ 
+    register int    sockfd;
+    register int    n;
+    register int    indx;
+    register int    i, j;
+    int             port;
+    struct          sockaddr_in serv_addr;
+    struct          hostent *server;
+    struct          in_addr *p;
+    unsigned char   *mail_to_check, *ret;
+    unsigned char   buffer[BUFLEN];
+ 
+    setbuf(stdout, NULL);   // flush stdout
+ 
+    /*-- program args */
+    i = 1;
+    if (argc < 4)
+        usage(argv[0]);
+ 
+    if (argc == 5) { 
+        if (strcmp(argv[i], "-v") == 0) { 
+            verbose = TRUE;
+            i++;
+        } else usage(argv[0]);
+    }
+    server = gethostbyname(argv[i]); i++;
+    if ( server == NULL) 
+        error("unknown host.");
+    port = atoi(argv[i]); i++;
+    memcpy((char *)&mail_to_check, &argv[i], sizeof(argv[i]));
+ 
+    /*-- create socket */
+    if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+        error("ERROR opening socket");
+ 
+    /*-- connect to server */
+    bzero((char *) &serv_addr, sizeof(serv_addr));
+    memcpy((char *)&serv_addr.sin_addr, server->h_addr, server->h_length);
+    serv_addr.sin_family    = AF_INET;
+    serv_addr.sin_port      = htons((u_short)port);
+ 
+    if (connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) 
+        error("ERROR connecting");
+ 
+    bzero(buffer, BUFLEN); 
+    if (n = recv(sockfd, buffer, BUFLEN, 0) < 0)
+         error("ERROR reading from socket");
+    if (verbose) printf("%s\n", buffer);
+ 
+    /*-- write n read to/from socket*/
+    indx=0;
+    while ( indx < NUMELEMS(commands) ) {
+        char * ret = replace_str(commands[indx], "__mail_to_check__", mail_to_check);
+        if (verbose) printf("%s", ret);
+        if (n = send(sockfd, ret, strlen(ret), 0) < 0 )
+            error("ERROR writing to socket");
+        bzero(buffer, BUFLEN);
+        if ( n = recv(sockfd, buffer, BUFLEN, 0) < 0 )
+            error("ERROR reading from socket");
+        if (verbose) printf("%s\n",buffer);
+        indx++;
+    }
+ 
+    /*-- return value and exit */
+    for (j = strlen(buffer)-5; buffer[j]; j++) buffer[j] = tolower(buffer[j]);
+    if (strstr(buffer, "ok") != NULL) {
+        if (verbose) printf("OK\n");
+        exit(TRUE);
+    } else {
+        if (verbose) printf("NOK\n");
+        exit(FALSE);
+    }
+ 
+}
+

+ 45 - 0
Mail-AddressCheck.pl

@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+ 
+# Copyright (c) 2015 by Peter_Siegrist(SystemLoesungen)  (PSS @ ZweierNet.ch)
+# pss.ZweierNet.ch
+#
+# This program is absolutely free software. 
+# Use it as you like but don't point the finger of blame to me.
+#
+ 
+use strict;
+use IO::Socket::INET;
+ 
+sub TRUE { 1; }
+sub FALSE { 0; }
+ 
+if ( ($#ARGV + 1) < 3 ) {
+	print "Usage: $0  < mailserver-to-check-for > < port-on-server-25-or-587 > < email-adress-to-check > \n";
+	exit;
+}
+my $server = shift;
+my $port = shift;
+my $address_to_check = shift;
+my $answer = "";
+my $SOCKET = IO::Socket::INET->new(
+        PeerAddr => "$server",
+        PeerPort => "$port",
+        Proto   => 'tcp') || die "Error in create socket!: $!";
+ 
+<$SOCKET>;
+print $SOCKET "HELO xy\n";
+<$SOCKET>;
+print $SOCKET "RSET\n";
+<$SOCKET>;
+print $SOCKET "MAIL FROM: addrecheck\@example.org\n";
+<$SOCKET>;
+$answer = "";
+print $SOCKET "RCPT TO: $address_to_check\n";
+$answer = <$SOCKET>;
+#print "Answer: $answer\n";
+ 
+close $SOCKET;
+ 
+exit FALSE if $answer !~ m/\s+ok/i;
+exit TRUE;
+

+ 74 - 0
Mail-AddressCheck.py

@@ -0,0 +1,74 @@
+#!/usr/bin/python
+ 
+# Copyright (c) 2015 by Peter_Siegrist(SystemLoesungen)  (PSS @ ZweierNet.ch)
+# pss.ZweierNet.ch
+#
+# This is absolutely free software. 
+# Use it as you like but don't point the finger of blame to me.
+#
+ 
+ 
+from socket import *
+import sys
+import string
+ 
+verbose = False
+mail_to_check = "";
+ 
+commands = ['HELO xy',
+            'RSET',
+            'MAIL FROM: mailverification@example.org',
+            'RCPT TO: <__mail_to_check__>']
+ 
+ 
+#-- subs
+def usage ():
+    print('usage: ', sys.argv[0], '[-v] < mailserver-to-check-for > < port-on-server-25-or-587 > < email-adress-to-check >')
+    sys.exit(False)
+ 
+#-- end subs
+ 
+ 
+if len(sys.argv) < 4:
+    usage()
+ 
+i = 1
+if len(sys.argv) == 5:
+    if sys.argv[i] == '-v':
+        verbose = True
+        i+=1
+    else:
+        usage()
+ 
+server = sys.argv[i]
+i+=1
+port = int(sys.argv[i])
+i+=1
+mail_to_check = sys.argv[i]
+ 
+ 
+ 
+sSocket = socket(AF_INET, SOCK_STREAM)
+sSocket.connect((server, port))
+recv = sSocket.recv(1024)
+if verbose:
+    print (recv.decode())
+ 
+for c in commands:
+    c = c.replace('__mail_to_check__', str(mail_to_check))
+    if verbose:
+        print(c)
+    sSocket.send(c.encode() + '\r\n'.encode())
+    recv = sSocket.recv(1024)
+    if verbose:
+        print (recv.decode())
+ 
+recv5 = recv.decode().replace("\n", "").replace("\r", "")
+if recv5[-2:].lower() == 'ok':
+    if verbose:
+        print ('OK')
+    sys.exit(True)
+if verbose:
+    print ('NOK')
+sys.exit(False)
+

+ 0 - 0
README.md