/* Copyright (c) 2003, Rene Luria aka herel <rene@luria.ch>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright notice,
 *     * this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *     * notice, this list of conditions and the following disclaimer in the
 *     * documentation and/or other materials provided with the distribution.
 *     * Neither the name of the author nor the names of its
 *     * contributors may be used to endorse or promote products derived from
 *     * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE. */

/* compile avec -lcrypt:
    gcc -o makepasswd makepasswd.c -lcrypt
*/
#define _XOPEN_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

char *make_password (char *orig);
char *crsalt (char *salt);

    int
main (int argc, char **argv)
{
    /* un argument: le mot de passe */
    if (argc != 2)
    {
        printf ("Usage: ./make_passwd <pass>\n");
        return (1);
    }

    argc--;

    printf("%s\n", make_password(*(++argv)));

    return (0);
}

/* Génération du password */
    char *
make_password (char *orig)
{
    char *salt = NULL, *passwd;

    /* on créé le salt */
    if ((salt = crsalt (salt)) == NULL)
        return (NULL);

    /* le pass crypté */
    passwd = (char *) crypt (orig, salt);
    free (salt);

    if (passwd == NULL)
        return (NULL);

    return (passwd);
}

    char *
crsalt (char *salt)
{
    int i, j;
    char *psalt;

    if ((salt = malloc (13)) == NULL)
    {
        perror ("malloc salt");
        return (NULL);
    }

    psalt = salt;

    srand (time (NULL) + clock ());
    salt = strcpy (salt, "$1$");
    psalt += 3;
    for (i = 0; i < 8; i++)
    {
        j = 1 + (int) (63.0 * rand () / (RAND_MAX + 1.0));

        if (j <= 11)
            j += 46;
        else if (j <= 37)
            j += 53;
        else
            j += 59;
        *psalt = j;
        psalt++;
    }

    *psalt = '$';
    psalt++;
    *psalt = '\0';

    return (salt);
}