~kennylevinsen/pam_uaccess

pam_uaccess/pam_uaccess.c -rw-r--r-- 7.6 KiB
54fbf043Kenny Levinsen Initial commit 8 months ago
                                                                                
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
#include <pwd.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>

#include <security/pam_modules.h>
#include <security/pam_appl.h>
#include <security/pam_ext.h>
#include <acl/libacl.h>
#include <libudev.h>

static struct udev_enumerate *enumerate_uaccess(struct udev *udev) {
        struct udev_enumerate *enumerator = udev_enumerate_new(udev);
        if (!enumerator) {
                return NULL;
        }

        udev_enumerate_add_match_tag(enumerator, "uaccess");
        if (udev_enumerate_scan_devices(enumerator) < 0) {
                udev_enumerate_unref(enumerator);
                return NULL;
        }

        return enumerator;
}

static int acl_find_uid(acl_t acl, uid_t uid, acl_entry_t *ret) {
        acl_entry_t idx;
        int res = acl_get_entry(acl, ACL_FIRST_ENTRY, &idx);
        while (res > 0) {
                acl_tag_t tag;
                if (acl_get_tag_type(idx, &tag) < 0) {
                        return -1;
                }
                if (tag == ACL_USER) {
                        uid_t *user = acl_get_qualifier(idx);
                        if (user == NULL) {
                                return -1;
                        }
                        bool match = *user == uid;
                        acl_free(user);
                        if (match) {
                                *ret = idx;
                                return 0;
                        }
                }
                res = acl_get_entry(acl, ACL_NEXT_ENTRY, &idx);
        }
        if (res < 0) {
                return -1;
        }
        *ret = NULL;
        return 0;
}

static void ungrant_access(const char *path, uid_t uid) {
        acl_t acl = acl_get_file(path, ACL_TYPE_ACCESS);
        if (acl == NULL) {
                return;
        }

        acl_entry_t entry;
        if (acl_find_uid(acl, uid, &entry) == -1) {
                goto error;
        }
        acl_delete_entry(acl, entry);
        if (acl_calc_mask(&acl) < 0) {
                goto error;
        }
        if (acl_set_file(path, ACL_TYPE_ACCESS, acl) < 0) {
                goto error;
        }

error:
        acl_free(acl);
        return;
}

static void grant_access(const char *path, uid_t uid) {
        acl_t acl = acl_get_file(path, ACL_TYPE_ACCESS);
        if (acl == NULL) {
                return;
        }

        acl_entry_t entry;
        if (acl_find_uid(acl, uid, &entry) == -1) {
                goto error;
        }
        if (entry == NULL) {
                if (acl_create_entry(&acl, &entry) < 0) {
                        goto error;
                }
                if (acl_set_tag_type(entry, ACL_USER) < 0) {
                        goto error;
                }
                if (acl_set_qualifier(entry, &uid) < 0) {
                        goto error;
                }
        }

        acl_permset_t permset;
        if (acl_get_permset(entry, &permset) < 0) {
                goto error;
        }
        if (acl_add_perm(permset, ACL_READ | ACL_WRITE) < 0) {
                goto error;
        }
        if (acl_calc_mask(&acl) < 0) {
                goto error;
        }
        if (acl_set_file(path, ACL_TYPE_ACCESS, acl) < 0) {
                goto error;
        }

error:
        acl_free(acl);
        return;
}

int pam_sm_open_session(pam_handle_t *pamh, int flags, int argc, const char **argv) {
        (void)flags;
        (void)argc;
        (void)argv;

        if (geteuid() != 0) {
                pam_syslog(pamh, LOG_ERR, "pam_uaccess can only be used by a root process");
                return PAM_SESSION_ERR;
        }

        for (int idx = 0; idx < argc; idx++) {
                if (strcmp(argv[idx], "skip_ungrant") == 0) {
                        // Used in pam_sm_close_session
                } else {
                        pam_syslog(pamh, LOG_ERR, "Unknown argument to pam_uaccess: %s", argv[idx]);
                        return PAM_SESSION_ERR;
                }
        }

        const char *user;
        if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS) {
                return PAM_USER_UNKNOWN;
        }

        const struct passwd *pw = getpwnam(user);
        if (pw == NULL) {
                return PAM_USER_UNKNOWN;
        }

        struct udev *udev = udev_new();
        if (udev == NULL) {
                pam_syslog(pamh, LOG_ERR, "Could not create udev instance");
                return PAM_SESSION_ERR;
        }

        struct udev_enumerate *enumerator = enumerate_uaccess(udev);
        if (enumerator == NULL) {
                udev_unref(udev);
                pam_syslog(pamh, LOG_ERR, "Could not create udev enumerator");
                return PAM_SESSION_ERR;
        }

        if (udev_enumerate_get_list_entry(enumerator) == NULL) {
                udev_enumerate_unref(enumerator);
                udev_unref(udev);
                pam_syslog(pamh, LOG_ERR, "Could not find any devices tagged with uaccess");
                return PAM_SESSION_ERR;
        }

        struct udev_list_entry *entry;
        udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(enumerator)) {
                const char *path = udev_list_entry_get_name(entry);
                struct udev_device *dev = udev_device_new_from_syspath(udev, path);
                grant_access(udev_device_get_devnode(dev), pw->pw_uid);
                udev_device_unref(dev);
        }

        udev_enumerate_unref(enumerator);
        udev_unref(udev);

        return PAM_SUCCESS;
}

int pam_sm_close_session(pam_handle_t *pamh, int flags, int argc, const char **argv) {
        (void)flags;
        (void)argc;
        (void)argv;

        if (geteuid() != 0) {
                pam_syslog(pamh, LOG_ERR, "pam_uaccess can only be used by a root process");
                return PAM_SESSION_ERR;
        }

        for (int idx = 0; idx < argc; idx++) {
                if (strcmp(argv[idx], "skip_ungrant") == 0) {
                        return PAM_SUCCESS;
                } else {
                        pam_syslog(pamh, LOG_ERR, "Unknown argument to pam_uaccess: %s", argv[idx]);
                        return PAM_SESSION_ERR;
                }
        }

        const char *user;
        if (pam_get_user(pamh, &user, NULL) != PAM_SUCCESS) {
                return PAM_USER_UNKNOWN;
        }

        const struct passwd *pw = getpwnam(user);
        if (pw == NULL) {
                return PAM_USER_UNKNOWN;
        }

        struct udev *udev = udev_new();
        if (udev == NULL) {
                pam_syslog(pamh, LOG_ERR, "Could not create udev instance");
                return PAM_SESSION_ERR;
        }

        struct udev_enumerate *enumerator = enumerate_uaccess(udev);
        if (enumerator == NULL) {
                udev_unref(udev);
                pam_syslog(pamh, LOG_ERR, "Could not create udev enumerator");
                return PAM_SESSION_ERR;
        }

        if (udev_enumerate_get_list_entry(enumerator) == NULL) {
                udev_enumerate_unref(enumerator);
                udev_unref(udev);
                pam_syslog(pamh, LOG_ERR, "Could not find any devices tagged with uaccess");
                return PAM_SESSION_ERR;
        }

        struct udev_list_entry *entry;
        udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(enumerator)) {
                const char *path = udev_list_entry_get_name(entry);
                struct udev_device *dev = udev_device_new_from_syspath(udev, path);
                ungrant_access(udev_device_get_devnode(dev), pw->pw_uid);
                udev_device_unref(dev);
        }

        udev_enumerate_unref(enumerator);
        udev_unref(udev);

        return PAM_SUCCESS;
}