1: /*
   2:  * enter a password in the password file
   3:  * this program should be suid with owner
   4:  * with an owner with write permission on /etc/passwd
   5:  */
   6: char    *tfile  { "/tmp/ptmp" };
   7: char    *pfile  { "/etc/passwd" };
   8: int tbuf[259];
   9: int pbuf[259];
  10: 
  11: main(argc, argv)
  12: char *argv[];
  13: {
  14:     register u, c;
  15:     register char *p;
  16: 
  17:     if(argc != 3) {
  18:         write(2, "Usage: passwd user password\n", 28);
  19:         goto bex;
  20:     }
  21:     signal(1, 1);
  22:     signal(2, 1);
  23:     signal(3, 1);
  24: 
  25:     if(stat(tfile, tbuf+20) >= 0) {
  26:         write(2, "Temporary file busy -- try again\n", 33);
  27:         goto bex;
  28:     }
  29:     tbuf[0] = creat(tfile, 0600);
  30:     if(tbuf[0] < 0) {
  31:         write(2, "Cannot create temporary file\n", 29);
  32:         goto bex;
  33:     }
  34:     pbuf[0] = open(pfile, 0);
  35:     if(pbuf[0] < 0) {
  36:         write(2, "Cannot open /etc/passwd\n", 24);
  37:         goto out;
  38:     }
  39:     goto l1;
  40: 
  41: /*
  42:  * skip to beginning of next line
  43:  */
  44: 
  45: skip:
  46:     while(c != '\n') {
  47:         if(c < 0)
  48:             goto ill;
  49:         c = getc(pbuf);
  50:         putc(c, tbuf);
  51:     }
  52: 
  53: /*
  54:  * compare user names
  55:  */
  56: 
  57: l1:
  58:     c = getc(pbuf);
  59:     putc(c, tbuf);
  60:     if(c < 0) {
  61:         write(2, "User name not found in password file\n", 37);
  62:         goto out;
  63:     }
  64:     p = argv[1];
  65:     while(c != ':') {
  66:         if(*p++ != c)
  67:             goto skip;
  68:         c = getc(pbuf);
  69:         putc(c, tbuf);
  70:     }
  71:     if(*p)
  72:         goto skip;
  73: /*
  74:  * skip old password
  75:  */
  76:     do {
  77:         c = getc(pbuf);
  78:         if(c < 0)
  79:             goto ill;
  80:     } while(c != ':');
  81: 
  82: /*
  83:  * copy in new password
  84:  */
  85:     p = argv[2];
  86:     for(c=0; c<9; c++)
  87:         if(*p++ == 0)
  88:             break;
  89:     *--p = 0;
  90:     if(p != argv[2])
  91:         p = crypt(argv[2]);
  92:     while(*p)
  93:         putc(*p++, tbuf);
  94:     putc(':', tbuf);
  95: 
  96: /*
  97:  * validate uid
  98:  */
  99: 
 100:     u = 0;
 101:     do {
 102:         c = getc(pbuf);
 103:         putc(c, tbuf);
 104:         if(c >= '0' && c <= '9')
 105:             u = u*10 + c-'0';
 106:         if(c < 0)
 107:             goto ill;
 108:     } while(c != ':');
 109:     c = getuid() & 0377;
 110:     if(c != 0 && c != u) {
 111:         write(2, "Permission denied\n", 18);
 112:         goto out;
 113:     }
 114: 
 115: /*
 116:  * copy out and back
 117:  */
 118: 
 119:     for(;;) {
 120:         c = getc(pbuf);
 121:         if(c < 0) {
 122:             fflush(tbuf);
 123:             close(pbuf[0]);
 124:             close(tbuf[0]);
 125:             tbuf[0] = open(tfile, 0);
 126:             if(tbuf[0] < 0) {
 127:                 write(2, "Urk\n", 4);
 128:                 goto out;
 129:             }
 130:             pbuf[0] = creat(pfile, 0644);
 131:             if(pbuf[0] < 0) {
 132:                 write(2, "Cannot create /etc/passwd\n", 26);
 133:                 goto out;
 134:             }
 135:             while((c = read(tbuf[0], tbuf+1, 512)) > 0)
 136:                 write(pbuf[0], tbuf+1, c);
 137:             unlink(tfile);
 138:             exit(0);
 139:         }
 140:         putc(c, tbuf);
 141:     }
 142: 
 143: ill:
 144:     write(2, "Password file illformed\n", 24);
 145: 
 146: out:
 147:     unlink(tfile);
 148: 
 149: bex:
 150:     exit(1);
 151: }

Defined functions

main defined in line 11; never used

Defined variables

pbuf defined in line 9; used 12 times
pfile defined in line 7; used 2 times
tbuf defined in line 8; used 17 times
tfile defined in line 6; used 5 times
Last modified: 1975-05-14
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 873
Valid CSS Valid XHTML 1.0 Strict