summaryrefslogtreecommitdiffstats
path: root/gatchat/ppp_auth.c
blob: 22704367f1e84f3bba0cb320872a0c39b2cf0ce9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 *
 *  PPP library with GLib integration
 *
 *  Copyright (C) 2009-2010  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <termios.h>
#include <arpa/inet.h>

#include <glib.h>

#include "gatppp.h"
#include "ppp.h"

struct chap_header {
	guint8 code;
	guint8 identifier;
	guint16 length;
	guint8 data[0];
} __attribute__((packed));

struct chap_data {
	guint8 method;
	struct auth_data *auth;
};

enum chap_code {
	CHALLENGE=1,
	RESPONSE,
	SUCCESS,
	FAILURE
};

void auth_set_credentials(struct auth_data *data, const char *username,
				const char *password)
{
	if (data == NULL)
		return;

	g_free(data->username);
	data->username = g_strdup(username);

	g_free(data->password);
	data->password = g_strdup(password);
}

static void chap_process_challenge(struct auth_data *auth, guint8 *packet)
{
	struct chap_header *header = (struct chap_header *) packet;
	struct chap_header *response;
	struct chap_data *data = auth->proto_data;
	GChecksum *checksum;
	gchar *secret = data->auth->password;
	guint16 response_length;
	struct ppp_header *ppp_packet;
	gsize digest_len;

	/* create a checksum over id, secret, and challenge */
	checksum = g_checksum_new(data->method);
	if (!checksum)
		return;
	g_checksum_update(checksum, &header->identifier, 1);
	g_checksum_update(checksum, (guchar *) secret, strlen(secret));
	g_checksum_update(checksum, &header->data[1], header->data[0]);

	/* transmit a response packet */
	/*
	 * allocate space for the header, the checksum, and the ppp header,
	 * and the value size byte
	 */
	digest_len = g_checksum_type_get_length(data->method);
	response_length = digest_len + sizeof(*header) + 1;
	ppp_packet = g_try_malloc0(response_length + 2);
	if (!ppp_packet)
		goto challenge_out;

	/* add our protocol information */
	ppp_packet->proto = htons(CHAP_PROTOCOL);
	response = (struct chap_header *) &ppp_packet->info;
	if (response) {
		response->code = RESPONSE;
		response->identifier = header->identifier;
		response->length = htons(response_length);
		response->data[0] = digest_len;
		g_checksum_get_digest(checksum, &response->data[1],
					(gsize *) &response->data[0]);
		/* leave the name empty? */
	}

	/* transmit the packet */
	ppp_transmit(auth->ppp, (guint8 *) ppp_packet, response_length);

challenge_out:
	g_checksum_free(checksum);
}

static void chap_process_success(struct auth_data *data, guint8 *packet)
{
	ppp_generate_event(data->ppp, PPP_SUCCESS);
}

static void chap_process_failure(struct auth_data *data, guint8 *packet)
{
	struct chap_header *header = (struct chap_header *) packet;

	g_print("Failed to authenticate, message %s\n", header->data);
}

/*
 * parse the packet
 */
static void chap_process_packet(gpointer priv, guint8 *new_packet)
{
	struct auth_data *data = priv;
	guint8 code = new_packet[0];

	switch (code) {
	case CHALLENGE:
		chap_process_challenge(data, new_packet);
		break;
	case RESPONSE:
		g_print("Oops, received RESPONSE, but I've not implemented\n");
		break;
	case SUCCESS:
		chap_process_success(data, new_packet);
		break;
	case FAILURE:
		chap_process_failure(data, new_packet);
		break;
	default:
		g_print("Unknown auth code\n");
		break;
	}
}

struct ppp_packet_handler chap_packet_handler = {
	.proto = CHAP_PROTOCOL,
	.handler = chap_process_packet,
};

static void chap_free(struct auth_data *auth)
{
	/* TBD unregister protocol handler */

	g_free(auth->proto_data);
}

static struct chap_data *chap_new(struct auth_data *auth, guint8 method)
{
	struct chap_data *data;

	data = g_try_malloc0(sizeof(*data));
	if (!data)
		return NULL;

	data->auth = auth;
	switch (method) {
	case MD5:
		data->method = G_CHECKSUM_MD5;
		break;
	default:
		g_print("Unknown method\n");
	}

	/* register packet handler for CHAP protocol */
	chap_packet_handler.priv = auth;
	ppp_register_packet_handler(&chap_packet_handler);
	return data;
}

void auth_set_proto(struct auth_data *data, guint16 proto, guint8 method)
{
	if (data == NULL)
		return;

	switch (proto) {
	case CHAP_PROTOCOL:
		data->proto_data = (gpointer) chap_new(data, method);
		break;
	default:
		g_print("Unknown auth protocol 0x%x\n", proto);
	}
}

void auth_free(struct auth_data *data)
{
	if (data == NULL)
		return;

	chap_free(data);
	g_free(data);
}

struct auth_data *auth_new(GAtPPP *ppp)
{
	struct auth_data *data;

	data = g_try_malloc0(sizeof(*data));
	if (!data)
		return NULL;

	data->ppp = ppp;
	return data;
}