[5] | 1 | /* |
---|
| 2 | run_email2trac.c |
---|
[24] | 3 | Authors: Bas van der Vlies, Walter de Jong and Michel Jouvin |
---|
| 4 | SVN Info: |
---|
| 5 | $Id: run_email2trac.c 318 2010-02-15 14:27:14Z bas $ |
---|
[5] | 6 | |
---|
| 7 | Only nobody can become the user www-data. Postfix uses this |
---|
| 8 | user to start an program |
---|
| 9 | |
---|
| 10 | # Copyright (C) 2002 |
---|
| 11 | # |
---|
| 12 | # This file is part of the email2trac utils |
---|
| 13 | # |
---|
| 14 | # This program is free software; you can redistribute it and/or modify it |
---|
| 15 | # under the terms of the GNU General Public License as published by the |
---|
| 16 | # Free Software Foundation; either version 2, or (at your option) any |
---|
| 17 | # later version. |
---|
| 18 | # |
---|
| 19 | # This program is distributed in the hope that it will be useful, |
---|
| 20 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 21 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 22 | # GNU General Public License for more details. |
---|
| 23 | # |
---|
| 24 | # You should have received a copy of the GNU General Public License |
---|
| 25 | # along with this program; if not, write to the Free Software |
---|
| 26 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
---|
| 27 | # |
---|
| 28 | */ |
---|
| 29 | #include <stdlib.h> |
---|
| 30 | #include <unistd.h> |
---|
| 31 | #include <pwd.h> |
---|
| 32 | #include <sys/stat.h> |
---|
| 33 | #include <string.h> |
---|
[31] | 34 | #include <stdio.h> |
---|
[35] | 35 | #include <limits.h> |
---|
[31] | 36 | |
---|
[5] | 37 | #include "run_email2trac.h" |
---|
| 38 | |
---|
| 39 | #ifndef DEBUG |
---|
| 40 | #define DEBUG 0 |
---|
| 41 | #endif |
---|
| 42 | |
---|
[130] | 43 | void check_username(char *name) |
---|
[110] | 44 | { |
---|
| 45 | if ( strlen(name) > 30 ) { |
---|
| 46 | if ( DEBUG ) printf("MTA_USERNAME is to large; %s\n", name); |
---|
| 47 | exit(-1); |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | |
---|
[5] | 51 | int main(int argc, char** argv) { |
---|
| 52 | |
---|
| 53 | int i,j; |
---|
| 54 | int caller = getuid(); |
---|
| 55 | int status; |
---|
[35] | 56 | |
---|
| 57 | char **trac_script_args; |
---|
[318] | 58 | char *python_egg_cache = NULL; |
---|
[5] | 59 | struct passwd *TRAC; |
---|
| 60 | struct passwd *MTA; |
---|
| 61 | struct stat script_attrs; |
---|
[202] | 62 | const char *trac_script = TRAC_SCRIPT_PATH "/" TRAC_SCRIPT_NAME; |
---|
[35] | 63 | |
---|
| 64 | /* |
---|
| 65 | printf("trac_script = %s\n", trac_script); |
---|
| 66 | */ |
---|
| 67 | |
---|
[5] | 68 | /* First copy arguments passed to the wrapper as scripts arguments |
---|
| 69 | after filtering out some of the possible script options */ |
---|
[202] | 70 | |
---|
[5] | 71 | trac_script_args = (char**) malloc((argc+1)*sizeof(char*)); |
---|
[202] | 72 | if (trac_script_args == NULL) { |
---|
| 73 | if ( DEBUG ) printf("malloc failed\n"); |
---|
| 74 | return 1; |
---|
| 75 | } |
---|
[5] | 76 | trac_script_args[0] = TRAC_SCRIPT_NAME; |
---|
| 77 | for (i=j=1; i<argc; i++) { |
---|
| 78 | if ( (strcmp(argv[i],"--file") == 0) || |
---|
| 79 | (strcmp(argv[i],"-f") == 0) ) { |
---|
| 80 | i++; |
---|
| 81 | continue; |
---|
| 82 | } |
---|
[308] | 83 | else if ( (strcmp(argv[i],"--eggcache") == 0) || |
---|
| 84 | (strcmp(argv[i],"-e") == 0) ) { |
---|
| 85 | i++; |
---|
| 86 | python_egg_cache = argv[i]; |
---|
| 87 | continue; |
---|
| 88 | } |
---|
[5] | 89 | |
---|
| 90 | trac_script_args[j] = argv[i]; |
---|
| 91 | j++; |
---|
| 92 | } |
---|
| 93 | trac_script_args[j] = NULL; |
---|
| 94 | |
---|
| 95 | /* Check caller */ |
---|
[110] | 96 | check_username(MTA_USER); |
---|
[5] | 97 | MTA = getpwnam(MTA_USER); |
---|
[110] | 98 | |
---|
| 99 | if ( MTA == NULL ) { |
---|
| 100 | if ( DEBUG ) printf("Invalid MTA user (%s)\n", MTA_USER); |
---|
| 101 | return -3; /* 253 : MTA user not found */ |
---|
| 102 | } |
---|
| 103 | |
---|
[5] | 104 | if ( caller != MTA->pw_uid ) { |
---|
| 105 | if ( DEBUG ) printf("Invalid caller UID (%d)\n",caller); |
---|
| 106 | return -2; /* 254 : Invalid caller */ |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /* set UID/GID to Trac (or apache) user */ |
---|
[110] | 110 | check_username(TRAC_USER); |
---|
[5] | 111 | if ( TRAC = getpwnam(TRAC_USER) ) { |
---|
[202] | 112 | if (setgid(TRAC->pw_gid) || setuid(TRAC->pw_uid)) { |
---|
| 113 | if ( DEBUG ) printf("setgid or setuid failed\n"); |
---|
| 114 | return -5; |
---|
| 115 | } |
---|
[5] | 116 | } else { |
---|
| 117 | if ( DEBUG ) printf("Invalid Trac user (%s)\n",TRAC_USER); |
---|
| 118 | return -3; /* 253 : Trac user not found */ |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | /* Check that script exists */ |
---|
| 122 | if ( stat(trac_script,&script_attrs) ) { |
---|
| 123 | if ( DEBUG ) printf("Script not found (%s)\n",trac_script); |
---|
| 124 | return -4; /* 252 : script not found */ |
---|
| 125 | } |
---|
| 126 | |
---|
[308] | 127 | /* Set PYTHON_EGG_CACHE env variable if we have been told to do so */ |
---|
[318] | 128 | if ( python_egg_cache != NULL ) { |
---|
[308] | 129 | setenv("PYTHON_EGG_CACHE",python_egg_cache ,1); |
---|
| 130 | } |
---|
| 131 | |
---|
[5] | 132 | /* Execute script */ |
---|
| 133 | status = execv(trac_script, trac_script_args); |
---|
[308] | 134 | |
---|
[5] | 135 | if ( DEBUG ) printf("Script %s execution failure (error=%d). Check permission and interpreter path.\n",trac_script,status); |
---|
| 136 | return -1; /* 255 : should never reach this point */ |
---|
| 137 | |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /* EOB */ |
---|