1: /*
   2: ** ident-tester.c           A small daemon that can be used to test Ident
   3: **                          servers
   4: **
   5: ** Author: Peter Eriksson <pen@lysator.liu.se>, 10 Aug 1992
   6: */
   7: 
   8: #ifdef NeXT3
   9: #  include <libc.h>
  10: #endif
  11: 
  12: #include <stdio.h>
  13: #include <netdb.h>
  14: #include <errno.h>
  15: #include <syslog.h>
  16: 
  17: #ifdef HAVE_ANSIHEADERS
  18: #  include <stdlib.h>
  19: #  include <unistd.h>
  20: #endif
  21: 
  22: #include <sys/types.h>
  23: #include <sys/socket.h>
  24: 
  25: #define IN_LIBIDENT_SRC
  26: #include "ident.h"
  27: 
  28: #include <arpa/inet.h>
  29: 
  30: /*
  31: ** Return the name of the connecting host, or the IP number as a string.
  32: */
  33: char *gethost __P1(struct in_addr *, addr)
  34: {
  35:   struct hostent *hp;
  36: 
  37: 
  38:   hp = gethostbyaddr((char *) addr, sizeof(struct in_addr), AF_INET);
  39:   if (hp)
  40:     return (char *) hp->h_name;
  41:   else
  42:     return inet_ntoa(*addr);
  43: }
  44: 
  45: void
  46: main __P2(int, argc,
  47:       char **, argv)
  48: {
  49:   struct sockaddr_in laddr, faddr;
  50:   int len, res, lport, fport;
  51:   ident_t *id;
  52:   char *identifier, *opsys, *charset;
  53: 
  54: 
  55:   puts("Welcome to the IDENT server tester, version 1.9\r\n");
  56:   printf("(Linked with libident-%s)\r\n\n", id_version);
  57: 
  58:   fflush(stdout);
  59: 
  60:   len = sizeof(faddr);
  61:   getpeername(0, (struct sockaddr *) &faddr, &len);
  62: 
  63:   len = sizeof(laddr);
  64:   getsockname(0, (struct sockaddr *) &laddr, &len);
  65: 
  66:   printf("Connecting to Ident server at %s...\r\n", inet_ntoa(faddr.sin_addr));
  67:   fflush(stdout);
  68: 
  69: #ifdef LOG_LOCAL3
  70:   openlog("tidentd", 0, LOG_LOCAL3);
  71: #else
  72:   openlog("tidentd", 0);
  73: #endif
  74: 
  75:   id = id_open(&laddr.sin_addr, &faddr.sin_addr, NULL);
  76:   if (!id)
  77:   {
  78:       if (errno)
  79:       {
  80:       int saved_errno = errno;
  81:       char *hs;
  82: 
  83:       perror("Connection denied");
  84:       fflush(stderr);
  85: 
  86:       hs = gethost(&faddr.sin_addr);
  87:       errno = saved_errno;
  88:       syslog(LOG_DEBUG, "Error: id_open(): host=%s, error=%m", hs);
  89:       }
  90:       else
  91:       puts("Connection denied.");
  92:       exit(0);
  93:   }
  94: 
  95:   printf("Querying for lport %d, fport %d....\r\n",
  96:      (int) ntohs(faddr.sin_port),
  97:      (int) ntohs(laddr.sin_port));
  98:   fflush(stdout);
  99: 
 100:   errno = 0;
 101:   if (id_query(id, ntohs(faddr.sin_port), ntohs(laddr.sin_port), 0) < 0)
 102:   {
 103:       if (errno)
 104:       {
 105:       int saved_errno = errno;
 106:       char *hs;
 107: 
 108:       perror("id_query()");
 109:       fflush(stderr);
 110: 
 111:       hs = gethost(&faddr.sin_addr);
 112:       errno = saved_errno;
 113:       syslog(LOG_DEBUG, "Error: id_query(): host=%s, error=%m", hs);
 114: 
 115:       }
 116:       else
 117:       {
 118:       puts("Query failed.\n");
 119:       }
 120: 
 121:     exit(0);
 122:   }
 123: 
 124:   printf("Reading response data...\r\n");
 125:   fflush(stdout);
 126: 
 127:   res = id_parse(id, NULL,
 128:            &lport, &fport,
 129:            &identifier,
 130:            &opsys,
 131:            &charset);
 132: 
 133:   switch (res)
 134:   {
 135:     default:
 136:       if (errno)
 137:       {
 138:       int saved_errno = errno;
 139:       char *hs;
 140: 
 141:       perror("id_parse()");
 142:       hs = gethost(&faddr.sin_addr);
 143:       errno = saved_errno;
 144:       syslog(LOG_DEBUG, "Error: id_parse(): host=%s, error=%m", hs);
 145:       }
 146:       else
 147:       puts("Error: Invalid response (empty response?).\n");
 148: 
 149:       break;
 150: 
 151:     case -2:
 152:     {
 153:         int saved_errno = errno;
 154:         char *hs = gethost(&faddr.sin_addr);
 155: 
 156:         errno = saved_errno;
 157:         syslog(LOG_DEBUG, "Error: id_parse(): host=%s, Parse Error: %s",
 158:            hs,
 159:            identifier ? identifier : "<no information available>");
 160:         if (identifier)
 161:         printf("Parse error on reply:\n  \"%s\"\n", identifier);
 162:         else
 163:         printf("Unidentifiable parse error on reply.\n");
 164:     }
 165:         break;
 166: 
 167:     case -3:
 168:     {
 169:         int saved_errno = errno;
 170:         char *hs = gethost(&faddr.sin_addr);
 171: 
 172:         errno = saved_errno;
 173: 
 174:         syslog(LOG_DEBUG,
 175:            "Error: id_parse(): host=%s, Illegal reply type: %s",
 176:            hs,
 177:            identifier);
 178: 
 179:         printf("Parse error in reply: Illegal reply type: %s\n",
 180:            identifier);
 181:     }
 182:         break;
 183: 
 184:     case 0:
 185:     {
 186:         int saved_errno = errno;
 187:         char *hs = gethost(&faddr.sin_addr);
 188: 
 189:         errno = saved_errno;
 190: 
 191:         syslog(LOG_DEBUG, "Error: id_parse(): host=%s, NotReady",
 192:            hs);
 193:         puts("Not ready. This should not happen...\r");
 194:     }
 195:       break;
 196: 
 197:     case 2:
 198:     {
 199:         int saved_errno = errno;
 200:         char *hs = gethost(&faddr.sin_addr);
 201: 
 202:         errno = saved_errno;
 203: 
 204:         syslog(LOG_DEBUG, "Reply: Error: host=%s, error=%s",
 205:            hs, identifier);
 206:     }
 207: 
 208:       printf("Error response is:\r\n");
 209:       printf("   Lport........ %d\r\n", lport);
 210:       printf("   Fport........ %d\r\n", fport);
 211:       printf("   Error........ %s\r\n", identifier);
 212:       break;
 213: 
 214:     case 1:
 215:       if (charset)
 216:     syslog(LOG_INFO,
 217:            "Reply: Userid: host=%s, opsys=%s, charset=%s, userid=%s",
 218:            gethost(&faddr.sin_addr), opsys, charset, identifier);
 219:       else
 220:     syslog(LOG_INFO, "Reply: Userid: host=%s, opsys=%s, userid=%s",
 221:            gethost(&faddr.sin_addr), opsys, identifier);
 222: 
 223:       printf("Userid response is:\r\n");
 224:       printf("   Lport........ %d\r\n", lport);
 225:       printf("   Fport........ %d\r\n", fport);
 226:       printf("   Opsys........ %s\r\n", opsys);
 227:       printf("   Charset...... %s\r\n", charset ? charset : "<not specified>");
 228:       printf("   Identifier... %s\r\n", identifier);
 229: 
 230:       if (id_query(id, ntohs(faddr.sin_port), ntohs(laddr.sin_port), 0) >= 0)
 231:       {
 232:     if (id_parse(id, NULL,
 233:              &lport, &fport,
 234:              &identifier,
 235:              &opsys,
 236:              &charset) == 1)
 237:       printf("   Multiquery... Enabled\r\n");
 238:       }
 239:   }
 240: 
 241:   fflush(stdout);
 242:   sleep(1);
 243:   exit(0);
 244: }

Defined functions

__P1 defined in line 33; never used
__P2 defined in line 45; never used

Defined variables

main defined in line 46; never used

Defined macros

IN_LIBIDENT_SRC defined in line 25; never used
Last modified: 1996-11-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: ?E00
Valid CSS Valid XHTML 1.0 Strict