File: passwordfd.c

package info (click to toggle)
ppp 2.5.2-1%2B1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 5,472 kB
  • sloc: ansic: 40,721; sh: 5,356; perl: 458; makefile: 453
file content (93 lines) | stat: -rw-r--r-- 1,760 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

/*
 *  Author: Arvin Schnell <arvin@suse.de>
 *
 *  This plugin let's you pass the password to the pppd via
 *  a file descriptor. That's easy and secure - no fiddling
 *  with pap- and chap-secrets files.
 */

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <sys/time.h>

#include <pppd/pppd.h>
#include <pppd/upap.h>
#include <pppd/chap.h>
#include <pppd/eap.h>
#include <pppd/options.h>

char pppd_version[] = PPPD_VERSION;

static char save_passwd[MAXSECRETLEN];

static int pwfd_read_password(char **argv)
{
    ssize_t readgood, red;
    int passwdfd;
    char passwd[MAXSECRETLEN];

    if (!ppp_int_option(argv[0], &passwdfd))
	return 0;

    readgood = 0;
    do {
	red = read (passwdfd, passwd + readgood, MAXSECRETLEN - 1 - readgood);
	if (red == 0)
	    break;
	if (red < 0) {
	    error ("Can't read secret from fd\n");
	    readgood = -1;
	    break;
	}
	readgood += red;
    } while (readgood < MAXSECRETLEN - 1);

    close (passwdfd);

    if (readgood < 0)
	return 0;

    passwd[readgood] = 0;
    strcpy (save_passwd, passwd);

    return 1;
}

static struct option options[] = {
    { "passwordfd", o_special, pwfd_read_password,
      "Receive password on this file descriptor" },
    { NULL }
};

static int pwfd_check (void)
{
    return 1;
}

static int pwfd_passwd (char *user, char *passwd)
{
    if (passwd != NULL)
	strcpy(passwd, save_passwd);
    return 1;
}

void plugin_init (void)
{
    ppp_add_options (options);

    pap_check_hook = pwfd_check;
    pap_passwd_hook = pwfd_passwd;

    chap_check_hook = pwfd_check;
    chap_passwd_hook = pwfd_passwd;

#ifdef PPP_WITH_EAPTLS
    eaptls_passwd_hook = pwfd_passwd;
#endif
}