-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdline.c
210 lines (180 loc) · 5.1 KB
/
cmdline.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
/*
* access -- authenticator for Unix systems.
*
* access is copyrighted:
* Copyright (C) 2014-2018 Andrey Rys. All rights reserved.
*
* access is licensed to you under the terms of std. MIT/X11 license:
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "access.h"
char *build_cmdline(int argc, char **argv)
{
const char *fmt;
size_t x;
char *r, *s;
fmt = "%s ";
for (x = 0, r = s = NULL; x < argc && *(argv+x); x++) {
if (acs_strchr(*(argv+x), ' ')) fmt = "\"%s\" ";
acs_asprintf(&s, fmt, *(argv+x));
acs_astrcat(&r, s);
fmt = "%s ";
}
x = shrink_dynstr(&r);
if (x > 1 && x != NOSIZE) *(r+x-2) = 0;
pfree(s);
return r;
}
char *build_protected_cmdline(int argc, char **argv)
{
int x;
size_t n, y;
char *r, *s;
for (x = 0, r = NULL; x < argc && *(argv+x); x++) {
n = acs_szalloc(r);
y = acs_strnlen(*(argv+x), ACS_XSALLOC_MAX)+1;
r = acs_realloc(r, n+y);
memcpy(r+n, *(argv+x), y);
}
s = r;
n = acs_szalloc(s);
r = acs_malloc(((n/2)*3)+8);
base64_encode(r, s, n);
pfree(s);
shrink_dynstr(&r);
return r;
}
/*
* To those who would grab it for free:
*
* It parses quotes and quote escapes, so strings like:
* `/bin/sh sh -c "env; sh -c \"echo 123\"; exit 1"` <-- yes, exactly.
* will be parsed as:
* a[0]->`/bin/sh`
* a[1]->`sh`
* a[2]->`-c`
* a[3]->`env; sh -c "echo 123"; exit 1`
* a[4]->NULL
* (here, backticks are just not to confuse with double quotes).
* However: no other special characters other than ' ' (space) and double quote
* could be escaped!
*
* This is really a simple function. It does not do whole shell logic there,
* and you should never expect it to be fully shell compatible!!
* Because it is never called for a user input, it is safe to have errors here.
* It simply gets the basic job right, and I am not going to enhance it anymore.
*
* -- Rys, 08Dec2017.
*/
char **parse_cmdline(char *p)
{
char *s, *d;
char **args = NULL;
int x, y;
size_t l, sz;
if (!p) return NULL;
if (str_empty(p)) return NULL;
l = acs_strnlen(p, ACS_ALLOC_MAX);
s = d = p; x = y = 0;
while (1) {
if (*d == '\"' && (d-p && *(d-1) != '\\')) {
memmove(d, d+1, l-(d-p)); l--;
if (!x) x = 1; else x = 0;
continue;
}
if (y || (*d == ' ' && (d-p && *(d-1) != '\\') && !x)) {
*d = 0;
acs_strlrep(s, l, "\\ ", " ");
acs_strlrep(s, l, "\\\"", "\"");
sz = DYN_ARRAY_SZ(args);
args = acs_realloc(args, (sz+(sz == 0 ? 2 : 1)) * sizeof(char *));
if (sz) sz--;
*(args+sz) = s;
s = d+1;
}
if (y) break;
d++; if (str_empty(d)) y = 1;
}
return args;
}
int is_exec(const char *path)
{
errno = 0;
if ((file_or_dir(path) == PATH_IS_FILE) && !errno) {
/* checking file mode is not enough. Ask system about if it's ready to be exec'd. */
if ((access(path, X_OK) == 0) && !errno) return 1;
}
return 0;
}
/* Pure `which` like, running over spath */
char *acs_which(const char *spathspec, const char *progname, const char *root)
{
char *tmp = NULL;
char *spath_copy, *s, *d, *t;
int x;
x = is_abs_rel(progname);
if (x) {
if (x == PATH_ABSOLUTE) {
if (root) acs_asprintf(&tmp, "%s%s", root, progname);
else acs_asprintf(&tmp, "%s", progname);
}
else if (x == PATH_RELATIVE) {
if (root) acs_asprintf(&tmp,
"%s%s/%s", root, dstdir, progname);
else acs_asprintf(&tmp, "%s/%s", dstdir, progname);
}
if (is_exec(tmp)) {
shrink_dynstr(&tmp);
return tmp;
}
pfree(tmp);
return NULL;
}
spath_copy = acs_strdup(spathspec ? spathspec : get_spath());
s = d = spath_copy; t = NULL; x = 0;
while ((s = acs_strtok_r(d, ":", &t))) {
if (d) d = NULL;
if (root) acs_asprintf(&tmp, "%s%s/%s", root, s, progname);
else acs_asprintf(&tmp, "%s/%s", s, progname);
errno = 0;
if (is_exec(tmp)) {
x = 1;
break;
}
}
pfree(spath_copy);
if (x) {
shrink_dynstr(&tmp);
return tmp;
}
pfree(tmp);
return NULL;
}
#if defined WITH_SU_PROG || defined WITH_DACCESS_PROG
char *find_access_exec(const char *name)
{
char *r = NULL;
acs_asprintf(&r, "%s/bin/%s", PREFIX, name ? name : PROGRAM_NAME);
if (is_exec(r)) return r;
pfree(r);
return acs_which(spath, name ? name : PROGRAM_NAME, NULL);
}
#endif