home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / permissions / part01 / permtest / permtest.c < prev   
Encoding:
C/C++ Source or Header  |  1992-03-22  |  1.6 KB  |  91 lines

  1. #include <sys/param.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include <netdb.h>
  8. #include <grp.h>
  9. #include <pwd.h>
  10.  
  11. char *grpnames[NGROUPS+1];
  12. int ngrps;
  13. struct passwd *pwd;
  14. struct group *grp;
  15. int verbose;
  16.  
  17. extern int permcheck();
  18.  
  19. main(argc, argv)
  20. char **argv;
  21. {
  22.     char *logn, *ttyn, *hostn;
  23.     struct hostent *hent;
  24.     int lp, i;
  25.  
  26.     if(argc<4) {
  27.         fprintf(stderr, "usage: %s [-v] username ttyname hostname\n",
  28.             *argv);
  29.         exit(0);
  30.     }
  31.     if(!strcmp(argv[1], "-v")) {
  32.         argv++;
  33.         verbose=1;
  34.     }
  35.  
  36.     logn = argv[1];
  37.     ttyn = argv[2];
  38.  
  39.     setpwent();
  40.     pwd = getpwnam(logn);
  41.     if(!pwd) {
  42.         fprintf(stderr, "%s: unknown user\n", logn);
  43.         exit(0);
  44.     }
  45.  
  46.     setgrent();
  47.     ngrps = 0;
  48.     if(!(grp=getgrgid(pwd->pw_gid))) {
  49.         fprintf(stderr, 
  50.             "%d: group id has no name\n", pwd->pw_gid);
  51.         exit(0);
  52.     }
  53.     grpnames[ngrps++] = strdup(grp->gr_name);
  54.     while( grp=getgrent() ) {
  55.         if(pwd->pw_gid == grp->gr_gid)
  56.             continue;
  57.         while(*grp->gr_mem) {
  58.             if( !strcmp(logn, *grp->gr_mem)) {
  59.                 grpnames[ngrps++] = strdup(grp->gr_name);
  60.             }
  61.             grp->gr_mem++;
  62.         }
  63.     }
  64.     endgrent();
  65.     grpnames[ngrps] = NULL;
  66.  
  67.     if(verbose) {
  68.         printf("%d group%s:", ngrps, (ngrps>1)?"s":"");
  69.         for(i=0; i<ngrps; i++)
  70.             printf(" %s", grpnames[i]);
  71.         printf("\n");
  72.     }
  73.  
  74.     for( argv= &argv[3]; *argv; argv++) {
  75.         if((hent = gethostbyname(*argv)))
  76.             hostn = hent->h_name;
  77.         else
  78.             hostn = *argv;
  79.         lp = permcheck(logn, ttyn, grpnames, hostn);
  80.         if(verbose)
  81.             printf("user %s %spermitted on %s:%s\n",
  82.                 logn, lp ? "":"not ", hostn, ttyn);
  83.         else
  84.             printf("%s: %s\n", hostn, lp ? "":"not");
  85.     }
  86.     exit(0);
  87. }
  88.  
  89.  
  90.  
  91.