2005-10-09 13:47:20 +02:00
|
|
|
|
/*
|
|
|
|
|
* $Id$
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the bip project
|
2022-01-06 19:48:59 +01:00
|
|
|
|
* Copyright (C) 2004,2005 Arnaud Cornet
|
|
|
|
|
* Copyright (C) 2004,2005,2022 Loïc Gomez
|
2005-10-09 13:47:20 +02:00
|
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
* See the file "COPYING" for the exact licensing terms.
|
|
|
|
|
*/
|
|
|
|
|
|
2005-04-28 10:26:44 +02:00
|
|
|
|
#include "config.h"
|
2005-07-12 21:31:59 +02:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <string.h>
|
2005-04-28 10:26:44 +02:00
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
2005-07-12 21:31:59 +02:00
|
|
|
|
#include <termios.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <errno.h>
|
2005-04-28 10:26:44 +02:00
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "md5.h"
|
|
|
|
|
|
2024-02-07 10:17:37 +01:00
|
|
|
|
extern int conf_log_level;
|
|
|
|
|
extern FILE *conf_global_log_file;
|
|
|
|
|
extern int conf_log_system;
|
2005-04-28 10:26:44 +02:00
|
|
|
|
|
2022-01-06 19:48:59 +01:00
|
|
|
|
void bipmkpw_fatal(char *msg, char *err)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "%s: %s\n", msg, err);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
2005-07-12 21:31:59 +02:00
|
|
|
|
void readpass(char *buffer, int buflen)
|
|
|
|
|
{
|
|
|
|
|
int ttyfd = open("/dev/tty", O_RDWR);
|
2022-01-06 19:48:59 +01:00
|
|
|
|
if (ttyfd == -1)
|
|
|
|
|
bipmkpw_fatal("Unable to open tty", strerror(errno));
|
2008-12-29 14:04:51 +01:00
|
|
|
|
|
2005-07-12 21:31:59 +02:00
|
|
|
|
struct termios tt, ttback;
|
|
|
|
|
memset(&ttback, 0, sizeof(ttback));
|
2022-01-06 19:48:59 +01:00
|
|
|
|
if (tcgetattr(ttyfd, &ttback) < 0)
|
|
|
|
|
bipmkpw_fatal("tcgetattr failed", strerror(errno));
|
2008-12-29 14:04:51 +01:00
|
|
|
|
|
2005-07-12 21:31:59 +02:00
|
|
|
|
memcpy(&tt, &ttback, sizeof(ttback));
|
2022-03-10 13:52:03 +01:00
|
|
|
|
// unsigned conversion from ‘int’ to ‘tcflag_t’ {aka ‘unsigned int’} changes
|
|
|
|
|
// value from ‘-11’ to ‘4294967285’
|
2022-01-06 19:48:59 +01:00
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
2022-03-10 13:52:03 +01:00
|
|
|
|
tt.c_lflag &= ~(ICANON | ECHO);
|
2022-01-06 19:48:59 +01:00
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
if (tcsetattr(ttyfd, TCSANOW, &tt) < 0)
|
|
|
|
|
bipmkpw_fatal("tcsetattr failed", strerror(errno));
|
2008-12-29 14:04:51 +01:00
|
|
|
|
|
2022-01-06 19:48:59 +01:00
|
|
|
|
if (!write(ttyfd, "Password: ", (size_t)10))
|
|
|
|
|
bipmkpw_fatal("tty write failed", strerror(errno));
|
2008-12-29 14:04:51 +01:00
|
|
|
|
|
2005-07-12 21:31:59 +02:00
|
|
|
|
int idx = 0;
|
2012-01-07 12:28:47 +01:00
|
|
|
|
int valid = 1;
|
2005-07-12 21:31:59 +02:00
|
|
|
|
while (idx < buflen) {
|
2022-03-10 13:52:03 +01:00
|
|
|
|
ssize_t rbytes = read(ttyfd, buffer + idx, (size_t)1);
|
2022-01-06 19:48:59 +01:00
|
|
|
|
if (rbytes <= 0) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2005-07-12 21:31:59 +02:00
|
|
|
|
if (buffer[idx] == '\n') {
|
|
|
|
|
buffer[idx] = 0;
|
|
|
|
|
break;
|
2012-01-07 12:28:47 +01:00
|
|
|
|
} else if (buffer[idx] == ' ') {
|
|
|
|
|
valid = 0;
|
2005-07-12 21:31:59 +02:00
|
|
|
|
}
|
|
|
|
|
idx++;
|
|
|
|
|
}
|
2008-12-29 14:04:51 +01:00
|
|
|
|
|
2022-01-06 19:48:59 +01:00
|
|
|
|
if (!write(ttyfd, "\n", (size_t)1))
|
|
|
|
|
bipmkpw_fatal("tty write failed", strerror(errno));
|
2008-12-29 14:04:51 +01:00
|
|
|
|
|
2005-07-12 21:31:59 +02:00
|
|
|
|
tcsetattr(ttyfd, TCSANOW, &ttback);
|
|
|
|
|
close(ttyfd);
|
2012-01-07 12:28:47 +01:00
|
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
|
fprintf(stderr, "Password cannot contain spaces.\n");
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
2005-07-12 21:31:59 +02:00
|
|
|
|
}
|
|
|
|
|
|
2005-08-01 13:24:10 +02:00
|
|
|
|
int main(void)
|
2005-04-28 10:26:44 +02:00
|
|
|
|
{
|
|
|
|
|
int i;
|
2005-07-12 21:31:59 +02:00
|
|
|
|
static char str[256];
|
2005-04-28 10:26:44 +02:00
|
|
|
|
unsigned char *md5;
|
|
|
|
|
unsigned int seed;
|
|
|
|
|
|
2005-07-12 21:31:59 +02:00
|
|
|
|
readpass(str, 256);
|
2008-12-29 14:04:51 +01:00
|
|
|
|
str[255] = 0;
|
2005-04-28 10:26:44 +02:00
|
|
|
|
|
2022-03-10 13:52:03 +01:00
|
|
|
|
// passing argument 1 of ‘srand’ with different width due to prototype
|
|
|
|
|
// [-Werror=traditional-conversion] conversion from ‘time_t’ {aka ‘long int’} to
|
|
|
|
|
// ‘unsigned int’ may change value [-Werror=conversion] We don't care.
|
2022-01-06 19:48:59 +01:00
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wtraditional-conversion"
|
|
|
|
|
#pragma GCC diagnostic ignored "-Wconversion"
|
2005-07-12 21:31:59 +02:00
|
|
|
|
// the time used to type the pass is entropy
|
|
|
|
|
srand(time(NULL));
|
2022-01-06 19:48:59 +01:00
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
|
seed = (unsigned)rand(); // rand should be > 0
|
2008-12-29 14:04:51 +01:00
|
|
|
|
|
2005-04-28 10:26:44 +02:00
|
|
|
|
md5 = chash_double(str, seed);
|
2022-03-10 13:52:03 +01:00
|
|
|
|
for (i = 0; i < 20; i++)
|
2005-04-28 10:26:44 +02:00
|
|
|
|
printf("%02x", md5[i]);
|
|
|
|
|
printf("\n");
|
|
|
|
|
free(md5);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|