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