summaryrefslogtreecommitdiffstats
path: root/pki/base/common/src/com/netscape/cmscore/ldap/LdapPredicateParser.java
blob: b8e62d8967a57cf9d0e70d33f5c2b0567a81e145 (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// --- BEGIN COPYRIGHT BLOCK ---
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; version 2 of the License.
//
// 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// (C) 2007 Red Hat, Inc.
// All rights reserved.
// --- END COPYRIGHT BLOCK ---
package com.netscape.cmscore.ldap;


import java.util.*;
import java.io.*;
import com.netscape.certsrv.base.*;
import com.netscape.certsrv.ldap.*;
import com.netscape.certsrv.publish.*;
import com.netscape.cmscore.util.*;
import com.netscape.certsrv.apps.*;


/**
 * Default implementation of predicate parser.
 *
 * Limitations:
 *
 *	1. Currently parentheses are not suported.
 *	2. Only ==, != <, >, <= and >= operators are supported.
 *	3. The only boolean operators supported are AND and OR. AND takes precedence
 *	   over OR. Example: a AND b OR e OR c AND d
 *					is treated as (a AND b) OR e OR (c AND d)
 *	4. If this is n't adequate, roll your own.
 *
 * @author mzhao
 * @version $Revision$, $Date$
 */
public class LdapPredicateParser {
    public static final int OP_AND = 1;
    public static final int OP_OR = 2;
    public static final int EXPRESSION = 0;

    public static final String AND = "AND";
    public static final String OR = "OR";

    private static final char COMMA = ',';

    /**
     * Parse the predicate expression and return a vector of expressions.
     *
     * @param predicateExp	The predicate expression as read from the config file.
     * @return expVector	The vector of expressions.
     */
    public static ILdapExpression parse(String predicateExpression)
        throws ELdapException {
        if (predicateExpression == null || 
            predicateExpression.length() == 0)
            return null;
        PredicateTokenizer pt = new PredicateTokenizer(predicateExpression);

        if (pt == null || !pt.hasMoreTokens())
            return null;

            // The first token cannot be an operator. We are not dealing with
            // reverse-polish notation.
        String token = pt.nextToken();
        boolean opANDSeen;
        boolean opORSeen;

        if (getOP(token) != EXPRESSION) {
            if (Debug.ON)
                Debug.trace("Malformed expression: " + predicateExpression);
            throw new ELdapException(CMS.getUserMessage("CMS_LDAP_BAD_LDAP_EXPRESSION", predicateExpression));
        }
        ILdapExpression current = parseExpression(token);
        boolean malformed = false;
        Vector expSet = new Vector();
        int prevType = EXPRESSION;

        while (pt.hasMoreTokens()) {
            token = pt.nextToken();
            int curType = getOP(token);

            if ((prevType != EXPRESSION && curType != EXPRESSION) ||
                (prevType == EXPRESSION && curType == EXPRESSION)) {
                malformed = true;
                break;
            }

            // If an operator seen skip to the next token
            if (curType != EXPRESSION) {
                prevType = curType;
                continue;
            }

            // If the previous type was an OR token, add the current expression to
            // the expression set;
            if (prevType == OP_OR) {
                expSet.addElement(current);
                current = parseExpression(token);
                prevType = curType;
                continue;
            }

            // If the previous type was an AND token, make an AND expression
            if (prevType == OP_AND) {
                current = new LdapAndExpression(current, parseExpression(token));
                prevType = curType;
            }
        }
        if (malformed) {
            if (Debug.ON)
                Debug.trace("Malformed expression: " + predicateExpression);
            throw new ELdapException(
              CMS.getUserMessage("CMS_LDAP_BAD_LDAP_EXPRESSION",
                    predicateExpression));
        }

        // Form an LdapOrExpression
        if (current != null)
            expSet.addElement(current);

        int size = expSet.size();

        if (size == 0)
            return null;
        LdapOrExpression orExp = new
            LdapOrExpression((ILdapExpression) expSet.elementAt(0), null);

        for (int i = 1; i < size; i++)
            orExp = new LdapOrExpression(orExp,
                        (ILdapExpression) expSet.elementAt(i));
        return orExp;
    }

    private static int getOP(String token) {
        if (token.equalsIgnoreCase(AND))
            return OP_AND;
        else if (token.equalsIgnoreCase(OR))
            return OP_OR;
        else
            return EXPRESSION;
    }

    private static ILdapExpression parseExpression(String input)
        throws ELdapException {
        // If the expression has multiple parts separated by commas
        // we need to construct an AND expression. Else we will return a
        // simple expression.
        int commaIndex = input.indexOf(COMMA);

        if (commaIndex < 0)
            return LdapSimpleExpression.parse(input);
        int currentIndex = 0;
        Vector expVector = new Vector();

        while (commaIndex > 0) {
            LdapSimpleExpression exp = (LdapSimpleExpression)
                LdapSimpleExpression.parse(input.substring(currentIndex,
                        commaIndex));

            expVector.addElement(exp);
            currentIndex = commaIndex + 1;
            commaIndex = input.indexOf(COMMA, currentIndex);
        }
        if (currentIndex < (input.length() - 1)) {
            LdapSimpleExpression exp = (LdapSimpleExpression)
                LdapSimpleExpression.parse(input.substring(currentIndex));

            expVector.addElement(exp);
        }

        int size = expVector.size();
        LdapSimpleExpression exp1 = (LdapSimpleExpression) expVector.elementAt(0);
        LdapSimpleExpression exp2 = (LdapSimpleExpression) expVector.elementAt(1);
        LdapAndExpression andExp = new LdapAndExpression(exp1, exp2);

        for (int i = 2; i < size; i++) {
            andExp = new LdapAndExpression(andExp, (LdapSimpleExpression) expVector.elementAt(i));
        }
        return andExp;
    }

    public static void main(String[] args) {

        /**
         AttributeSet req = new AttributeSet();
         try
         {
         req.set("ou", "people");
         req.set("cn", "John Doe");
         req.set("uid", "jdoes");
         req.set("o", "airius.com");
         req.set("certtype", "client");
         req.set("request", "issuance");
         req.set("id", new Integer(10));
         req.set("dualcerts", new Boolean(true));

         Vector v = new Vector();
         v.addElement("one");
         v.addElement("two");
         v.addElement("three");
         req.set("count", v);
         }
         catch (Exception e){e.printStackTrace();}
         String[] array = { "ou == people AND certtype == client",
         "ou == servergroup AND certtype == server",
         "uid == jdoes, ou==people, o==airius.com OR ou == people AND certType == client OR certType == server AND cn == needles.mcom.com",
         };
         for (int i = 0; i < array.length; i++)
         {
         System.out.println();
         System.out.println("String: " + array[i]);
         ILdapExpression exp = null;
         try
         {
         exp = parse(array[i]);
         if (exp != null)
         {
         System.out.println("Parsed Expression: " + exp);
         boolean result = exp.evaluate(req);
         System.out.println("Result: " + result);
         }
         }
         catch (Exception e) {e.printStackTrace(); }
         }


         try
         {
         BufferedReader rdr = new BufferedReader(
         new FileReader(args[0]));
         String line;
         while((line=rdr.readLine()) != null)
         {
         System.out.println();
         System.out.println("Line Read: " + line);
         ILdapExpression exp = null;
         try
         {
         exp = parse(line);
         if (exp != null)
         {
         System.out.println(exp);
         boolean result = exp.evaluate(req);
         System.out.println("Result: " + result);
         }

         }catch (Exception e){e.printStackTrace();}
         }
         }
         catch (Exception e){e.printStackTrace(); }

         **/
    }

}


class PredicateTokenizer {
    String input;
    int currentIndex;
    int endOfString;
    String nextToken;
    boolean first;

    public PredicateTokenizer(String predString) {
        input = predString;
        currentIndex = 0;
        nextToken = null;
    }

    public boolean hasMoreTokens() {
        return (currentIndex != -1);
    }

    public String nextToken() {
        if (nextToken != null) {
            String toReturn = nextToken;

            nextToken = null;
            return toReturn;
        }

        int andIndex = input.indexOf(" AND", currentIndex);

        if (andIndex < 0)
            andIndex = input.indexOf(" and", currentIndex);
        int orIndex = input.indexOf(" OR", currentIndex);

        if (orIndex < 0)
            orIndex = input.indexOf(" or", currentIndex);
        String toReturn = null;

        if (andIndex == -1 && orIndex == -1) {
            if (currentIndex == 0) {
                currentIndex = -1;
                toReturn = input;
            } else {
                int temp = currentIndex;

                currentIndex = -1;
                toReturn = input.substring(temp);
            }
        } else if (andIndex >= 0 && (andIndex < orIndex || orIndex == -1)) {
            if (currentIndex != andIndex) {
                toReturn = input.substring(currentIndex, andIndex);
                nextToken = input.substring(andIndex + 1, andIndex + 4);
                currentIndex = andIndex + 4;
            } else {
                toReturn = "AND";
                currentIndex += 4;
            }
        } else if (orIndex >= 0 && (orIndex < andIndex || andIndex == -1)) {
            if (currentIndex != orIndex) {
                toReturn = input.substring(currentIndex, orIndex);
                nextToken = input.substring(orIndex + 1, orIndex + 3);
                currentIndex = orIndex + 3;
            } else {
                toReturn = "OR";
                currentIndex += 3;
            }
        } else {
            // Cannot happen; Assert here.
            toReturn = null;
            System.out.println("We shouldn't be here!");
        }
        if (toReturn == null)
            return null;
        else {
            String trimmed = toReturn.trim();

            if (trimmed == null || trimmed.length() == 0)
                return nextToken();
            else
                return trimmed;
        }
    }
}


class AttributeSet implements IAttrSet {
    Hashtable ht = new Hashtable();
    public AttributeSet() {
    }

    public void delete(String name)
        throws EBaseException {
        Object ob = ht.get(name);

        ht.remove(ob);
    }

    public Object get(String name)
        throws EBaseException {
        return ht.get(name);
    }

    public void set(String name, Object ob)
        throws EBaseException {
        ht.put(name, ob);
    }

    public Enumeration getElements() {
        return ht.elements();
    }
}