]> git.armaanb.net Git - opendoas.git/blob - doas.c
c95dee3b7af0ff87a9b0d22d1263037aa4a5967d
[opendoas.git] / doas.c
1 /* $OpenBSD: doas.c,v 1.52 2016/04/28 04:48:56 tedu Exp $ */
2 /*
3  * Copyright (c) 2015 Ted Unangst <tedu@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/ioctl.h>
21
22 #include <limits.h>
23 #ifdef HAVE_LOGIN_CAP_H
24 #include <login_cap.h>
25 #endif
26 #include <string.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <err.h>
30 #include <unistd.h>
31 #include <pwd.h>
32 #include <grp.h>
33 #include <syslog.h>
34 #include <errno.h>
35 #include <fcntl.h>
36
37 #include "includes.h"
38 #include "doas.h"
39
40 static void __dead
41 usage(void)
42 {
43         fprintf(stderr, "usage: doas [-Lns] [-C config] [-u user]"
44             " command [args]\n");
45         exit(1);
46 }
47
48 static int
49 parseuid(const char *s, uid_t *uid)
50 {
51         struct passwd *pw;
52         const char *errstr;
53
54         if ((pw = getpwnam(s)) != NULL) {
55                 *uid = pw->pw_uid;
56                 if (*uid == UID_MAX)
57                         return -1;
58                 return 0;
59         }
60         *uid = strtonum(s, 0, UID_MAX - 1, &errstr);
61         if (errstr)
62                 return -1;
63         return 0;
64 }
65
66 static int
67 uidcheck(const char *s, uid_t desired)
68 {
69         uid_t uid;
70
71         if (parseuid(s, &uid) != 0)
72                 return -1;
73         if (uid != desired)
74                 return -1;
75         return 0;
76 }
77
78 static int
79 parsegid(const char *s, gid_t *gid)
80 {
81         struct group *gr;
82         const char *errstr;
83
84         if ((gr = getgrnam(s)) != NULL) {
85                 *gid = gr->gr_gid;
86                 if (*gid == GID_MAX)
87                         return -1;
88                 return 0;
89         }
90         *gid = strtonum(s, 0, GID_MAX - 1, &errstr);
91         if (errstr)
92                 return -1;
93         return 0;
94 }
95
96 static int
97 match(uid_t uid, gid_t *groups, int ngroups, uid_t target, const char *cmd,
98     const char **cmdargs, struct rule *r)
99 {
100         int i;
101
102         if (r->ident[0] == ':') {
103                 gid_t rgid;
104                 if (parsegid(r->ident + 1, &rgid) == -1)
105                         return 0;
106                 for (i = 0; i < ngroups; i++) {
107                         if (rgid == groups[i])
108                                 break;
109                 }
110                 if (i == ngroups)
111                         return 0;
112         } else {
113                 if (uidcheck(r->ident, uid) != 0)
114                         return 0;
115         }
116         if (r->target && uidcheck(r->target, target) != 0)
117                 return 0;
118         if (r->cmd) {
119                 if (strcmp(r->cmd, cmd))
120                         return 0;
121                 if (r->cmdargs) {
122                         /* if arguments were given, they should match explicitly */
123                         for (i = 0; r->cmdargs[i]; i++) {
124                                 if (!cmdargs[i])
125                                         return 0;
126                                 if (strcmp(r->cmdargs[i], cmdargs[i]))
127                                         return 0;
128                         }
129                         if (cmdargs[i])
130                                 return 0;
131                 }
132         }
133         return 1;
134 }
135
136 static int
137 permit(uid_t uid, gid_t *groups, int ngroups, const struct rule **lastr,
138     uid_t target, const char *cmd, const char **cmdargs)
139 {
140         int i;
141
142         *lastr = NULL;
143         for (i = 0; i < nrules; i++) {
144                 if (match(uid, groups, ngroups, target, cmd,
145                     cmdargs, rules[i]))
146                         *lastr = rules[i];
147         }
148         if (!*lastr)
149                 return 0;
150         return (*lastr)->action == PERMIT;
151 }
152
153 static void
154 parseconfig(const char *filename, int checkperms)
155 {
156         extern FILE *yyfp;
157         extern int yyparse(void);
158         struct stat sb;
159
160         yyfp = fopen(filename, "r");
161         if (!yyfp)
162                 err(1, checkperms ? "doas is not enabled, %s" :
163                     "could not open config file %s", filename);
164
165         if (checkperms) {
166                 if (fstat(fileno(yyfp), &sb) != 0)
167                         err(1, "fstat(\"%s\")", filename);
168                 if ((sb.st_mode & (S_IWGRP|S_IWOTH)) != 0)
169                         errx(1, "%s is writable by group or other", filename);
170                 if (sb.st_uid != 0)
171                         errx(1, "%s is not owned by root", filename);
172         }
173
174         yyparse();
175         fclose(yyfp);
176         if (parse_errors)
177                 exit(1);
178 }
179
180 static void __dead
181 checkconfig(const char *confpath, int argc, char **argv,
182     uid_t uid, gid_t *groups, int ngroups, uid_t target)
183 {
184         const struct rule *rule;
185
186         if (setresuid(uid, uid, uid) != 0)
187                 err(1, "setresuid");
188
189         parseconfig(confpath, 0);
190         if (!argc)
191                 exit(0);
192
193         if (permit(uid, groups, ngroups, &rule, target, argv[0],
194             (const char **)argv + 1)) {
195                 printf("permit%s\n", (rule->options & NOPASS) ? " nopass" : "");
196                 exit(0);
197         } else {
198                 printf("deny\n");
199                 exit(1);
200         }
201 }
202
203 int
204 mygetpwuid_r(uid_t uid, struct passwd *pwd, struct passwd **result)
205 {
206         int rv;
207         char *buf;
208         static long pwsz = 0;
209         size_t buflen;
210
211         *result = NULL;
212
213         if (pwsz == 0)
214                 pwsz = sysconf(_SC_GETPW_R_SIZE_MAX);
215
216         buflen = pwsz > 0 ? pwsz : 1024;
217
218         buf = malloc(buflen);
219         if (buf == NULL)
220                 return errno;
221
222         while ((rv = getpwuid_r(uid, pwd, buf, buflen, result)) == ERANGE) {
223                 size_t newsz;
224                 newsz = buflen * 2;
225                 if (newsz < buflen)
226                         return rv;
227                 buflen = newsz;
228                 buf = realloc(buf, buflen);
229                 if (buf == NULL)
230                         return errno;
231         }
232
233         return rv;
234 }
235
236 int
237 main(int argc, char **argv)
238 {
239         const char *safepath = "/bin:/sbin:/usr/bin:/usr/sbin:"
240             "/usr/local/bin:/usr/local/sbin";
241         const char *confpath = NULL;
242         char *shargv[] = { NULL, NULL };
243         char *sh;
244         const char *p;
245         const char *cmd;
246         char cmdline[LINE_MAX];
247         struct passwd mypwstore, targpwstore;
248         struct passwd *mypw, *targpw;
249         const struct rule *rule;
250         uid_t uid;
251         uid_t target = 0;
252         gid_t groups[NGROUPS_MAX + 1];
253         int ngroups;
254         int i, ch, rv;
255         int sflag = 0;
256         int nflag = 0;
257         char cwdpath[PATH_MAX];
258         const char *cwd;
259         char **envp;
260
261         setprogname("doas");
262
263         closefrom(STDERR_FILENO + 1);
264
265         uid = getuid();
266
267         while ((ch = getopt(argc, argv, "+C:Lnsu:")) != -1) {
268                 switch (ch) {
269                 case 'C':
270                         confpath = optarg;
271                         break;
272                 case 'L':
273 #if defined(USE_TIMESTAMP)
274                         exit(timestamp_clear() == -1);
275 #else
276                         exit(0);
277 #endif
278                 case 'u':
279                         if (parseuid(optarg, &target) != 0)
280                                 errx(1, "unknown user");
281                         break;
282                 case 'n':
283                         nflag = 1;
284                         break;
285                 case 's':
286                         sflag = 1;
287                         break;
288                 default:
289                         usage();
290                         break;
291                 }
292         }
293         argv += optind;
294         argc -= optind;
295
296         if (confpath) {
297                 if (sflag)
298                         usage();
299         } else if ((!sflag && !argc) || (sflag && argc))
300                 usage();
301
302         rv = mygetpwuid_r(uid, &mypwstore, &mypw);
303         if (rv != 0)
304                 err(1, "getpwuid_r failed");
305         if (mypw == NULL)
306                 errx(1, "no passwd entry for self");
307         ngroups = getgroups(NGROUPS_MAX, groups);
308         if (ngroups == -1)
309                 err(1, "can't get groups");
310         groups[ngroups++] = getgid();
311
312         if (sflag) {
313                 sh = getenv("SHELL");
314                 if (sh == NULL || *sh == '\0') {
315                         shargv[0] = mypw->pw_shell;
316                 } else
317                         shargv[0] = sh;
318                 argv = shargv;
319                 argc = 1;
320         }
321
322         if (confpath) {
323                 checkconfig(confpath, argc, argv, uid, groups, ngroups,
324                     target);
325                 exit(1);        /* fail safe */
326         }
327
328         if (geteuid())
329                 errx(1, "not installed setuid");
330
331         parseconfig(DOAS_CONF, 1);
332
333         /* cmdline is used only for logging, no need to abort on truncate */
334         (void)strlcpy(cmdline, argv[0], sizeof(cmdline));
335         for (i = 1; i < argc; i++) {
336                 if (strlcat(cmdline, " ", sizeof(cmdline)) >= sizeof(cmdline))
337                         break;
338                 if (strlcat(cmdline, argv[i], sizeof(cmdline)) >= sizeof(cmdline))
339                         break;
340         }
341
342         cmd = argv[0];
343         if (!permit(uid, groups, ngroups, &rule, target, cmd,
344             (const char **)argv + 1)) {
345                 syslog(LOG_AUTHPRIV | LOG_NOTICE,
346                     "command not permitted for %s: %s", mypw->pw_name, cmdline);
347                 errc(1, EPERM, NULL);
348         }
349
350 #if defined(USE_SHADOW)
351         if (!(rule->options & NOPASS)) {
352                 if (nflag)
353                         errx(1, "Authorization required");
354
355                 shadowauth(mypw->pw_name, rule->options & PERSIST);
356         }
357 #elif !defined(USE_PAM)
358         /* no authentication provider, only allow NOPASS rules */
359         (void) nflag;
360         if (!(rule->options & NOPASS))
361                 errx(1, "Authorization required");
362 #endif
363
364         if ((p = getenv("PATH")) != NULL)
365                 formerpath = strdup(p);
366         if (formerpath == NULL)
367                 formerpath = "";
368
369         if (rule->cmd) {
370                 if (setenv("PATH", safepath, 1) == -1)
371                         err(1, "failed to set PATH '%s'", safepath);
372         }
373
374         rv = mygetpwuid_r(target, &targpwstore, &targpw);
375         if (rv != 0)
376                 err(1, "getpwuid_r failed");
377         if (targpw == NULL)
378                 errx(1, "no passwd entry for target");
379
380 #if defined(USE_PAM)
381         pamauth(targpw->pw_name, mypw->pw_name, !nflag, rule->options & NOPASS,
382             rule->options & PERSIST);
383 #endif
384
385 #ifdef HAVE_LOGIN_CAP_H
386         if (setusercontext(NULL, targpw, target, LOGIN_SETGROUP |
387             LOGIN_SETPRIORITY | LOGIN_SETRESOURCES | LOGIN_SETUMASK |
388             LOGIN_SETUSER) != 0)
389                 errx(1, "failed to set user context for target");
390 #else
391         if (setresgid(targpw->pw_gid, targpw->pw_gid, targpw->pw_gid) != 0)
392                 err(1, "setresgid");
393         if (initgroups(targpw->pw_name, targpw->pw_gid) != 0)
394                 err(1, "initgroups");
395         if (setresuid(target, target, target) != 0)
396                 err(1, "setresuid");
397 #endif
398
399         if (getcwd(cwdpath, sizeof(cwdpath)) == NULL)
400                 cwd = "(failed)";
401         else
402                 cwd = cwdpath;
403
404         if (!(rule->options & NOLOG)) {
405                 syslog(LOG_AUTHPRIV | LOG_INFO,
406                     "%s ran command %s as %s from %s",
407                     mypw->pw_name, cmdline, targpw->pw_name, cwd);
408         }
409
410         envp = prepenv(rule, mypw, targpw);
411
412         /* setusercontext set path for the next process, so reset it for us */
413         if (rule->cmd) {
414                 if (setenv("PATH", safepath, 1) == -1)
415                         err(1, "failed to set PATH '%s'", safepath);
416         } else {
417                 if (setenv("PATH", formerpath, 1) == -1)
418                         err(1, "failed to set PATH '%s'", formerpath);
419         }
420         execvpe(cmd, argv, envp);
421         if (errno == ENOENT)
422                 errx(1, "%s: command not found", cmd);
423         err(1, "%s", cmd);
424 }