home *** CD-ROM | disk | FTP | other *** search
- /* History:
- 5/3/91 DJB modified to not compile without HAVE_UCRED
- 5/1/91 DJB baseline public domain
- */
-
- #include "confhaveucred.h"
- #ifndef HAVE_UCRED
- error! error! error! XXX
- netstatuids will not work on a system without struct ucred.
- #endif
-
- #include <stdio.h>
- #include "structfile.h"
- #include "structucred.h"
- #include "structinpcb.h"
- #include "inpcblist.h"
- #include "filetable.h"
- #include "getfcred.h"
- #include "strerr.h"
- #include "authuser.h"
- #include <pwd.h>
- #include "mallocfree.h"
-
- char *pw(u)
- int u;
- {
- struct passwd *pw;
- char buf[20];
-
- pw = getpwuid(u);
- if (pw)
- return pw->pw_name;
- sprintf(buf,"%d",u);
- return buf;
- }
-
- main()
- {
- struct inpcb *inp;
- struct file *ft;
- struct file *fp;
- struct ucred *uc;
- int *sfs;
- int numsfs;
- int i;
- unsigned char *fa;
- unsigned char *la;
- char *user;
-
- if (filetableinit() == -1)
- {
- fprintf(stderr,"ns5: %s\n",strerr(filetablestrerr));
- exit(1);
- }
- if (inpcblistinit() == -1)
- {
- fprintf(stderr,"ns5: %s\n",strerr(inpcbliststrerr));
- exit(1);
- }
-
- ft = getfiletable();
- if (!ft)
- {
- fprintf(stderr,"ns5: %s\n",strerr(filetablestrerr));
- exit(1);
- }
- sfs = (int *) malloc((unsigned) (sizeof(int) * mynfile));
- if (!sfs)
- {
- fprintf(stderr,"ns5: out of memory\n");
- exit(1);
- }
- numsfs = 0;
-
- for (fp = ft;fp < ft + mynfile;++fp)
- if (fp->f_count && fp->f_type == DTYPE_SOCKET)
- sfs[numsfs++] = fp - ft;
-
- inp = 0;
- while (inp = nextinpcb(inp))
- for (i = 0;i < numsfs;++i)
- if ((fp = ft + sfs[i])->f_data == (char *) inp->inp_socket)
- {
- sfs[i] = sfs[--numsfs];
- uc = getfcred(fp);
- if (!uc)
- {
- fprintf(stderr,"ns5: warning: %s\n",strerr(getfcredstrerr));
- break;
- }
- fa = (unsigned char *) &(inp->inp_faddr);
- la = (unsigned char *) &(inp->inp_laddr);
- user = 0;
- if (inp->inp_fport)
- user = auth_tcpuser(inp->inp_faddr.s_addr
- ,ntohs(inp->inp_lport)
- ,ntohs(inp->inp_fport));
- else
- break; /*XXX: unconnected server*/
- if (!user)
- user = "?";
- printf("%s@%d.%d.%d.%d:%d\t%s@%d.%d.%d.%d:%d\n"
- ,user
- ,(unsigned) fa[0],(unsigned) fa[1],(unsigned) fa[2],(unsigned) fa[3]
- ,ntohs(inp->inp_fport)
- ,pw(uc->cr_ruid)
- ,(unsigned) la[0],(unsigned) la[1],(unsigned) la[2],(unsigned) la[3]
- ,ntohs(inp->inp_lport)
- );
- fflush(stdout); /*XXX*/
- break;
- }
-
- if (inperrno)
- {
- fprintf(stderr,"ns5: %s\n",strerr(inpcbliststrerr));
- exit(1);
- }
- exit(0);
- }
-