Mail-Address Check.c

Aus SiWiki
Wechseln zu: Navigation, Suche
/*-----------------------------------------------------------------------------
 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);
    }
 
}
......