source: emailtotracscript/trunk/run_email2trac.c @ 35

Last change on this file since 35 was 35, checked in by bas, 17 years ago

EmailtoTracScript?:

email2trac:

  • Define TRAC_SCRIPT_NAME in Makefile.in
  • Improved run_email2trac.c and made it working.
  • Property svn:keywords set to Id
File size: 3.0 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 35 2006-01-20 20:35:04Z 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
43int main(int argc, char** argv) {
44
45  int i,j;
46  int caller = getuid();
47  int status;
48
49  char   *trac_script;
50  char   **trac_script_args;
51  struct passwd *TRAC; 
52  struct passwd *MTA;
53  struct stat script_attrs;
54 
55  trac_script = malloc((strlen(TRAC_SCRIPT_PATH) 
56        + strlen(TRAC_SCRIPT_NAME) + 10) * sizeof(char));
57
58  strncat(trac_script,TRAC_SCRIPT_PATH, strlen(TRAC_SCRIPT_PATH));
59  strcat(trac_script,"/");
60  strncat(trac_script,TRAC_SCRIPT_NAME, strlen(TRAC_SCRIPT_NAME));
61
62  /*
63  printf("trac_script = %s\n", trac_script);
64  */
65
66  /* First copy arguments passed to the wrapper as scripts arguments
67     after filtering out some of the possible script options */
68  trac_script_args = (char**) malloc((argc+1)*sizeof(char*));
69  trac_script_args[0] = TRAC_SCRIPT_NAME;
70  for (i=j=1; i<argc; i++) {
71    if ( (strcmp(argv[i],"--file") == 0) || 
72         (strcmp(argv[i],"-f") == 0) ) {
73      i++;
74      continue;
75    }
76   
77    trac_script_args[j] = argv[i];
78    j++;
79  }
80  trac_script_args[j] = NULL;
81
82 
83  /* Check caller */
84 
85  MTA = getpwnam(MTA_USER);
86  if ( caller !=  MTA->pw_uid ) {
87    if ( DEBUG ) printf("Invalid caller UID (%d)\n",caller);
88    return -2;     /* 254 : Invalid caller */
89  }
90 
91 
92  /* set UID/GID to Trac (or apache) user */
93  if ( TRAC = getpwnam(TRAC_USER) ) {
94    setuid(TRAC->pw_uid);
95    setgid(TRAC->pw_gid);
96  } else {
97    if ( DEBUG ) printf("Invalid Trac user (%s)\n",TRAC_USER);
98    return -3;     /* 253 : Trac user not found */
99  }
100         
101  /* Check that script exists */
102  if ( stat(trac_script,&script_attrs) ) {
103    if ( DEBUG ) printf("Script not found (%s)\n",trac_script);
104    return -4;    /* 252 : script not found */
105  }
106 
107  /* Execute script */
108  status = execv(trac_script, trac_script_args);
109  if ( DEBUG ) printf("Script %s execution failure (error=%d). Check permission and interpreter path.\n",trac_script,status);
110  return -1;     /* 255 : should never reach this point */
111
112}
113
114/* EOB */
Note: See TracBrowser for help on using the repository browser.