Radcli library 1.5.2
A simple radius library
Loading...
Searching...
No Matches
radexample.c
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
9#include <config.h>
10#include <stdio.h>
11#include <syslog.h>
12#include <radcli/radcli.h>
13
14int
15main (int argc, char **argv)
16{
17 int result;
18 char username[128];
19 char passwd[AUTH_PASS_LEN + 1];
20 VALUE_PAIR *send = NULL, *received = NULL;
21 uint32_t service;
22 rc_handle *rh;
23
24 /* openlog() sets the syslog identity used by radcli's internal messages */
25 openlog("my-prog-name", LOG_PID, LOG_DAEMON);
26
27 if ((rh = rc_read_config(RC_CONFIG_FILE)) == NULL)
28 return ERROR_RC;
29
30 snprintf(username, sizeof(username), "my-username");
31 snprintf(passwd, sizeof(passwd), "my-password");
32
33 /*
34 * Fill in User-Name
35 */
36 if (rc_avpair_add(rh, &send, PW_USER_NAME, username, -1, 0) == NULL) {
37 rc_destroy(rh);
38 return ERROR_RC;
39 }
40
41 /*
42 * Fill in User-Password
43 */
44 if (rc_avpair_add(rh, &send, PW_USER_PASSWORD, passwd, -1, 0) == NULL) {
45 rc_avpair_free(send);
46 rc_destroy(rh);
47 return ERROR_RC;
48 }
49
50 /*
51 * Fill in Service-Type
52 */
53 service = PW_AUTHENTICATE_ONLY;
54 if (rc_avpair_add(rh, &send, PW_SERVICE_TYPE, &service, -1, 0) == NULL) {
55 rc_avpair_free(send);
56 rc_destroy(rh);
57 return ERROR_RC;
58 }
59
60 result = rc_auth(rh, 0, send, &received, NULL);
61
62 if (result == OK_RC) {
63 VALUE_PAIR *vp = received;
64 char name[128];
65 char value[128];
66
67 fprintf(stderr, "\"%s\" RADIUS Authentication OK\n", username);
68
69 /* print the known attributes in the reply */
70 while(vp != NULL) {
71 if (rc_avpair_tostr(rh, vp, name, sizeof(name), value, sizeof(value)) == 0) {
72 fprintf(stderr, "%s:\t%s\n", name, value);
73 }
74 vp = rc_avpair_next(vp);
75 }
76 } else {
77 fprintf(stderr, "\"%s\" RADIUS Authentication failure (RC=%i)\n", username, result);
78 }
79
80 rc_avpair_free(send);
81 rc_avpair_free(received);
82 rc_destroy(rh);
83
84 return result;
85}
VALUE_PAIR * rc_avpair_next(VALUE_PAIR *t)
Definition avpair.c:112
int rc_auth(rc_handle *rh, uint32_t nas_port, VALUE_PAIR *send, VALUE_PAIR **received, char *msg)
Definition buildreq.c:252
void rc_destroy(rc_handle *rh)
Definition config.c:1179
void rc_avpair_free(VALUE_PAIR *pair)
Definition avpair.c:593
int rc_avpair_tostr(rc_handle const *rh, VALUE_PAIR *pair, char *name, int ln, char *value, int lv)
Definition avpair.c:886
rc_handle * rc_read_config(char const *filename)
Definition config.c:618
VALUE_PAIR * rc_avpair_add(rc_handle const *rh, VALUE_PAIR **list, uint32_t attrid, void const *pval, int len, uint32_t vendorspec)
Definition avpair.c:46
@ PW_SERVICE_TYPE
Its type is integer.
Definition radcli.h:152
@ PW_USER_NAME
Its type is string.
Definition radcli.h:147
@ PW_USER_PASSWORD
Its type is string.
Definition radcli.h:148