]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/passwordfd.c
c1f782e0c947e3fed686b26490ed2f965aced1de
[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 #include <stdio.h>
11 #include <errno.h>
12 #include <string.h>
13 #include <unistd.h>
14 #include <stdarg.h>
15 #include <stdbool.h>
16 #include <stdint.h>
17 #include <sys/time.h>
18
19 #include <pppd/pppd.h>
20 #include <pppd/upap.h>
21 #include <pppd/chap.h>
22 #include <pppd/eap.h>
23 #include <pppd/options.h>
24
25 char pppd_version[] = PPPD_VERSION;
26
27 static int passwdfd = -1;
28 static char save_passwd[MAXSECRETLEN];
29
30 static struct option options[] = {
31     { "passwordfd", o_int, &passwdfd,
32       "Receive password on this file descriptor" },
33     { NULL }
34 };
35
36 static int pwfd_check (void)
37 {
38     return 1;
39 }
40
41 static int pwfd_passwd (char *user, char *passwd)
42 {
43     int readgood, red;
44
45     if (passwdfd == -1)
46         return -1;
47
48     if (passwd == NULL)
49         return 1;
50
51     if (passwdfd == -2) {
52         strcpy (passwd, save_passwd);
53         return 1;
54     }
55
56     readgood = 0;
57     do {
58         red = read (passwdfd, passwd + readgood, MAXSECRETLEN - 1 - readgood);
59         if (red == 0)
60             break;
61         if (red < 0) {
62             error ("Can't read secret from fd\n");
63             readgood = -1;
64             break;
65         }
66         readgood += red;
67     } while (readgood < MAXSECRETLEN - 1);
68
69     close (passwdfd);
70
71     if (readgood < 0)
72         return 0;
73
74     passwd[readgood] = 0;
75     strcpy (save_passwd, passwd);
76     passwdfd = -2;
77
78     return 1;
79 }
80
81 void plugin_init (void)
82 {
83     ppp_add_options (options);
84
85     pap_check_hook = pwfd_check;
86     pap_passwd_hook = pwfd_passwd;
87
88     chap_check_hook = pwfd_check;
89     chap_passwd_hook = pwfd_passwd;
90
91 #ifdef PPP_WITH_EAPTLS
92     eaptls_passwd_hook = pwfd_passwd;
93 #endif
94 }