File: ident-tester.c

package info (click to toggle)
socks4-server 4.3.beta2-20
  • links: PTS
  • area: main
  • in suites: buster, stretch
  • size: 1,512 kB
  • ctags: 1,778
  • sloc: ansic: 19,305; makefile: 399; sh: 52
file content (171 lines) | stat: -rw-r--r-- 4,115 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
** ident-tester.c           A small daemon that can be used to test Ident
**                          servers
**
** Author: Peter Eriksson <pen@lysator.liu.se>, 10 Aug 1992
*/

#include <stdio.h>
#include <netdb.h>
#include <syslog.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include "ident.h"

/*
** Return the name of the connecting host, or the IP number as a string.
*/
char *gethost(addr)
  struct in_addr *addr;
{
  struct hostent *hp;

  
  hp = gethostbyaddr(addr, sizeof(struct in_addr), AF_INET);
  if (hp)
    return hp->h_name;
  else
    return inet_ntoa(*addr);
}


main(argc,argv)
  int argc;
  char *argv[];
{
  struct sockaddr_in laddr, faddr;
  int len, res, lport, fport;
  ident_t *id;
  char *identifier, *opsys, *charset;
  

  puts("Welcome to the IDENT server tester, version 1.7\r\n\r");
  fflush(stdout);
  
  len = sizeof(faddr);
  getpeername(0, &faddr, &len);

  len = sizeof(laddr);
  getsockname(0, &laddr, &len);

  printf("Connecting to Ident server at %s...\r\n", inet_ntoa(faddr.sin_addr));
  fflush(stdout);

#ifdef LOG_LOCAL3
  openlog("tidentd", 0, LOG_LOCAL3);
#else
  openlog("tidentd", 0);
#endif
  
  id = id_open(&laddr.sin_addr, &faddr.sin_addr, NULL);
  if (!id)
  {
    perror("id_open()");
    fflush(stderr);
    syslog(LOG_ERR, "Error: id_open(): host=%s, error=%m",
	   gethost(&faddr.sin_addr));
    exit(1);
  }

  printf("Querying for lport %d, fport %d....\r\n",
	 (int) ntohs(faddr.sin_port),
	 (int) ntohs(laddr.sin_port));
  fflush(stdout);
  
  if (id_query(id, ntohs(faddr.sin_port), ntohs(laddr.sin_port), 0) < 0)
  {
    perror("id_query()");
    fflush(stderr);
    syslog(LOG_ERR, "Error: id_query(): host=%s, error=%m",
	   gethost(&faddr.sin_addr));
    exit(1);
  }

  printf("Reading response data...\r\n");
  fflush(stdout);

  res = id_parse(id, NULL,
		   &lport, &fport,
		   &identifier,
		   &opsys,
		   &charset);

  switch (res)
  {
    default:
      perror("id_parse()");
      syslog(LOG_ERR, "Error: id_parse(): host=%s, error=%m",
	     gethost(&faddr.sin_addr));
      break;

    case -2:
      syslog(LOG_ERR, "Error: id_parse(): host=%s, Parse Error: %s",
	     gethost(&faddr.sin_addr),
	     identifier ? identifier : "<no information available>");

      if (identifier)
	printf("Parse error on reply:\n  \"%s\"\n", identifier);
      else
	printf("Unidentifiable parse error on reply.\n");
      break;

    case -3:
      syslog(LOG_ERR, "Error: id_parse(): host=%s, Illegal reply type: %s",
	     gethost(&faddr.sin_addr),
	     identifier);

      printf("Parse error in reply: Illegal reply type: %s\n", identifier);
      break;
	     
    case 0:
      syslog(LOG_ERR, "Error: id_parse(): host=%s, NotReady",
	     gethost(&faddr.sin_addr));
      puts("Not ready. This should not happen...\r");
      break;

    case 2:
      syslog(LOG_INFO, "Reply: Error: host=%s, error=%s",
	     gethost(&faddr.sin_addr), identifier);
      
      printf("Error response is:\r\n");
      printf("   Lport........ %d\r\n", lport);
      printf("   Fport........ %d\r\n", fport);
      printf("   Error........ %s\r\n", identifier);
      break;

    case 1:
      if (charset)
	syslog(LOG_INFO,
	       "Reply: Userid: host=%s, opsys=%s, charset=%s, userid=%s",
	       gethost(&faddr.sin_addr), opsys, charset, identifier);
      else
	syslog(LOG_INFO, "Reply: Userid: host=%s, opsys=%s, userid=%s",
	       gethost(&faddr.sin_addr), opsys, identifier);
      
      printf("Userid response is:\r\n");
      printf("   Lport........ %d\r\n", lport);
      printf("   Fport........ %d\r\n", fport);
      printf("   Opsys........ %s\r\n", opsys);
      printf("   Charset...... %s\r\n", charset ? charset : "<not specified>");
      printf("   Identifier... %s\r\n", identifier);

      if (id_query(id, ntohs(faddr.sin_port), ntohs(laddr.sin_port), 0) >= 0)
      {
	if (id_parse(id, NULL,
		     &lport, &fport,
		     &identifier,
		     &opsys,
		     &charset) == 1)
	  printf("   Multiquery... Enabled\r\n");
      }
  }

  fflush(stdout);
  sleep(1);
  exit(0);
}