home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume06 / mverify < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  7.9 KB

  1. From decwrl!ucbvax!agate!helios.ee.lbl.gov!ncis.llnl.gov!lll-winken!uunet!allbery Fri Mar 24 22:25:46 PST 1989
  2. Article 831 of comp.sources.misc:
  3. Path: decwrl!ucbvax!agate!helios.ee.lbl.gov!ncis.llnl.gov!lll-winken!uunet!allbery
  4. From: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  5. Newsgroups: comp.sources.misc
  6. Subject: v06i064: mverify - Mail alias/user verification
  7. Message-ID: <51316@uunet.UU.NET>
  8. Date: 21 Mar 89 01:37:06 GMT
  9. Sender: allbery@uunet.UU.NET
  10. Reply-To: jeff@quark.wv.tek.com (Jeff Beadles)
  11. Lines: 266
  12. Approved: allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc)
  13.  
  14. Posting-number: Volume 6, Issue 64
  15. Submitted-by: jeff@quark.wv.tek.com (Jeff Beadles)
  16. Archive-name: mverify
  17.  
  18. This program opens a connection to SMTP sendmail on a target host.  It then
  19. attempts to verify that an alias/user exists in the eyes of the sendmail.
  20.  
  21. I use it to verify users on remote hosts without waiting for mail to bounce.
  22.  
  23.     -Jeff Beadles
  24.  
  25. --
  26. Jeff Beadles
  27. jeff@quark.WV.TEK.COM
  28.  
  29.  
  30. #--------------------------------CUT HERE-------------------------------------
  31. #! /bin/sh
  32. #
  33. # This is a shell archive.  Save this into a file, edit it
  34. # and delete all lines above this comment.  Then give this
  35. # file to sh by executing the command "sh file".  The files
  36. # will be extracted into the current directory owned by
  37. # you with default permissions.
  38. #
  39. # The files contained herein are:
  40. #
  41. # -rw-r--r--  1 jeff         1647 Mar  7 20:32 README
  42. # -rw-r--r--  1 jeff         4049 Mar  6 10:47 mverify.c
  43. #
  44. echo 'x - README'
  45. if test -f README; then echo 'shar: not overwriting README'; else
  46. sed 's/^X//' << '________This_Is_The_END________' > README
  47. X
  48. XRequirements:    SMTP sendmail on the receiving (target) host, and an OS that
  49. X        supports sockets.
  50. X
  51. XTo compile:    "make mverify"    (Yes that's right, there's no makefile.)
  52. X       (or) cc -O -o mverify mverify.c
  53. X
  54. XTo execute:    "mverify user@host"
  55. X
  56. X---------------------------------------------------------------------------
  57. Xmverify.c    Copyright 1989, Jeff Beadles jeff@quark.WV.TEK.COM
  58. X
  59. XPermission is granted to freely copy this program without charge.
  60. XIf you distribute a binary to someone, you must also provide the source
  61. Xupon request.
  62. X
  63. XThis started out as a little tool, and a learning experience for sockets &
  64. Xthe like.  If you find it useful, then good for you.  If not, then read rm(1).
  65. XWhat it does is fairly simple. It opens a connection to SMTP sendmail on a
  66. Xtarget host.  It then attempts to verify that the user exists in the eyes
  67. Xof the sendmail.  For example:  Let's say that I want to know who the
  68. Xuser/alias 'postmaster@quark' expanded to.  Mverify would be invoked as:
  69. X
  70. X% mverify postmaster@quark
  71. X
  72. XJeff Beadles  jeff 
  73. X
  74. XWith the /usr/lib/sendmail -bv command/option, it would only tell you that
  75. Xthe mail was 'deliverable' if the target was on another host.
  76. X
  77. XMost all of the error messages should be self explainatory.
  78. X
  79. XAs long as the local host can find the target host DIRECTLY, it
  80. Xshould work.  This program has no concept of mail domains.
  81. XIf it is given a domain-style host, it truncates everything up to
  82. Xincluding the first '.'  IE:  jeff@quark.WV.TEK.COM = jeff@quark
  83. X
  84. XIf you think of something useful to add, please send it to me, and
  85. XI'll see about adding it.
  86. X
  87. XJeff Beadles
  88. Xjeff@quark.WV.TEK.COM
  89. X...!uunet!tektronix.TEK.COM!quark.WV!jeff
  90. ________This_Is_The_END________
  91. if test `wc -l < README` -ne 43; then
  92.     echo 'shar: README was damaged during transit (should have been 43 lines)'
  93. fi
  94. fi        ; : end of overwriting check
  95. echo 'x - mverify.c'
  96. if test -f mverify.c; then echo 'shar: not overwriting mverify.c'; else
  97. sed 's/^X//' << '________This_Is_The_END________' > mverify.c
  98. X/*
  99. X * mverify.c    Copyright 1989, Jeff Beadles
  100. X *                jeff@quark.WV.TEK.COM
  101. X *
  102. X *    Permission is granted to freely copy this program without charge.
  103. X *    There are a couple of lines of autobounce.c by Pete Shipley
  104. X *    shipley@berkley.edu.
  105. X *
  106. X *    To compile, cc -o mverify mverify  : To use, mverify user@host
  107. X *
  108. X *    Note:  This program has no concept of mail domains.
  109. X *    If it is given a domain-style host, it truncates everything up to
  110. X *    including the '.'  IE:  jeff@quark.WV.TEK.COM = jeff@quark
  111. X *
  112. X *    This started out as a little tool.  If you find it useful, then
  113. X *    good for you.  If not, then read rm(1).
  114. X *
  115. X *    If you think of something useful to add, please send it to me, and
  116. X *    I'll see about adding it.
  117. X *
  118. X *    Jeff Beadles
  119. X *    jeff@quark.WV.TEK.COM
  120. X *
  121. X */
  122. X
  123. X/* #define index strchr   /* */
  124. X
  125. X#include <sys/param.h>
  126. X#include <sys/socket.h>
  127. X#include <netdb.h> 
  128. X#include <netinet/in.h>
  129. X#include <stdio.h>
  130. X#include <strings.h>
  131. X
  132. Xmain(argc, argv)
  133. Xint argc;
  134. Xchar **argv;
  135. X{
  136. X    FILE    *fin;
  137. X    FILE    *fout;
  138. X    char    *retval,
  139. X        *fgets(),
  140. X        *index(),
  141. X        buffer[2048],
  142. X        targetuser[1024],
  143. X        targethost[1024];
  144. X
  145. X    int    s;
  146. X    struct    servent  *sp;
  147. X    struct    hostent *hp, *gethostbyname();
  148. X    struct    sockaddr_in server;
  149. X
  150. X    if ( argc != 2) {
  151. X        (void)fprintf(stderr,"Usage: %s user@host\n",argv[0]);
  152. X        exit(1);
  153. X    }
  154. X    strcpy(targetuser,argv[1]);
  155. X    if( (retval=index(targetuser,'@')) == 0) {
  156. X        (void)fprintf(stdout,"Usage: %s user@host\n",argv[0]);
  157. X        exit(1);
  158. X    }
  159. X    *retval='\0';
  160. X    retval++;
  161. X    strcpy(targethost,retval);
  162. X    if ( (retval=index(targethost,'.')) != 0) {
  163. X           fprintf(stderr,"\nWarning: I do not know about mail domains.\n");
  164. X           fprintf(stderr,"Converting %s to ",targethost);
  165. X           *retval='\0';
  166. X           fprintf(stderr,"%s\n\n",targethost);
  167. X    }
  168. X
  169. X/*
  170. X *    One socket please...
  171. X */
  172. X    if ( (s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  173. X        perror("opening stream socket");
  174. X        exit(1);
  175. X    }
  176. X/*
  177. X *    Now get the information for the @host part of the address.
  178. X */
  179. X    server.sin_family = AF_INET;
  180. X    if ( (hp = gethostbyname(targethost)) == 0) {
  181. X        (void) fprintf(stderr, "%s: unknown host\n", targethost);
  182. X        exit(2);
  183. X    }
  184. X
  185. X
  186. X/*
  187. X *    Now get the smtp port using tcp.
  188. X */
  189. X    bcopy(hp->h_addr, (char *)&server.sin_addr, hp->h_length);
  190. X    sp = getservbyname("smtp", "tcp"); 
  191. X    server.sin_port = sp->s_port;
  192. X    if(server.sin_port == htons(0)) {
  193. X        (void)fprintf(stderr,"Unknown service: smtp/tcp\n");
  194. X        exit(1);
  195. X    }
  196. X
  197. X/*
  198. X *    Connecting to the socket might make things go a little easier...  :-)
  199. X */
  200. X    if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0) {
  201. X        perror("connecting stream socket");
  202. X        exit(1);
  203. X    }
  204. X
  205. X/*
  206. X *    Change the to streams 'cause I like'em better.
  207. X */
  208. X    fout = fdopen(s,"w");
  209. X    fin  = fdopen(s,"r");
  210. X
  211. X/*
  212. X *    Wait for the smtp mailer to answer.  It will greet us with:
  213. X * 220 quark.WV.TEK.COM Sendmail 5.17/6.24 ready at Thu, 2 Mar 89 23:52:29 PST
  214. X */
  215. X    while( (retval=fgets(buffer,sizeof(buffer) - 1,fin)) != NULL) {
  216. X        if ( !strncmp(buffer,"220 ",4))
  217. X            break;
  218. X    }
  219. X
  220. X/*
  221. X *    Now that we have the mailers attention, tell it to 'verify' the user.
  222. X */
  223. X    fprintf(fout,"\nVRFY %s\n",targetuser);
  224. X    fflush(fout);
  225. X
  226. X/*
  227. X *    Now just go into a loop reading responces from the mailer.
  228. X */
  229. X    while( (retval=fgets(buffer,sizeof(buffer) -1,fin)) != NULL) {
  230. X/*
  231. X *    Zap ^M < > from the lines.
  232. X */
  233. X        if ( (retval=index(buffer,'\r')) != 0)
  234. X            *retval='\0';
  235. X        if ( (retval=index(buffer,'<')) != 0)
  236. X            *retval=' ';
  237. X        if ( (retval=index(buffer,'>')) != 0)
  238. X            *retval=' ';
  239. X/*
  240. X *    250 = valid data follows.  If true, then this is a valid line.
  241. X *    The +4 skips the '250-'
  242. X */
  243. X        if ( !strncmp(buffer,"250",3)) {
  244. X            puts( buffer + 4);
  245. X        }
  246. X/*
  247. X *     550 is 'Command unrecognized.  Tell the user and exit.
  248. X *    The +4 skips the '550-'
  249. X */
  250. X        if ( !strncmp(buffer,"550",3)) {
  251. X            puts( buffer + 4);
  252. X            break;
  253. X        }
  254. X/*
  255. X * If "250 " then this was the last line of data.  
  256. X * (Note the space.  a '-' means that there's still more to come.)
  257. X */
  258. X        if ( !strncmp(buffer,"250 ", 4))
  259. X            break;
  260. X    }
  261. X/*
  262. X *    Close the SMTP connection.
  263. X */
  264. X    fputs("\n\nQUIT\n", fout);
  265. X    fflush(fout);
  266. X    fclose(fout);
  267. X    fclose(fin);
  268. X/*
  269. X *    And leave this nice program.
  270. X */
  271. X    exit(0);
  272. X}
  273. X
  274. ________This_Is_The_END________
  275. if test `wc -l < mverify.c` -ne 176; then
  276.     echo 'shar: mverify.c was damaged during transit (should have been 176 lines)'
  277. fi
  278. fi        ; : end of overwriting check
  279. exit 0
  280.  
  281.  
  282.