LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   adding users and passwords with adduser (https://www.linuxquestions.org/questions/programming-9/adding-users-and-passwords-with-adduser-602588/)

sean04 11-26-2007 08:33 PM

adding users and passwords with adduser
 
I'm trying to create a new user with a password via script file and I'm using the -p option for the password but, I need to give it the crypted password as returned by crypt() function. This is what is says in the man pages for adduser.

The problem is in my script the crypt command is not found. Therefore I cannot create a crypted password.

Help please! This is in RHEL 4.

druuna 11-27-2007 01:23 PM

Hi,

crypt() is not a function that you can use in bash, it is a c(++) function. If you want to use it you need to write a little c (or c++) script around it. After compiling you'll end up with a binary that can produce the encrypted password needed by useradd's -p option.

This rough example creates md5 encrypted passwords:
Code:

#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>

int main (int argc, char* argv[])
{
  char *result;

  printf ("%s\n", crypt (argv[1], "$1$"));

  return (0);
}

/*  gcc -lcrypt md5.crypt.c -o md5.crypt  */

Save it as md5.crypt.c . After compiling it with: gcc -lcrypt md5.crypt.c -o md5.crypt, you'll end up with md5.crypt

An example run:
Code:

$ ./md5.crypt FooBar
$1$$yNWeilBBQ6tKlG4nsnV9W.

Hope this helps.

matthewg42 11-27-2007 04:38 PM

You could use the makepasswd program (you will probably need to install it first):
Code:

echo "mypassword" | makepasswd --clearfrom=- --crypt-md5 |awk '{ print $2 }'
Weird that so many users are suddenly asking this question, or one very like it. Is this some sort of assignment, or are you asking the same question logged in as multiple users?

1300 03-12-2012 03:11 AM

Quote:

Originally Posted by druuna (Post 2972426)
Hi,

crypt() is not a function that you can use in bash, it is a c(++) function. If you want to use it you need to write a little c (or c++) script around it. After compiling you'll end up with a binary that can produce the encrypted password needed by useradd's -p option.

This rough example creates md5 encrypted passwords:
Code:

#define _XOPEN_SOURCE
#include <stdio.h>
#include <unistd.h>

int main (int argc, char* argv[])
{
  char *result;

  printf ("%s\n", crypt (argv[1], "$1$"));

  return (0);
}

/*  gcc -lcrypt md5.crypt.c -o md5.crypt  */

Save it as md5.crypt.c . After compiling it with: gcc -lcrypt md5.crypt.c -o md5.crypt, you'll end up with md5.crypt

An example run:
Code:

$ ./md5.crypt FooBar
$1$$yNWeilBBQ6tKlG4nsnV9W.

Hope this helps.

sample:
useradd -g addm -d /home/bbd -m -c "ADDM user" -p '$1$$obKSzVJoc/mJ1ozCv.5/z.' -s /bin/bash bbd


All times are GMT -5. The time now is 09:28 PM.