]> git.sur5r.net Git - openldap/blob - servers/slapd/back-tcl/tcl_config.c
196b2d639e4b09d49cd91b66d407892acbb4dc64
[openldap] / servers / slapd / back-tcl / tcl_config.c
1 /*
2  * config.c - tcl backend configuration file routine
3  *
4  * Copyright 1999, Ben Collins <bcollins@debian.org>, All rights reserved.
5  *
6  * Redistribution and use in source and binary forms are permitted only
7  * as authorized by the OpenLDAP Public License.  A copy of this
8  * license is available at http://www.OpenLDAP.org/license.html or
9  * in file LICENSE in the top-level directory of the distribution.
10  */
11
12 #include "portable.h"
13
14 #include <stdio.h>
15
16 #include "slap.h"
17 #include "tcl_back.h"
18
19 struct i_info *global_i;
20
21 int tcl_back_db_config (
22         BackendDB * bd,
23         char *fname,
24         int lineno,
25         int argc,
26         char **argv
27 )
28 {
29         struct tclinfo *ti = (struct tclinfo *) bd->be_private;
30         int script_loaded = 0;
31
32         if (ti == NULL) {
33                 fprintf (stderr, "%s: line %d: tcl backend info is null!\n", fname,
34                         lineno);
35                 exit (1);
36         }
37         if (ti->ti_ii == NULL) {
38                 ti->ti_ii = global_i;
39         }
40
41         /* Script to load */
42         if (strcasecmp (argv[0], "scriptpath") == 0) {
43                 if (argc < 2) {
44                         Debug (LDAP_DEBUG_ANY,
45                                 "%s: line %d: missing script in \"scriptpath <script>\" line\n",
46                                 fname, lineno, 0);
47                         exit (1);
48                 }
49                 ti->script_path = (char *) strdup (argv[1]);
50
51         /* use local interpreter */
52         } else if (strcasecmp (argv[0], "tclrealm") == 0) {
53                 struct i_info *ii;
54
55                 if (argc < 2) {
56                         Debug (LDAP_DEBUG_ANY,
57                                 "%s: line %d: missing script in \"tclrealm <name>\" line\n",
58                                 fname, lineno, 0);
59                         exit (1);
60                 }
61                 ti->ti_ii = NULL;
62
63                 ii = global_i;
64                 do {
65                         if (ii != NULL && !strcasecmp (ii->name, argv[1]))
66                         ti->ti_ii = ii;
67                         if (ii->next != NULL)
68                         ii = ii->next;
69                 } while (ii->next != NULL);
70
71                 if (ti->ti_ii == NULL) {        /* we need to make a new one */
72                         ii->next = (struct i_info *) ch_malloc (sizeof (struct i_info));
73
74                         ii->next->count = 0;
75                         ii->next->name = (char *) strdup (argv[1]);
76                         ii->next->next = NULL;
77                         ii->next->interp = Tcl_CreateInterp ();
78                         Tcl_Init (ii->next->interp);
79                         ti->ti_ii = ii;
80                 }
81
82         /* proc for binds */
83         } else if (strcasecmp (argv[0], "bind") == 0) {
84                 if (argc < 2) {
85                         Debug (LDAP_DEBUG_ANY,
86                                 "%s: line %d: missing proc in \"bind <proc>\" line\n",
87                                 fname, lineno, 0);
88                         exit (1);
89                 }
90                 ti->ti_bind = (char *) strdup (argv[1]);
91
92         /* proc for unbinds */
93         } else if (strcasecmp (argv[0], "unbind") == 0) {
94                 if (argc < 2) {
95                         Debug (LDAP_DEBUG_ANY,
96                         "%s: line %d: missing proc in \"unbind <proc>\" line\n",
97                         fname, lineno, 0);
98                         exit (1);
99                 }
100                 ti->ti_unbind = (char *) strdup (argv[1]);
101
102         /* proc for search */
103         } else if (strcasecmp (argv[0], "search") == 0) {
104                 if (argc < 2) {
105                         Debug (LDAP_DEBUG_ANY,
106                                 "%s: line %d: missing proc in \"search <proc>\" line\n",
107                                 fname, lineno, 0);
108                         exit (1);
109                 }
110                 ti->ti_search = (char *) strdup (argv[1]);
111
112         /* proc for compares */
113         } else if (strcasecmp (argv[0], "compare") == 0) {
114                 if (argc < 2) {
115                         Debug (LDAP_DEBUG_ANY,
116                                 "%s: line %d: missing proc in \"compare <proc>\" line\n",
117                                 fname, lineno, 0);
118                         exit (1);
119                 }
120                 ti->ti_compare = (char *) strdup (argv[1]);
121
122         /* proc for modify */
123         } else if (strcasecmp (argv[0], "modify") == 0) {
124                 if (argc < 2) {
125                         Debug (LDAP_DEBUG_ANY,
126                                 "%s: line %d: missing proc in \"modify <proc>\" line\n",
127                                 fname, lineno, 0);
128                         exit (1);
129                 }
130                 ti->ti_modify = (char *) strdup (argv[1]);
131
132         /* proc for modrdn */
133         } else if (strcasecmp (argv[0], "modrdn") == 0) {
134                 if (argc < 2) {
135                         Debug (LDAP_DEBUG_ANY,
136                                 "%s: line %d: missing proc in \"modrdn <proc>\" line\n",
137                                 fname, lineno, 0);
138                         exit (1);
139                 }
140                 ti->ti_modrdn = (char *) strdup (argv[1]);
141
142         /* proc for add */
143         } else if (strcasecmp (argv[0], "add") == 0) {
144                 if (argc < 2) {
145                         Debug (LDAP_DEBUG_ANY,
146                                 "%s: line %d: missing proc in \"add <proc>\" line\n",
147                                 fname, lineno, 0);
148                         exit (1);
149                 }
150                 ti->ti_add = (char *) strdup (argv[1]);
151
152         /* proc for delete */
153         } else if (strcasecmp (argv[0], "delete") == 0) {
154                 if (argc < 2) {
155                         Debug (LDAP_DEBUG_ANY,
156                                 "%s: line %d: missing proc in \"delete <proc>\" line\n",
157                                 fname, lineno, 0);
158                         exit (1);
159                 }
160                 ti->ti_delete = (char *) strdup (argv[1]);
161
162         /* proc for abandon */
163         } else if (strcasecmp (argv[0], "abandon") == 0) {
164                 if (argc < 2) {
165                         Debug (LDAP_DEBUG_ANY,
166                                 "%s: line %d: missing proc in \"abandon <proc>\" line\n",
167                                 fname, lineno, 0);
168                         exit (1);
169                 }
170                 ti->ti_search = (char *) strdup (argv[1]);
171
172         } else {
173                 fprintf (stderr,
174                         "Unknown tcl backend config: %s\n", argv[0]);
175                 exit (1);
176         }
177
178         return 0;
179 }
180
181 int tcl_back_db_open (
182         BackendDB * bd
183 )
184 {
185         struct tclinfo *ti = (struct tclinfo *) bd->be_private;
186
187         /* raise that count for the interpreter */
188         ti->ti_ii->count++;
189
190         /* now let's (try to) load the script */
191         readtclscript (ti->script_path, ti->ti_ii->interp);
192
193         /* Intall the debug command */
194         Tcl_CreateCommand( ti->ti_ii->interp, "ldap:debug", &tcl_ldap_debug,
195         NULL, NULL);
196
197         return 0;
198 }
199
200 void readtclscript (char *script, Tcl_Interp * my_tcl)
201 {
202         int code;
203         FILE *f;
204         f = fopen (script, "r");
205         if (f == NULL) {
206                 Debug (LDAP_DEBUG_ANY, "Could not open scriptpath %s\n", script,
207                         0, 0);
208                 exit (1);
209         }
210         fclose (f);
211         code = Tcl_EvalFile (my_tcl, script);
212         if (code != TCL_OK) {
213                 Debug (LDAP_DEBUG_ANY, "%s: %s\n", script,
214                         Tcl_GetVar (my_tcl, "errorInfo", TCL_GLOBAL_ONLY), 0);
215                 Debug (LDAP_DEBUG_ANY, "%s: error at line\n", script,
216                         my_tcl->errorLine, 0);
217                 exit (1);
218         }
219 }