home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume1 / 8706 / 6 < prev    next >
Text File  |  1993-09-01  |  2KB  |  78 lines

  1. Article 86 of comp.sources.misc:
  2. Relay-Version: version B 2.10.3 alpha 5/22/85; site osu-eddie.UUCP
  3. Path: osu-eddie!cbosgd!ihnp4!ptsfa!ames!necntc!ncoast!allbery
  4. From: jfh@killer.UUCP (John Haugh)
  5. Newsgroups: comp.sources.misc
  6. Subject: Setgrp - spawn a process into a new group
  7. Message-ID: <2738@ncoast.UUCP>
  8. Date: 29 Jun 87 00:47:41 GMT
  9. Date-Received: 29 Jun 87 20:13:40 GMT
  10. Sender: allbery@ncoast.UUCP
  11. Lines: 60
  12. Keywords: newgrp
  13. Approved: allbery@ncoast.UUCP
  14. X-Archive: comp.sources.misc/8706/6
  15.  
  16. I wrote this to handle a problem that a poster asked about in
  17. comp.unix.questions.  Hope you like it as much as we do.  Must
  18. be run set-uid (boo) and probably is only useful on System V.
  19.  
  20. Basically, the program checks the /etc/group file and sees
  21. if you are in the group.  Probably not totally sexure, mostly
  22. because it ignores passwords!  But then its free ...
  23.  
  24. - john.
  25. ------------------------- cut here ----------------------
  26.  
  27. #include <grp.h>
  28. #include <pwd.h>
  29. #include <stdio.h>
  30.  
  31. struct    passwd    *getpwuid ();
  32. struct    group    *getgrnam ();
  33.  
  34. main  (argc, argv)
  35. int    argc;
  36. char    **argv;
  37. {
  38.     char    username[20];
  39.     char    groupname[20];
  40.     struct    passwd    *ppwd;
  41.     struct    group    *pgrp;
  42.     int    i;
  43.  
  44.     if (argc < 3) {
  45.         fprintf (stderr, "usage: %s group command [ args ... ]\n", arg[0]);
  46.         exit (1);
  47.     }
  48.     if (! (ppwd = getpwuid (getuid ()))) {
  49.         fprintf (stderr, "Error reading password file\n");
  50.         exit (1);
  51.     }
  52.     strncpy (username, ppwd->pw_name, sizeof (username));
  53.     strncpy (groupname, argv[1], sizeof (groupname));
  54.     if (! (pgrp = getgrnam (groupname))) {
  55.         fprintf (stderr, "Nonexistent group: %s\n", groupname);
  56.         exit (1);
  57.     }
  58.     for (i = 0;pgrp->gr_mem[i] != (char *) 0;i++) {
  59.         if (strncmp (username, pgrp->gr_mem[i], sizeof (username)))
  60.             continue;
  61.         else
  62.             break;
  63.     }
  64.     if (pgrp->gr_mem[i] == (char *) 0) {
  65.         fprintf (stderr, "Permission denied\n");
  66.         exit (1);
  67.     }
  68.     setgid (pgrp->gr_gid);
  69.     setuid (getuid ());
  70.  
  71.     execvp (argv[2], &argv[2]);
  72.  
  73.     perror (argv[2]);
  74.     exit (1);
  75. }
  76.  
  77.  
  78.