-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthfile.c
245 lines (219 loc) · 6.29 KB
/
authfile.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
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
/*
Copyright (c) 2013 Eugene Crosser
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must
not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <alloca.h>
#include "base64.h"
#include "authobj.h"
#include "authfile.h"
/*
* Template string may contain zero or one '~' and zero or one '?'.
* '~' at the beginning of the template string is substituted with
* the home directory of the userid. In any other position it is
* substituted with the userid itself. There is no way to make the
* resulting path contain '~'. If there is more than one '~', or if
* the '~' is at the beginning but userid does not resolve via
* getpwnam, or '~' is present but the argument is NULL, path_size
* returns 0, and make_path returns 1.
*/
static const char *template = "~/.pam_cr/auth";
void authfile_template(const char *str)
{
template = str;
}
/*
I know using these two functions and alloca() in between it ugly, but
I like the alternatives even less. =ec
*/
static int path_size(const struct passwd *pw)
{
const char *p;
if ((p = strchr(template, '~')) != strrchr(template, '~')) return 0;
if (p && !pw) return 0;
if (p == template) return strlen(template)+strlen(pw->pw_dir)+1;
else return strlen(template)+strlen(pw->pw_name)+1;
}
static int
make_path(char * const path, const struct passwd *pw)
{
const char *p;
char *q;
path[0] = '\0';
q = path;
for (p = template; *p; p++) switch (*p) {
case '~':
if (!pw) return 1;
if (p == template) strcpy(q, pw->pw_dir);
else strcpy(q, pw->pw_name);
while (*q) q++;
break;
default:
*q++ = *p;
break;
}
*q = '\0';
return 0;
}
int parse(char * const buf, const int argc, const char *argv[const])
{
char *p, *q;
int i;
for (i = 0, p = buf; *p; p = q+1, i++) {
for (q = p; *q && *q != ':' && *q != '\r' && *q != '\n'; q++) ;
*q = '\0';
if (*p && i < argc) argv[i] = p;
}
return i != argc;
}
struct _auth_obj authfile(const char *userid, const char *password,
void (*update_nonce)(char *nonce, const size_t nonsize),
const unsigned char *secret, const size_t secsize,
const unsigned char *payload, const size_t paylsize,
struct _auth_chunk (*fetch_key)(const unsigned char *chal,
const size_t csize))
{
struct _auth_obj ret = {0};
const struct passwd *pw = NULL;
mode_t oldmask;
FILE *fp = NULL;
char *fn, *nfn;
int fnl;
struct stat st = {0};
char *buf = NULL;
struct {
const char *userid;
const char *nonce;
const char *hablob;
} w = {NULL, NULL, NULL};
unsigned char *ablob = NULL;
int blobsize = 0;
char *newnonce;
int nonsize;
struct _auth_obj ao;
if (userid) pw = getpwnam(userid);
if ((fnl = path_size(pw)) == 0) {
ret.err = "authfile path_size failed";
return ret;
}
fn = alloca(fnl);
if (make_path(fn, pw)) {
ret.err = "authfile make_path failed";
return ret;
}
nfn = alloca(fnl+32);
snprintf(nfn, fnl+32, "%s.%d.%ld", fn, (int)getpid(), (long)time(NULL));
fp = fopen(fn, "r");
if (fp) {
if (fstat(fileno(fp), &st)) st.st_size = 2047;
if (st.st_size > 2047) st.st_size = 2047;
buf = alloca(st.st_size + 1);
if (!fgets(buf, st.st_size + 1, fp)) {
ret.err = strerror(errno);
} else if (parse(buf, sizeof(w)/sizeof(char*),
(const char ** const)&w)){
ret.err = "error: unparseable auth file";
}
fclose(fp);
}
if (ret.err) return ret;
if (w.hablob) {
blobsize = strlen(w.hablob)*3/4;
ablob = alloca(blobsize);
if (b64_decode(w.hablob, ablob, &blobsize))
ret.err = "error: undecodeable auth string";
}
if (ret.err) return ret;
nonsize = w.nonce ? strlen(w.nonce)*2 : 32;
if (nonsize < 32) nonsize = 32;
newnonce = alloca(nonsize);
if (w.nonce) strcpy(newnonce, w.nonce);
else memset(newnonce, 0, nonsize);
update_nonce(newnonce, nonsize);
ao = authobj(userid?userid:w.userid, password,
w.nonce, newnonce, secret, secsize,
payload, paylsize, ablob, blobsize,
fetch_key);
if (ao.err) {
ret.err = ao.err;
if (ao.data) memset(ao.data, 0, ao.datasize);
if (ao.payload) memset(ao.payload, 0, ao.paylsize);
if (ao.buffer) free(ao.buffer);
return ret;
}
oldmask = umask(077);
if ((fp = fopen(nfn, "w"))) {
int bsize = ((ao.datasize-1)/3+1)*4+1;
char *b64 = alloca(bsize);
if (b64_encode(ao.data, ao.datasize, b64, &bsize)) {
ret.err = "error: could not encode auth string";
} else if (fprintf(fp, "%s:%s:%s\n",
userid?userid:w.userid, newnonce, b64) < 0) {
ret.err = strerror(errno);
}
if (st.st_uid || st.st_gid) {
if (fchown(fileno(fp), st.st_uid, st.st_gid)) {/*ign*/;}
}
if (fclose(fp) < 0) {
ret.err = strerror(errno);
}
} else {
ret.err = strerror(errno);
}
(void)umask(oldmask);
if (ret.err) {
unlink(nfn); /* may not exist but no matter */
} else if (rename(nfn, fn)) {
ret.err = strerror(errno);
}
if (!ret.err) {
int bufsize = (w.userid?strlen(w.userid)+1:0) + ao.paylsize + 1;
if (bufsize) {
if ((ret.buffer = malloc(bufsize)) == NULL) {
ret.err = "authfile malloc failed";
} else {
unsigned char *p = ret.buffer;
if (w.userid) {
strcpy((char*)p, w.userid);
ret.data = p;
ret.datasize = strlen(w.userid)+1;
p += strlen(w.userid)+1;
}
if (ao.payload) {
memcpy(p, ao.payload, ao.paylsize);
p[ao.paylsize] = '\0';
ret.payload = p;
ret.paylsize = ao.paylsize+1;
}
}
}
}
if (ao.data) memset(ao.data, 0, ao.datasize);
if (ao.payload) memset(ao.payload, 0, ao.paylsize);
if (ao.buffer) free(ao.buffer);
return ret;
}