home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume15 / sec / part01 / secpw.c < prev   
Encoding:
C/C++ Source or Header  |  1990-10-14  |  1.6 KB  |  92 lines

  1. /*
  2.  *
  3.  * secpw.c
  4.  *
  5.  * Change passwords for the sec password file
  6.  *
  7.  * Usage: secpw [username]
  8.  *
  9.  * It may seem odd that users are allowed to change other people's
  10.  * passwords, and change their own without supplying the old password,
  11.  * but whoever runs this must already have a uid of root, so it
  12.  * really doesn't matter.
  13.  *
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <strings.h>
  18. #include <sys/file.h>
  19. #include "config.h"
  20.  
  21. extern long random();
  22. extern char *getlogin();
  23. extern long time();
  24.  
  25. char charset[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789./";
  26.  
  27. char getline(file,s)
  28. FILE *file;
  29. char *s;
  30. {
  31.   while(((*s=getc(file))!=EOF) && (*s!='\n')) s++;
  32.   if ((*s)==EOF) return 1;
  33.   *s=0;
  34.   return 0;
  35. }
  36.  
  37. main(argc,argv)
  38. int argc;
  39. char **argv;
  40. {
  41.  
  42.   char key[9],salt[3],line[80],line2[80],*who,ok=0;
  43.   FILE *f1,*f2;
  44.  
  45.   if (setuid(0))
  46.   {
  47.     printf("Must be root.\n");
  48.     exit(1);
  49.   }
  50.  
  51.   who=getlogin();
  52.   if (argc>1)
  53.     who=*(argv+1);
  54.  
  55.   strcpy(key,getpass("New Password:"));
  56.   if (strcmp(key,getpass("Again:")))
  57.   {
  58.     printf("No Match.\n");
  59.     exit(1);
  60.   }
  61.  
  62.   srandom((int) time(0L));
  63.   salt[0]=charset[random()%strlen(charset)];
  64.   salt[1]=charset[random()%strlen(charset)];
  65.   strcpy(line,who);
  66.   strcat(line,":");
  67.   strcat(line,crypt(key,salt));
  68.  
  69.   umask(0377);
  70.   f1=fopen(FILENAME,"r");
  71.   f2=fopen(TEMPNAME,"w");
  72.   flock(fileno(f2),LOCK_EX);
  73.   while(!getline(f1,line2))
  74.     if (strncmp(line2,who,strlen(who)))
  75.       fprintf(f2,"%s\n",line2);
  76.     else
  77.     {
  78.       fprintf(f2,"%s\n",line);
  79.       ok++;
  80.     }
  81.   fclose(f1);
  82.   fclose(f2);
  83.   rename(TEMPNAME,FILENAME);
  84.  
  85.   if (!ok)
  86.   {
  87.     printf("Cannot change: User not authorized.\n");
  88.     exit(1);
  89.   }
  90.   
  91. }
  92.