summaryrefslogtreecommitdiff
path: root/server/responder/pam/pamsrv_cache.c
blob: 9c5c209f2f8a50c563f2c1f8b5087df1e7a9d1a0 (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
/*
   SSSD

   PAM cache credentials

   Copyright (C) Simo Sorce <ssorce@redhat.com>	2009
   Copyright (C) Sumit Bose <sbose@redhat.com>	2009

   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 <security/pam_modules.h>
#include <time.h>
#include "util/util.h"
#include "db/sysdb.h"
#include "util/nss_sha512crypt.h"
#include "providers/data_provider.h"
#include "responder/pam/pamsrv.h"

static int authtok2str(const void *mem_ctx,
                       uint8_t *src, const int src_size,
                       char **_dest)
{
    char *dest;

    if ((src == NULL && src_size != 0) ||
        (src != NULL && *src != '\0' && src_size == 0)) {
        return EINVAL;
    }

    dest = talloc_size(mem_ctx, src_size + 1);
    if (dest == NULL) {
        return ENOMEM;
    }

    memcpy(dest, src, src_size);
    dest[src_size]='\0';

    *_dest = dest;
    return EOK;
}

static void pam_cache_auth_return(struct pam_auth_req *preq, int error)
{
    preq->pd->pam_status = error;
    preq->callback(preq);
}

static void pam_cache_auth_callback(void *pvt, int ldb_status,
                                    struct ldb_result *res)
{
    struct pam_auth_req *preq;
    struct pam_data *pd;
    const char *userhash;
    char *comphash;
    char *password = NULL;
    int i, ret;

    preq = talloc_get_type(pvt, struct pam_auth_req);
    pd = preq->pd;

    if (ldb_status != LDB_SUCCESS) {
        DEBUG(4, ("User info retireval failed! (%d [%s])\n",
                  ldb_status, sysdb_error_to_errno(ldb_status)));

        ret = PAM_SYSTEM_ERR;
        goto done;
    }

    if (res->count == 0) {
        DEBUG(4, ("User [%s@%s] not found.\n",
                  pd->user, preq->domain->name));
        ret = PAM_USER_UNKNOWN;
        goto done;
    }

    if (res->count != 1) {
        DEBUG(4, ("Too manyt results for user [%s@%s].\n",
                  pd->user, preq->domain->name));
        ret = PAM_SYSTEM_ERR;
        goto done;
    }

    /* TODO: verify user account (failed logins, disabled, expired ...) */

    ret = authtok2str(preq, pd->authtok, pd->authtok_size, &password);
    if (ret) {
        DEBUG(4, ("Invalid auth token.\n"));
        ret = PAM_AUTH_ERR;
        goto done;
    }

    userhash = ldb_msg_find_attr_as_string(res->msgs[0], SYSDB_CACHEDPWD, NULL);
    if (userhash == NULL || *userhash == '\0') {
        DEBUG(4, ("Cached credentials not available.\n"));
        ret = PAM_AUTHINFO_UNAVAIL;
        goto done;
    }

    ret = s3crypt_sha512(preq, password, userhash, &comphash);
    if (ret) {
        DEBUG(4, ("Failed to create password hash.\n"));
        ret = PAM_SYSTEM_ERR;
        goto done;
    }

    if (strcmp(userhash, comphash) == 0) {
        /* TODO: probable good point for audit logging */
        DEBUG(4, ("Hashes do match!\n"));
        ret = PAM_SUCCESS;
        goto done;
    }

    DEBUG(4, ("Authentication failed.\n"));
    ret = PAM_AUTH_ERR;

done:
    if (password) for (i = 0; password[i]; i++) password[i] = 0;
    pam_cache_auth_return(preq, ret);
}

int pam_cache_auth(struct pam_auth_req *preq)
{
    struct sysdb_ctx *sysdb;
    int ret;

    static const char *attrs[] = {SYSDB_NAME,
                                  SYSDB_CACHEDPWD,
                                  SYSDB_DISABLED,
                                  SYSDB_LAST_LOGIN,
                                  "lastCachedPasswordChange",
                                  "accountExpires",
                                  "failedLoginAttempts",
                                  "lastFailedLogin",
                                  NULL};

    ret = sysdb_get_ctx_from_list(preq->cctx->rctx->db_list,
                                  preq->domain, &sysdb);
    if (ret != EOK) {
        DEBUG(0, ("Fatal: Sysdb CTX not found for this domain!\n"));
        return ret;
    }
    ret = sysdb_get_user_attr(preq, sysdb,
                              preq->domain, preq->pd->user, attrs,
                              pam_cache_auth_callback, preq);

    if (ret != EOK) {
        DEBUG(2, ("sysdb_get_user_attr failed.\n"));
    }

    return ret;
}