File: auth_helper_common.c

package info (click to toggle)
sfs 1%3A0.8-0%2Bpre20060720.1-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 9,668 kB
  • ctags: 14,317
  • sloc: cpp: 78,358; ansic: 15,494; sh: 9,540; yacc: 786; makefile: 706; perl: 676; lex: 553; python: 146; sed: 70
file content (167 lines) | stat: -rw-r--r-- 4,052 bytes parent folder | download | duplicates (2)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/* $Id: auth_helper_common.c,v 1.3 2004/06/12 07:52:01 dm Exp $ */

/*
 *
 * Copyright (C) 2004 David Mazieres (dm@uun.org)
 *
 * 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 "sfs-internal.h"
#include "auth_helper.h"
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>

static int use_rpc;		/* For debugging */
char *progname;
char *service;
char *user;

#define PW_BUF_SIZE 256

static char *
authhelp_input_tty (char *prompt, int echo)
{
  printf ("%s", prompt);
  fflush (stdout);
  if (!echo)
    return strdup (getpass (""));
  else {
    char *buf = malloc (PW_BUF_SIZE);
    int n = read (0, buf, PW_BUF_SIZE);
    if (n <= 0) {
      fprintf (stderr, "%s(%s,%s): EOF\n", progname,
	       user ? user : "unknown user", service);
      exit (1);
    }
    if (n >= PW_BUF_SIZE) {
      fprintf (stderr, "%s(%s,%s): input too long\n",
	       progname, user ? user : "unknown user", service);
      exit (1);
    }
    buf[n] = '\0';
    while (n > 0 && (buf[n-1] == '\r' || buf[n-1] == '\n'))
      buf[--n] = '\0';
    return buf;
  }
}

static void
authhelp_approve_tty (char *u, char *hello)
{
  if (hello)
    fprintf (stderr,"%s\n", hello);
  fprintf (stderr, "%s(%s,%s): authenticated %s\n",
	   progname, user, service, u);
}

static char *
authhelp_input_rpc (char *prompt, int echo)
{
  authhelp_getpass_arg arg;
  authhelp_getpass_res res;
  enum clnt_stat err;

  bzero (&arg, sizeof (arg));
  bzero (&res, sizeof (res));
  arg.prompt = prompt;
  arg.echo = echo;

  err = srpc_call (&authhelp_prog_1, 0, AUTHHELPPROG_GETPASS, &arg, &res);
  if (err) {
    fprintf (stderr, "%s(%s,%s): %s\n",
	     progname, user ? user : "unknown user",
	     service, clnt_sperrno (err));
    exit (1);
  }

  return res.response;
}

static void
authhelp_approve_rpc (char *u, char *hello)
{
  authhelp_succeed_arg arg;
  bzero (&arg, sizeof (arg));
  arg.user = u;
  arg.hello = hello ? hello : "";
  srpc_call (&authhelp_prog_1, 0, AUTHHELPPROG_SUCCEED, &arg, NULL);
}

char *
authhelp_input (char *prompt, int echo)
{
  if (use_rpc)
    return authhelp_input_rpc (prompt, echo);
  else
    return authhelp_input_tty (prompt, echo);
}

void
authhelp_approve (char *user, char *hello)
{
  if (use_rpc)
    authhelp_approve_rpc (user, hello);
  else
    authhelp_approve_tty (user, hello);
}

static void usage (void) __attribute__ ((noreturn));
static void
usage (void)
{
  fprintf (stderr, "usage: %s [-r] <service> [<user>]\n", progname);
  exit (1);
}

int
main (int argc, char **argv)
{
  if ((progname = strrchr (argv[0], '/')))
    progname++;
  else
    progname = argv[0];

  /* N.B.:  We explicitly do not want to use getopt here, because
   * Linux getopt sometimes does some very weird and un-posix things,
   * which could cause late arguments (after the service) to be
   * interpreted as command line arguments.  We definitely want to
   * avoid any kind of "login -froot" type bugs here.
   */
  optind = 1;
  if (optind < argc && !strcmp (argv[optind], "--"))
    optind++;
  else if (optind < argc && !strcmp (argv[optind], "-r")) {
    use_rpc = 1;
    optind++;
  }
  else if (optind < argc && argv[optind][0] == '-')
    usage ();

  if (optind + 2 == argc)
    user = argv[optind+1];
  else if (optind + 1 != argc)
    usage ();

  service = argv[optind];

  if (!user)
    user = authhelp_input ("Username: ", 1);

  exit (authhelp_go () != 0);
}