View Full Version : [C] utilizzo delle pw
Ciao a tutti, ho un problema!
Ho bisogno di fare in modo che inserendo una password da terminale non sia possibile visualizzarla mentre la si sta inserendo.
L'unica funzione che ho trovato a tale fine è getpass() che fa parte della libreria crypt.h.
Il problema di questa funzione è che genera un buffer overflow visto che non c'è nessun controllo sull'input.
Qualcuno ha qualche soluzione???
Come posso creare in C una funzione che non faccia visualizzare la pw a terminale???
Grazie
Claudia
Gino+89+
26-09-2006, 16:05
Prova a vedere questo:
#include <stdio.h>
#include <conio.h>
int main()
{
int i;
char car;
char password[20];
printf("Inserisci password:");
for(i=0;i<20;i++)
{
car=getch();
password[i]=car;
if(car==13)
break;
else
printf("*");
}
password[i]='\0';
printf("\n\nPassword inserita: %s",password);
getch();
return 0;
}
ilsensine
26-09-2006, 16:27
Ricordo di una funzione, ma non ricordo quale...
Puoi fare manualmente qualcosa del genere:
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <sys/mman.h>
#include <unistd.h>
char *getpass(const char *prompt)
{
struct termios tios;
struct termios torg;
char *buf = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
char *ret;
if (mlock(buf, getpagesize())<0)
fprintf(stderr, "Aehm... ;-)\n");
tcgetattr(fileno(stdin), &tios);
torg = tios;
tios.c_lflag &= ~ECHO;
tcsetattr(fileno(stdin), TCSAFLUSH, &tios);
printf("%s", prompt);
fflush(stdout);
ret = fgets(buf, getpagesize(), stdin);
printf("\n");
tcsetattr(fileno(stdin), TCSAFLUSH, &torg);
if (ret!=NULL) {
int len = strnlen(buf, getpagesize());
if (len==getpagesize()) {
fprintf(stderr, "Error: password too long\n");
munmap(buf, getpagesize());
ret = NULL;
} else {
// Elimina il '\n' finale
if (len>1)
ret[len-1] = '\0';
}
}
return ret;
}
int main()
{
char *p = getpass("Enter password: ");
printf("Password: %s\n", p);
munmap(p, getpagesize());
return 0;
}
trallallero
27-09-2006, 09:32
infatti dal man getpass
...
This function is obsolete. Do not use it.
...
che sistema usi ? Linux se non ricordo male ...
OT: a proposito com'é finita la storia delle librerie ? ha funzionato ? non m'hai fatto sapere piú niente ... /OT
Come giá t'ha postato ilsensine dovresti usare i termios ... é un po lunga da spiegare.
Questa funzione abilita/disabilita l'echo del carattere premuto tramite un flag della struttura termios:
/* *\
\* */
int EchoOnOff(int On)
{
struct termios attr;
if(!(isatty)(STDIN_FILENO))
return IOERROR;
if((tcgetattr)(STDIN_FILENO,&attr) < 0)
return IOERROR;
if (On)
attr.c_lflag &= ~ECHO; // DISABILITA L'ECHO
else
attr.c_lflag |= ECHO; // ABILITA L'ECHO
if((tcsetattr)(STDIN_FILENO,TCSAFLUSH,&attr) < 0)
return IOERROR;
return 0;
}
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.