1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
/* dircproxy-crypt
* Copyright (C) 2002 Scott James Remnant <scott@netsplit.com>.
* All Rights Reserved.
*
* main.c
* - Encyrpt a password taken from stdin or the command line
*
* This is deliberately simple, it doesn't even malloc() anything (which,
* for something I wrote, is amazing).
* --
* @(#) $Id: main.c,v 1.3 2001/12/21 20:15:54 keybuk Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_CONFIG_H
#include <config.h>
#else /* HAVE_CONFIG_H */
#define PACKAGE "dircproxy"
#define VERSION "-debug"
#endif /* HAVE_CONFIG_H */
#ifdef HAVE_CRYPT_H
#include <crypt.h>
#else /* HAVE_CRYPT_H */
#include <unistd.h>
#endif /* HAVE_CRYPT_H */
#include "getopt/getopt.h"
/* forward declarations */
static void _encrypt(const char *);
static void _saltchar(char *);
static int _print_usage(void);
static int _print_version(void);
static int _print_help(void);
/* This is so "ident" and "what" can query version etc - userful (not) */
const char *rcsid = "@(#) $Id: main.c,v 1.3 2001/12/21 20:15:54 keybuk Exp $";
/* The name of the program */
static char *progname;
/* Long options */
static struct option long_opts[] = {
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
{ 0, 0, 0, 0}
};
/* Options */
#define GETOPTIONS "hv"
/* The main func */
int main(int argc, char *argv[]) {
int optc, show_help, show_version, show_usage;
/* Get arguments */
progname = argv[0];
show_help = show_version = show_usage = 0;
while ((optc = getopt_long(argc, argv, GETOPTIONS, long_opts, NULL)) != -1) {
switch (optc) {
case 'h':
show_help = 1;
break;
case 'v':
show_version = 1;
break;
default:
show_usage = 1;
break;
}
}
if (show_usage) {
_print_usage();
return 1;
}
if (show_version) {
_print_version();
if (!show_help)
return 0;
}
if (show_help) {
_print_help();
return 0;
}
/* Randomize */
srand(time(0));
if (optind < argc) {
while (optind < argc) {
_encrypt(argv[optind]);
optind++;
}
} else {
char pass[80], *ret;
printf("Password: ");
ret = fgets(pass, sizeof(pass), stdin);
printf("\n");
if (ret) {
char *ptr;
ptr = pass + strlen(pass);
while ((ptr >= ret) && (*ptr <= 32)) *(ptr--) = 0;
_encrypt(pass);
}
}
return 0;
}
/* Encrypt a password */
static void _encrypt(const char *pass) {
char salt[3], *enc;
_saltchar(&(salt[0]));
_saltchar(&(salt[1]));
salt[2] = 0;
enc = crypt(pass, salt);
printf("%s = %s\n", pass, enc);
}
/* Pick a random salt character */
static void _saltchar(char *c) {
static char *chars = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"01234567890";
int ran;
ran = (int)((double)(strlen(chars) - 1) * (rand() / (RAND_MAX + 1.0)));
*c = chars[ran];
}
/* Print the usage instructions to stderr */
static int _print_usage(void) {
fprintf(stderr, "%s: Try '%s --help' for more information.\n",
progname, progname);
return 0;
}
/* Print the version number to stderr */
static int _print_version(void) {
fprintf(stderr, "dircproxy-crypt (%s %s)\n", PACKAGE, VERSION);
return 0;
}
/* Print the help to stderr */
static int _print_help(void) {
fprintf(stderr, "dircproxy-crypt. Encyrpt passwords for %s.\n\n", PACKAGE);
fprintf(stderr, "Usage: %s [OPTION]... [PASSWORD]...\n\n", progname);
fprintf(stderr, "If a long option shows an argument as mandatory, then "
"it is mandatory\nfor the equivalent short option also. "
"Similarly for optional arguments.\n\n");
fprintf(stderr, " -h, --help Print a summary of the options\n");
fprintf(stderr, " -v, --version Print the version number\n\n");
fprintf(stderr, " PASSWORD Plaintext password to crypt\n\n");
fprintf(stderr, "If no passwords are given on the command line, you will "
"be asked for one\non standard input.\n\n");
return 0;
}
|