File: authbind.c

package info (click to toggle)
authbind 2.2.0
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 88 kB
  • sloc: ansic: 489; makefile: 79; sh: 25
file content (98 lines) | stat: -rw-r--r-- 2,845 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
 *  authbind.c - main invoker program
 *
 *  authbind is Copyright (C) 1998 Ian Jackson
 * 
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software Foundation,
 *  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 
 * 
 */

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>

#include "authbind.h"

static void printusage(FILE *f) {
  if (fprintf(f,
	      "usage:       authbind [<options>] <program> <arg> <arg> ...\n"
	      "options:     --deep    --depth <levels>\n")
      == EOF) { perror("printf usage"); exit(-1); }
}

static void usageerror(const char *msg) {
  fprintf(stderr,"usage error: %s\n",msg);
  printusage(stderr);
  exit(-1);
}

static void mustsetenv(const char *var, const char *val) {
  if (setenv(var,val,1)) { perror("authbind: setenv"); exit(-1); }
}

int main(int argc, char *const *argv) {
  const char *expreload, *authbindlib, *preload;
  char *newpreload, *ep;
  char buf[50];
  unsigned long depth;

  depth= 1;
  while (argc>1 && argv[1][0]=='-') {
    argc--; argv++;
    if (!argv[0][1]) break;
    if (!strcmp("--deep",argv[0])) { depth= 0; }
    else if (!strcmp("--depth",argv[0])) {
      if (argc<=1) usageerror("--depth requires a value");
      argc--; argv++;
      depth= strtoul(argv[0],&ep,10);
      if (*ep || depth<=0 || depth>100) usageerror("--depth value is not valid");
    } else if (!strcmp("--help",argv[0]) || !strcmp("--help",argv[0])) {
      printusage(stdout);
      exit(0);
    }
  }
  if (argc<2) usageerror("need program name");

  authbindlib= getenv(AUTHBINDLIB_VAR);
  if (!authbindlib) {
    authbindlib= LIBAUTHBIND;
    mustsetenv(AUTHBINDLIB_VAR,authbindlib);
  }
    
  if ((expreload= getenv(PRELOAD_VAR))) {
    newpreload= malloc(strlen(expreload)+strlen(authbindlib)+2);
    strcpy(newpreload,expreload);
    strcat(newpreload,":");
    strcat(newpreload,authbindlib);
    preload= newpreload;
  } else {
    preload= authbindlib;
  }
  mustsetenv(PRELOAD_VAR,preload);

  if (depth > 1) {
    sprintf(buf,"%ld",depth-1);
    mustsetenv(AUTHBIND_LEVELS_VAR,buf);
  } else if (depth == 0) {
    mustsetenv(AUTHBIND_LEVELS_VAR,"y");
  } else {
    assert(depth==1);
  }

  execvp(argv[1],argv+1);
  perror(argv[1]); exit(-1);
}