-
Notifications
You must be signed in to change notification settings - Fork 0
/
pam_user_authorized_keys.c
169 lines (147 loc) · 5.78 KB
/
pam_user_authorized_keys.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
/*
* Copyright (c) 2008, Jamie Beverly.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice, this list
* of conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY Jamie Beverly ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Jamie Beverly OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those of the
* authors and should not be interpreted as representing official policies, either expressed
* or implied, of Jamie Beverly.
*/
#include "includes.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#ifndef HOST_NAME_MAX
#ifdef MAXHOSTNAMELEN
#define HOST_NAME_MAX MAXHOSTNAMELEN
#endif
#endif
#include <netinet/in.h>
#include <errno.h>
#ifdef HAVE_PATHS_H
# include <paths.h>
#endif
#include <pwd.h>
#ifdef HAVE_LOGIN_H
#include <login.h>
#endif
#ifdef USE_SHADOW
#include <shadow.h>
#endif
#ifdef HAVE_LIBGEN_H
#include <libgen.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include "xmalloc.h"
#include "match.h"
#include "log.h"
#include "buffer.h"
#include "key.h"
#include "misc.h"
#include "xmalloc.h"
#include "ssh.h"
#include "ssh2.h"
#include "compat.h"
#include "pathnames.h"
#include "secure_filename.h"
#include "identity.h"
#include "pam_user_key_allowed2.h"
extern char *authorized_keys_file;
extern char *authorized_keys_command;
extern char *authorized_keys_command_user;
extern uint8_t allow_user_owned_authorized_keys_file;
uid_t authorized_keys_file_allowed_owner_uid;
void
parse_authorized_key_file(const char *user,
const char *authorized_keys_file_input)
{
char fqdn[HOST_NAME_MAX] = "";
char hostname[HOST_NAME_MAX] = "";
char auth_keys_file_buf[4096] = "";
char *slash_ptr = NULL;
char owner_uname[128] = "";
size_t owner_uname_len = 0;
/*
* temporary copy, so that both tilde expansion and percent expansion both
* get to apply to the path
*/
strncat(auth_keys_file_buf, authorized_keys_file_input,
sizeof(auth_keys_file_buf) - 1);
if(allow_user_owned_authorized_keys_file)
authorized_keys_file_allowed_owner_uid = getpwnam(user)->pw_uid;
if(*auth_keys_file_buf == '~') {
if(*(auth_keys_file_buf + 1) == '/') {
authorized_keys_file_allowed_owner_uid = getpwnam(user)->pw_uid;
} else {
slash_ptr = strchr(auth_keys_file_buf, '/');
if(!slash_ptr)
pamsshagentauth_fatal
("cannot expand tilde in path without a `/'");
owner_uname_len = slash_ptr - auth_keys_file_buf - 1;
if(owner_uname_len > (sizeof(owner_uname) - 1))
pamsshagentauth_fatal("Username too long");
strncat(owner_uname, auth_keys_file_buf + 1, owner_uname_len);
if(!authorized_keys_file_allowed_owner_uid)
authorized_keys_file_allowed_owner_uid =
getpwnam(owner_uname)->pw_uid;
}
authorized_keys_file =
pamsshagentauth_tilde_expand_filename(auth_keys_file_buf,
authorized_keys_file_allowed_owner_uid);
strncpy(auth_keys_file_buf, authorized_keys_file,
sizeof(auth_keys_file_buf) - 1);
pamsshagentauth_xfree(authorized_keys_file) /* when we
percent_expand
later, we'd step
on this, so free
it immediately */ ;
}
if(strstr(auth_keys_file_buf, "%h")) {
authorized_keys_file_allowed_owner_uid = getpwnam(user)->pw_uid;
}
#if HAVE_GETHOSTNAME
*hostname = '\0';
gethostname(fqdn, HOST_NAME_MAX);
strncat(hostname, fqdn, strcspn(fqdn, "."));
#endif
authorized_keys_file =
pamsshagentauth_percent_expand(auth_keys_file_buf, "h",
getpwnam(user)->pw_dir, "H", hostname,
"f", fqdn, "u", user, NULL);
}
int
pam_user_key_allowed(const char *ruser, Key * key)
{
return
pamsshagentauth_user_key_allowed2(getpwuid(authorized_keys_file_allowed_owner_uid),
key, authorized_keys_file)
|| pamsshagentauth_user_key_allowed2(getpwuid(0), key,
authorized_keys_file)
|| pamsshagentauth_user_key_command_allowed2(authorized_keys_command,
authorized_keys_command_user,
getpwnam(ruser), key);
}