File: authenticate.c

package info (click to toggle)
prayer 1.3.5-dfsg1-6
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,576 kB
  • sloc: ansic: 43,161; makefile: 817; sh: 445; perl: 166
file content (423 lines) | stat: -rw-r--r-- 11,482 bytes parent folder | download
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
/* $Cambridge: hermes/src/prayer/accountd/authenticate.c,v 1.6 2009/09/28 10:17:27 dpc22 Exp $ */
/************************************************
 *    Prayer - a Webmail Interface              *
 ************************************************/

/* Copyright (c) University of Cambridge 2000 - 2002 */
/* See the file NOTICE for conditions of use and distribution. */

#include "accountd.h"

#define HAVE_SHADOW 1

#include <pwd.h>
#if HAVE_SHADOW
#include <shadow.h>
#endif

/* ====================================================================== */

#ifdef RAVEN_ENABLE
static int
check_domain(char *a, char *b)
{
    int len = strlen(b);

    if (strncmp(a, b, len) != 0)
        return(0);

    /* Three possible terminators to host part of URL */
    if (a[len] == '/' || a[len] == ':' || a[len] == '\0')
        return(1);

    return(0);
}

static int
check_url(struct raven_wls_response *wls)
{
    char *s = wls->url;
    char *t;

    if (!strncmp(s, "https://", strlen("https://")))
        s += strlen("https://");
    else if (!strncmp(s, "http://", strlen("http://")))
        s += strlen("http://");
    else
        return(0);

    if ((t = strchr(s, '.')) && check_domain(t+1, "hermes.cam.ac.uk"))
        return(1);

    if (check_domain(s, "magenta.csi.cam.ac.uk") ||
        check_domain(s, "cyan.csi.cam.ac.uk"))
        return(1);

    return(0);
}

static int checkpassword_raven(struct config *config,
                               char *username, char *password)
{
    BOOL response = NIL;
    struct raven_wls_response *wls = raven_wls_response_parse
        ((char *)password, config->raven_key_path, NULL);

    if (!wls)
        return(NIL);

    if (!strcmp(wls->status, "200") &&
        !strcmp(wls->principal, username) &&
        check_url(wls) &&
        raven_wls_response_check_timestamp(wls,
                                           RAVEN_WLS_TICKET_MAXSKEW,
                                           RAVEN_WLS_TICKET_LIFETIME))
                                               
        response = T;

    raven_wls_response_free(wls);
    return(response);
}
#else
static int checkpassword_raven(struct config *config,
                               char *username, char *password)
{
    return(0);
}
#endif
/* ====================================================================== */

/* Code for authenticating user across iostream connection to client.
 * User interaction involving a root process: important that this code is
 * protected and properly verified! */

/* Definies a whole series of different authentication methods, including
 * PAM if PAM support configured in ../Config */

/* ====================================================================== */

#ifdef ACCOUNTD_PAM_ENABLE
#include <libgen.h>
#include <security/pam_appl.h>

/* checkpassword() code checks password using PAM library. Probably need
 * simple crypt and compare version as well */

static int
chkpwconv(int num_msg,
          struct pam_message **msg,
          struct pam_response **resp, void *appdata_ptr)
{
    char *password = (char *) appdata_ptr;
    const struct pam_message *m;
    struct pam_response *r;

    if (num_msg <= 0)
        return PAM_CONV_ERR;

    if (!(*resp = calloc(num_msg, sizeof(struct pam_response))))
        return PAM_BUF_ERR;

    m = *msg;
    r = *resp;
    if (m->msg_style == PAM_PROMPT_ECHO_OFF) {
        if (!(r->resp = strdup(password))) {
            free(*resp);
            *resp = 0;
            return PAM_BUF_ERR;
        }
    }
    return PAM_SUCCESS;
}

static int checkpassword_pam(char *username, char *password)
{
    pam_handle_t *pamh;
    struct pam_conv conv;
    int status;

    conv.conv = (int (*)()) chkpwconv;  /* Prevents hdr mismatch */
    conv.appdata_ptr = password;

    status = pam_start("accountd", username, &conv, &pamh);
    if (status == PAM_SUCCESS)
        status = pam_authenticate(pamh, 0);
    pam_end(pamh, status);

    return ((status == PAM_SUCCESS) ? T : NIL);
}
#else
static int checkpassword_pam(char *username, char *password)
{
    log_panic("PAM authentication selected but support not compiled in");
    return (NIL);
}
#endif

/* ====================================================================== */

/* Naive implementation: no support for expired passwords yet
 * (we don't use them)
 */

#if HAVE_SHADOW
static int checkpassword_spnam(char *username, char *password, int md5)
{
    struct spwd *spwd;
    char *crypted;
    int rc;

    if ((spwd = getspnam(username)) == NIL)
        return (NIL);

    if (md5) {
        char tmp[64];

        sprintf(tmp, "%s", spwd->sp_pwdp);
        tmp[11] = '\0';

        if ((crypted = crypt(password, tmp)) != NIL)
            rc = !strcmp(spwd->sp_pwdp, crypted) ? T : NIL;
        else
            rc = NIL;
    } else {
        if ((crypted = crypt(password, spwd->sp_pwdp)) != NIL)
            rc = !strcmp(spwd->sp_pwdp, crypted);
        else
            rc = NIL;
    }

    endspent();
    return (rc);
}
#endif

static int checkpassword_pwnam(char *username, char *password, int md5)
{
    struct passwd *pwd;
    char *crypted;
    int rc;

    if ((pwd = getpwnam(username)) == NIL)
        return (NIL);

    if (md5) {
        char tmp[64];

        sprintf(tmp, "$1$%s", pwd->pw_passwd);
        tmp[8] = '\0';

        if ((crypted = crypt(password, tmp)) != NIL)
            rc = !strcmp(pwd->pw_passwd, crypted) ? T : NIL;
        else
            rc = NIL;
    } else {
        if ((crypted = crypt(password, pwd->pw_passwd)) != NIL)
            rc = !strcmp(pwd->pw_passwd, crypted);
        else
            rc = NIL;
    }

    endpwent();
    return (rc);
}

/* ====================================================================== */

static void ucase(char *s)
{
    while (*s) {
        *s = Utoupper(*s);
        s++;
    }
}

/* authenticate() *********************************************************
 *
 * Authenticate login session and drop priveledges.
 *     config:  Global configuration
 *     stream:  iostream connection to client
 *   usernamep: Used to return username of logged in user
 *
 * Returns: T => successful login. NIL otherwise
 ************************************************************************/

BOOL
authenticate(struct config *config, struct iostream *stream,
            char **usernamep)
{
    char buffer[MAXLENGTH];
    char *username = NIL;
    char *password = NIL;
    struct passwd *pwd;
    char *cmd, *line;
    int rc = NIL;

    *usernamep = NIL;

    if (getuid() != 0)
        log_fatal("authenticate(): called without root priveledges");

    ioputs(stream, "* ");
    if (config->filter_restricted)
        ioputs(stream, "[FILTER_RESTRICTED] ");

    ioprintf(stream, "Prayer ACCOUNTD server %s" CRLF, VERSION);
    ioflush(stream);

    while (1) {
        if (!iostream_getline(stream, buffer, MAXLENGTH))
            return (NIL);

        if (buffer[0] == '\0')  /* Ignore empty lines */
            continue;

        line = buffer;
        if (!(cmd = string_get_token(&line))) {
            ioputs(stream, "BAD Invalid command" CRLF);
            ioflush(stream);
            continue;
        }
        ucase(cmd);

        if (!strcmp(cmd, "LOGOUT")) {
            ioputs(stream, "OK Logout" CRLF);
            ioflush(stream);
            return (NIL);
        }

        if (strcmp(cmd, "LOGIN") != NIL) {
            ioputs(stream, "BAD Invalid command" CRLF);
            ioflush(stream);
            continue;
        }

        if (!((username = string_get_token(&line)) && (username[0]))) {
            ioputs(stream, "BAD No username" CRLF);
            ioflush(stream);
            continue;
        }

        string_canon_decode(username);

        if (!((password = string_get_token(&line)) && (password[0]))) {
            ioputs(stream, "BAD No password" CRLF);
            ioflush(stream);
            continue;
        }

        string_canon_decode(password);

        /* Empty username/password already reported, just need to loop */
        if (!(username && username[0] && password && password[0]))
            continue;

        /* Only need to authenticate if root */
        if (config->raven_enable && (strlen(password) > 256)) {
            rc = checkpassword_raven(config, username, password);
        } else switch (config->authtype) {
        case AUTHTYPE_PAM:
            rc = checkpassword_pam(username, password);
            break;
        case AUTHTYPE_PWD:
            rc = checkpassword_pwnam(username, password, 0);
            break;
        case AUTHTYPE_PWD_MD5:
            rc = checkpassword_pwnam(username, password, 1);
            break;
#if HAVE_SHADOW
        case AUTHTYPE_SHADOW:
            rc = checkpassword_spnam(username, password, 0);
            break;
        case AUTHTYPE_SHADOW_MD5:
            rc = checkpassword_spnam(username, password, 1);
            break;
#endif
        default:
            log_fatal("Unsupported authentication type");
        }

        if (!rc) {
            ioputs(stream, "NO Incorrect authentication" CRLF);
            ioflush(stream);
            log_misc("Incorrect authentication for: %s", username);
            continue;
        }

        /* getpwnam() shouldn't fail if PAM authentication working properly */
        if ((pwd = getpwnam(username)) == NIL) {
            ioputs(stream, "NO Incorrect authentication" CRLF);
            ioflush(stream);
            log_misc("Incorrect authentication for: %s", username);
            continue;
        }

        if (pwd->pw_uid == 0) {
            ioputs(stream, "NO Root login disabled" CRLF);
            ioflush(stream);
            continue;
        }
        break;
    }

    setgid(pwd->pw_gid);        /* Change GID permanently for session */
    setuid(pwd->pw_uid);        /* Change UID permanently for session */

    if (chdir(pwd->pw_dir)) {
        ioputs(stream, "NO Can't select user home directory" CRLF);
        ioflush(stream);
        return (NIL);
    }

    ioputs(stream, "OK Session authenticated" CRLF);
    ioflush(stream);

    if (getuid() == 0)
        log_fatal("authenticate(): Ended up as root user!");

    *usernamep = strdup(username);
    return (T);
}

/* ====================================================================== */

/* authenticate_preauth() ************************************************
 *
 * Authenticate login session and drop priveledges.
 *     config:  Global configuration
 *     stream:  iostream connection to client
 *   usernamep: Used to return username of logged in user
 *
 * Returns: T => successful login. NIL otherwise
 ************************************************************************/

BOOL
authenticate_preauth(struct config * config,
                     struct iostream * stream, char **usernamep)
{
    struct passwd *pwd;

    if (getuid() == 0)
        log_fatal("authenticate_preauth(): called as root");

    if ((pwd = getpwuid(getuid())) == NIL)
        log_fatal("session(): getpwuid() failed");

    *usernamep = strdup(pwd->pw_name);

    ioputs(stream, "* ");
    if (config->filter_restricted)
        ioputs(stream, "[FILTER_RESTRICTED] ");

    ioprintf(stream, "PREAUTH Prayer ACCOUNTD server %s [user=%s]" CRLF,
             VERSION, *usernamep);
    ioflush(stream);

    if (chdir(pwd->pw_dir)) {
        ioputs(stream,
               "* Can't switch to PREAUTH user's home directory" CRLF);
        ioflush(stream);
        return (NIL);
    }
    return (T);
}