aboutsummaryrefslogtreecommitdiff
path: root/passgen.c
blob: aaa2e91e4cba8b529d19676ac3341883ccd59f24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#if    defined (__linux__) \
    || defined (__APPLE__) \
    || defined (__FreeBSD__) \
    || defined (__OpenBSD__)
#   define USE_GETENTROPY 1
#endif

/* getentropy() vs rand()+time()+getpid() */
#ifdef USE_GETENTROPY
#if defined (__linux__) || defined (__APPLE__)
#    include <sys/random.h>
#else
#    include <unistd.h>
#endif
#else
#include <time.h>

/* getpid() on Windows */
#if defined (_WIN32) && ! defined (__MINGW32__)
#include <io.h>
#else
#include <unistd.h>
#endif

#endif


#define CLASS(c, chars) \
    case c: \
        class = chars; \
        class_size = sizeof(chars)-1; \
        break;


void init_rng(void)
{
    /* For now, this is only needed on rand() */
#ifndef USE_GETENTROPY
    /*
     * TODO: seed better RNG
     * this isn't very good, but it's enough(?) for now
     *
     * on linux we use the `getrandom` api which is supposedly
     * what crypto uses to generate. potentially on win we
     * can use the crypto thing (but it's more complicated
     * to invoke).
     *
     * anything else we could use on posix systems?
     */
    srand(time(NULL) + getpid() % 420 - 69);
#endif
}

unsigned int get_rng(void)
{
#ifdef USE_GETENTROPY
    unsigned int r;
    getentropy(&r, sizeof(r));
    return r;
#else
    return rand();
#endif
}

char *build_grammar(int triplets, int specials, int numbers)
{
    if (triplets < 1) {
        fprintf(stderr, "ERROR: Cannot have less than one triplet.");
        return NULL;
    }

    int grammar_size = triplets * 3 + specials + numbers;
    char *build = malloc(grammar_size + 1);
    if (build == NULL) {
        perror("malloc");
        return NULL;
    }
    build[grammar_size] = 0;

    memcpy(build, "Cvc", 3);
    for (int i = 1; i < triplets; ++i)
        memcpy(build + (i * 3), "cvc", 3);

    memset(build + (triplets * 3), '!', specials);
    memset(build + (triplets * 3) + specials, '#', numbers);
    return build;
}

int main(int argc, char *argv[])
{
    bool err = false;
    bool custom_grammar = false;
    char *grammar = DEFAULT_GRAMMAR;
    int grammar_size = sizeof(DEFAULT_GRAMMAR) - 1;

    if (argc == 2) {
        /* Take first argument as the grammar */
        grammar = argv[1];
        grammar_size = strlen(grammar);
    } else if (argc == 4) {
        /* 
         * Take arguments as triplets, specials, numbers
         * atoi might be scuffed but so be it (it just goes = 0 if invalid input)
         */
        grammar = build_grammar(atoi(argv[1]), atoi(argv[2]), atoi(argv[3]));
        if (grammar == NULL) {
            err = true;
            goto cleanup;
        }
        custom_grammar = true;
    }

    char *password = malloc(grammar_size + 1);
    if (password == NULL) {
        perror("malloc");
        err = true;
        goto cleanup;
    }
    password[grammar_size] = 0;

    init_rng();

    for (int i = 0; i < grammar_size; ++i) {
        char c = grammar[i];

        bool caps = false;
        if (c >= 'A' && c <= 'Z') {
            caps = true;
            c += 'a' - 'A';
        }

        char *class;
        int class_size = 0;
        switch (c) {
            CLASSES
        default:
            fprintf(stderr, "ERROR: Invalid grammar character '%c'.\n", c);
            err = true;
            goto cleanup;
        }

        do {
            password[i] = class[get_rng() % class_size] - (caps ? 'a' - 'A' : 0);
        } while (i != 0 && password[i] == password[i - 1]);
    }

    printf("%s\n", password);

cleanup:
    if (custom_grammar)
        free(grammar);
    if (password)
        free(password);

    if (err)
        return 1;

    return 0;
}