-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
146 lines (122 loc) · 3.85 KB
/
parse.c
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
/*
* parse.c - account module to check GeoIP information
*
* $Id$
*
*/
#include "pam_geoip.h"
struct locations *parse_locations(pam_handle_t *pamh, struct options *opts, char *location_string)
{
float radius;
double latitude, longitude;
struct locations *entry, *walker, *list;
char *country, *city, *single, *end, *next;
char *string = strdup(location_string ? location_string : "");
if (!string) {
pam_syslog(pamh, LOG_CRIT, "failed to strdup: %m");
return NULL;
}
entry = walker = list = NULL;
single = string;
while (*single) {
if (isspace(*single)) {
++single;
continue;
}
country = NULL;
city = NULL;
end = single;
while (*end && *end != ';') ++end;
if (*end) next = end + 1;
else next = end;
*end-- = 0;
while (isspace(*end)) *end-- = 0;
if (!single[0]) {
single = next;
continue;
}
if (sscanf(single, "%f { %lf , %lf }", &radius, &latitude, &longitude) == 3) {
if (fabs(latitude) > 90.0 || fabs(longitude) > 180.0) {
pam_syslog(pamh, LOG_WARNING, "illegal value(s) in LAT/LONG: %f, %f", latitude, longitude);
single = next;
continue;
}
}
else {
country = single;
while (*single && *single != ',') ++single;
/* single is now at the end of country */
if (*single) city = single + 1;
else city = "*";
*single-- = 0;
while (isspace(*single)) *single-- = 0;
if (!country[0]) country = "*";
while (isspace(*city)) ++city;
if (!city[0]) city = "*";
}
single = next;
entry = malloc(sizeof(struct locations));
if (!entry) {
pam_syslog(pamh, LOG_CRIT, "failed to malloc: %m");
free(string);
return NULL;
}
entry->next = NULL;
if (!country) {
entry->radius = radius;
entry->longitude = longitude;
entry->latitude = latitude;
entry->country = NULL;
entry->city = NULL;
}
else {
entry->country = strdup(country);
if (!entry->country) {
pam_syslog(pamh, LOG_CRIT, "failed to malloc: %m");
free(entry);
free(string);
return NULL;
}
entry->city = strdup(city);
if (!entry->city) {
pam_syslog(pamh, LOG_CRIT, "failed to malloc: %m");
free(entry->country);
free(entry);
free(string);
return NULL;
}
}
if (!list) list = entry;
else walker->next = entry;
walker = entry;
}
free(string);
return list;
}
int parse_action(pam_handle_t *pamh, char *name)
{
int action = -1;
if (!strcmp(name, "deny")) action = PAM_PERM_DENIED;
else if (!strcmp(name, "allow")) action = PAM_SUCCESS;
else if (!strcmp(name, "ignore")) action = PAM_IGNORE;
else pam_syslog(pamh, LOG_WARNING, "invalid action '%s' - skipped", name);
return action;
}
int parse_conf_line(pam_handle_t *pamh, char *line, char *domain, char *service, char *location)
{
char *str;
char action[LINE_LENGTH+1];
if ((service && sscanf(line, "%s %s %s %[^\n]", domain, service, action, location) != 4) ||
(!service && sscanf(line, "%s %s %[^\n]", domain, action, location) != 3)
) {
pam_syslog(pamh, LOG_WARNING, "invalid line '%s' - skipped", line);
return -1;
}
/* remove white space from the end */
str = location + strlen(location) - 1;
while (isspace(*str)) *str-- = 0;
return parse_action(pamh, action);
}
/*
* vim: ts=4 sw=4 expandtab
*/