chiark / gitweb /
Fix coredump bug.
[authbind.git] / authbind.c
1 /*
2  *  authbind.c - main invoker program
3  *
4  *  authbind is Copyright (C) 1998 Ian Jackson
5  * 
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2, or (at your option)
9  *  any later version.
10  * 
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  * 
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software Foundation,
18  *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
19  * 
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <assert.h>
27
28 #include "authbind.h"
29
30 static const char *rcsid="$Id$";
31
32 static void printusage(FILE *f) {
33   if (fprintf(f,
34               "usage:       authbind [<options>] <program> <arg> <arg> ...\n"
35               "options:     --deep    --depth <levels>\n"
36               "version:     " MAJOR_VER "." MINOR_VER "  %s\n",
37               rcsid) == EOF) { perror("printf usage"); exit(-1); }
38 }
39
40 static void usageerror(const char *msg) {
41   fprintf(stderr,"usage error: %s\n",msg);
42   printusage(stderr);
43   exit(-1);
44 }
45
46 static void mustsetenv(const char *var, const char *val) {
47   if (setenv(var,val,1)) { perror("authbind: setenv"); exit(-1); }
48 }
49
50 int main(int argc, char *const *argv) {
51   const char *expreload, *authbindlib, *preload;
52   char *newpreload, *ep;
53   char buf[50];
54   unsigned long depth;
55
56   depth= 1;
57   while (argc>1 && argv[1][0]=='-') {
58     argc--; argv++;
59     if (!argv[0][1]) break;
60     if (!strcmp("--deep",argv[0])) { depth= 0; }
61     else if (!strcmp("--depth",argv[0])) {
62       if (argc<=1) usageerror("--depth requires a value");
63       argc--; argv++;
64       depth= strtoul(argv[0],&ep,10);
65       if (*ep || depth<=0 || depth>100) usageerror("--depth value is not valid");
66     } else if (!strcmp("--help",argv[0]) || !strcmp("--help",argv[0])) {
67       printusage(stdout);
68       exit(0);
69     }
70   }
71   if (argc<2) usageerror("need program name");
72
73   authbindlib= getenv(AUTHBINDLIB_VAR);
74   if (!authbindlib) {
75     authbindlib= LIBAUTHBIND;
76     mustsetenv(AUTHBINDLIB_VAR,authbindlib);
77   }
78     
79   if ((expreload= getenv(PRELOAD_VAR))) {
80     newpreload= malloc(strlen(expreload)+strlen(authbindlib)+2);
81     strcpy(newpreload,expreload);
82     strcat(newpreload,":");
83     strcat(newpreload,authbindlib);
84     preload= newpreload;
85   } else {
86     preload= authbindlib;
87   }
88   mustsetenv(PRELOAD_VAR,preload);
89
90   if (depth > 1) {
91     sprintf(buf,"%ld",depth-1);
92     mustsetenv(AUTHBIND_LEVELS_VAR,buf);
93   } else if (depth == 0) {
94     mustsetenv(AUTHBIND_LEVELS_VAR,"y");
95   } else {
96     assert(depth==1);
97   }
98
99   execvp(argv[1],argv+1);
100   perror(argv[1]); exit(-1);
101 }