1: /*
   2:  * chsh
   3:  */
   4: #include <stdio.h>
   5: #include <whoami.h>
   6: #include <signal.h>
   7: #include <pwd.h>
   8: 
   9: char    passwd[] = "/etc/passwd";
  10: char    temp[]   = "/etc/ptmp";
  11: struct  passwd *pwd;
  12: struct  passwd *getpwent();
  13: int endpwent();
  14: char    *crypt();
  15: char    *getpass();
  16: char    buf[BUFSIZ];
  17: 
  18: main(argc, argv)
  19: char *argv[];
  20: {
  21:     int u,fi,fo;
  22:     FILE *tf;
  23:     int found = 0;
  24: 
  25:     if(argc < 2 || argc > 3) {
  26:         printf("Usage: chsh user [ /bin/csh ] [ /bin/newcsh ] [ /bin/oldcsh ]\n");
  27:         goto bex;
  28:     }
  29:     if (argc == 2)
  30:         argv[2] = "";
  31:     else if (strcmp(argv[2], "/bin/csh") && strcmp(argv[2], "/bin/newcsh")
  32:     && strcmp(argv[2], "/bin/oldcsh") && getuid()) {
  33:         printf("Only /bin/csh or /bin/newcsh or /bin/oldcsh may be specified\n");
  34:         exit(1);
  35:     }
  36:     if (argv[2][0] && access(argv[2],1)){
  37:         perror(argv[2]);
  38:         goto out;
  39:     }
  40:     while((pwd=getpwent()) != NULL){
  41:         if(strcmp(pwd->pw_name,argv[1]) == 0){
  42:             u = getuid();
  43:             if(u!=0 && u != pwd->pw_uid){
  44:                 printf("Permission denied.\n");
  45:                 goto bex;
  46:                 }
  47:             found++;
  48:             break;
  49:             }
  50:         }
  51:     endpwent();
  52:     if (found == 0) {
  53:         printf("Unknown user: %s\n", argv[1]);
  54:         goto bex;
  55:     }
  56:     signal(SIGHUP, 1);
  57:     signal(SIGINT, 1);
  58:     signal(SIGQUIT, 1);
  59: #ifdef SIGTSTP
  60:     signal(SIGTSTP, 1);
  61: #endif
  62: 
  63:     if(access(temp, 0) >= 0) {
  64:         printf("Temporary file busy -- try again\n");
  65:         goto bex;
  66:     }
  67:     if((tf=fopen(temp,"w")) == NULL) {
  68:         printf("Cannot create temporary file\n");
  69:         goto bex;
  70:     }
  71: 
  72: /*
  73:  *	copy passwd to temp, replacing matching lines
  74:  *	with new shell.
  75:  */
  76: 
  77:     while((pwd=getpwent()) != NULL) {
  78:         if(strcmp(pwd->pw_name,argv[1]) == 0) {
  79:             u = getuid();
  80:             if(u != 0 && u != pwd->pw_uid) {
  81:                 printf("Permission denied.\n");
  82:                 goto out;
  83:             }
  84:             pwd->pw_shell = argv[2];
  85:         }
  86:         fprintf(tf,"%s:%s:%d:%d:%s:%s:%s\n",
  87:             pwd->pw_name,
  88:             pwd->pw_passwd,
  89:             pwd->pw_uid,
  90:             pwd->pw_gid,
  91:             pwd->pw_gecos,
  92:             pwd->pw_dir,
  93:             pwd->pw_shell);
  94:     }
  95:     endpwent();
  96:     fclose(tf);
  97: 
  98: /*
  99:  *	copy temp back to passwd file
 100:  */
 101: 
 102:     if((fi=open(temp,0)) < 0) {
 103:         printf("Temp file disappeared!\n");
 104:         goto out;
 105:     }
 106:     if((fo=creat(passwd, 0644)) < 0) {
 107:         printf("Cannot recreate passwd file.\n");
 108:         goto out;
 109:     }
 110:     while((u=read(fi,buf,sizeof(buf))) > 0) write(fo,buf,u);
 111: 
 112: out:
 113:     unlink(temp);
 114: 
 115: bex:
 116:     exit(1);
 117: }

Defined functions

main defined in line 18; never used

Defined variables

buf defined in line 16; used 3 times
  • in line 110(3)
passwd defined in line 9; used 1 times
pwd defined in line 11; used 14 times
temp defined in line 10; used 4 times
Last modified: 1982-09-21
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 845
Valid CSS Valid XHTML 1.0 Strict