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