]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/passwordfd.c
pppd: Allow use of additional Bnnn constants (#325)
[ppp.git] / pppd / plugins / passwordfd.c
1
2 /*
3  *  Author: Arvin Schnell <arvin@suse.de>
4  *
5  *  This plugin let's you pass the password to the pppd via
6  *  a file descriptor. That's easy and secure - no fiddling
7  *  with pap- and chap-secrets files.
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <stdio.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <unistd.h>
18
19 #include "pppd.h"
20
21 char pppd_version[] = VERSION;
22
23 static int passwdfd = -1;
24 static char save_passwd[MAXSECRETLEN];
25
26 static option_t options[] = {
27     { "passwordfd", o_int, &passwdfd,
28       "Receive password on this file descriptor" },
29     { NULL }
30 };
31
32 static int pwfd_check (void)
33 {
34     return 1;
35 }
36
37 static int pwfd_passwd (char *user, char *passwd)
38 {
39     int readgood, red;
40
41     if (passwdfd == -1)
42         return -1;
43
44     if (passwd == NULL)
45         return 1;
46
47     if (passwdfd == -2) {
48         strcpy (passwd, save_passwd);
49         return 1;
50     }
51
52     readgood = 0;
53     do {
54         red = read (passwdfd, passwd + readgood, MAXSECRETLEN - 1 - readgood);
55         if (red == 0)
56             break;
57         if (red < 0) {
58             error ("Can't read secret from fd\n");
59             readgood = -1;
60             break;
61         }
62         readgood += red;
63     } while (readgood < MAXSECRETLEN - 1);
64
65     close (passwdfd);
66
67     if (readgood < 0)
68         return 0;
69
70     passwd[readgood] = 0;
71     strcpy (save_passwd, passwd);
72     passwdfd = -2;
73
74     return 1;
75 }
76
77 void plugin_init (void)
78 {
79     add_options (options);
80
81     pap_check_hook = pwfd_check;
82     pap_passwd_hook = pwfd_passwd;
83
84     chap_check_hook = pwfd_check;
85     chap_passwd_hook = pwfd_passwd;
86
87 #ifdef USE_EAPTLS
88     eaptls_passwd_hook = pwfd_passwd;
89 #endif
90 }