summaryrefslogtreecommitdiffstats
path: root/src/util/util_preauth.c
blob: a2b0ac622903828a6ec05e51025b47e38892aade (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
/*
    SSSD

    Calls to manage the preauth indicator file

    Authors:
        Sumit Bose <sbose@redhat.com>

    Copyright (C) 2018 Red Hat

    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 <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "util/util.h"
#include "sss_client/sss_cli.h"

static void cleanup_preauth_indicator(void)
{
    int ret;

    ret = unlink(PAM_PREAUTH_INDICATOR);
    if (ret != EOK && errno != ENOENT) {
        ret = errno;
        DEBUG(SSSDBG_OP_FAILURE,
              "Failed to remove preauth indicator file [%s] %d [%s].\n",
              PAM_PREAUTH_INDICATOR, ret, sss_strerror(ret));
    }
}

errno_t create_preauth_indicator(void)
{
    TALLOC_CTX *tmp_ctx;
    errno_t ret;
    int fd;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_OP_FAILURE, "talloc_new failed.\n");
        return ENOMEM;
    }

    fd = open(PAM_PREAUTH_INDICATOR, O_CREAT | O_EXCL | O_WRONLY | O_NOFOLLOW,
              0644);
    if (fd < 0) {
        if (errno != EEXIST) {
            DEBUG(SSSDBG_OP_FAILURE,
                  "Failed to create preauth indicator file [%s].\n",
                  PAM_PREAUTH_INDICATOR);
            ret = EOK;
            goto done;
        }

        DEBUG(SSSDBG_TRACE_FUNC,
              "Preauth indicator file [%s] already exists. Continuing.\n",
              PAM_PREAUTH_INDICATOR);
    } else {
        close(fd);
    }

    ret = atexit(cleanup_preauth_indicator);
    if (ret != EOK) {
        DEBUG(SSSDBG_OP_FAILURE, "atexit failed. Continuing.\n");
    }

    ret = EOK;

done:
    talloc_free(tmp_ctx);

    return ret;
}