Annotation of embedaddon/sudo/plugins/sample/sample_plugin.c, revision 1.1.1.5
1.1 misho 1: /*
1.1.1.4 misho 2: * Copyright (c) 2010-2013 Todd C. Miller <Todd.Miller@courtesan.com>
1.1 misho 3: *
4: * Permission to use, copy, modify, and distribute this software for any
5: * purpose with or without fee is hereby granted, provided that the above
6: * copyright notice and this permission notice appear in all copies.
7: *
8: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15: */
16:
17: #include <config.h>
18:
19: #include <sys/types.h>
20: #include <sys/stat.h>
21: #include <sys/wait.h>
22:
23: #include <stdio.h>
24: #ifdef STDC_HEADERS
25: # include <stdlib.h>
26: # include <stddef.h>
27: #else
28: # ifdef HAVE_STDLIB_H
29: # include <stdlib.h>
30: # endif
31: #endif /* STDC_HEADERS */
1.1.1.2 misho 32: #ifdef HAVE_STDBOOL_H
33: # include <stdbool.h>
34: #else
35: # include "compat/stdbool.h"
36: #endif /* HAVE_STDBOOL_H */
1.1 misho 37: #ifdef HAVE_STRING_H
38: # if defined(HAVE_MEMORY_H) && !defined(STDC_HEADERS)
39: # include <memory.h>
40: # endif
41: # include <string.h>
42: #endif /* HAVE_STRING_H */
43: #ifdef HAVE_STRINGS_H
44: # include <strings.h>
45: #endif /* HAVE_STRINGS_H */
46: #ifdef HAVE_UNISTD_H
47: # include <unistd.h>
48: #endif /* HAVE_UNISTD_H */
49: #include <ctype.h>
50: #include <fcntl.h>
51: #include <limits.h>
52: #include <grp.h>
53: #include <pwd.h>
54: #include <stdarg.h>
55:
56: #include <pathnames.h>
57: #include "sudo_plugin.h"
1.1.1.5 ! misho 58: #include "sudo_util.h"
1.1 misho 59: #include "missing.h"
60:
61: /*
62: * Sample plugin module that allows any user who knows the password
63: * ("test") to run any command as root. Since there is no credential
64: * caching the validate and invalidate functions are NULL.
65: */
66:
67: #ifdef __TANDEM
68: # define ROOT_UID 65535
69: #else
70: # define ROOT_UID 0
71: #endif
72:
73: static struct plugin_state {
74: char **envp;
75: char * const *settings;
76: char * const *user_info;
77: } plugin_state;
78: static sudo_conv_t sudo_conv;
79: static sudo_printf_t sudo_log;
80: static FILE *input, *output;
81: static uid_t runas_uid = ROOT_UID;
82: static gid_t runas_gid = -1;
1.1.1.2 misho 83: static int use_sudoedit = false;
1.1 misho 84:
85: /*
86: * Allocate storage for a name=value string and return it.
87: */
88: static char *
89: fmt_string(const char *var, const char *val)
90: {
91: size_t var_len = strlen(var);
92: size_t val_len = strlen(val);
93: char *cp, *str;
94:
95: cp = str = malloc(var_len + 1 + val_len + 1);
96: if (str != NULL) {
97: memcpy(cp, var, var_len);
98: cp += var_len;
99: *cp++ = '=';
100: memcpy(cp, val, val_len);
101: cp += val_len;
102: *cp = '\0';
103: }
104:
105: return str;
106: }
107:
108: /*
109: * Plugin policy open function.
110: */
111: static int
112: policy_open(unsigned int version, sudo_conv_t conversation,
113: sudo_printf_t sudo_printf, char * const settings[],
1.1.1.2 misho 114: char * const user_info[], char * const user_env[], char * const args[])
1.1 misho 115: {
116: char * const *ui;
117: struct passwd *pw;
118: const char *runas_user = NULL;
119: struct group *gr;
120: const char *runas_group = NULL;
121:
122: if (!sudo_conv)
123: sudo_conv = conversation;
124: if (!sudo_log)
125: sudo_log = sudo_printf;
126:
127: if (SUDO_API_VERSION_GET_MAJOR(version) != SUDO_API_VERSION_MAJOR) {
128: sudo_log(SUDO_CONV_ERROR_MSG,
129: "the sample plugin requires API version %d.x\n",
130: SUDO_API_VERSION_MAJOR);
1.1.1.2 misho 131: return -1;
1.1 misho 132: }
133:
134: /* Only allow commands to be run as root. */
135: for (ui = settings; *ui != NULL; ui++) {
136: if (strncmp(*ui, "runas_user=", sizeof("runas_user=") - 1) == 0) {
137: runas_user = *ui + sizeof("runas_user=") - 1;
138: }
139: if (strncmp(*ui, "runas_group=", sizeof("runas_group=") - 1) == 0) {
140: runas_group = *ui + sizeof("runas_group=") - 1;
141: }
142: if (strncmp(*ui, "progname=", sizeof("progname=") - 1) == 0) {
1.1.1.5 ! misho 143: initprogname(*ui + sizeof("progname=") - 1);
1.1 misho 144: }
145: /* Check to see if sudo was called as sudoedit or with -e flag. */
146: if (strncmp(*ui, "sudoedit=", sizeof("sudoedit=") - 1) == 0) {
147: if (strcasecmp(*ui + sizeof("sudoedit=") - 1, "true") == 0)
1.1.1.2 misho 148: use_sudoedit = true;
1.1 misho 149: }
150: /* This plugin doesn't support running sudo with no arguments. */
151: if (strncmp(*ui, "implied_shell=", sizeof("implied_shell=") - 1) == 0) {
152: if (strcasecmp(*ui + sizeof("implied_shell=") - 1, "true") == 0)
153: return -2; /* usage error */
154: }
155: }
156: if (runas_user != NULL) {
157: if ((pw = getpwnam(runas_user)) == NULL) {
158: sudo_log(SUDO_CONV_ERROR_MSG, "unknown user %s\n", runas_user);
159: return 0;
160: }
161: runas_uid = pw->pw_uid;
162: }
163: if (runas_group != NULL) {
164: if ((gr = getgrnam(runas_group)) == NULL) {
165: sudo_log(SUDO_CONV_ERROR_MSG, "unknown group %s\n", runas_group);
166: return 0;
167: }
168: runas_gid = gr->gr_gid;
169: }
170:
171: /* Plugin state. */
172: plugin_state.envp = (char **)user_env;
173: plugin_state.settings = settings;
174: plugin_state.user_info = user_info;
175:
176: return 1;
177: }
178:
179: static char *
180: find_in_path(char *command, char **envp)
181: {
182: struct stat sb;
183: char *path, *path0, **ep, *cp;
184: char pathbuf[PATH_MAX], *qualified = NULL;
185:
186: if (strchr(command, '/') != NULL)
187: return command;
188:
189: path = _PATH_DEFPATH;
190: for (ep = plugin_state.envp; *ep != NULL; ep++) {
191: if (strncmp(*ep, "PATH=", 5) == 0) {
192: path = *ep + 5;
193: break;
194: }
195: }
196: path = path0 = strdup(path);
197: do {
198: if ((cp = strchr(path, ':')))
199: *cp = '\0';
200: snprintf(pathbuf, sizeof(pathbuf), "%s/%s", *path ? path : ".",
201: command);
202: if (stat(pathbuf, &sb) == 0) {
203: if (S_ISREG(sb.st_mode) && (sb.st_mode & 0000111)) {
204: qualified = pathbuf;
205: break;
206: }
207: }
208: path = cp + 1;
209: } while (cp != NULL);
210: free(path0);
211: return qualified ? strdup(qualified) : NULL;
212: }
213:
214: static int
215: check_passwd(void)
216: {
217: struct sudo_conv_message msg;
218: struct sudo_conv_reply repl;
219:
220: /* Prompt user for password via conversation function. */
221: memset(&msg, 0, sizeof(msg));
222: msg.msg_type = SUDO_CONV_PROMPT_ECHO_OFF;
223: msg.msg = "Password: ";
224: memset(&repl, 0, sizeof(repl));
225: sudo_conv(1, &msg, &repl);
226: if (repl.reply == NULL) {
227: sudo_log(SUDO_CONV_ERROR_MSG, "missing password\n");
1.1.1.2 misho 228: return false;
1.1 misho 229: }
230: if (strcmp(repl.reply, "test") != 0) {
231: sudo_log(SUDO_CONV_ERROR_MSG, "incorrect password\n");
1.1.1.2 misho 232: return false;
1.1 misho 233: }
1.1.1.2 misho 234: return true;
1.1 misho 235: }
236:
237: static char **
1.1.1.3 misho 238: build_command_info(const char *command)
1.1 misho 239: {
240: static char **command_info;
241: int i = 0;
242:
243: /* Setup command info. */
244: command_info = calloc(32, sizeof(char *));
245: if (command_info == NULL)
246: return NULL;
247: if ((command_info[i++] = fmt_string("command", command)) == NULL ||
248: asprintf(&command_info[i++], "runas_euid=%ld", (long)runas_uid) == -1 ||
249: asprintf(&command_info[i++], "runas_uid=%ld", (long)runas_uid) == -1) {
250: return NULL;
251: }
252: if (runas_gid != -1) {
253: if (asprintf(&command_info[i++], "runas_gid=%ld", (long)runas_gid) == -1 ||
254: asprintf(&command_info[i++], "runas_egid=%ld", (long)runas_gid) == -1) {
255: return NULL;
256: }
257: }
258: if (use_sudoedit) {
259: command_info[i] = strdup("sudoedit=true");
260: if (command_info[i++] == NULL)
261: return NULL;
262: }
263: #ifdef USE_TIMEOUT
264: command_info[i++] = "timeout=30";
265: #endif
266: return command_info;
267: }
268:
269: static char *
270: find_editor(int nfiles, char * const files[], char **argv_out[])
271: {
272: char *cp, **ep, **nargv, *editor, *editor_path;
273: int ac, i, nargc, wasblank;
274:
275: /* Lookup EDITOR in user's environment. */
276: editor = _PATH_VI;
277: for (ep = plugin_state.envp; *ep != NULL; ep++) {
278: if (strncmp(*ep, "EDITOR=", 7) == 0) {
279: editor = *ep + 7;
280: break;
281: }
282: }
283: editor = strdup(editor);
284: if (editor == NULL) {
285: sudo_log(SUDO_CONV_ERROR_MSG, "unable to allocate memory\n");
286: return NULL;
287: }
288:
289: /*
290: * Split editor into an argument vector; editor is reused (do not free).
291: * The EDITOR environment variables may contain command
292: * line args so look for those and alloc space for them too.
293: */
294: nargc = 1;
295: for (wasblank = 0, cp = editor; *cp != '\0'; cp++) {
296: if (isblank((unsigned char) *cp))
297: wasblank = 1;
298: else if (wasblank) {
299: wasblank = 0;
300: nargc++;
301: }
302: }
303: /* If we can't find the editor in the user's PATH, give up. */
304: cp = strtok(editor, " \t");
305: if (cp == NULL ||
306: (editor_path = find_in_path(editor, plugin_state.envp)) == NULL) {
1.1.1.4 misho 307: free(editor);
1.1 misho 308: return NULL;
309: }
1.1.1.3 misho 310: if (editor_path != editor)
311: free(editor);
1.1 misho 312: nargv = (char **) malloc((nargc + 1 + nfiles + 1) * sizeof(char *));
313: if (nargv == NULL) {
314: sudo_log(SUDO_CONV_ERROR_MSG, "unable to allocate memory\n");
1.1.1.3 misho 315: free(editor_path);
1.1 misho 316: return NULL;
317: }
318: for (ac = 0; cp != NULL && ac < nargc; ac++) {
319: nargv[ac] = cp;
320: cp = strtok(NULL, " \t");
321: }
322: nargv[ac++] = "--";
323: for (i = 0; i < nfiles; )
324: nargv[ac++] = files[i++];
325: nargv[ac] = NULL;
326:
327: *argv_out = nargv;
328: return editor_path;
329: }
330:
331: /*
332: * Plugin policy check function.
333: * Simple example that prompts for a password, hard-coded to "test".
334: */
335: static int
336: policy_check(int argc, char * const argv[],
337: char *env_add[], char **command_info_out[],
338: char **argv_out[], char **user_env_out[])
339: {
340: char *command;
341:
342: if (!argc || argv[0] == NULL) {
343: sudo_log(SUDO_CONV_ERROR_MSG, "no command specified\n");
1.1.1.2 misho 344: return false;
1.1 misho 345: }
346:
347: if (!check_passwd())
1.1.1.2 misho 348: return false;
1.1 misho 349:
350: command = find_in_path(argv[0], plugin_state.envp);
351: if (command == NULL) {
352: sudo_log(SUDO_CONV_ERROR_MSG, "%s: command not found\n", argv[0]);
1.1.1.2 misho 353: return false;
1.1 misho 354: }
355:
356: /* If "sudo vi" is run, auto-convert to sudoedit. */
357: if (strcmp(command, _PATH_VI) == 0)
1.1.1.2 misho 358: use_sudoedit = true;
1.1 misho 359:
360: if (use_sudoedit) {
361: /* Rebuild argv using editor */
1.1.1.3 misho 362: free(command);
1.1 misho 363: command = find_editor(argc - 1, argv + 1, argv_out);
364: if (command == NULL) {
365: sudo_log(SUDO_CONV_ERROR_MSG, "unable to find valid editor\n");
1.1.1.2 misho 366: return -1;
1.1 misho 367: }
1.1.1.2 misho 368: use_sudoedit = true;
1.1 misho 369: } else {
370: /* No changes needd to argv */
371: *argv_out = (char **)argv;
372: }
373:
374: /* No changes to envp */
375: *user_env_out = plugin_state.envp;
376:
377: /* Setup command info. */
378: *command_info_out = build_command_info(command);
1.1.1.3 misho 379: free(command);
1.1 misho 380: if (*command_info_out == NULL) {
381: sudo_log(SUDO_CONV_ERROR_MSG, "out of memory\n");
1.1.1.2 misho 382: return -1;
1.1 misho 383: }
384:
1.1.1.2 misho 385: return true;
1.1 misho 386: }
387:
388: static int
389: policy_list(int argc, char * const argv[], int verbose, const char *list_user)
390: {
391: /*
392: * List user's capabilities.
393: */
394: sudo_log(SUDO_CONV_INFO_MSG, "Validated users may run any command\n");
1.1.1.2 misho 395: return true;
1.1 misho 396: }
397:
398: static int
399: policy_version(int verbose)
400: {
401: sudo_log(SUDO_CONV_INFO_MSG, "Sample policy plugin version %s\n", PACKAGE_VERSION);
1.1.1.2 misho 402: return true;
1.1 misho 403: }
404:
405: static void
406: policy_close(int exit_status, int error)
407: {
408: /*
409: * The policy might log the command exit status here.
410: * In this example, we just print a message.
411: */
412: if (error) {
413: sudo_log(SUDO_CONV_ERROR_MSG, "Command error: %s\n", strerror(error));
414: } else {
415: if (WIFEXITED(exit_status)) {
416: sudo_log(SUDO_CONV_INFO_MSG, "Command exited with status %d\n",
417: WEXITSTATUS(exit_status));
418: } else if (WIFSIGNALED(exit_status)) {
419: sudo_log(SUDO_CONV_INFO_MSG, "Command killed by signal %d\n",
420: WTERMSIG(exit_status));
421: }
422: }
423: }
424:
425: static int
426: io_open(unsigned int version, sudo_conv_t conversation,
427: sudo_printf_t sudo_printf, char * const settings[],
428: char * const user_info[], char * const command_info[],
1.1.1.2 misho 429: int argc, char * const argv[], char * const user_env[], char * const args[])
1.1 misho 430: {
431: int fd;
432: char path[PATH_MAX];
433:
434: if (!sudo_conv)
435: sudo_conv = conversation;
436: if (!sudo_log)
437: sudo_log = sudo_printf;
438:
439: /* Open input and output files. */
440: snprintf(path, sizeof(path), "/var/tmp/sample-%u.output",
441: (unsigned int)getpid());
442: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, 0644);
443: if (fd == -1)
1.1.1.2 misho 444: return false;
1.1 misho 445: output = fdopen(fd, "w");
446:
447: snprintf(path, sizeof(path), "/var/tmp/sample-%u.input",
448: (unsigned int)getpid());
449: fd = open(path, O_WRONLY|O_CREAT|O_EXCL, 0644);
450: if (fd == -1)
1.1.1.2 misho 451: return false;
1.1 misho 452: input = fdopen(fd, "w");
453:
1.1.1.2 misho 454: return true;
1.1 misho 455: }
456:
457: static void
458: io_close(int exit_status, int error)
459: {
460: fclose(input);
461: fclose(output);
462: }
463:
464: static int
465: io_version(int verbose)
466: {
467: sudo_log(SUDO_CONV_INFO_MSG, "Sample I/O plugin version %s\n",
468: PACKAGE_VERSION);
1.1.1.2 misho 469: return true;
1.1 misho 470: }
471:
472: static int
473: io_log_input(const char *buf, unsigned int len)
474: {
1.1.1.2 misho 475: ignore_result(fwrite(buf, len, 1, input));
476: return true;
1.1 misho 477: }
478:
479: static int
480: io_log_output(const char *buf, unsigned int len)
481: {
1.1.1.2 misho 482: ignore_result(fwrite(buf, len, 1, output));
483: return true;
1.1 misho 484: }
485:
486: struct policy_plugin sample_policy = {
487: SUDO_POLICY_PLUGIN,
488: SUDO_API_VERSION,
489: policy_open,
490: policy_close,
491: policy_version,
492: policy_check,
493: policy_list,
494: NULL, /* validate */
1.1.1.2 misho 495: NULL, /* invalidate */
496: NULL, /* init_session */
497: NULL, /* register_hooks */
498: NULL /* deregister_hooks */
1.1 misho 499: };
500:
501: /*
502: * Note: This plugin does not differentiate between tty and pipe I/O.
503: * It all gets logged to the same file.
504: */
1.1.1.4 misho 505: __dso_public struct io_plugin sample_io = {
1.1 misho 506: SUDO_IO_PLUGIN,
507: SUDO_API_VERSION,
508: io_open,
509: io_close,
510: io_version,
511: io_log_input, /* tty input */
512: io_log_output, /* tty output */
513: io_log_input, /* command stdin if not tty */
514: io_log_output, /* command stdout if not tty */
515: io_log_output /* command stderr if not tty */
516: };
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>