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: #include <stdio.h> 19: #include <ctype.h> 20: #ifdef sequent 21: #include <strings.h> 22: #define strrchr rindex 23: #else 24: #include <string.h> 25: #include <stdlib.h> 26: #endif 27: #include <sys/time.h> 28: #include <sys/types.h> 29: #include <sys/socket.h> 30: #include <netinet/in.h> 31: #include <arpa/inet.h> 32: #include <pwd.h> 33: 34: #include "identd.h" 35: #include "error.h" 36: 37: 38: #ifdef LOG_DAEMON 39: static int syslog_facility = LOG_DAEMON; 40: #endif 41: 42: /* 43: * To resolve external references that are usually resolved 44: * from identd.c 45: */ 46: char *path_unix = NULL; 47: char *path_kmem = NULL; 48: int lport = 0; 49: int fport = 0; 50: 51: int debug_flag = 1; 52: int syslog_flag = 1; 53: 54: 55: #ifdef sequent 56: char *strtok(str, dels) 57: char *str; 58: char *dels; 59: { 60: static char *bufp; 61: 62: 63: if (str) 64: bufp = str; 65: 66: if (!bufp || !*bufp) 67: return (char *) 0;; 68: 69: while (*bufp && index(dels, *bufp) != (char *) 0) 70: ++bufp; 71: 72: str = bufp; 73: 74: while (*bufp && index(dels, *bufp) == (char *) 0) 75: ++bufp; 76: 77: if (*bufp) 78: *bufp++ = '\0'; 79: 80: return str; 81: } 82: #endif 83: 84: int 85: main() 86: { 87: char buf[500]; 88: 89: /* 90: ** Open the connection to the Syslog daemon if requested 91: */ 92: if (syslog_flag) 93: { 94: #ifdef LOG_DAEMON 95: openlog("identd_test", LOG_PID, syslog_facility); 96: #else 97: openlog("identd_test", LOG_PID); 98: #endif 99: } 100: 101: 102: if (k_open() < 0) { 103: fprintf(stderr,"k_open failed\n"); 104: exit(1); 105: } 106: while (fgets(buf,sizeof(buf),stdin)) { 107: char *loc, *rem, *tmp; 108: unsigned short lport, fport; 109: struct in_addr faddr, laddr; 110: int uid; 111: #ifdef ALLOW_FORMAT 112: int pid; 113: char *cmd, *cmd_and_args; 114: #endif 115: struct passwd *pwd; 116: char buf2[sizeof(buf)]; 117: 118: strcpy(buf2,buf); 119: 120: loc = strtok(buf, " \t"); 121: if (strcmp(loc,"tcp") == 0) { 122: int i; 123: for (i = 0; i < 3; i++) 124: loc = strtok(NULL, " \t"); 125: } 126: rem = strtok(NULL, " \t"); 127: if (loc == NULL || rem == NULL) { 128: fprintf(stderr,"Malformed line: %s\n", buf2); 129: continue; 130: } 131: /* parse remote, local address */ 132: tmp = strrchr(loc,'.'); 133: if (tmp == NULL) { 134: fprintf(stderr,"Malformed line: %s\n", buf2); 135: continue; 136: } 137: *tmp++ ='\0'; 138: laddr.s_addr = inet_addr(loc); 139: lport = atoi(tmp); 140: 141: tmp = strrchr(rem,'.'); 142: if (tmp == NULL) { 143: fprintf(stderr,"Malformed line: %s\n", buf2); 144: continue; 145: } 146: *tmp++ ='\0'; 147: fport = atoi(tmp); 148: faddr.s_addr = inet_addr(rem); 149: 150: uid = -1; 151: #ifdef ALLOW_FORMAT 152: pid = 0; 153: cmd = "unknown"; 154: cmd_and_args = "unknown"; 155: #endif 156: 157: if (k_getuid(&faddr, htons(fport), &laddr, htons(lport), &uid 158: #ifdef ALLOW_FORMAT 159: , &pid 160: , &cmd 161: , &cmd_and_args 162: #endif 163: ) != 0) { 164: fprintf(stderr,"*unknown*\t%s\t%d\t\t%s\t%d\n", loc, lport, rem, fport); 165: continue; 166: } 167: 168: pwd = getpwuid(uid); 169: if (pwd) 170: printf("%-8.8s", pwd->pw_name); 171: else 172: printf("%-8.8d", uid); 173: #ifdef ALLOW_FORMAT 174: printf (" \t%-13s\t%-4d\t%-13s\t%-4d\tPID=%d\tCMD=%s\tCMD+ARG=%s\n", loc, lport, rem, fport, pid, cmd, cmd_and_args); 175: #else 176: printf(" \t%s\t%d\t\t%s\t%d\n", loc, lport, rem, fport); 177: #endif 178: } 179: 180: return 0; 181: }