SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,637
Rep:
I searched for that some days ago, but although I have a strong hint I could not get it working - the computed output and the entry in /etc/shadow (generated from "passwd") still differ. openssl might be able to do this: http://www.linuxjournal.com/node/8958/print
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541
Rep:
Perhaps you mean something like this?
This program, cvspas, is used to generate the password entry for CVS; it prompts for the password and prints the login name of the person running the program along with the encrypted password.
The output is, for example,
Code:
# cvspas
Password to encrypt: type_the_password
userid:KeGoQCkfp7XYw
Compile it with
Code:
cc -O3 -s -o cvspas cvspas.c -lcrypt
Here's the source.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <time.h>
#define _XOPEN_SOURCE
#include <unistd.h>
extern char *crypt (const char *, const char *);
int main (int argc, char *argv [])
{
char salt [3];
char *passwd, *encryptedpw;
char *user;
int i;
/* seed the random number generator */
srand ((int) time ((unsigned int) NULL));
/*
* we need two random numbers in the range
* >= 65 <= 90 or >= 97 <= 122 (that's A - Z
* or a - z inclusive) for the salt characters
*/
while ((i = rand()) < 65 ||
i > 90 && i < 97 ||
i > 122)
;
salt [0] = i;
while ((i = rand()) < 65 ||
i > 90 && i < 97 ||
i > 122)
;
salt [1] = i;
salt [2] = '\0';
/* find out who we are */
if ((user = getenv ("USER")) == (char *) NULL) {
(void) fprintf (stderr,
"%s:\tunable to determine user id\n",
argv [0]);
exit (EXIT_FAILURE);
}
/* ask for the password */
passwd = getpass ("Password to encrypt: ");
/* crypt() only looks at the first two characters of salt */
encryptedpw = crypt (passwd, salt);
(void) fprintf (stdout, "%s:%s\n", user, encryptedpw);
exit (EXIT_SUCCESS);
}
Now, this is just a demonstration program -- the use of the rand() function to generate the salt characters is not the most efficient way to that and, consequently, the program takes a few seconds before the password prompt appears. But, it does produce a usable encrypted password so there you are.
See the manual pages for the functions the program calls if you want more information.
Distribution: Slackware64 14.2 and current, SlackwareARM current
Posts: 1,637
Rep:
It's not exactly a one-liner, although you can of course append the lines with ";". And it's not the best method I guess.
I export first some variables, a better choice I guess would to put the lines in a bash script and use parameters $1 $2 and $3 instead of the static export lines.
Code:
export SET_USER=username
export SET_SALT=3gZekj8m # should be random of course
export SET_PASSWD=mypassword
export ENC_PASSWD=$(openssl passwd -1 -salt $SET_SALT $SET_PASSWD)
useradd -g users -G video,cdrom,floppy -m -p "$ENC_PASSWD" -s /bin/bash $SET_USER
# clean up, not necessary if called from script
unset SET_USER SET_SALT SET_PASSWD ENC_PASSWD
EDIT: I assumed you might want to use it in a bash script to automate the process of creating a user. If you don't need this you could simplify my script and write all commands in one line:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.