r11995: A big kerberos-related update.
[jelmer/samba4-debian.git] / source / scripting / ejs / smbcalls_creds.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide hooks credentials calls
5
6    Copyright (C) Andrew Tridgell 2005
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "lib/cmdline/popt_common.h"
27
28 /*
29   helper function to get the local objects credentials ptr
30 */
31 static struct cli_credentials *ejs_creds_get_credentials(int eid)
32 {
33         struct cli_credentials *creds = mprGetThisPtr(eid, "creds");
34         if (creds == NULL) {
35                 ejsSetErrorMsg(eid, "NULL ejs credentials");
36         }
37         return creds;
38 }
39
40 /*
41   get a domain
42 */
43 static int ejs_creds_get_domain(MprVarHandle eid, int argc, struct MprVar **argv)
44 {
45         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
46
47         mpr_Return(eid, mprString(cli_credentials_get_domain(creds)));
48         return 0;
49 }
50
51
52 /*
53   set a domain
54 */
55 static int ejs_creds_set_domain(MprVarHandle eid, int argc, char **argv)
56 {
57         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
58         if (argc != 1) {
59                 ejsSetErrorMsg(eid, "bad arguments to set_domain");
60                 return -1;
61         }
62
63         cli_credentials_set_domain(creds, argv[0], CRED_SPECIFIED);
64         mpr_Return(eid, mprCreateBoolVar(True));
65         return 0;
66 }
67
68
69 /*
70   get a username
71 */
72 static int ejs_creds_get_username(MprVarHandle eid, int argc, struct MprVar **argv)
73 {
74         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
75
76         mpr_Return(eid, mprString(cli_credentials_get_username(creds)));
77         return 0;
78 }
79
80
81 /*
82   set a username
83 */
84 static int ejs_creds_set_username(MprVarHandle eid, int argc, char **argv)
85 {
86         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
87         if (argc != 1) {
88                 ejsSetErrorMsg(eid, "bad arguments to set_username");
89                 return -1;
90         }
91
92         cli_credentials_set_username(creds, argv[0], CRED_SPECIFIED);
93         mpr_Return(eid, mprCreateBoolVar(True));
94         return 0;
95 }
96
97
98 /*
99   get user password
100 */
101 static int ejs_creds_get_password(MprVarHandle eid, int argc, struct MprVar **argv)
102 {
103         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
104         
105         mpr_Return(eid, mprString(cli_credentials_get_password(creds)));
106         return 0;
107 }
108
109
110 /*
111   set user password
112 */
113 static int ejs_creds_set_password(MprVarHandle eid, int argc, char **argv)
114 {
115         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
116         if (argc != 1) {
117                 ejsSetErrorMsg(eid, "bad arguments to set_password");
118                 return -1;
119         }
120
121         cli_credentials_set_password(creds, argv[0], CRED_SPECIFIED);
122         mpr_Return(eid, mprCreateBoolVar(True));
123         return 0;
124 }
125
126
127 /*
128   set realm
129 */
130 static int ejs_creds_set_realm(MprVarHandle eid, int argc, char **argv)
131 {
132         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
133         if (argc != 1) {
134                 ejsSetErrorMsg(eid, "bad arguments to set_realm");
135                 return -1;
136         }
137
138         cli_credentials_set_realm(creds, argv[0], CRED_SPECIFIED);
139         mpr_Return(eid, mprCreateBoolVar(True));
140         return 0;
141 }
142
143
144 /*
145   get realm
146 */
147 static int ejs_creds_get_realm(MprVarHandle eid, int argc, struct MprVar **argv)
148 {
149         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
150         
151         mpr_Return(eid, mprString(cli_credentials_get_realm(creds)));
152         return 0;
153 }
154
155
156 /*
157   set workstation
158 */
159 static int ejs_creds_set_workstation(MprVarHandle eid, int argc, char **argv)
160 {
161         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
162         if (argc != 1) {
163                 ejsSetErrorMsg(eid, "bad arguments to set_workstation");
164                 return -1;
165         }
166         
167         cli_credentials_set_workstation(creds, argv[0], CRED_SPECIFIED);
168         mpr_Return(eid, mprCreateBoolVar(True));
169         return 0;
170 }
171
172
173 /*
174   get workstation
175 */
176 static int ejs_creds_get_workstation(MprVarHandle eid, int argc, struct MprVar **argv)
177 {
178         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
179         
180         mpr_Return(eid, mprString(cli_credentials_get_workstation(creds)));
181         return 0;
182 }
183
184
185 /*
186   initialise credentials ejs object
187 */
188 static int ejs_credentials_obj(struct MprVar *obj, struct cli_credentials *creds)
189 {
190         mprSetPtrChild(obj, "creds", creds);
191
192         /* setup our object methods */
193         mprSetCFunction(obj, "get_domain", ejs_creds_get_domain);
194         mprSetStringCFunction(obj, "set_domain", ejs_creds_set_domain);
195         mprSetCFunction(obj, "get_username", ejs_creds_get_username);
196         mprSetStringCFunction(obj, "set_username", ejs_creds_set_username);
197         mprSetCFunction(obj, "get_password", ejs_creds_get_password);
198         mprSetStringCFunction(obj, "set_password", ejs_creds_set_password);
199         mprSetCFunction(obj, "get_realm", ejs_creds_get_realm);
200         mprSetStringCFunction(obj, "set_realm", ejs_creds_set_realm);
201         mprSetCFunction(obj, "get_workstation", ejs_creds_get_workstation);
202         mprSetStringCFunction(obj, "set_workstation", ejs_creds_set_workstation);
203
204         return 0;
205 }
206
207
208 struct MprVar mprCredentials(struct cli_credentials *creds)
209 {
210         struct MprVar mpv = mprObject("credentials");
211
212         ejs_credentials_obj(&mpv, creds);
213         
214         return mpv;
215 }
216
217
218 /*
219   initialise credentials ejs object
220 */
221 static int ejs_credentials_init(MprVarHandle eid, int argc, struct MprVar **argv)
222 {
223         struct cli_credentials *creds;
224         struct MprVar *obj = mprInitObject(eid, "credentials", argc, argv);
225
226         creds = cli_credentials_init(mprMemCtx());
227         if (creds == NULL) {
228                 return -1;
229         }
230
231         return ejs_credentials_obj(obj, creds);
232 }
233
234 /*
235   initialise cmdline credentials ejs object
236 */
237 int ejs_credentials_cmdline(int eid, int argc, struct MprVar **argv)
238 {
239         struct MprVar *obj = mprInitObject(eid, "credentials", argc, argv);
240         return ejs_credentials_obj(obj, cmdline_credentials);
241 }
242
243 static int ejs_credentials_update_all_keytabs(MprVarHandle eid, int argc, struct MprVar **argv)
244 {
245         if (!NT_STATUS_IS_OK(cli_credentials_update_all_keytabs(mprMemCtx()))) {
246                 mpr_Return(eid, mprCreateBoolVar(False));
247         } else {
248                 mpr_Return(eid, mprCreateBoolVar(True));
249         }
250         return 0;
251 }
252
253
254 /*
255   setup C functions that be called from ejs
256 */
257 void smb_setup_ejs_credentials(void)
258 {
259         ejsDefineCFunction(-1, "credentials_init", ejs_credentials_init, NULL, MPR_VAR_SCRIPT_HANDLE);
260         ejsDefineCFunction(-1, "credentials_update_all_keytabs", ejs_credentials_update_all_keytabs, NULL, MPR_VAR_SCRIPT_HANDLE);
261 }
262