1 | /* |
---|
2 | run_email2trac.c |
---|
3 | Authors: Bas van der Vlies, Walter de Jong and Michel Jouvin |
---|
4 | SVN Info: |
---|
5 | $Id: run_email2trac.c 202 2008-05-27 20:43:52Z bas $ |
---|
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> |
---|
34 | #include <stdio.h> |
---|
35 | #include <limits.h> |
---|
36 | |
---|
37 | #include "run_email2trac.h" |
---|
38 | |
---|
39 | #ifndef DEBUG |
---|
40 | #define DEBUG 0 |
---|
41 | #endif |
---|
42 | |
---|
43 | void check_username(char *name) |
---|
44 | { |
---|
45 | if ( strlen(name) > 30 ) { |
---|
46 | if ( DEBUG ) printf("MTA_USERNAME is to large; %s\n", name); |
---|
47 | exit(-1); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | int main(int argc, char** argv) { |
---|
52 | |
---|
53 | int i,j; |
---|
54 | int caller = getuid(); |
---|
55 | int status; |
---|
56 | |
---|
57 | char **trac_script_args; |
---|
58 | struct passwd *TRAC; |
---|
59 | struct passwd *MTA; |
---|
60 | struct stat script_attrs; |
---|
61 | const char *trac_script = TRAC_SCRIPT_PATH "/" TRAC_SCRIPT_NAME; |
---|
62 | |
---|
63 | /* |
---|
64 | printf("trac_script = %s\n", trac_script); |
---|
65 | */ |
---|
66 | |
---|
67 | /* First copy arguments passed to the wrapper as scripts arguments |
---|
68 | after filtering out some of the possible script options */ |
---|
69 | |
---|
70 | trac_script_args = (char**) malloc((argc+1)*sizeof(char*)); |
---|
71 | if (trac_script_args == NULL) { |
---|
72 | if ( DEBUG ) printf("malloc failed\n"); |
---|
73 | return 1; |
---|
74 | } |
---|
75 | trac_script_args[0] = TRAC_SCRIPT_NAME; |
---|
76 | for (i=j=1; i<argc; i++) { |
---|
77 | if ( (strcmp(argv[i],"--file") == 0) || |
---|
78 | (strcmp(argv[i],"-f") == 0) ) { |
---|
79 | i++; |
---|
80 | continue; |
---|
81 | } |
---|
82 | |
---|
83 | trac_script_args[j] = argv[i]; |
---|
84 | j++; |
---|
85 | } |
---|
86 | trac_script_args[j] = NULL; |
---|
87 | |
---|
88 | |
---|
89 | /* Check caller */ |
---|
90 | |
---|
91 | |
---|
92 | check_username(MTA_USER); |
---|
93 | MTA = getpwnam(MTA_USER); |
---|
94 | |
---|
95 | if ( MTA == NULL ) { |
---|
96 | if ( DEBUG ) printf("Invalid MTA user (%s)\n", MTA_USER); |
---|
97 | return -3; /* 253 : MTA user not found */ |
---|
98 | } |
---|
99 | |
---|
100 | if ( caller != MTA->pw_uid ) { |
---|
101 | if ( DEBUG ) printf("Invalid caller UID (%d)\n",caller); |
---|
102 | return -2; /* 254 : Invalid caller */ |
---|
103 | } |
---|
104 | |
---|
105 | |
---|
106 | /* set UID/GID to Trac (or apache) user */ |
---|
107 | check_username(TRAC_USER); |
---|
108 | if ( TRAC = getpwnam(TRAC_USER) ) { |
---|
109 | if (setgid(TRAC->pw_gid) || setuid(TRAC->pw_uid)) { |
---|
110 | if ( DEBUG ) printf("setgid or setuid failed\n"); |
---|
111 | return -5; |
---|
112 | } |
---|
113 | } else { |
---|
114 | if ( DEBUG ) printf("Invalid Trac user (%s)\n",TRAC_USER); |
---|
115 | return -3; /* 253 : Trac user not found */ |
---|
116 | } |
---|
117 | |
---|
118 | /* Check that script exists */ |
---|
119 | if ( stat(trac_script,&script_attrs) ) { |
---|
120 | if ( DEBUG ) printf("Script not found (%s)\n",trac_script); |
---|
121 | return -4; /* 252 : script not found */ |
---|
122 | } |
---|
123 | |
---|
124 | /* Execute script */ |
---|
125 | status = execv(trac_script, trac_script_args); |
---|
126 | if ( DEBUG ) printf("Script %s execution failure (error=%d). Check permission and interpreter path.\n",trac_script,status); |
---|
127 | return -1; /* 255 : should never reach this point */ |
---|
128 | |
---|
129 | } |
---|
130 | |
---|
131 | /* EOB */ |
---|