-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.c
213 lines (194 loc) · 5.96 KB
/
support.c
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
/*
* Support functions for pam_krb5.
*
* Some general utility functions used by multiple PAM groups that aren't
* associated with any particular chunk of functionality.
*
* Copyright 2005, 2006, 2007, 2009 Russ Allbery <rra@stanford.edu>
* Copyright 2005 Andres Salomon <dilinger@debian.org>
* Copyright 1999, 2000 Frank Cusack <fcusack@fcusack.com>
*
* See LICENSE for licensing terms.
*/
#include <config.h>
#include <portable/pam.h>
#include <krb5.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <internal.h>
/*
* Given the PAM arguments and the user we're authenticating, see if we should
* ignore that user because they're root or have a low-numbered UID and we
* were configured to ignore such users. Returns true if we should ignore
* them, false otherwise.
*/
int
pamk5_should_ignore(struct pam_args *args, PAM_CONST char *username)
{
struct passwd *pwd;
if (args->ignore_root && strcmp("root", username) == 0) {
pamk5_debug(args, "ignoring root user");
return 1;
}
if (args->minimum_uid > 0) {
pwd = pam_modutil_getpwnam(args->pamh, username);
if (pwd != NULL && pwd->pw_uid < (unsigned long) args->minimum_uid) {
pamk5_debug(args, "ignoring low-UID user (%lu < %d)",
(unsigned long) pwd->pw_uid, args->minimum_uid);
return 1;
}
}
return 0;
}
/*
* Map the user to a Kerberos principal according to alt_auth_map. Returns
* PAM_SUCCESS on success, storing the mapped principal name in newly
* allocated memory in principal. The caller is responsible for freeing.
* Returns PAM_SERVICE_ERROR on any error.
*/
int
pamk5_map_principal(struct pam_args *args, const char *username,
char **principal)
{
char *user = NULL;
char *realm;
const char *i;
size_t needed, offset;
/* Makes no sense if alt_auth_map isn't set. */
if (args->alt_auth_map == NULL)
return PAM_SERVICE_ERR;
/* Need to split off the realm if it is present. */
realm = strchr(username, '@');
if (realm == NULL)
user = (char *) username;
else {
user = strdup(username);
if (user == NULL)
return PAM_SERVICE_ERR;
realm = strchr(user, '@');
if (realm == NULL)
goto fail;
*realm = '\0';
realm++;
}
/* Now, allocate a string and build the principal. */
needed = 0;
for (i = args->alt_auth_map; *i != '\0'; i++) {
if (i[0] == '%' && i[1] == 's') {
needed += strlen(user);
i++;
} else {
needed++;
}
}
if (realm != NULL)
needed += 1 + strlen(realm);
needed++;
*principal = malloc(needed);
if (*principal == NULL)
goto fail;
offset = 0;
for (i = args->alt_auth_map; *i != '\0'; i++) {
if (i[0] == '%' && i[1] == 's') {
memcpy(*principal + offset, user, strlen(user));
offset += strlen(user);
i++;
} else {
(*principal)[offset] = *i;
offset++;
}
}
(*principal)[offset] = '\0';
return PAM_SUCCESS;
fail:
if (user != NULL && user != username)
free(user);
return PAM_SERVICE_ERR;
}
/*
* Verify the user authorization. Call krb5_kuserok if this is a local
* account, or do the krb5_aname_to_localname verification if ignore_k5login
* was requested. For non-local accounts, the principal must match the
* authentication identity.
*/
int
pamk5_authorized(struct pam_args *args)
{
struct context *ctx;
krb5_context c;
struct passwd *pwd;
char kuser[65]; /* MAX_USERNAME == 65 (MIT Kerberos 1.4.1). */
if (args == NULL || args->ctx == NULL || args->ctx->context == NULL)
return PAM_SERVICE_ERR;
ctx = args->ctx;
if (ctx->name == NULL)
return PAM_SERVICE_ERR;
c = ctx->context;
/*
* If alt_auth_map was set, authorize the user if the authenticated
* principal matches the mapped principal. alt_auth_map essentially
* serves as a supplemental .k5login.
*/
if (args->alt_auth_map != NULL) {
char *mapped;
char *authed;
int retval;
krb5_principal princ;
if (pamk5_map_principal(args, ctx->name, &mapped) != PAM_SUCCESS)
return PAM_SERVICE_ERR;
retval = krb5_parse_name(c, mapped, &princ);
if (retval != 0) {
free(mapped);
return PAM_SERVICE_ERR;
}
free(mapped);
retval = krb5_unparse_name(c, princ, &mapped);
if (retval != 0)
return PAM_SERVICE_ERR;
retval = krb5_unparse_name(c, ctx->princ, &authed);
if (retval != 0) {
free(mapped);
return PAM_SERVICE_ERR;
}
if (strcmp(authed, mapped) == 0) {
free(authed);
free(mapped);
return PAM_SUCCESS;
}
free(authed);
free(mapped);
}
/*
* If the name to which we're authenticating contains @ (is fully
* qualified), it must match the principal exactly.
*/
if (strchr(ctx->name, '@') != NULL) {
char *principal;
int retval;
retval = krb5_unparse_name(c, ctx->princ, &principal);
if (retval != 0)
return PAM_SERVICE_ERR;
if (strcmp(principal, ctx->name) != 0) {
free(principal);
return PAM_AUTH_ERR;
}
return PAM_SUCCESS;
}
/*
* Otherwise, apply either krb5_aname_to_localname or krb5_kuserok
* depending on the situation.
*/
pwd = pam_modutil_getpwnam(args->pamh, ctx->name);
if (args->ignore_k5login || pwd == NULL) {
if (krb5_aname_to_localname(c, ctx->princ, sizeof(kuser), kuser) != 0)
return PAM_AUTH_ERR;
if (strcmp(kuser, ctx->name) != 0)
return PAM_AUTH_ERR;
} else {
if (!krb5_kuserok(c, ctx->princ, ctx->name))
return PAM_AUTH_ERR;
}
return PAM_SUCCESS;
}