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