chiark / gitweb /
finalise 2.1.2
[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 void printusage(FILE *f) {
31   if (fprintf(f,
32               "usage:       authbind [<options>] <program> <arg> <arg> ...\n"
33               "options:     --deep    --depth <levels>\n")
34       == EOF) { perror("printf usage"); exit(-1); }
35 }
36
37 static void usageerror(const char *msg) {
38   fprintf(stderr,"usage error: %s\n",msg);
39   printusage(stderr);
40   exit(-1);
41 }
42
43 static void mustsetenv(const char *var, const char *val) {
44   if (setenv(var,val,1)) { perror("authbind: setenv"); exit(-1); }
45 }
46
47 int main(int argc, char *const *argv) {
48   const char *expreload, *authbindlib, *preload;
49   char *newpreload, *ep;
50   char buf[50];
51   unsigned long depth;
52
53   depth= 1;
54   while (argc>1 && argv[1][0]=='-') {
55     argc--; argv++;
56     if (!argv[0][1]) break;
57     if (!strcmp("--deep",argv[0])) { depth= 0; }
58     else if (!strcmp("--depth",argv[0])) {
59       if (argc<=1) usageerror("--depth requires a value");
60       argc--; argv++;
61       depth= strtoul(argv[0],&ep,10);
62       if (*ep || depth<=0 || depth>100) usageerror("--depth value is not valid");
63     } else if (!strcmp("--help",argv[0]) || !strcmp("--help",argv[0])) {
64       printusage(stdout);
65       exit(0);
66     }
67   }
68   if (argc<2) usageerror("need program name");
69
70   authbindlib= getenv(AUTHBINDLIB_VAR);
71   if (!authbindlib) {
72     authbindlib= LIBAUTHBIND;
73     mustsetenv(AUTHBINDLIB_VAR,authbindlib);
74   }
75     
76   if ((expreload= getenv(PRELOAD_VAR))) {
77     newpreload= malloc(strlen(expreload)+strlen(authbindlib)+2);
78     strcpy(newpreload,expreload);
79     strcat(newpreload,":");
80     strcat(newpreload,authbindlib);
81     preload= newpreload;
82   } else {
83     preload= authbindlib;
84   }
85   mustsetenv(PRELOAD_VAR,preload);
86
87   if (depth > 1) {
88     sprintf(buf,"%ld",depth-1);
89     mustsetenv(AUTHBIND_LEVELS_VAR,buf);
90   } else if (depth == 0) {
91     mustsetenv(AUTHBIND_LEVELS_VAR,"y");
92   } else {
93     assert(depth==1);
94   }
95
96   execvp(argv[1],argv+1);
97   perror(argv[1]); exit(-1);
98 }