1: #include <stdio.h>
   2: #include <grp.h>
   3: #include <pwd.h>
   4: 
   5: struct  group   *getgrnam(), *grp;
   6: struct  passwd  *getpwuid(), *pwd;
   7: char    *getpass(), *crypt();
   8: 
   9: #ifdef MENLO
  10: char **environ;
  11: char *shell ={ "/bin/sh" };
  12: #endif
  13: 
  14: main(argc,argv)
  15: int argc;
  16: char    **argv;
  17: {
  18:     register i;
  19:     if(argc != 2) {
  20:         printf("usage: newgrp groupname\n");
  21:         done();
  22:     }
  23:     if((grp=getgrnam(argv[1])) == NULL) {
  24:         printf("%s: no such group\n", argv[1]);
  25:         done();
  26:     }
  27:     if((pwd=getpwuid(getuid())) == NULL) {
  28:         printf("You do not exist!\n");
  29:         done();
  30:     }
  31:     for(i=0;grp->gr_mem[i];i++)
  32:         if(strcmp(grp->gr_mem[i], pwd->pw_name) == 0)
  33:             break;
  34:     if(grp->gr_mem[i] == 0 && strcmp(grp->gr_name,"other")) {
  35:         printf("Sorry\n");
  36:         done();
  37:     }
  38: 
  39:     if(grp->gr_passwd[0] != '\0' && pwd->pw_passwd[0] == '\0') {
  40:         if(strcmp(grp->gr_passwd, crypt(getpass("Password:"),grp->gr_passwd)) != 0) {
  41:             printf("Sorry\n");
  42:             done();
  43:         }
  44:     }
  45:     if(setgid(grp->gr_gid) < 0)
  46:         perror("setgid");
  47:     done();
  48: }
  49: 
  50: done()
  51: {
  52:     register i;
  53: 
  54: #ifdef MENLO
  55:     register char *cp;
  56:     register char *cp2;
  57:     register char **p;
  58:     char hometmp[50];
  59: #endif
  60:     setuid(getuid());
  61:     for (i=3; i<15; i++)
  62:         close(i);
  63: #ifndef MENLO
  64:     execl("/bin/sh","sh",0);
  65: #else
  66: /*
  67:  * to enable users to specify which shell they want in the environment
  68:  * (as opposed to the default shell), search for a "SHELL" variable
  69:  *		pag -- 11/11/79
  70:  */
  71:     for(p=environ; *p; p++)
  72:         if (strncmp("SHELL=", *p, 6) == 0)
  73:         {
  74:             for(cp = *p;*cp++ != '=';)
  75:             ;
  76:             shell = cp;
  77:         }
  78: /*
  79:  * the C shell is very fussy about 'sourcing' in .login
  80:  * and .cshrc files if they are not owned by the invoker
  81:  * therefore, set HOME beforehand so that the old HOME
  82:  * won't be used.
  83:  *		pag -- 1/10/80
  84:  */
  85:         if (strncmp("HOME=", *p, 5) == 0)
  86:         {
  87:         for(cp = *p,cp2=hometmp;*cp != '=';)
  88:             *cp2++ = *cp++;
  89:         *cp2++ = '=';
  90:         strcpy(cp2,pwd->pw_dir);
  91:         *p = calloc(1,strlen(hometmp)+1);
  92:         strcat(*p,hometmp);
  93:         }
  94: /*
  95:  * slight kludge here:  csh strips off the first letter of argv[1]
  96:  * when it is "newgrp", so add a dummy first letter! -- pag
  97:  */
  98:     if(strcmp(shell,"/bin/csh") != 0)
  99:     execl(shell, "newgrp", 0);
 100:     else
 101:         execl(shell, "_newgrp", 0);
 102: #endif
 103:     printf("No shell\n");
 104:     exit(3);
 105: }

Defined functions

done defined in line 50; used 6 times
main defined in line 14; never used

Defined variables

environ defined in line 10; used 1 times
  • in line 71
grp defined in line 5; used 9 times
pwd defined in line 6; used 4 times
shell defined in line 11; used 4 times
Last modified: 1981-07-10
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 870
Valid CSS Valid XHTML 1.0 Strict