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
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
// This is a roxen module. Copyright © 2000, Roxen IS 
// 
// Module code updated to new 2.0 API 
 
constant cvs_version="$Id: ldaptag.pike,v 2.6 2001/03/07 13:40:46 kuntri Exp $"; 
constant thread_safe=1; 
#include <module.h> 
#include <config.h> 
 
inherit "module"; 
 
Configuration conf; 
 
#define LDAP_DEBUG 1 
#ifdef LDAP_DEBUG 
# define LDAP_WERR(X) werror("LDAPtags: "+X+"\n") 
#else 
# define LDAP_WERR(X) 
#endif 
 
 
// Module interface functions 
 
//constant module_type=MODULE_TAG|MODULE_PROVIDER; 
constant module_type=MODULE_TAG; 
constant module_name="LDAP tags"; 
constant module_doc  = "This module gives the tag <tt>&lt;ldap&gt;</tt> and " 
  "<tt>&lt;emit&gt;</tt> plugin (<tt>&lt;emit source=\"ldap\" ... &gt;</tt>).\n"; 
 
 
TAGDOCUMENTATION; 
#ifdef manual 
constant tagdoc=([ 
 "ldap":#"<desc tag='tag'><p><short> 
 Executes a LDAP operation, but doesn't do anything with the 
 result.</short>The <tag>ldap</tag> tag is mostly used for LDAP 
 operation that change the contents of the directory, for example 
 <i>add</i> or <i>modify</i>.</p> 
</desc> 
 
<attr name='server' value='LDAP URL' default='Server URL'><p> 
 Connection LDAP URL. If omitted the <i>Default server URL</i> 
 will be used.</p> 
</attr> 
 
<attr name='password' value='password'><p> 
 User password for connection to the directory server. If omitted the 
 default will be used.</p> 
 </attr> 
 
<attr name='dn' value='distinguished name' required='required'><p> 
 Distinguished name of object.</p> 
</attr> 
 
<attr name='op' value='add,delete,modify,replace' required='required'><p> 
 The actual LDAP operation.</p> 
 
 <p>Note that <att>op='modify'</att> will change only the attributes 
 given by the <att>attr</att> attribute.</p> 
</attr> 
 
<attr name='attr' value=''attribute_name1':[('attribute_value1'[,... ])][,'attribute_name2',...]'><p> 
 The actual values of attributes.</p> 
  <p> for example:</p> 
 <ex type='box'> 
 (sn:'Zappa'),(mail:'hello@nowhere.org','athell@pandemonium.com') 
 </ex> 
</attr> 
 
<attr name='parser'><p> 
 If specified, the query will be parsed by the RXML parser. This is 
 useful if the operation is to be built dynamically.</p> 
</attr>", 
 
"emit#ldap":#"<desc plugin='plugin'><p><short> 
 Use this source to search LDAP directory for information.</short> The 
 result will be available in variables named as the LDAP entries 
 attribute.</p> 
</desc> 
 
<attr name='server' value='LDAP URL' default='Server URL'><p> 
 Connection LDAP URL. If omitted the <i>Default server URL</i> 
 will be used.</p> 
</attr> 
 
<attr name='password' value='user password'><p> 
 User password for connection to the directory server. If omitted the 
 default will be used.</p> 
</attr> 
 
" 
]); 
#endif 
 
// Internal helpers 
 
mapping(string:array(mixed))|int read_attrs(string attrs, int op) { 
// from string: (attname1:'val1','val2'...)(attname2:'val1',...) to 
// ADD: (["attname1":({"val1","val2"...}), ... 
// REPLACE|MODIFY: (["attname1":({op, "val1","val2"...}), ... 
 
  array(string) atypes; // = ({"objectclass","cn","mail","o"}); 
  array(array(mixed)) avals; // = ({({op, "top","person"}),({op, "sysdept"}), ({op, "sysdept@unibase.cz", "xx@yy.cc"}),({op, "UniBASE Ltd."})}); 
  array(mixed) tmpvals, tmparr; 
  string aval; 
  mapping(string:array(mixed)) rv; 
  int vcnt, ix, cnt = 0, flg = (op > 0);; 
 
  if(flg) 
    flg = 1; 
  if (sizeof(attrs / "(") < 2) 
    return(0); 
  atypes = allocate(sizeof(attrs / "(")-1); 
  avals = allocate(sizeof(attrs / "(")-1); 
  foreach(attrs / "(", string tmp) 
    if (sizeof(tmp)) { // without empty '()' 
      if ((ix = search(tmp, ":")) < 1) // missed ':' or is first char 
        continue; 
      //atypes[cnt] = (tmp / ":")[0]; 
      atypes[cnt] = tmp[..(ix-1)]; 
      //tmparr = tmp[(sizeof(atypes[cnt])+1)..] / ","; 
      tmparr = (tmp[(ix+1)..] / ",") - ({ "" }); 
      vcnt = sizeof(tmparr); // + 1; 
      tmpvals = allocate(vcnt+flg); 
      for (ix=0; ix<vcnt; ix++) { 
        tmpvals[ix+flg] = (sscanf(tmparr[ix], "'%s'", aval))? aval : ""; 
      } 
      if(flg) 
        tmpvals[0] = op; 
      avals[cnt] = tmpvals; 
      cnt++; 
    } // if 
 
    /*if (avals[al] != ")") { 
      // Missed right ')' 
      return (0); 
    }*/ 
 
  rv = mkmapping(atypes,avals); 
  //LDAP_WERR(sprintf("DEB: mapping: %O\n",rv)); 
  return rv; 
} 
 
 
array|object|int do_ldap_op(string op, mapping args, RequestID id) 
{ 
  string host = query("server"); 
  string pass = ""; // = query("server"); 
 
  if (args->server) { 
    host=args->server; 
    args->server="CENSORED"; 
  } 
 
  if (args->password) { 
    pass=args->password; 
    args->password="CENSORED"; 
  } 
 
  switch (op) { 
    case "search": 
        break; 
 
    case "add":  
    case "replace":  
    case "modify":  
        if (!args->dn) 
          RXML.parse_error("No DN."); 
        if (!args->attr) 
          RXML.parse_error("No attribute."); 
        break; 
 
    case "delete":  
        if (!args->dn) 
          RXML.parse_error("No DN."); 
        break; 
 
  } //switch 
 
  if (args->parse) 
    args->query = Roxen.parse_rxml(args->query, id); 
 
  Protocols.LDAP.client con; 
  array(mapping(string:mixed))|object|int result; 
  function ldap_connect = id->conf->ldap_connect; 
  mixed error; 
  mapping|int attrvals; 
 
  if(ldap_connect) 
    error = catch(con = ldap_connect(host)); 
  else 
    error = catch(con = Protocols.LDAP.client(host)); 
 
  if (error) 
    RXML.run_error("Couldn't connect to LDAP server. "+Roxen.html_encode_string(error[0])); 
 
 
  if(op != "delete" && op != "search") { 
    attrvals = read_attrs(args->attr, (args->op == "replace") ? 2 : 0); // ldap->modify with flag 'replace' 
    if(intp(attrvals)) 
      RXML.run_error("Couldn't parse attribute values."); 
  } 
 
  switch (op) { 
    case "search": 
        error = catch(result = (con->search())); 
        break; 
 
    case "add": 
        error = catch(result = (con->add(args->dn, attrvals))); 
        break; 
 
    case "delete": 
        error = catch(result = (con->delete(args->dn))); 
        break; 
 
  } //switch 
 
 
  if(error) { 
    error = Roxen.html_encode_string(sprintf("LDAP operation %s failed. %s", 
            op, con->error_string()||"")); 
    con->unbind(); 
    RXML.run_error(error); 
  } 
  con->unbind(); 
 
#if 0 
  args["ldapobj"]=con; 
  if(result && args->rowinfo) { 
    int rows; 
    if(arrayp(result)) rows=sizeof(result); 
    if(objectp(result)) rows=result->num_rows(); 
    RXML.user_set_var(args->rowinfo, rows); 
  } 
#endif 
 
  if(op = "search" && objectp(result) && result->num_entries()) { 
    array res = ({}); 
    do 
      res += ({ result->fetch() }); 
    while(result->next()); 
    return res - ({}); 
  } 
     
  return result; 
} 
 
 
// -------------------------------- Tag handlers ----------------------------- 
 
class TagLDAPplugin { 
  inherit RXML.Tag; 
  constant name = "emit"; 
  constant plugin_name = "ldap"; 
 
 
  array get_dataset(mapping m, RequestID id) { 
    array res; 
    array rv = ({}); 
    string split = m->split || "\0"; 
 
    res=do_ldap_op("search", m, id); 
 
   if(arrayp(res) && sizeof(res)) { 
     foreach(res, mapping elem) { 
       mapping avalnew = ([]);; 
       foreach(indices(elem), string attr) { 
#if 0 // var_name.0 .. var_name.n  
         for(int ix=0; ix<sizeof(elem[attr]); ix++) 
           avalnew += ([ (attr+"."+(string)ix): elem[attr][ix] ]); 
#else // var_name0\0var_name1 ... 
       switch(upper_case(attr)) { // special attributes 
           case "LABELEDURI": 
                avalnew += ([ "labeleduriuri":((Array.map(elem[attr], 
                                lambda(string el1) { 
                                  return ((el1/" ")[0]); 
                                }) 
                              )*split) 
                            ]); 
                avalnew += ([ "labeledurilabel":((Array.map(elem[attr], 
                                lambda(string el1) { 
                                  string rv = el1; 
                                  if (sizeof(el1/" ")>1) rv = (el1/" ")[1..]*" "; 
                                  return rv; 
                                }) 
                              )*split) 
                            ]); 
/* 
                avalnew += ([ "labeledurianchor":((Array.map(elem[attr], 
                                lambda(string el1) { 
                                  string rv = el1; 
                                  if (sizeof(el1/" ")>1) rv = (el1/" ")[1..]*" "; 
                                  return rv; 
                                }) 
                              )*split) 
                            ]); 
*/ 
                break; 
         } 
         avalnew += ([ attr:(elem[attr]*split) ]); 
#endif 
       } 
       rv += ({ avalnew }); 
     } 
   } else 
     rv = ({([])}); 
 
   //LDAP_WERR(sprintf("emit search: rv: %O", rv)); 
   return(rv); 
  
  } 
} 
 
class TagLDAPQuery { 
  inherit RXML.Tag; 
  constant name = "ldap"; 
  constant flags = RXML.FLAG_EMPTY_ELEMENT; 
 
  class Frame { 
    inherit RXML.Frame; 
 
    array do_return(RequestID id) { 
      NOCACHE(); 
 
      array res=do_ldap_op(args->op, args, id); 
 
      id->misc->defines[" _ok"] = 1; 
      return 0; 
    } 
 
  } 
} 
 
// ------------------- Callback functions ------------------------- 
 
Protocols.LDAP.client ldap_object(void|string a_host) 
{ 
  string host = stringp(a_host)?a_host:query("server"); 
  Protocols.LDAP.client con; 
  function ldap_connect = conf->ldap_connect; 
  mixed error; 
  return ldap_connect(host); 
} 
 
string query_provides() 
{ 
  return "ldap"; 
} 
 
 
// ------------------------ Setting the defaults ------------------------- 
 
void create() 
{ 
  defvar("server", "ldap://localhost/??sub", "Default server URL", 
         TYPE_STRING | VAR_INITIAL, 
         "The default LDAP URL that will be used if no <i>host</i> " 
         "attribute is given to the tags. Usually the <i>host</i> " 
         "attribute should be used with a symbolic name definied " 
         "in the <i>Symbolic names</i>." 
         "<p>The default connection is specified as a LDAP URL in the " 
         "format " 
         "<tt>ldap://host:port/basedn??scope?filter?!...</tt>.\n"); 
} 
 
 
// --------------------- More interface functions -------------------------- 
 
void start(int level, Configuration _conf) 
{ 
  if (_conf) 
    conf = _conf; 
} 
 
string status() 
{ 
      "<font color=\"red\">Not connected:</font> " + 
      replace (Roxen.html_encode_string ("BLAHBLAH..."), "\n", "<br />\n") + 
      "<br />\n"; 
}