[ PHPXref.com ] [ Generated: Sun Jul 20 19:15:28 2008 ] [ OverLook 4.02 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/config/ -> chpasswd.c (source)

   1  /*
   2    chpasswd.c
   3  
   4    Changes local user password
   5  
   6    Developed by
   7    Pedro L Orso - orso@onda.com.br
   8    Changed by
   9    Thiago Melo de Paula - thiago@fafibe.br
  10    Paul Lesneiwski - pdontthink@angrynerds.com
  11  
  12    Released under GNU GPL - see http://www.gnu.org/copyleft/gpl.html
  13  
  14    How to compile:
  15      gcc -lcrypt -O -o chpasswd chpasswd.c; chmod 4750 chpasswd; chown root:apache chpasswd
  16      gcc -Wall -lcrypt -O -o chpasswd chpasswd.c; chmod 4750 chpasswd; chown root:apache chpasswd
  17  
  18  */
  19  
  20  #define TMPFILE "/tmp/chpasswdXXXXXX"
  21  
  22  #define PASSWD "/etc/passwd"
  23  #define SHADOW "/etc/shadow"
  24  
  25  #define STR_MAX 100
  26  #define MAXLEN 1024
  27  #define hhex(x) (((x) >= '0' && (x) <= '9') || ((x) >= 'a' && (x) <= 'f') || ((x) >= 'A' && (x) <= 'F'))
  28  
  29  #include <stdio.h>
  30  #include <time.h>
  31  #include <unistd.h>
  32  
  33  #include <crypt.h>
  34  #include <string.h>
  35  #include <ctype.h>
  36  #include <stdlib.h>
  37  //#include <errno.h>
  38  #include <sys/stat.h>
  39  
  40  
  41  void eperror(register char *);
  42  void getword(char *, char *, char);
  43  void to64(register char *, register long, register int);
  44  void putline(FILE *,char *);
  45  void *xmalloc(size_t);
  46  static void fixpwd(unsigned char *);
  47  static int htoi(unsigned char *s);
  48  
  49  int main(int argc, char *argv[]){
  50      int ok, fdes, test = 0;
  51      char User[STR_MAX];
  52      char buf[MAXLEN];
  53      char PUser[50];
  54      char New_pw[50];
  55      char Old_pw[50];
  56      char WOld_pw[50];
  57      char Wrest[MAXLEN];
  58      char pwdfile[255] = PASSWD;
  59      //char command[255];
  60      char WUser[50];
  61      char *cpw, salt[9];
  62      char *tn, *cypher;
  63      FILE *fpw, *tmp;
  64  
  65      tn = NULL;
  66  
  67      if((setuid(0)) < 0) eperror("setuid");
  68      // unecessary:  if((setgid(3)) < 0) eperror("setgid");
  69  
  70      //sprintf(User,"%s",argv[1]);
  71          snprintf(User, sizeof(User)-1, "%s", argv[1]);
  72          User[sizeof(User)-1] = '\0';
  73  
  74      //sprintf(Old_pw,"%s",argv[2]);
  75          snprintf(Old_pw, sizeof(Old_pw)-1, "%s", argv[2]);
  76          Old_pw[sizeof(Old_pw)-1] = '\0';
  77  
  78      //sprintf(New_pw,"%s",argv[3]);
  79          snprintf(New_pw, sizeof(New_pw)-1, "%s", argv[3]);
  80          New_pw[sizeof(New_pw)-1] = '\0';
  81  
  82      if(!strcmp("(null)",User)){
  83          printf("Missing username\n");
  84          return 12;
  85      }
  86  
  87      if(!strcmp("(null)",New_pw)){
  88          printf("Missing new password\n");
  89          return 2;
  90      }
  91  
  92      if(!strcmp("(null)",Old_pw)){
  93          printf("Missing current password\n");
  94          return 3;
  95      }
  96  
  97      if(!strcmp(User,"root")){
  98          printf("The password for this user cannot be changed due to security constraints: %s\n",User);
  99          return 4; //the root user cannot be edited for security reasons
 100      }
 101  
 102      fixpwd(New_pw);
 103      fixpwd(Old_pw);
 104      fixpwd(User);
 105  
 106      if(!strcmp(Old_pw,New_pw)){
 107          printf("The new password is equal to the current password. Choose another password.\n");
 108          return 5;
 109      }
 110  
 111      if (access(SHADOW, R_OK) == 0){
 112          sprintf(pwdfile, SHADOW);
 113          test = 1;
 114      }
 115  
 116      //strcpy(PUser,User);
 117      strncpy(PUser, User, sizeof(PUser)-1);
 118          PUser[sizeof(PUser)-1] = '\0';
 119  
 120      //strcat(PUser,":");
 121      strncat(PUser, ":", sizeof(PUser)-1);
 122          PUser[sizeof(PUser)-1] = '\0';
 123  
 124      if((fpw=fopen(pwdfile,"r"))==NULL){
 125          printf("Could not read password file: %s\n",pwdfile);
 126          if(!test)
 127              return 6; // means we are not using shadow pwd file
 128          return 7; // means we are using shadow pwd file
 129      }
 130  
 131      tn = (char *)xmalloc(strlen(TMPFILE) + 1);
 132      strcpy(tn, TMPFILE);
 133  /* 
 134     mode_t oldUmask; 
 135     oldUmask = umask(0177);
 136     ...mkstemp()...
 137     umask (oldUmask); 
 138  */
 139      umask(0177);
 140      if ((tmp = fdopen((fdes = mkstemp(tn)), "w+")) == NULL) {
 141          printf("Temporary file could not be opened: %s\n", tn);
 142          return 8;
 143      }
 144  
 145      ok = 0;
 146      while(fgets(buf,MAXLEN,fpw)!=NULL){
 147          if(!ok){
 148              if(strncmp(buf,PUser,strlen(PUser)) == 0){
 149                  getword(WUser,buf,':');
 150                  getword(WOld_pw,buf,':');
 151                  strcpy(Wrest,buf);
 152  
 153                  if(strcmp(WOld_pw, (char *)crypt(Old_pw, WOld_pw)) != 0){
 154                      if(fpw)
 155                          fclose(fpw);
 156                      if(tmp){
 157                          fclose(tmp);
 158                          close(fdes);
 159                          unlink(tn);
 160                      }
 161                      printf("Current password is incorrect\n");
 162                      return 9;
 163                  }
 164  
 165                  (void)srand((int)time((time_t *)NULL));
 166                  //cpw = (char *)crypt(New_pw,salt);
 167                  cypher = (char *)xmalloc(12); //MD5
 168                  strcpy(cypher, "$1$"); //MD5
 169                  strcat(cypher, salt); //MD5
 170                  cpw = (char *)crypt(New_pw, cypher); //MD5
 171                  sprintf(buf,"%s:%s:%s\n",User,cpw,Wrest);
 172                  buf[strlen(buf)-1]='\0';
 173                  ok++;
 174              }
 175          } 
 176          putline(tmp,buf);
 177      }
 178  
 179      fclose(fpw);
 180  //    fclose(tmp);
 181  
 182      if(ok) {
 183          //por nm@g only, en reemplazo del system cp, más rápido y menos recursos
 184          rewind(tmp);
 185          if((fpw=fopen(pwdfile,"w"))==NULL){
 186              printf("Could not read password file: %s\n",pwdfile);
 187              if(!test)
 188                  return 6; // means we are not using shadow pwd file
 189              return 7; // means we are using shadow pwd file
 190          }
 191          while( fgets(buf, MAXLEN, tmp) != NULL ) {
 192              putline(fpw,buf);
 193          }
 194          fclose(fpw);
 195          fclose(tmp);
 196          close(fdes);
 197          unlink(tn);
 198          printf("The password was modified successfully\n");
 199          return 0;
 200      } else {
 201          printf("User does not exist: %s\n", User);
 202          fclose(tmp);
 203          close(fdes);
 204          unlink(tn);
 205          return 10;
 206      }
 207  
 208  }
 209  
 210  void eperror(s)
 211  register char *s;
 212  {
 213      /*
 214      Developed by
 215      Pedro L Orso - orso@onda.com.br
 216      Changed by
 217      Thiago Melo de Paula - thiago@fafibe.br
 218      */
 219     char str[50];
 220  
 221     snprintf(str, sizeof(str)-1, "chpasswd - %s", s);
 222     str[sizeof(str)-1] = '\0';
 223     perror(str);
 224     exit(1);
 225  }
 226  
 227  void getword(char *word, char *line, char stop) 
 228  {
 229      /*
 230      Developed by
 231      Pedro L Orso - orso@onda.com.br
 232      */
 233     int x = 0,y;
 234  
 235     for(x=0;((line[x]) && (line[x] != stop));x++)
 236        word[x] = line[x];
 237  
 238     word[x] = '\0';
 239     if(line[x]) ++x;
 240     y=0;
 241  
 242     while((line[y++] = line[x++]));
 243  }
 244  
 245  static void
 246  fixpwd(str)
 247     unsigned char   *str;
 248  {
 249      /*
 250      Developed by
 251      Pedro L Orso - orso@onda.com.br
 252      */     
 253     unsigned char   *dest = str;
 254  
 255     while (str[0]) {
 256        if (str[0] == '+')
 257           dest[0] = ' ';
 258        else if (str[0] == '%' && hhex(str[1]) && hhex(str[2])) {
 259           dest[0] = (unsigned char) htoi(str + 1);
 260           str += 2;
 261        } else dest[0] = str[0];
 262  
 263        str++;
 264        dest++;
 265     }
 266  
 267     dest[0] = '\0';
 268     return;
 269  }
 270  
 271  static int
 272  htoi(s)
 273     unsigned char   *s;
 274  {
 275      /*
 276      Developed by
 277      Pedro L Orso - orso@onda.com.br
 278      */
 279     int     value;
 280     char    c;
 281  
 282     c = s[0];
 283     if (isupper(c))
 284        c = tolower(c);
 285     value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
 286  
 287     c = s[1];
 288     if (isupper(c))
 289        c = tolower(c);
 290     value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
 291  
 292     return (value);
 293  }
 294  
 295  static unsigned char itoa64[] =         /* 0 ... 63 => ascii - 64 */
 296          "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
 297      /*
 298      Developed by
 299      Pedro L Orso - orso@onda.com.br
 300      */
 301  
 302  void to64(s, v, n)
 303     register char *s;
 304     register long v;
 305     register int n;
 306  {
 307      /*
 308      Developed by
 309      Pedro L Orso - orso@onda.com.br
 310      Improved (md5) by nmag only <nmag@softhome.net>
 311      */
 312     while (--n >= 0) {
 313        *s++ = itoa64[v&0x3f];
 314        v >>= 3;
 315        v = ~v;
 316     }
 317  }
 318  
 319  void putline(FILE *f,char *l) {
 320      /*
 321      Developed by
 322      Pedro L Orso - orso@onda.com.br
 323      */
 324     int x;
 325  
 326     for(x=0;l[x];x++) fputc(l[x],f);
 327     return;
 328  }
 329  
 330  // Developed by nmag only <nmag@softhome.net>
 331  void *xmalloc (size_t size) {
 332      register void *value = malloc(size);
 333      if ( value == 0 ) {
 334          printf("Virtual memory exhausted\n");
 335          exit(11);
 336  //        exit(EXIT_FAILURE);
 337      }
 338      return value;
 339  }


[ Powered by PHPXref - Served by Debian GNU/Linux ]