aboutsummaryrefslogtreecommitdiff
path: root/src/auth.c
blob: c7b53d36261e3cc3d09e06d9bbc99bbb583be97a (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 *   NetCosm - a MUD server
 *   Copyright (C) 2016 Franklin Wei
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "globals.h"

#include "auth.h"
#include "client.h"
#include "userdb.h"

static bool valid_login_name(const char *name);

/* returns a pointer to a malloc-allocated buffer containing the salted hex hash of pass */
/* salt should point to a buffer containing SALT_LEN random characters */
/* pass must be null-terminated */
static char *hash_pass_hex(const char *pass, const char *salt)
{
    size_t pass_len = strlen(pass);

    unsigned char *salted = malloc(pass_len + SALT_LEN + 1);
    memcpy(salted, salt, SALT_LEN);
    memcpy(salted + SALT_LEN, pass, pass_len);
    salted[pass_len + SALT_LEN] = '\0';

    unsigned char hash[AUTH_HASHLEN];

    SHA512(salted, pass_len + SALT_LEN, hash);

    unsigned char tmp[AUTH_HASHLEN];
    /* now hash the hash a million times to slow things down */
    for(int i = 0; i < (HASH_ITERS / 2) - 1; ++i)
    {
        AUTH_HASHFUNC(hash, AUTH_HASHLEN, tmp);
        AUTH_HASHFUNC(tmp, AUTH_HASHLEN, hash);
    }
    memset(tmp, 0, sizeof(tmp));

    memset(salted, 0, pass_len + SALT_LEN + 1);
    free(salted);

    /* convert to hex */
    char *hex = malloc(AUTH_HASHLEN * 2 + 1);
    char *ptr = hex;
    for(unsigned int i = 0; i < AUTH_HASHLEN; ++i, ptr += 2)
        snprintf(ptr, 3, "%02x", hash[i]);
    hex[AUTH_HASHLEN * 2] = '\0';

    return hex;
}

static void add_user_internal(const char *name, const char *pass, int authlevel)
{
    char salt[SALT_LEN + 1];
    for(int i = 0; i < SALT_LEN; ++i)
    {
        salt[i] = 'A' + rand()%26;
    }
    salt[SALT_LEN] = '\0';

    char *hexhash = hash_pass_hex(pass, salt);

    /* doesn't need to be malloc'd */
    struct userdata_t userdata;
    memset(&userdata, 0, sizeof(userdata));

    strncpy(userdata.username, name, sizeof(userdata.username));
    memcpy(userdata.passhash, hexhash, sizeof(userdata.passhash));

    free(hexhash);

    userdata.priv = authlevel;
    userdata.last_login = time(0);
    memcpy(userdata.salt, salt, sizeof(salt));

    userdb_request_add(&userdata);
}

bool auth_user_del(const char *user2)
{
    char *user = strdup(user2);
    remove_cruft(user);
    if(valid_login_name(user))
        return userdb_request_remove(user);
    else
    {
        free(user);
        return false;
    }
}

bool auth_user_add(const char *user2, const char *pass2, int level)
{
    char *user = strdup(user2);
    remove_cruft(user);
    char *pass = strdup(pass2);
    remove_cruft(pass);

    debugf("Add user '%s'\n", user);

    if(!valid_login_name(user))
    {
        free(user);
        free(pass);
        return false;
    }

    add_user_internal(user, pass, level);

    free(user);
    memset(pass, 0, strlen(pass));
    free(pass);

    return true;
}

static bool valid_login_name(const char *name)
{
    size_t len = 0;
    while(*name)
    {
        char c = *name++;
        ++len;
        if(len > MAX_NAME_LEN)
            return false;
        if(!(isalpha(c) || isdigit(c)))
            return false;
    }
    return true;
}

void first_run_setup(void)
{
    debugf("Welcome to NetCosm!\n");
    debugf("Please set up the administrator account now.\n");

    char *admin_name;
    size_t len = 0;
    do {
        admin_name = NULL;
        debugf("Admin account name: ");
        fflush(stdout);
        getline(&admin_name, &len, stdin);
        remove_cruft(admin_name);
    } while(!valid_login_name(admin_name));

    debugf("Admin password (_DO_NOT_ USE A VALUABLE PASSWORD): ");
    fflush(stdout);
    char *admin_pass = NULL;
    len = 0;
    getline(&admin_pass, &len, stdin);
    remove_cruft(admin_pass);

    if(!auth_user_add(admin_name, admin_pass, PRIV_ADMIN))
        error("Unknown error");

    /* zero the memory */
    memset(admin_name, 0, strlen(admin_name));
    memset(admin_pass, 0, strlen(admin_pass));
    free(admin_name);
    free(admin_pass);
}

struct userdata_t *auth_check(const char *name2, const char *pass2)
{
    /* get our own copy to remove newlines */
    char *name = strdup(name2);
    char *pass = strdup(pass2);
    remove_cruft(name);
    remove_cruft(pass);

    /* find it in the user list */
    struct userdata_t *data = userdb_request_lookup(name);

    free(name);

    char *salt = data->salt, *hash = data->passhash;

    if(data)
    {
        debugf("auth module: user %s found\n", name2);

        /* hashes are in lowercase hex to avoid the Trucha bug
         * but still allow comparison with strcmp() */
        char *new_hash_hex = hash_pass_hex(pass, salt);

        bool success = true;
        /* constant-time comparison to hopefully prevent a timing attack */
        for(int i = 0; i < AUTH_HASHLEN; ++i)
        {
            if(new_hash_hex[i] != hash[i])
                success = false;
        }

        free(new_hash_hex);

        if(success)
        {
            memset(pass, 0, strlen(pass));
            free(pass);

            return data;
        }
    }

    debugf("auth failure for user %s\n", name2);

    memset(pass, 0, strlen(pass));
    free(pass);

    /* failure */
    sleep(2);
    return NULL;
}