[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 31 2006-01-20 14:46:56Z 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 | |
---|
[5] | 36 | #include "run_email2trac.h" |
---|
| 37 | |
---|
| 38 | #ifndef DEBUG |
---|
| 39 | #define DEBUG 0 |
---|
| 40 | #endif |
---|
| 41 | |
---|
| 42 | int main(int argc, char** argv) { |
---|
| 43 | |
---|
| 44 | int i,j; |
---|
| 45 | int caller = getuid(); |
---|
| 46 | int status; |
---|
| 47 | char trac_script[255]; |
---|
| 48 | char** trac_script_args; |
---|
| 49 | struct passwd *TRAC; |
---|
| 50 | struct passwd *MTA; |
---|
| 51 | struct stat script_attrs; |
---|
| 52 | |
---|
| 53 | strcat(trac_script,TRAC_SCRIPT_PATH); |
---|
| 54 | strcat(trac_script,"/"); |
---|
| 55 | strcat(trac_script,TRAC_SCRIPT_NAME); |
---|
| 56 | |
---|
| 57 | /* First copy arguments passed to the wrapper as scripts arguments |
---|
| 58 | after filtering out some of the possible script options */ |
---|
| 59 | trac_script_args = (char**) malloc((argc+1)*sizeof(char*)); |
---|
| 60 | trac_script_args[0] = TRAC_SCRIPT_NAME; |
---|
| 61 | for (i=j=1; i<argc; i++) { |
---|
| 62 | if ( (strcmp(argv[i],"--file") == 0) || |
---|
| 63 | (strcmp(argv[i],"-f") == 0) ) { |
---|
| 64 | i++; |
---|
| 65 | continue; |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | trac_script_args[j] = argv[i]; |
---|
| 69 | j++; |
---|
| 70 | } |
---|
| 71 | trac_script_args[j] = NULL; |
---|
| 72 | |
---|
| 73 | |
---|
| 74 | /* Check caller */ |
---|
| 75 | |
---|
| 76 | MTA = getpwnam(MTA_USER); |
---|
| 77 | if ( caller != MTA->pw_uid ) { |
---|
| 78 | if ( DEBUG ) printf("Invalid caller UID (%d)\n",caller); |
---|
| 79 | return -2; /* 254 : Invalid caller */ |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | /* set UID/GID to Trac (or apache) user */ |
---|
| 84 | if ( TRAC = getpwnam(TRAC_USER) ) { |
---|
| 85 | setuid(TRAC->pw_uid); |
---|
| 86 | setgid(TRAC->pw_gid); |
---|
| 87 | } else { |
---|
| 88 | if ( DEBUG ) printf("Invalid Trac user (%s)\n",TRAC_USER); |
---|
| 89 | return -3; /* 253 : Trac user not found */ |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /* Check that script exists */ |
---|
| 93 | if ( stat(trac_script,&script_attrs) ) { |
---|
| 94 | if ( DEBUG ) printf("Script not found (%s)\n",trac_script); |
---|
| 95 | return -4; /* 252 : script not found */ |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /* Execute script */ |
---|
| 99 | status = execv(trac_script, trac_script_args); |
---|
| 100 | if ( DEBUG ) printf("Script %s execution failure (error=%d). Check permission and interpreter path.\n",trac_script,status); |
---|
| 101 | return -1; /* 255 : should never reach this point */ |
---|
| 102 | |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /* EOB */ |
---|