1: /*
   2:  * Copyright (c) 1989 Regents of the University of California.
   3:  * All rights reserved.  The Berkeley software License Agreement
   4:  * specifies the terms and conditions for redistribution.
   5:  */
   6: 
   7: #ifndef lint
   8: static char copyright[] = "Copyright (c) 1990 Regents of the University of California.\nAll rights reserved.\n";
   9: static char SccsId[] = "@(#)@(#)pop_init.c	2.1  2.1 3/18/91";
  10: #endif not lint
  11: 
  12: #include <errno.h>
  13: #include <stdio.h>
  14: #include <sys/types.h>
  15: #include <sys/socket.h>
  16: #include <netinet/in.h>
  17: #include <netdb.h>
  18: #include <arpa/inet.h>
  19: #include "popper.h"
  20: 
  21: extern int      errno;
  22: 
  23: /*
  24:  *  init:   Start a Post Office Protocol session
  25:  */
  26: 
  27: pop_init(p,argcount,argmessage)
  28: POP     *       p;
  29: int             argcount;
  30: char    **      argmessage;
  31: {
  32: 
  33:     struct sockaddr_in      cs;                 /*  Communication parameters */
  34:     struct hostent      *   ch;                 /*  Client host information */
  35:     int                     errflag = 0;
  36:     int                     c;
  37:     int                     len;
  38:     extern char         *   optarg;
  39:     int                     options = 0;
  40:     int                     sp = 0;             /*  Socket pointer */
  41:     char                *   trace_file_name;
  42: 
  43:     /*  Initialize the POP parameter block */
  44:     bzero ((char *)p,(int)sizeof(POP));
  45: 
  46:     /*  Save my name in a global variable */
  47:     p->myname = argmessage[0];
  48: 
  49:     /*  Get the name of our host */
  50:     (void)gethostname(p->myhost,MAXHOSTNAMELEN);
  51: 
  52:     /*  Open the log file */
  53: #ifdef SYSLOG42
  54:     (void)openlog(p->myname,0);
  55: #else
  56:     (void)openlog(p->myname,POP_LOGOPTS,POP_FACILITY);
  57: #endif
  58: 
  59:     /*  Process command line arguments */
  60:     while ((c = getopt(argcount,argmessage,"dt:")) != EOF)
  61:         switch (c) {
  62: 
  63:             /*  Debugging requested */
  64:             case 'd':
  65:                 p->debug++;
  66:                 options |= SO_DEBUG;
  67:                 break;
  68: 
  69:             /*  Debugging trace file specified */
  70:             case 't':
  71:                 p->debug++;
  72:                 if ((p->trace = fopen(optarg,"a+")) == NULL) {
  73:                     pop_log(p,POP_PRIORITY,
  74:                         "Unable to open trace file \"%s\", err = %d",
  75:                             optarg,errno);
  76:                     exit(-1);
  77:                 }
  78:                 trace_file_name = optarg;
  79:                 break;
  80: 
  81:             /*  Unknown option received */
  82:             default:
  83:                 errflag++;
  84:         }
  85: 
  86:     /*  Exit if bad options specified */
  87:     if (errflag) {
  88:         (void)fprintf(stderr,"Usage: %s [-d]\n",argmessage[0]);
  89:         exit(-1);
  90:     }
  91: 
  92:     /*  Get the address and socket of the client to whom I am speaking */
  93:     len = sizeof(cs);
  94:     if (getpeername(sp,(struct sockaddr *)&cs,&len) < 0){
  95:         pop_log(p,POP_PRIORITY,
  96:             "Unable to obtain socket and address of client, err = %d",errno);
  97:         exit(-1);
  98:     }
  99: 
 100:     /*  Save the dotted decimal form of the client's IP address
 101:         in the POP parameter block */
 102:     p->ipaddr = inet_ntoa(cs.sin_addr);
 103: 
 104:     /*  Save the client's port */
 105:     p->ipport = ntohs(cs.sin_port);
 106: 
 107:     /*  Get the canonical name of the host to whom I am speaking */
 108:     ch = gethostbyaddr((char *) &cs.sin_addr, sizeof(cs.sin_addr), AF_INET);
 109:     if (ch == NULL){
 110:         pop_log(p,POP_PRIORITY,
 111:             "Unable to get canonical name of client, err = %d",errno);
 112:         p->client = p->ipaddr;
 113:     }
 114:     /*  Save the cannonical name of the client host in
 115:         the POP parameter block */
 116:     else {
 117: 
 118: #ifndef BIND43
 119:         p->client = ch->h_name;
 120: #else
 121: #       include <arpa/nameser.h>
 122: #       include <resolv.h>
 123: 
 124:         /*  Distrust distant nameservers */
 125:         extern struct state     _res;
 126:         struct hostent      *   ch_again;
 127:         char            *   *   addrp;
 128: 
 129:         /*  We already have a fully-qualified name */
 130:         _res.options &= ~RES_DEFNAMES;
 131: 
 132:         /*  See if the name obtained for the client's IP
 133:             address returns an address */
 134:         if ((ch_again = gethostbyname(ch->h_name)) == NULL) {
 135:             pop_log(p,POP_PRIORITY,
 136:                 "Client at \"%s\" resolves to an unknown host name \"%s\"",
 137:                     p->ipaddr,ch->h_name);
 138:             p->client = p->ipaddr;
 139:         }
 140:         else {
 141:             /*  Save the host name (the previous value was
 142:                 destroyed by gethostbyname) */
 143:             p->client = ch_again->h_name;
 144: 
 145:             /*  Look for the client's IP address in the list returned
 146:                 for its name */
 147:             for (addrp=ch_again->h_addr_list; *addrp; ++addrp)
 148:                 if (bcmp(*addrp,&(cs.sin_addr),sizeof(cs.sin_addr)) == 0) break;
 149: 
 150:             if (!*addrp) {
 151:                 pop_log (p,POP_PRIORITY,
 152:                     "Client address \"%s\" not listed for its host name \"%s\"",
 153:                         p->ipaddr,ch->h_name);
 154:                 p->client = p->ipaddr;
 155:             }
 156:         }
 157: #endif BIND43
 158:     }
 159: 
 160:     /*  Create input file stream for TCP/IP communication */
 161:     if ((p->input = fdopen(sp,"r")) == NULL){
 162:         pop_log(p,POP_PRIORITY,
 163:             "Unable to open communication stream for input, err = %d",errno);
 164:         exit (-1);
 165:     }
 166: 
 167:     /*  Create output file stream for TCP/IP communication */
 168:     if ((p->output = fdopen(sp,"w")) == NULL){
 169:         pop_log(p,POP_PRIORITY,
 170:             "Unable to open communication stream for output, err = %d",errno);
 171:         exit (-1);
 172:     }
 173: 
 174:     pop_log(p,POP_PRIORITY,
 175:         "(v%s) Servicing request from \"%s\" at %s\n",
 176:             VERSION,p->client,p->ipaddr);
 177: 
 178: #ifdef DEBUG
 179:     if (p->trace)
 180:         pop_log(p,POP_PRIORITY,
 181:             "Tracing session and debugging information in file \"%s\"",
 182:                 trace_file_name);
 183:     else if (p->debug)
 184:         pop_log(p,POP_PRIORITY,"Debugging turned on");
 185: #endif DEBUG
 186: 
 187:     return(POP_SUCCESS);
 188: }

Defined functions

pop_init defined in line 27; used 1 times

Defined variables

SccsId defined in line 9; never used
copyright defined in line 8; never used
Last modified: 1991-04-04
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 3813
Valid CSS Valid XHTML 1.0 Strict