pidentd  3.0.19
About: implementation of the RFC1413 identification server (more or less complete rewrite compared to version 2)
  Fossies Dox: pidentd-3.0.19.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

itest.c
Go to the documentation of this file.
1 /*
2  * Simple but effective hack to test the kernel.o file.
3  * It takes on stdin the output from "netstat -f inet -n | grep ESTAB"
4  * in either Solaris 2.x (non-standard formats can easily be converted
5  * to this)
6  * laddr.lport faddr.fport .....
7  * or BSD 4.x (the defacto standard netstat output):
8  * tcp <num> <num> laddr.lport faddr.fport
9  * format.
10  *
11  * The output must be numeric, as non-numeric output is truncated when
12  * hostnames get too long and ambiguous. And we don't want netstat to
13  * first convert numbers to names and then this program to convert
14  * names back to numbers.
15  *
16  * Casper Dik (casper@fwi.uva.nl)
17  */
18 
19 #include "config.h"
20 
21 #include <stdio.h>
22 #include <ctype.h>
23 #ifdef sequent
24 #include <strings.h>
25 #define strrchr rindex
26 #else
27 #include <string.h>
28 #include <stdlib.h>
29 #endif
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
35 #include <pwd.h>
36 
37 #include "pidentd.h"
38 
39 
40 int debug = 0;
41 
42 
43 #ifdef sequent
44 char *strtok(str, dels)
45  char *str;
46  char *dels;
47 {
48  static char *bufp;
49 
50 
51  if (str)
52  bufp = str;
53 
54  if (!bufp || !*bufp)
55  return (char *) 0;;
56 
57  while (*bufp && index(dels, *bufp) != (char *) 0)
58  ++bufp;
59 
60  str = bufp;
61 
62  while (*bufp && index(dels, *bufp) == (char *) 0)
63  ++bufp;
64 
65  if (*bufp)
66  *bufp++ = '\0';
67 
68  return str;
69 }
70 #endif
71 
72 int
73 parse_addr(struct sockaddr_gen *addr, char *loc)
74 {
75  char *tmp = strrchr(loc,'.');
76  int af;
77 
78  if (tmp == NULL)
79  return -1;
80 
81  *tmp++ ='\0';
82 
83 #ifdef HAVE_IPV6
84  SGFAM(*addr) = strchr(loc, ':') != 0 ? AF_INET6 : AF_INET;
85  if (inet_pton(SGFAM(*addr), loc, SGADDRP(*addr)) != 1)
86  return -1;
87 #else
88  addr->sin_family = AF_INET;
89  ((struct in_addr *) SGADDRP(*addr))->s_addr = inet_addr(loc);
90 #endif
91  SGPORT(*addr) = htons(atoi(tmp));
92  return 0;
93 }
94 
95 int
96 main(int argc, char **argv)
97 {
98  int c;
99  char buf[500];
100  int try;
101  void *kp;
102 
103 
104  while ((c = getopt(argc, argv, "d")) != -1) {
105  switch (c)
106  {
107  case 'd':
108  debug++;
109  break;
110  default:
111  fprintf(stderr, "Usage: %s [-d]\n", argv[0]);
112  return 1;
113  }
114  }
115 
116  if (ka_init()) {
117  fprintf(stderr,
118  "%s: OS version mismatch - compiled for: %s\n",
119  argv[0], osinfo_build);
120  return 1;
121  }
122  if (ka_open(&kp) < 0) {
123  fprintf(stderr, "%s: Cannot open kernel memory\n", argv[0]);
124  return 1;
125  }
126 
127  while (fgets(buf,sizeof(buf),stdin)) {
128  char *loc, *rem, *tmp;
129  unsigned short lport, fport;
130  struct in_addr faddr, laddr;
131  struct kernel k;
132  struct passwd *pwd;
133  char buf2[sizeof(buf)];
134 
135  strcpy(buf2,buf);
136 
137  loc = strtok(buf, " \t");
138  if (strcmp(loc,"tcp") == 0) {
139  int i;
140  for (i = 0; i < 3; i++)
141  loc = strtok(NULL, " \t");
142  }
143  rem = strtok(NULL, " \t");
144  if (loc == NULL || rem == NULL) {
145  fprintf(stderr,"Malformed line: %s\n", buf2);
146  continue;
147  }
148  /* parse remote, local address */
149  if (parse_addr(&k.local, loc) != 0) {
150  fprintf(stderr,"Malformed line: %s\n", buf2);
151  continue;
152  }
153  if (parse_addr(&k.remote, rem) != 0) {
154  fprintf(stderr,"Malformed line: %s\n", buf2);
155  continue;
156  }
157 
158  k.status = -1;
159  k.euid = -1;
160  k.ruid = -1;
161  k.pid = -1;
162  k.cmd = NULL;
163  k.argv = NULL;
164 
165  lport = ntohs(SGPORT(k.local));
166  fport = ntohs(SGPORT(k.remote));
167 
168  for (try = 0; try < 5; try++)
169  {
170  k.status = ka_lookup(kp, &k);
171  if (k.status>0)
172  break;
173  }
174 
175  if (k.status<=0)
176  {
177  fprintf(stderr,"%-9.9s\t%-13s\t%-4d\t%-13s\t%-4d\n",
178  "*unknown*", loc, lport, rem, fport);
179  continue;
180  }
181 
182  pwd = getpwuid(k.ruid);
183  if (pwd)
184  printf("%-8.8s[%d]", pwd->pw_name, k.euid);
185  else
186  printf("%-8.8d[%d]", k.ruid, k.euid);
187  if (k.cmd)
188  {
189  printf (" \t%-13s\t%-4d\t%-13s\t%-4d\tPID=%d\tCMD=%s\tCMD+ARG=%s\n",
190  loc, lport, rem, fport, k.pid, k.cmd, k.argv);
191  s_free(k.cmd); s_free(k.argv);
192  }
193  else
194  printf (" \t%-13s\t%-4d\t%-13s\t%-4d pid[%d]\n",
195  loc, lport, rem, fport, k.pid);
196 
197  }
198 
199  return 0;
200 }
SGPORT
#define SGPORT(sag)
Definition: sockaddr.h:57
ka_lookup
int ka_lookup(void *vp, struct kernel *kp)
Definition: k_aix42.c:125
s_free
void s_free(void *p)
Definition: safeio.c:153
parse_addr
int parse_addr(struct sockaddr_in *addr, char *loc)
Definition: itest.c:73
ka_open
int ka_open(void **misc)
Definition: k_aix42.c:87
main
int main(int argc, char **argv)
Definition: itest.c:96
pidentd.h
kernel::remote
struct sockaddr_gen remote
Definition: kernel.h:29
kernel::local
struct sockaddr_gen local
Definition: kernel.h:28
debug
int debug
Definition: itest.c:40
kernel::euid
uid_t euid
Definition: kernel.h:35
kernel::ruid
uid_t ruid
Definition: kernel.h:36
sockaddr_gen
#define sockaddr_gen
Definition: sockaddr.h:53
SGFAM
#define SGFAM(sag)
Definition: sockaddr.h:54
kernel::pid
pid_t pid
Definition: kernel.h:38
kernel::cmd
char * cmd
Definition: kernel.h:39
ka_init
#define ka_init
Definition: k_sunos510.c:36
SGADDRP
#define SGADDRP(sag)
Definition: sockaddr.h:58
kernel::argv
char * argv
Definition: kernel.h:40
kernel::status
int status
Definition: kernel.h:34
osinfo_build
char osinfo_build[]
kernel
Definition: kernel.h:25