File: auth_helper.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 (151 lines) | stat: -rw-r--r-- 4,136 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
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
/* $Id: auth_helper_pam.c,v 1.9 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 "auth_helper.h"
#include "suio.h"

#ifdef HAVE_PAM_PAM_APPL_H
#include <pam/pam_appl.h>
#else /* !HAVE_PAM_PAM_APPL_H */
#include <security/pam_appl.h>
#endif /* !HAVE_PAM_PAM_APPL_H */

static suio *chatter;
static int conversed;

static int
rpc_conv (int num_msg, const struct pam_message **msg,
	  struct pam_response **resp, void *appdata_ptr)
{
  struct pam_response *res = malloc (num_msg * sizeof (*res));
  int i;

  if (!res) {
    const char msg[] = "malloc failed\n";
    write (2, msg, sizeof (msg) - 1);
    abort ();
  }
#if !defined (__linux__) && !defined (__FreeBSD__)
  if (num_msg > 1) {
    fprintf (stderr, "%s(%s,%s): PAM conversation w. more than one message\n"
	     "Conservatively refusing to authenticate because "
	     "incompatibilities\n"
	     "in different OS APIs could lead to memory corruption\n",
	     progname, user, service);
    exit (1);
  }
#endif /* not linux or FreeBSD */

  bzero (res, num_msg * sizeof (*res));
  for (i = 0; i < num_msg; i++)
    switch (msg[i]->msg_style) {
    case PAM_PROMPT_ECHO_OFF:
    case PAM_PROMPT_ECHO_ON:
      {
	char *text;
	conversed = 1;
	suio_copy (chatter, msg[i]->msg, strlen (msg[i]->msg));
	suio_copy (chatter, "", 1);
	text = suio_flatten (chatter);
	suio_clear (chatter);
	res[i].resp = authhelp_input (text,
				      msg[i]->msg_style == PAM_PROMPT_ECHO_ON);
	xfree (text);
	break;
      }
    case PAM_ERROR_MSG:
      fprintf (stderr, "%s(%s,%s): %s\n", progname, service, user,
	       msg[i]->msg);
      break;
    case PAM_TEXT_INFO:
      suio_copy (chatter, msg[i]->msg, strlen (msg[i]->msg));
      suio_copy (chatter, "\n", 1);
      break;
    default:
      fprintf (stderr, "%s(%s,%s): unsupported PAM message type %d\n",
	       progname, user, service, msg[i]->msg_style);
      exit (1);
      break;
    }

  *resp = res;
  return PAM_SUCCESS;
}

int
authhelp_go (void)
{
  pam_handle_t *pamh = NULL;
  struct pam_conv conv;
  int err;
  char *u = NULL;

  chatter = suio_alloc ();
  bzero (&conv, sizeof (conv));
  conv.conv = rpc_conv;
  err = pam_start (service, user, &conv, &pamh);
  if (!user)
    user = "unknown user";
  if (err == PAM_SUCCESS)
    err = pam_authenticate (pamh, 0);
  if (err == PAM_SUCCESS)
    err = pam_acct_mgmt (pamh, 0);
  if (err == PAM_SUCCESS) {
    const void *vv;
    pam_get_item (pamh, PAM_USER, &vv);
    u = (char *) vv;
  }

  if (err == PAM_SUCCESS) {
    char *text;
    suio_copy (chatter, "", 1);
    text = suio_flatten (chatter);
    suio_clear (chatter);

    authhelp_approve (u, text);

    xfree (text);
  }
  else if (!conversed) {
    fprintf (stderr, "%s PAM(%s): %s\n", progname,
	     service, pam_strerror (pamh, err));
    if (!chdir ("/etc/pam.d") && access (service, F_OK))
      fprintf (stderr, "%s PAM(%s): %s\n", progname, service,
	       "Try running: ln -s system-auth /etc/pam.d/sfs");
    else
      fprintf (stderr, "%s PAM(%s): make PAM config allows service %s"
	       " or other\n", progname, service, service);
  }

#if 0
  if (err == PAM_SUCCESS)
    fprintf (stderr, "%s(%s,%s): authenticated %s\n", progname,
	     user, service, u);
  else
    fprintf (stderr, "%s(%s,%s): %s\n", progname,
	     user, service, pam_strerror (pamh, err));
#endif

  pam_end (pamh, err);
  return err == PAM_SUCCESS ? 0 : -1;
}