source: trunk/run_email2trac.c @ 628

Last change on this file since 628 was 628, checked in by bas, 11 years ago

renamed SARA to SURFsara

  • Property svn:keywords set to Id
File size: 3.9 KB
Line 
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 628 2013-06-18 10:05:30Z bas $
6
7    Only nobody can become the user www-data. Postfix uses this
8    user to start an program
9
10    Copyright 2002 SURFsara
11
12    Licensed under the Apache License, Version 2.0 (the "License");
13    you may not use this file except in compliance with the License.
14    You may obtain a copy of the License at
15
16    http://www.apache.org/licenses/LICENSE-2.0
17
18    Unless required by applicable law or agreed to in writing, software
19    distributed under the License is distributed on an "AS IS" BASIS,
20    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21    See the License for the specific language governing permissions and
22    limitations under the License.
23*/
24#include "config.h"
25
26#include <sys/types.h>
27#include <stdlib.h>
28#include <unistd.h>
29#include <pwd.h>
30#include <sys/stat.h>
31#include <string.h>
32#include <stdio.h>
33#include <limits.h>
34#ifdef HAVE_INITGROUPS
35#include <grp.h>
36#endif
37
38#include "run_email2trac.h"
39
40#ifndef DEBUG
41#define DEBUG 0
42#endif
43
44void check_username(char *name)
45{
46  if ( strlen(name) > 30 ) {
47          if ( DEBUG ) printf("MTA_USERNAME is to large; %s\n", name);
48          exit(-1);
49  }
50}
51
52int main(int argc, char** argv) {
53
54  int i,j;
55  int caller = getuid();
56  int status;
57
58  char   **trac_script_args;
59  char   *python_egg_cache = NULL;
60  struct passwd *TRAC; 
61  struct passwd *MTA;
62  struct stat script_attrs;
63  const char *trac_script = TRAC_SCRIPT_PATH "/" TRAC_SCRIPT_NAME;
64 
65  /*
66  printf("trac_script = %s\n", trac_script);
67  */
68
69  /* First copy arguments passed to the wrapper as scripts arguments
70     after filtering out some of the possible script options */
71
72  trac_script_args = (char**) malloc((argc+1)*sizeof(char*));
73  if (trac_script_args == NULL) {
74    if ( DEBUG ) printf("malloc failed\n");
75    return 1;
76  }
77  trac_script_args[0] = TRAC_SCRIPT_NAME;
78  for (i=j=1; i<argc; i++) {
79    if ( (strcmp(argv[i],"--file") == 0) || 
80         (strcmp(argv[i],"-f") == 0) ) {
81      i++;
82      continue;
83    }
84    else if ( (strcmp(argv[i],"--eggcache") == 0) ||
85         (strcmp(argv[i],"-e") == 0) ) {
86      i++;
87      python_egg_cache = argv[i];
88      continue;
89    }
90   
91    trac_script_args[j] = argv[i];
92    j++;
93  }
94  trac_script_args[j] = NULL;
95
96  /* Check caller */
97  check_username(MTA_USER);
98  MTA = getpwnam(MTA_USER);
99
100  if ( MTA == NULL ) {
101    if ( DEBUG ) printf("Invalid MTA user (%s)\n", MTA_USER);
102    return -3;     /* 253 : MTA user not found */
103  }
104
105  if ( caller !=  MTA->pw_uid ) {
106    if ( DEBUG ) printf("Invalid caller UID (%d)\n",caller);
107    return -2;     /* 254 : Invalid caller */
108  }
109 
110  /* set UID/GID and supplementary groups to be Trac (or apache) user */
111  check_username(TRAC_USER);
112  if ( TRAC = getpwnam(TRAC_USER) ) {
113#ifdef HAVE_INITGROUPS
114    if (initgroups(TRAC_USER, TRAC->pw_gid)) {
115      if ( DEBUG ) printf("initgroups failed\n");
116      return -7;    /* 249 : Can't set supplementary groups */
117    }
118#endif
119    if (setgid(TRAC->pw_gid) || setuid(TRAC->pw_uid)) {
120      if ( DEBUG ) printf("setgid or setuid failed\n");
121      return -5;   /* 251: Can't set gid or uid */
122    }
123  } else {
124    if ( DEBUG ) printf("Invalid Trac user (%s)\n",TRAC_USER);
125    return -6;     /* 250 : Trac user not found */
126  }
127         
128  /* Check that script exists */
129  if ( stat(trac_script,&script_attrs) ) {
130    if ( DEBUG ) printf("Script not found (%s)\n",trac_script);
131    return -4;    /* 252 : script not found */
132  }
133 
134  /* Set PYTHON_EGG_CACHE env variable if we have been told to do so */
135  if ( python_egg_cache != NULL ) {
136    setenv("PYTHON_EGG_CACHE",python_egg_cache ,1);
137  }
138
139  /* Execute script */
140  status = execv(trac_script, trac_script_args);
141 
142  if ( DEBUG ) printf("Script %s execution failure (error=%d). Check permission and interpreter path.\n",trac_script,status);
143  return -1;     /* 255 : should never reach this point */
144
145}
146
147/* EOB */
Note: See TracBrowser for help on using the repository browser.