]> git.ozlabs.org Git - ppp.git/blob - pppd/plugins/radius/radiusclient/src/radexample.c
93f814a31f4673e99887bd218180aa480f8058dd
[ppp.git] / pppd / plugins / radius / radiusclient / src / radexample.c
1 /*
2  * $Id: radexample.c,v 1.2 2002/04/02 14:09:35 dfs Exp $
3  *
4  * Copyright (C) 1995,1996,1997 Lars Fenneberg
5  *
6  * See the file COPYRIGHT for the respective terms and conditions.
7  * If the file is missing contact me at lf@elemental.net
8  * and I'll send you a copy.
9  *
10  */
11
12
13 static char     rcsid[] =
14                 "$Id: radexample.c,v 1.2 2002/04/02 14:09:35 dfs Exp $";
15
16 #include        <config.h>
17 #include        <includes.h>
18 #include        <radiusclient.h>
19 #include        <pathnames.h>
20
21 static char *pname = NULL;
22
23 int
24 main (int argc, char **argv)
25 {
26         int             result;
27         char            username[128];
28         char            passwd[AUTH_PASS_LEN + 1];
29         VALUE_PAIR      *send, *received;
30         UINT4           service;
31         char            msg[4096], username_realm[256];
32         char            *default_realm = rc_conf_str("default_realm");
33         char name[2048];
34         char value[2048]; /* more than enough */
35         char *cfile;
36
37         pname = (pname = strrchr(argv[0],'/'))?pname+1:argv[0];
38
39         rc_openlog(pname);
40
41
42         if (argc >= 2) {
43             cfile = argv[1];
44         } else {
45             cfile = RC_CONFIG_FILE;
46         }
47         if (rc_read_config(cfile) != 0)
48                 return(ERROR_RC);
49
50         if (rc_read_dictionary(rc_conf_str("dictionary")) != 0)
51                 return(ERROR_RC);
52
53         strncpy(username, rc_getstr ("login: ",1), sizeof(username));
54         strncpy (passwd, rc_getstr("Password: ",0), sizeof (passwd));
55
56         send = NULL;
57
58         /*
59          * Fill in User-Name
60          */
61
62         strncpy(username_realm, username, sizeof(username_realm));
63
64         /* Append default realm */
65         if ((strchr(username_realm, '@') == NULL) && default_realm &&
66             (*default_realm != '\0'))
67         {
68                 strncat(username_realm, "@", sizeof(username_realm));
69                 strncat(username_realm, default_realm, sizeof(username_realm));
70         }
71
72         if (rc_avpair_add(&send, PW_USER_NAME, username_realm, 0, VENDOR_NONE) == NULL)
73                 return(ERROR_RC);
74
75         /*
76          * Fill in User-Password
77          */
78
79         if (rc_avpair_add(&send, PW_USER_PASSWORD, passwd, 0, VENDOR_NONE) == NULL)
80                 return (ERROR_RC);
81
82         /*
83          * Fill in Service-Type
84          */
85
86         service = PW_AUTHENTICATE_ONLY;
87         if (rc_avpair_add(&send, PW_SERVICE_TYPE, &service, 0, VENDOR_NONE) == NULL)
88                 return (ERROR_RC);
89
90         result = rc_auth(0, send, &received, msg, NULL);
91
92         if (result == OK_RC)
93         {
94                 fprintf(stderr, "\"%s\" RADIUS Authentication OK\n", username);
95         }
96         else
97         {
98                 fprintf(stderr, "\"%s\" RADIUS Authentication failure (RC=%i)\n", username, result);
99         }
100
101         /* Print returned attributes */
102         for( ; received ; received = received->next) {
103             if (rc_avpair_tostr(received, name, sizeof(name), value,
104                                 sizeof(value)) < 0) {
105                 continue;
106             }
107             printf("Attr '%s' ==> Val '%s'\n",
108                    name, value);
109         }
110
111         return result;
112 }