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