masqmail

annotate src/peopen.c @ 259:f4117fd5a163

made default mode sensible Until now in case no mode was specified, a message would have been read from stdin but then it would fail because no recipients had been given on the cmd line. (This had been useful only if -t was given.) Now in case of no mode being specified neither address arguments or -t is given then the version message is printed, otherwise mode accept is entered (as until yet).
author markus schnalke <meillo@marmaro.de>
date Thu, 02 Dec 2010 16:22:55 -0300
parents 7cdd30429cc9
children b27f66555ba8
rev   line source
meillo@0 1 /* This a snippet I found in sourceforge. I just changed the identing
meillo@29 2 style to my own and deleted the main function. -- oku
meillo@29 3 The functions destroy_argv() and create_argv() were added by oku.
meillo@29 4 */
meillo@0 5
meillo@0 6 #include <errno.h>
meillo@0 7 #include <stdio.h>
meillo@0 8 #include <stdlib.h>
meillo@0 9 #include <unistd.h>
meillo@0 10 #include <string.h>
meillo@0 11 #include <sys/types.h>
meillo@15 12 #include <sysexits.h>
meillo@0 13
meillo@0 14 #include "peopen.h"
meillo@0 15 #include "masqmail.h"
meillo@0 16
meillo@10 17 static void
meillo@10 18 destroy_argv(char **arr)
meillo@0 19 {
meillo@10 20 char *p = arr[0];
meillo@10 21 int i = 0;
meillo@0 22
meillo@10 23 while (p) {
meillo@10 24 free(p);
meillo@10 25 p = arr[i++];
meillo@10 26 }
meillo@10 27 free(arr);
meillo@0 28 }
meillo@0 29
meillo@10 30 static char**
meillo@10 31 create_argv(const char *cmd, int count)
meillo@0 32 {
meillo@10 33 char buf[strlen(cmd) + 1];
meillo@10 34 char **arr, *q;
meillo@10 35 const char *p;
meillo@10 36 int i = 0;
meillo@0 37
meillo@27 38 arr = (char **) g_malloc(sizeof(char *) * count);
meillo@0 39
meillo@10 40 p = cmd;
meillo@10 41 while (*p && i < (count - 1)) {
meillo@10 42 while (*p && isspace(*p))
meillo@10 43 p++;
meillo@10 44 q = buf;
meillo@10 45 while (*p && !isspace(*p))
meillo@10 46 *q++ = *p++;
meillo@15 47 *q = '\0';
meillo@10 48 arr[i++] = strdup(buf);
meillo@10 49 while (*p && isspace(*p))
meillo@10 50 p++;
meillo@10 51 }
meillo@10 52 arr[i] = NULL;
meillo@10 53
meillo@10 54 return arr;
meillo@0 55 }
meillo@0 56
meillo@10 57 FILE*
meillo@10 58 peidopen(const char *command, const char *type, char *const envp[], int *ret_pid, uid_t uid, gid_t gid)
meillo@0 59 {
meillo@10 60 enum { Read, Write } mode;
meillo@10 61 int pipe_fd[2];
meillo@10 62 pid_t pid;
meillo@0 63
meillo@10 64 if (command == NULL || type == NULL) {
meillo@10 65 errno = EINVAL;
meillo@10 66 return NULL;
meillo@10 67 }
meillo@0 68
meillo@10 69 if (strcmp(type, "r")) {
meillo@10 70 if (strcmp(type, "w")) {
meillo@10 71 errno = EINVAL;
meillo@10 72 return NULL;
meillo@10 73 } else
meillo@10 74 mode = Write;
meillo@10 75 } else
meillo@10 76 mode = Read;
meillo@0 77
meillo@10 78 if (pipe(pipe_fd) == -1)
meillo@10 79 return NULL;
meillo@0 80
meillo@10 81 switch (pid = fork()) {
meillo@10 82 case 0: /* child thread */
meillo@0 83
meillo@10 84 {
meillo@10 85 int i, max_fd = sysconf(_SC_OPEN_MAX);
meillo@10 86
meillo@10 87 if (max_fd <= 0)
meillo@10 88 max_fd = 64;
meillo@10 89 for (i = 0; i < max_fd; i++)
meillo@10 90 if ((i != pipe_fd[0]) && (i != pipe_fd[1]))
meillo@10 91 close(i);
meillo@10 92 }
meillo@10 93 if (close(pipe_fd[mode == Read ? 0 : 1]) != -1 &&
meillo@10 94 dup2(pipe_fd[mode == Read ? 1 : 0],
meillo@10 95 mode == Read ? STDOUT_FILENO : STDIN_FILENO) != -1) {
meillo@207 96 /* char *argv [] = { "/bin/sh", "-c", (char*) command, NULL }; */
meillo@10 97 char **argv = create_argv(command, 10);
meillo@10 98 int ret;
meillo@10 99
meillo@10 100 if (uid != (uid_t) - 1) {
meillo@10 101 if ((ret = seteuid(0)) != 0) {
meillo@10 102 exit(EX_NOPERM);
meillo@10 103 }
meillo@10 104 }
meillo@10 105 if (gid != (gid_t) - 1) {
meillo@10 106 if ((ret = setgid(gid)) != 0) {
meillo@10 107 exit(EX_NOPERM);
meillo@10 108 }
meillo@10 109 }
meillo@10 110 if (uid != (uid_t) - 1) {
meillo@10 111 if ((ret = setuid(uid)) != 0) {
meillo@10 112 exit(EX_NOPERM);
meillo@10 113 }
meillo@10 114 }
meillo@10 115 execve(*argv, argv, envp);
meillo@10 116 }
meillo@10 117
meillo@10 118 _exit(errno);
meillo@10 119
meillo@10 120 default: /* parent thread */
meillo@10 121 *ret_pid = pid;
meillo@10 122 close(pipe_fd[mode == Read ? 1 : 0]);
meillo@10 123 return fdopen(pipe_fd[mode == Read ? 0 : 1], type);
meillo@10 124
meillo@10 125 case -1:
meillo@10 126 close(pipe_fd[0]);
meillo@10 127 close(pipe_fd[1]);
meillo@10 128 return NULL;
meillo@0 129 }
meillo@0 130 }
meillo@0 131
meillo@10 132 FILE*
meillo@10 133 peopen(const char *command, const char *type, char *const envp[], int *ret_pid)
meillo@0 134 {
meillo@10 135 return peidopen(command, type, envp, ret_pid, -1, -1);
meillo@0 136 }