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