-
Notifications
You must be signed in to change notification settings - Fork 18
/
otplock.c
213 lines (193 loc) · 6.07 KB
/
otplock.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
/*
* otplock - Apache mod_authn_otp one-time users file locker
*
* Copyright 2023 Archie L. Cobbs <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "otpdefs.h"
#include "config.h"
#if HAVE_APR_1_APR_FILE_IO_H
#include <apr-1/apr_file_io.h>
#include <apr-1/apr_lib.h>
#include <apr-1/apr_strings.h>
#include <apr-1/apr_thread_proc.h>
#elif HAVE_APR_1_0_APR_FILE_IO_H
#include <apr-1.0/apr_file_io.h>
#include <apr-1.0/apr_lib.h>
#include <apr-1.0/apr_strings.h>
#include <apr-1.0/apr_thread_proc.h>
#else
#error "libapr header files not found"
#endif
/* Program name */
#define PROG_NAME "otplock"
#define LOCKFILE_SUFFIX ".lock"
#define DEFAULT_EDITOR "vim"
/* Error exit values */
#undef EXIT_USAGE_ERROR
#undef EXIT_SYSTEM_ERROR
#define EXIT_USAGE_ERROR 85 /* Incorrect command line usage */
#define EXIT_SYSTEM_ERROR 86 /* Could not open file, etc. */
#define EXIT_CAUGHT_SIGNAL 87
extern const char *const *environ;
static void usage(void);
int
main(int argc, const char *const *argv)
{
char lockfile[APR_PATH_MAX];
const char *usersfile;
const char *editcmd[3];
char errbuf[256];
apr_pool_t *pool;
apr_file_t *handle = NULL;
apr_status_t status;
int edit = 0;
int ch;
int r;
// Initialize APR
if ((status = apr_app_initialize(&argc, &argv, &environ)) != APR_SUCCESS) {
warnx("%s: %s", "apr_app_initialize", apr_strerror(status, errbuf, sizeof(errbuf)));
r = EXIT_SYSTEM_ERROR;
goto out0;
}
if ((status = apr_pool_create(&pool, NULL)) != APR_SUCCESS) {
warnx("%s: %s", "apr_pool_create", apr_strerror(status, errbuf, sizeof(errbuf)));
r = EXIT_SYSTEM_ERROR;
goto out1;
}
// Parse command line
while ((ch = getopt(argc, (char **)(intptr_t)argv, "eh")) != -1) {
switch (ch) {
case 'e':
edit = 1;
break;
case 'h':
usage();
r = 0;
goto out2;
default:
usage();
r = EXIT_USAGE_ERROR;
goto out2;
}
}
argc -= optind;
argv += optind;
switch (argc) {
case 0:
usage();
r = EXIT_USAGE_ERROR;
goto out2;
default:
argc--;
usersfile = *argv++;
break;
}
// Handle "-e" flag
if (edit) {
const char *const *ev;
if (argc > 0) {
r = EXIT_USAGE_ERROR;
goto out2;
}
editcmd[0] = DEFAULT_EDITOR;
for (ev = environ; *ev != NULL; ev++) {
if (strncmp(*ev, "EDITOR=", 7) == 0) {
editcmd[0] = *ev + 7;
break;
}
}
editcmd[1] = usersfile;
editcmd[2] = NULL;
argv = editcmd;
argc = 2;
}
// Open the lock file
apr_snprintf(lockfile, sizeof(lockfile), "%s%s", usersfile, LOCKFILE_SUFFIX);
if ((status = apr_file_open(&handle, lockfile, APR_WRITE|APR_CREATE|APR_TRUNCATE, APR_UREAD|APR_UWRITE, pool)) != APR_SUCCESS) {
warnx("can't open \"%s\": %s", lockfile, apr_strerror(status, errbuf, sizeof(errbuf)));
r = EXIT_SYSTEM_ERROR;
goto out2;
}
// Lock the lock file
if ((status = apr_file_lock(handle, APR_FLOCK_EXCLUSIVE)) != APR_SUCCESS) {
warnx("can't lock \"%s\": %s", lockfile, apr_strerror(status, errbuf, sizeof(errbuf)));
r = EXIT_SYSTEM_ERROR;
goto out3;
}
// Execute command, if any
if (*argv != NULL) {
apr_procattr_t *pattr;
apr_exit_why_e why;
apr_proc_t proc;
int rval;
if ((status = apr_procattr_create(&pattr, pool)) != APR_SUCCESS) {
warnx("%s: %s", "apr_procattr_create", apr_strerror(status, errbuf, sizeof(errbuf)));
r = EXIT_SYSTEM_ERROR;
goto out4;
}
if ((status = apr_procattr_cmdtype_set(pattr, APR_SHELLCMD_ENV)) != APR_SUCCESS) {
warnx("%s: %s", "apr_procattr_cmdtype_set", apr_strerror(status, errbuf, sizeof(errbuf)));
r = EXIT_SYSTEM_ERROR;
goto out4;
}
if ((status = apr_proc_create(&proc, *argv, argv, environ, pattr, pool)) != APR_SUCCESS) {
warnx("%s: %s", "apr_proc_create", apr_strerror(status, errbuf, sizeof(errbuf)));
r = EXIT_SYSTEM_ERROR;
goto out4;
}
if ((status = apr_proc_wait(&proc, &rval, &why, APR_WAIT)) != APR_CHILD_DONE) {
warnx("%s: %s", "apr_proc_wait", apr_strerror(status, errbuf, sizeof(errbuf)));
r = EXIT_SYSTEM_ERROR;
goto out4;
}
switch (why) {
case APR_PROC_EXIT:
if ((r = rval) == APR_ENOTIMPL)
r = 0;
break;
case APR_PROC_SIGNAL:
case APR_PROC_SIGNAL_CORE:
r = EXIT_CAUGHT_SIGNAL;
break;
default:
warnx("%s: %s", "apr_proc_wait", "unknown 'why' code");
r = EXIT_SYSTEM_ERROR;
break;
}
} else
r = 0;
// Clean up and exit
out4:
apr_file_unlock(handle);
out3:
apr_file_close(handle);
out2:
apr_pool_destroy(pool);
out1:
apr_terminate();
out0:
return r;
}
static void
usage()
{
fprintf(stderr, "Usage:\n");
fprintf(stderr, " %s usersfile [ command ... ]\n", PROG_NAME);
fprintf(stderr, " %s -e usersfile\n", PROG_NAME);
fprintf(stderr, " %s -h\n", PROG_NAME);
fprintf(stderr, "Options:\n");
fprintf(stderr, " -e\tInvoke $EDITOR with usersfile\n");
fprintf(stderr, " -h\tDisplay this usage message\n");
}