source: emailtotracscript/trunk/run_email2trac.c @ 125

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

EmailtoTracScript?:

run_email2trac.c:

  • setgid() must be called before setuid()
  • Property svn:keywords set to Id
File size: 3.4 KB
RevLine 
[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 125 2006-10-17 09:18:16Z 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
[110]43int 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
[5]51int main(int argc, char** argv) {
52
53  int i,j;
54  int caller = getuid();
55  int status;
[35]56
57  char   *trac_script;
58  char   **trac_script_args;
[5]59  struct passwd *TRAC; 
60  struct passwd *MTA;
61  struct stat script_attrs;
[35]62 
63  trac_script = malloc((strlen(TRAC_SCRIPT_PATH) 
64        + strlen(TRAC_SCRIPT_NAME) + 10) * sizeof(char));
65
66  strncat(trac_script,TRAC_SCRIPT_PATH, strlen(TRAC_SCRIPT_PATH));
[5]67  strcat(trac_script,"/");
[35]68  strncat(trac_script,TRAC_SCRIPT_NAME, strlen(TRAC_SCRIPT_NAME));
[5]69
[35]70  /*
71  printf("trac_script = %s\n", trac_script);
72  */
73
[5]74  /* First copy arguments passed to the wrapper as scripts arguments
75     after filtering out some of the possible script options */
76  trac_script_args = (char**) malloc((argc+1)*sizeof(char*));
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   
85    trac_script_args[j] = argv[i];
86    j++;
87  }
88  trac_script_args[j] = NULL;
89
90 
91  /* Check caller */
[110]92
93
94  check_username(MTA_USER);
[5]95  MTA = getpwnam(MTA_USER);
[110]96
97  if ( MTA == NULL ) {
98    if ( DEBUG ) printf("Invalid MTA user (%s)\n", MTA_USER);
99    return -3;     /* 253 : MTA user not found */
100  }
101
[5]102  if ( caller !=  MTA->pw_uid ) {
103    if ( DEBUG ) printf("Invalid caller UID (%d)\n",caller);
104    return -2;     /* 254 : Invalid caller */
105  }
106 
107 
108  /* set UID/GID to Trac (or apache) user */
[110]109  check_username(TRAC_USER);
[5]110  if ( TRAC = getpwnam(TRAC_USER) ) {
[125]111    setgid(TRAC->pw_gid);
[5]112    setuid(TRAC->pw_uid);
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 */
Note: See TracBrowser for help on using the repository browser.