Radcli library 1.5.3
A simple radius library
Loading...
Searching...
No Matches
radexample.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 1995,1996,1997 Lars Fenneberg
3 * Copyright (C) 2015 Nikos Mavrogiannopoulos
4 *
5 * See the file COPYRIGHT for the respective terms and conditions.
6 *
7 */
8
12
13#include <config.h>
14#include <stdio.h>
15#include <syslog.h>
16#include <radcli/radcli.h>
17
18int
19main (int argc, char **argv)
20{
21 int result;
22 char username[128];
23 char passwd[AUTH_PASS_LEN + 1];
24 VALUE_PAIR *send = NULL, *received = NULL;
25 uint32_t service;
26 rc_handle *rh;
27
28 /* openlog() sets the syslog identity used by radcli's internal messages */
29 openlog("my-prog-name", LOG_PID, LOG_DAEMON);
30
31 if ((rh = rc_read_config(RC_CONFIG_FILE)) == NULL)
32 return ERROR_RC;
33
34 snprintf(username, sizeof(username), "my-username");
35 snprintf(passwd, sizeof(passwd), "my-password");
36
37 /*
38 * Fill in User-Name
39 */
40 if (rc_avpair_add(rh, &send, PW_USER_NAME, username, -1, 0) == NULL) {
41 rc_destroy(rh);
42 return ERROR_RC;
43 }
44
45 /*
46 * Fill in User-Password
47 */
48 if (rc_avpair_add(rh, &send, PW_USER_PASSWORD, passwd, -1, 0) == NULL) {
49 rc_avpair_free(send);
50 rc_destroy(rh);
51 return ERROR_RC;
52 }
53
54 /*
55 * Fill in Service-Type
56 */
57 service = PW_AUTHENTICATE_ONLY;
58 if (rc_avpair_add(rh, &send, PW_SERVICE_TYPE, &service, -1, 0) == NULL) {
59 rc_avpair_free(send);
60 rc_destroy(rh);
61 return ERROR_RC;
62 }
63
64 result = rc_auth(rh, 0, send, &received, NULL);
65
66 if (result == OK_RC) {
67 VALUE_PAIR *vp = received;
68 char name[128];
69 char value[128];
70
71 fprintf(stderr, "\"%s\" RADIUS Authentication OK\n", username);
72
73 /* print the known attributes in the reply */
74 while(vp != NULL) {
75 if (rc_avpair_tostr(rh, vp, name, sizeof(name), value, sizeof(value)) == 0) {
76 fprintf(stderr, "%s:\t%s\n", name, value);
77 }
78 vp = rc_avpair_next(vp);
79 }
80 } else {
81 fprintf(stderr, "\"%s\" RADIUS Authentication failure (RC=%i)\n", username, result);
82 }
83
84 rc_avpair_free(send);
85 rc_avpair_free(received);
86 rc_destroy(rh);
87
88 return result;
89}
VALUE_PAIR * rc_avpair_next(VALUE_PAIR *t)
Iterates through the attribute-value pairs.
Definition avpair.c:112
int rc_auth(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
Builds an authentication request for port id nas_port with the value_pairs send and submits it to a s...
Definition buildreq.c:303
void rc_destroy(rc_handle *rh)
Destroys Radius Client handle reclaiming all memory.
Definition config.c:1249
void rc_avpair_free(VALUE_PAIR *pair)
Frees all value_pairs in the list.
Definition avpair.c:584
int rc_avpair_tostr(rc_handle const *rh, VALUE_PAIR *pair, char *name, int ln, char *value, int lv)
Translate an av_pair into printable strings.
Definition avpair.c:883
rc_handle * rc_read_config(char const *filename)
Read the global config file.
Definition config.c:676
VALUE_PAIR * rc_avpair_add(rc_handle const *rh, VALUE_PAIR **list, uint32_t attrid, void const *pval, int len, uint32_t vendorspec)
Adds an attribute-value pair to the given list.
Definition avpair.c:46
@ PW_SERVICE_TYPE
Its type is integer.
Definition radcli.h:156
@ PW_USER_NAME
Its type is string.
Definition radcli.h:151
@ PW_USER_PASSWORD
Its type is string.
Definition radcli.h:152
Public API of the radcli library.