summaryrefslogtreecommitdiff
path: root/sss_client/pam_sss.c
blob: d03407c929181645d01ad5e0e563677bb7ce57b4 (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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

#define PAM_SM_AUTH
#define PAM_SM_ACCOUNT
#define PAM_SM_SESSION
#define PAM_SM_PASSWORD

#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdint.h>
#include <security/pam_modules.h>
#include <security/pam_misc.h>

#include "sss_cli.h"

struct pam_items {
    const char* pam_service;
    const char* pam_user;
    const char* pam_tty;
    const char* pam_ruser;
    const char* pam_rhost;
    const char* pam_authtok;
    const char* pam_newauthtok;
    const char* pamstack_authtok;
    const char* pamstack_oldauthtok;
    int pam_service_size;
    int pam_user_size;
    int pam_tty_size;
    int pam_ruser_size;
    int pam_rhost_size;
    int pam_authtok_type;
    int pam_authtok_size;
    int pam_newauthtok_type;
    int pam_newauthtok_size;
};

static int eval_response(pam_handle_t *pamh, int buflen, uint8_t *buf)
{
    int ret;
    int p=0;
    char *env_item;
    int32_t *c;
    int32_t *type;
    int32_t *len;
    int32_t *pam_status;

    pam_status = ((int32_t *)(buf+p));
    p += sizeof(int32_t);


    c = ((int32_t *)(buf+p));
    p += sizeof(int32_t);

    while(*c>0) {
        type = ((int32_t *)(buf+p));
        p += sizeof(int32_t);
        len = ((int32_t *)(buf+p));
        p += sizeof(int32_t);
        switch(*type) {
            case PAM_USER_INFO:
                D(("user info: [%s]", &buf[p]));
                break;
            case PAM_DOMAIN_NAME:
                D(("domain name: [%s]", &buf[p]));
                break;
            case ENV_ITEM:
            case PAM_ENV_ITEM:
            case ALL_ENV_ITEM:
                if (buf[p + (*len -1)] != '\0') {
                    D(("env item does not end with \\0.\n"));
                    break;
                }

                D(("env item: [%s]", &buf[p]));
                if (*type == PAM_ENV_ITEM || *type == ALL_ENV_ITEM) {
                    ret = pam_putenv(pamh, (char *)&buf[p]);
                    if (ret != PAM_SUCCESS) {
                        D(("pam_putenv failed.\n"));
                        break;
                    }
                }

                if (*type == ENV_ITEM || *type == ALL_ENV_ITEM) {
                    env_item = strdup((char *)&buf[p]);
                    if (env_item == NULL) {
                        D(("strdup failed\n"));
                        break;
                    }
                    ret = putenv(env_item);
                    if (ret == -1) {
                        D(("putenv failed.\n"));
                        break;
                    }
                }
                break;
        }
        p += *len;

        --(*c);
    }

    return 0;
}

static int get_pam_items(pam_handle_t *pamh, struct pam_items *pi)
{
    int ret;

    ret = pam_get_item(pamh, PAM_SERVICE, (const void **) &(pi->pam_service));
    if (ret != PAM_SUCCESS) return ret;
    if (pi->pam_service == NULL) pi->pam_service="";
    pi->pam_service_size=strlen(pi->pam_service)+1;

    ret = pam_get_item(pamh, PAM_USER, (const void **) &(pi->pam_user));
    if (ret != PAM_SUCCESS) return ret;
    if (pi->pam_user == NULL) pi->pam_user="";
    pi->pam_user_size=strlen(pi->pam_user)+1;

    ret = pam_get_item(pamh, PAM_TTY, (const void **) &(pi->pam_tty));
    if (ret != PAM_SUCCESS) return ret;
    if (pi->pam_tty == NULL) pi->pam_tty="";
    pi->pam_tty_size=strlen(pi->pam_tty)+1;

    ret = pam_get_item(pamh, PAM_RUSER, (const void **) &(pi->pam_ruser));
    if (ret != PAM_SUCCESS) return ret;
    if (pi->pam_ruser == NULL) pi->pam_ruser="";
    pi->pam_ruser_size=strlen(pi->pam_ruser)+1;

    ret = pam_get_item(pamh, PAM_RHOST, (const void **) &(pi->pam_rhost));
    if (ret != PAM_SUCCESS) return ret;
    if (pi->pam_rhost == NULL) pi->pam_rhost="";
    pi->pam_rhost_size=strlen(pi->pam_rhost)+1;

    ret = pam_get_item(pamh, PAM_AUTHTOK, (const void **) &(pi->pamstack_authtok));
    if (ret != PAM_SUCCESS) return ret;
    if (pi->pamstack_authtok == NULL) pi->pamstack_authtok="";

    ret = pam_get_item(pamh, PAM_OLDAUTHTOK, (const void **) &(pi->pamstack_oldauthtok));
    if (ret != PAM_SUCCESS) return ret;
    if (pi->pamstack_oldauthtok == NULL) pi->pamstack_oldauthtok="";

    return PAM_SUCCESS;
}

static void print_pam_items(struct pam_items pi)
{
    D(("Service: %s", *pi.pam_service!='\0' ? pi.pam_service : "(not available)"));
    D(("User: %s", *pi.pam_user!='\0' ? pi.pam_user : "(not available)"));
    D(("Tty: %s", *pi.pam_tty!='\0' ? pi.pam_tty : "(not available)"));
    D(("Ruser: %s", *pi.pam_ruser!='\0' ? pi.pam_ruser : "(not available)"));
    D(("Rhost: %s", *pi.pam_rhost!='\0' ? pi.pam_rhost : "(not available)"));
    D(("Authtok: %s", *pi.pamstack_authtok!='\0' ? pi.pamstack_authtok : "(not available)"));
    D(("Oldauthtok: %s", *pi.pamstack_oldauthtok!='\0' ? pi.pamstack_oldauthtok : "(not available)"));
}

static int pam_sss(int task, pam_handle_t *pamh, int flags, int argc,
                   const char **argv)
{
    int ret;
    int errnop;
    int c;
    struct pam_items pi;
    struct sss_cli_req_data rd;
    uint8_t *repbuf=NULL;
    size_t replen;
    size_t rp;
    char *buf=NULL;
    struct pam_conv *conv;
    struct pam_message *mesg[1];
    struct pam_response *resp=NULL;
    int pam_status;
    char *newpwd[2];
    int forward_pass = 0;

    D(("Hello pam_sssd: %d", task));

    for (; argc-- > 0; ++argv) {
        if (strcmp(*argv, "forward_pass") == 0) {
            forward_pass = 1;
        } else {
            D(("unknown option: %s", *argv));
        }
    }

/* TODO: add useful prelim check */
    if (task == SSS_PAM_CHAUTHTOK && (flags & PAM_PRELIM_CHECK)) {
        D(("ignoring PAM_PRELIM_CHECK"));
        return PAM_SUCCESS;
    }

    ret = get_pam_items(pamh, &pi);
    if (ret != PAM_SUCCESS) {
        D(("get items returned error: %s", pam_strerror(pamh,ret)));
        return ret;
    }

    pi.pam_authtok_type = SSS_AUTHTOK_TYPE_EMPTY;
    pi.pam_authtok = NULL;
    pi.pam_authtok_size = 0;
    pi.pam_newauthtok_type = SSS_AUTHTOK_TYPE_EMPTY;
    pi.pam_newauthtok = NULL;
    pi.pam_newauthtok_size = 0;
/* according to pam_conv(3) only one message should be requested by conv to
 * keep compatibility to Solaris. Therefore we make separate calls to request
 * AUTHTOK and OLDAUTHTOK. */
    if ( task == SSS_PAM_AUTHENTICATE ||
        (task == SSS_PAM_CHAUTHTOK && getuid() != 0)) {
        ret=pam_get_item(pamh, PAM_CONV, (const void **) &conv);
        if (ret != PAM_SUCCESS) return ret;

        mesg[0] = malloc(sizeof(struct pam_message));
        if (mesg[0] == NULL) {
            D(("Malloc failed.\n"));
            return PAM_SYSTEM_ERR;
        }
        mesg[0]->msg_style = PAM_PROMPT_ECHO_OFF;
        mesg[0]->msg = strdup("Password: ");

        ret=conv->conv(1, (const struct pam_message **) mesg, &resp,
                       conv->appdata_ptr);
        free((void *)mesg[0]->msg);
        free(mesg[0]);
        if (ret != PAM_SUCCESS) {
            D(("Conversation failure: %s.\n",  pam_strerror(pamh,ret)));
            pam_status = ret;
            goto done;
        }

        if (resp[0].resp == NULL) {
            D(("Empty password\n"));
            pi.pam_authtok = NULL;
            pi.pam_authtok_type = SSS_AUTHTOK_TYPE_EMPTY;
        } else {
            pi.pam_authtok = strdup(resp[0].resp);
            pi.pam_authtok_type = SSS_AUTHTOK_TYPE_PASSWORD;
        }
        pi.pam_authtok_size=strlen(pi.pam_authtok);

        if (forward_pass != 0) {
            ret = pam_set_item(pamh, PAM_AUTHTOK, resp[0].resp);
            if (ret != PAM_SUCCESS) {
                D(("Failed to set PAM_AUTHTOK, authtok may not be available for other modules"));
            }
        }
    }

    if (task == SSS_PAM_CHAUTHTOK) {
        ret=pam_get_item(pamh, PAM_CONV, (const void **) &conv);
        if (ret != PAM_SUCCESS) return ret;

        mesg[0] = malloc(sizeof(struct pam_message));
        if (mesg[0] == NULL) {
            D(("Malloc failed.\n"));
            return PAM_SYSTEM_ERR;
        }
        mesg[0]->msg_style = PAM_PROMPT_ECHO_OFF;
        mesg[0]->msg = strdup("New Password: ");

        c = 0;
        do {
            ret=conv->conv(1, (const struct pam_message **) mesg, &resp,
                           conv->appdata_ptr);
            free((void *)mesg[0]->msg);
            if (ret != PAM_SUCCESS) {
                D(("Conversation failure: %s.\n",  pam_strerror(pamh,ret)));
                pam_status = ret;
                goto done;
            }

            newpwd[c++] = strdup(resp[0].resp);
            _pam_overwrite((void *)resp[0].resp);
            free(resp[0].resp);
            free(resp);
            resp = NULL;

            mesg[0]->msg = strdup("Reenter new password: ");
        } while(c < 2);
        free(mesg[0]);

        if (strcmp(newpwd[0],newpwd[1]) != 0) {
            pam_status = PAM_AUTHTOK_ERR;
            goto done;
        }

        if (newpwd[0] == NULL) {
            D(("Empty password\n"));
            pi.pam_newauthtok = NULL;
            pi.pam_newauthtok_type = SSS_AUTHTOK_TYPE_EMPTY;
        } else {
            pi.pam_newauthtok = strdup(newpwd[0]);
            pi.pam_newauthtok_type = SSS_AUTHTOK_TYPE_PASSWORD;
        }
        pi.pam_newauthtok_size=strlen(pi.pam_newauthtok);

        _pam_overwrite((void *)newpwd[0]);
        free(newpwd[0]);
        _pam_overwrite((void *)newpwd[1]);
        free(newpwd[1]);
    }

    print_pam_items(pi);

    if (pi.pam_user) {
        rd.len = pi.pam_user_size +
                 pi.pam_service_size +
                 pi.pam_tty_size +
                 pi.pam_ruser_size +
                 pi.pam_rhost_size +
                 2*sizeof(uint32_t) + pi.pam_authtok_size +
                 2*sizeof(uint32_t) + pi.pam_newauthtok_size +
                 sizeof(uint32_t);
        buf = malloc(rd.len);

        memcpy(buf, pi.pam_user, pi.pam_user_size);
        rp = pi.pam_user_size;

        memcpy(&buf[rp],  pi.pam_service, pi.pam_service_size);
        rp += pi.pam_service_size;

        memcpy(&buf[rp],  pi.pam_tty, pi.pam_tty_size);
        rp += pi.pam_tty_size;

        memcpy(&buf[rp],  pi.pam_ruser, pi.pam_ruser_size);
        rp += pi.pam_ruser_size;

        memcpy(&buf[rp],  pi.pam_rhost, pi.pam_rhost_size);
        rp += pi.pam_rhost_size;

        ((uint32_t *)(&buf[rp]))[0] = pi.pam_authtok_type;
        rp += sizeof(uint32_t);
        ((uint32_t *)(&buf[rp]))[0] = pi.pam_authtok_size;
        rp += sizeof(uint32_t);
        memcpy(&buf[rp],  pi.pam_authtok, pi.pam_authtok_size);
        rp += pi.pam_authtok_size;
        _pam_overwrite((void *)pi.pam_authtok);
        free((void *)pi.pam_authtok);

        ((uint32_t *)(&buf[rp]))[0] = pi.pam_newauthtok_type;
        rp += sizeof(uint32_t);
        ((uint32_t *)(&buf[rp]))[0] = pi.pam_newauthtok_size;
        rp += sizeof(uint32_t);
        memcpy(&buf[rp],  pi.pam_newauthtok, pi.pam_newauthtok_size);
        rp += pi.pam_newauthtok_size;
        _pam_overwrite((void *)pi.pam_newauthtok);
        free((void *)pi.pam_newauthtok);

        ((uint32_t *)(&buf[rp]))[0] = END_OF_PAM_REQUEST;
        rp += sizeof(uint32_t);

        if (rp != rd.len) {
            D(("error during packet creation."));
            pam_status = PAM_ABORT;
            goto done;
        }
        rd.data = buf;

        ret = sss_pam_make_request(task, &rd, &repbuf, &replen, &errnop);

        if (ret != NSS_STATUS_SUCCESS) {
            D(("sss_pam_make_request failed."));
            pam_status = ret;
            goto done;
        }

/* FIXME: add an end signature */
        if (replen<sizeof(int)) {
            D(("response not in expected format."));
            pam_status=PAM_SYSTEM_ERR;
            goto done;
        }

        pam_status = ((int32_t *)repbuf)[0];
        eval_response(pamh, replen, repbuf);
        D(("received: %d (%s)", pam_status, pam_strerror(pamh,pam_status)));
    } else {
        D(("no user found, doing nothing"));
        return PAM_SUCCESS;
    }

done:
    if ( resp != NULL ) {
        _pam_overwrite((void *)resp[0].resp);
        free(resp[0].resp);
        free(resp);
    }
    if ( buf != NULL && repbuf != NULL) _pam_overwrite_n(buf, rd.len);
    free(buf);
    free(repbuf);

    return pam_status;
}

PAM_EXTERN int pam_sm_authenticate(pam_handle_t *pamh, int flags, int argc,
                                   const char **argv )
{
    return pam_sss(SSS_PAM_AUTHENTICATE, pamh, flags, argc, argv);
}


PAM_EXTERN int pam_sm_setcred(pam_handle_t *pamh, int flags, int argc,
                              const char **argv )
{
    return pam_sss(SSS_PAM_SETCRED, pamh, flags, argc, argv);
}

PAM_EXTERN int pam_sm_acct_mgmt(pam_handle_t *pamh, int flags, int argc,
                                const char **argv )
{
    return pam_sss(SSS_PAM_ACCT_MGMT, pamh, flags, argc, argv);
}

PAM_EXTERN int pam_sm_chauthtok(pam_handle_t *pamh, int flags, int argc,
                                const char **argv )
{
    return pam_sss(SSS_PAM_CHAUTHTOK, pamh, flags, argc, argv);
}

PAM_EXTERN int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc,
                                   const char **argv )
{
    return pam_sss(SSS_PAM_OPEN_SESSION, pamh, flags, argc, argv);
}

PAM_EXTERN int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc,
                                    const char **argv )
{
    return pam_sss(SSS_PAM_CLOSE_SESSION, pamh, flags, argc, argv);
}


#ifdef PAM_STATIC

/* static module data */

struct pam_module _pam_sssd_modstruct ={
     "pam_sssd",
     pam_sm_authenticate,
     pam_sm_setcred,
     pam_sm_acct_mgmt,
     pam_sm_open_session,
     pam_sm_close_session,
     pam_sm_chauthtok
};

#endif