Back to index

wims  3.65+svn20090927
passcrypt.c
Go to the documentation of this file.
00001 /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
00002  *
00003  *  This program is free software; you can redistribute it and/or modify
00004  *  it under the terms of the GNU General Public License as published by
00005  *  the Free Software Foundation; either version 2 of the License, or
00006  *  (at your option) any later version.
00007  *
00008  *  This program is distributed in the hope that it will be useful,
00009  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *  GNU General Public License for more details.
00012  *
00013  *  You should have received a copy of the GNU General Public License
00014  *  along with this program; if not, write to the Free Software
00015  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00016  */
00017 
00018        /* Crypt password as in /etc/shadow */
00019 
00020 #define MAX_LINELEN 1024
00021 
00022 #include <stdio.h>
00023 #include <stdlib.h>
00024 #include <string.h>
00025 #include <unistd.h>
00026 #include <sys/types.h>
00027 #include <ctype.h>
00028 #include "../config.h"
00029 #ifdef HAVE_SYS_TIME_H
00030  #include <sys/time.h>
00031 #endif
00032 #ifdef HAVE_CRYPT
00033  #include <crypt.h>
00034 #endif
00035 
00036 int salt;
00037 char schar[]="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./";
00038 
00039 void pcrypt(char *p)
00040 {
00041 #ifdef HAVE_CRYPT
00042     char saltstr[4];
00043     char *pp, buf[MAX_LINELEN+1];
00044     saltstr[0]=schar[salt%64];
00045     saltstr[1]=schar[(salt/64)%64];
00046     saltstr[2]=0;
00047     while(isspace(*p)) p++;
00048     for(pp=p+strlen(p); pp>p && isspace(*(pp-1)); pp--); *pp=0;
00049     snprintf(buf,sizeof(buf),"%s",crypt(p,saltstr));
00050     strcpy(p,buf);
00051 #endif
00052 }
00053 
00054 int main(int argc,char *argv[])
00055 {
00056     int salt1;
00057     char p[MAX_LINELEN+1];
00058 
00059 #ifdef HAVE_GETTIMEOFDAY
00060     struct timeval tv;
00061     gettimeofday(&tv,NULL); salt1=tv.tv_sec+tv.tv_usec;
00062 #else
00063     salt1=3219;
00064 #endif
00065     if(argc<1) return 1;
00066     srandom(salt1+getpid());
00067     salt=random()/101;
00068     snprintf(p,sizeof(p),"%s",argv[1]);
00069     pcrypt(p); printf("%s\n",p);
00070     
00071     return 0;
00072 }
00073