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