/
dn.c
153 lines (139 loc) · 3.86 KB
/
dn.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
147
148
149
150
151
152
153
/* Copyright (c) 1992-1996 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* provided that this notice is preserved and that due credit is given
* to the University of Michigan at Ann Arbor. The name of the University
* may not be used to endorse or promote products derived from this
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
/* dn.c - routines for dealing with distinguished names
*
* Copied from UM-LDAP
*/
#include <ctype.h>
#include <stdio.h>
#include "dn.h"
#define B4TYPE 0
#define INTYPE 1
#define B4EQUAL 2
#define B4VALUE 3
#define INVALUE 4
#define INQUOTEDVALUE 5
#define B4SEPARATOR 6
#define DNSEPARATOR(c) ((c) == ',' || (c) == ';')
#define SEPARATOR(c) ((c) == ',' || (c) == ';' || (c) == '+')
#define SPACE(c) ((c) == ' ' || (c) == '\n')
#define NEEDSESCAPE(c) ((c) == '\\' || (c) == '"')
/*
* dn_normalize - put dn into a canonical format. the dn is
* normalized in place, as well as returned.
*/
char *
dn_normalize(char *dn) {
char *d, *s;
int state, gotesc;
gotesc = 0;
state = B4TYPE;
for (d = s = dn; *s; s++) {
switch (state) {
case B4TYPE:
if (!SPACE(*s)) {
state = INTYPE;
*d++ = *s;
}
break;
case INTYPE:
if (*s == '=') {
state = B4VALUE;
*d++ = *s;
} else if (SPACE(*s)) {
state = B4EQUAL;
} else {
*d++ = *s;
}
break;
case B4EQUAL:
if (*s == '=') {
state = B4VALUE;
*d++ = *s;
} else if (!SPACE(*s)) {
/* not a valid dn - but what can we do here? */
*d++ = *s;
}
break;
case B4VALUE:
if (*s == '"') {
state = INQUOTEDVALUE;
*d++ = *s;
} else if (!SPACE(*s)) {
state = INVALUE;
*d++ = *s;
}
break;
case INVALUE:
if (!gotesc && SEPARATOR(*s)) {
while (SPACE(*(d - 1)))
d--;
state = B4TYPE;
if (*s == '+') {
*d++ = *s;
} else {
*d++ = ',';
}
} else if (gotesc && !NEEDSESCAPE(*s) && !SEPARATOR(*s)) {
*--d = *s;
d++;
} else {
*d++ = *s;
}
break;
case INQUOTEDVALUE:
if (!gotesc && *s == '"') {
state = B4SEPARATOR;
*d++ = *s;
} else if (gotesc && !NEEDSESCAPE(*s)) {
*--d = *s;
d++;
} else {
*d++ = *s;
}
break;
case B4SEPARATOR:
if (SEPARATOR(*s)) {
state = B4TYPE;
*d++ = *s;
}
break;
default:
fprintf(stderr, "dn_normalize - unknown state %d dn:%s \n", state,
dn);
break;
}
if (*s == '\\') {
gotesc = 1;
} else {
gotesc = 0;
}
}
*d = '\0';
return (dn);
}
/*
* dn_normalize_case - put dn into a canonical form suitable for storing
* in a hash database. this involves normalizing the case as well as
* the format. the dn is normalized in place as well as returned.
*/
char *
dn_normalize_case(char *dn) {
char *s;
/* normalize format */
dn_normalize(dn);
/* normalize case */
for (s = dn; *s; s++) {
*s = tolower((unsigned char)*s);
}
return (dn);
}
/* vim: set softtabstop=4 shiftwidth=4 expandtab :*/