Mail-Address Check.c: Unterschied zwischen den Versionen

Aus Si:Wiki von Siegrist SystemLösungen - Informatik und Rezepte
Zur Navigation springen Zur Suche springen
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
Zeile 4: Zeile 4:
  mail_addr_check.c (c) 2015 by Siegrist(SystemLoesungen) <PSS @ ZweierNet.ch>
  mail_addr_check.c (c) 2015 by Siegrist(SystemLoesungen) <PSS @ ZweierNet.ch>
   
   
# All Rights reserved.
# http://pss.ZweierNet.ch
# This program is free software; you can redistribute it and/or
#
# modify it under the terms of the GNU General Public License as
# This program is absolutely free software.
# published by the Free Software Foundation.
# Use it as you like but don't point the finger of blame to me ;)
#
#
# This program is distributed in the hope that it will be useful,
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.


   
   
Zeile 26: Zeile 22:
  Each command sent to the server is represented in the commands[] array ascending from commands[0] to commands[n].
  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.
  Fit it to your own taste.
  Don't forget the trailing "\r\n", sometimes this is "\n".
  Don't forget the trailing "\r\n", sometimes this might be "\n".
   
   
  Compile with 'gcc -o mail_addr_check mail_addr_check.c'
  Compile with 'gcc -o mail_addr_check mail_addr_check.c'

Aktuelle Version vom 26. Dezember 2015, 03:58 Uhr

<syntaxhighlight lang="c">

/*-----------------------------------------------------------------------------

mail_addr_check.c (c) 2015 by Siegrist(SystemLoesungen) <PSS @ ZweierNet.ch>

  1. http://pss.ZweierNet.ch
  2. This program is absolutely free software.
  3. 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'
------------------------------------------------------------------------------
  • /
  1. include <stdlib.h>
  2. include <stdio.h>
  3. include <sys/types.h>
  4. include <sys/socket.h>
  5. include <netinet/in.h>
  6. include <netdb.h>
  7. include <string.h>
  8. include <ctype.h>
  1. define VERSION "V0.5"
  2. define BUFLEN 1024
  3. define TRUE 1
  4. define FALSE 0
  5. 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);
   }
   

} ......