Join The Austin Linux Meetup Group

You'll get invited to our Meetups as soon as they're scheduled!

The Austin Linux Meetup Group Message Board › Utility to prompt user to enter password (text-mode, shell script)

Utility to prompt user to enter password (text-mode, shell script)

puddles
Posted May 4, 2009 11:10 PM
user 9248335
Austin, TX
Post #: 5
Send an Email Post a Greeting
#include <newt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char *passphrase = NULL;

int main (int argc, char *argv[])
{
newtComponent form, ok_button, cancel_button, text_box, passphrase_entry, which_one;
const char *passphrase_ptr;
newtInit();
newtCls();
newtCenteredWindow(45,12, "Enter Passphrase for access");
ok_button = newtButton( 21, 7, "OK");
cancel_button = newtButton(31, 7, "Cancel");
text_box = newtTextbox(2, 1, 43, 2, NEWT_FLAG_WRAP);
newtTextboxSetText(text_box, "The resource you are trying to access is protected by a passphrase.");
passphrase_entry = newtEntry(2, 4, "", 43, &passphrase_ptr, NEWT_FLAG_PASSWORD | NEWT_FLAG_SCROLL | NEWT_FLAG_RETURNEXIT);
form = newtForm(NULL, NULL, 0);
newtFormAddComponents(form, text_box, passphrase_entry, ok_button, cancel_button, NULL);
which_one = newtRunForm(form);
if ((which_one == ok_button) || (which_one == passphrase_entry)) {
if (passphrase_ptr == NULL) {
passphrase_ptr = newtEntryGetValue(passphrase_entry);
}
if ((passphrase_ptr) && (*passphrase_ptr != '\0')) passphrase = strdup(passphrase_ptr);
}
newtFormDestroy(form);
newtFinished();
if (passphrase) {
fprintf(stderr,"%s",passphrase);
return 0;
}
return 1;
}

Cut & paste the above to a file, then compile it.

The differences between using the above program vs "whiptail --passwordbox" or "dialog" are:

(1) if user enters an empty password or press [ OK ] button without entering password, the exitcode will be 1 (same as pressing [ CANCEL ]. This avoids having to test for empty passwords in your script explicitly.

(2) you get a nice little message above the passwordbox widget


Use it like this:

secret_passwd=`getpassphrase 2>&1 >/dev/tty`
if [ $? -eq 1 ]; then
echo "You really should enter a password"
exit 1
fi
[ ... more code here ... ]
Powered by mvnForum
Organized by
Asst. Organizers:

Our Sponsors

Austin Community College

Offering a class on "How to Get the Most from Linux"

Austin CMS

..seminars to organizations and businesses..about open-source technology

Potbelly Sandwich Works

Excellent Sandwiches in a fun environment.