#include #include #include #include #include #include #include #include #include #define USER_FILE "/export/home/cvs/cvsroot/CVSROOT/passwd" char *makeword(char *line, char stop); char *fmakeword(FILE *f, char stop, int *len); char x2c(char *what); void unescape_url(char *url); void plustospace(char *str); int cp(char *in, char *out); extern char *safepwd(struct passwd *pwd, char *pass); char *tn; /* From local_passwd.c (C) Regents of Univ. of California blah blah */ static unsigned char itoa64[] = /* 0 ... 63 => ascii - 64 */ "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; to64(s, v, n) register char *s; register long v; register int n; { while (--n >= 0) { *s++ = itoa64[v&0x3f]; v >>= 6; } } void change_password(char *user, char *pw, FILE *f) { char *cpw, salt[3]; (void)srand((int)time((time_t *)NULL)); to64(&salt[0],rand(),2); cpw = crypt(pw,salt); free(pw); fprintf(f,"%s:%s:%s\n",user,cpw,user); } void putline(FILE *f,char *l) { int x; for(x=0;l[x];x++) fputc(l[x],f); fputc('\n',f); } main(int argc, char *argv[]) { register int x; int cl,found,create; char *u,*t1,*t2,*p1,*p2,*user, line[256], l[256], w[256]; FILE *tfp,*f; struct passwd *pw; char *msg; int itn; /* tn = NULL;*/ if( NULL == ( pw = getpwuid( getuid () ))) { fprintf(stderr, "getpw failed (%s) for %d", strerror(errno), getuid()); exit(errno); } user = pw->pw_name; p1=getpass("New CVS password? "); p2=getpass("New CVS password again? "); if(strcmp(p1,p2)) { fprintf(stderr,"The two copies of your password do not match. Please try again.\n"); exit(1); } if ( NULL != ( msg = safepwd( pw, p1 ))) { fprintf(stderr,"%s\n",msg); exit(1); } /* tn = tmpnam(NULL);*/ /* if(!(tfp = fopen(tn,"w"))) { */ tn=strdup("/tmp/cvspw_XXXXXX"); if ( -1 == (itn = mkstemp(tn))) { fprintf(stderr,"Could not open temp file.\n"); exit(1); } if (NULL==(tfp=fdopen(itn, "w"))) { perror("fdopen"); exit(1); } if(!(f = fopen(USER_FILE,"r"))) { fprintf(stderr, "Could not open passwd file for reading.\n",USER_FILE); exit(1); } found = 0; while(!(getline(line,256,f))) { if(found || (line[0] == '#') || (!line[0])) { putline(tfp,line); continue; } strcpy(l,line); getword(w,l,':'); if(strcmp(user,w)) { putline(tfp,line); continue; } else { change_password(user,p1,tfp); found=1; } } fclose(f); fclose(tfp); if(!found) { fprintf(stderr,"You don't exist\n"); exit(1); } if ( 0 == cp( tn, USER_FILE )) { unlink(tn); fprintf(stderr,"Your password has been successfully changed.\n"); exit(0); } else { unlink(tn); fprintf(stderr,"Your password could not be changed.\n"); exit(1); } } int cp(char *in, char *out) { #define BS 512 FILE *i; FILE *o; char buffer[BS]; size_t n; if ( NULL == ( o = fopen( out, "wb"))) { perror(out); return 1; } if ( NULL == ( i = fopen( in, "rb"))) { perror(in); fclose(o); return 1; } while ( 0 != ( n = fread( buffer, 1, BS, i ))) fwrite( buffer, 1, n, o ); if (!feof(i)) perror("fread"); fclose(i); fclose(o); return(0); }