r10510: Decrease the amount of data included by includes.h a bit
[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 #include "credentials.h"
28
29 /*
30   helper function to get the local objects credentials ptr
31 */
32 static struct cli_credentials *ejs_creds_get_credentials(int eid)
33 {
34         struct cli_credentials *creds = mprGetThisPtr(eid, "creds");
35         if (creds == NULL) {
36                 ejsSetErrorMsg(eid, "NULL ejs credentials");
37         }
38         return creds;
39 }
40
41 /*
42   get a domain
43 */
44 static int ejs_creds_get_domain(MprVarHandle eid, int argc, struct MprVar **argv)
45 {
46         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
47
48         mpr_Return(eid, mprString(cli_credentials_get_domain(creds)));
49         return 0;
50 }
51
52
53 /*
54   set a domain
55 */
56 static int ejs_creds_set_domain(MprVarHandle eid, int argc, char **argv)
57 {
58         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
59         if (argc != 1) {
60                 ejsSetErrorMsg(eid, "bad arguments to set_domain");
61                 return -1;
62         }
63
64         cli_credentials_set_domain(creds, argv[0], CRED_SPECIFIED);
65         mpr_Return(eid, mprCreateBoolVar(True));
66         return 0;
67 }
68
69
70 /*
71   get a username
72 */
73 static int ejs_creds_get_username(MprVarHandle eid, int argc, struct MprVar **argv)
74 {
75         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
76
77         mpr_Return(eid, mprString(cli_credentials_get_username(creds)));
78         return 0;
79 }
80
81
82 /*
83   set a username
84 */
85 static int ejs_creds_set_username(MprVarHandle eid, int argc, char **argv)
86 {
87         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
88         if (argc != 1) {
89                 ejsSetErrorMsg(eid, "bad arguments to set_username");
90                 return -1;
91         }
92
93         cli_credentials_set_username(creds, argv[0], CRED_SPECIFIED);
94         mpr_Return(eid, mprCreateBoolVar(True));
95         return 0;
96 }
97
98
99 /*
100   get user password
101 */
102 static int ejs_creds_get_password(MprVarHandle eid, int argc, struct MprVar **argv)
103 {
104         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
105         
106         mpr_Return(eid, mprString(cli_credentials_get_password(creds)));
107         return 0;
108 }
109
110
111 /*
112   set user password
113 */
114 static int ejs_creds_set_password(MprVarHandle eid, int argc, char **argv)
115 {
116         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
117         if (argc != 1) {
118                 ejsSetErrorMsg(eid, "bad arguments to set_password");
119                 return -1;
120         }
121
122         cli_credentials_set_password(creds, argv[0], CRED_SPECIFIED);
123         mpr_Return(eid, mprCreateBoolVar(True));
124         return 0;
125 }
126
127
128 /*
129   set realm
130 */
131 static int ejs_creds_set_realm(MprVarHandle eid, int argc, char **argv)
132 {
133         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
134         if (argc != 1) {
135                 ejsSetErrorMsg(eid, "bad arguments to set_realm");
136                 return -1;
137         }
138
139         cli_credentials_set_realm(creds, argv[0], CRED_SPECIFIED);
140         mpr_Return(eid, mprCreateBoolVar(True));
141         return 0;
142 }
143
144
145 /*
146   get realm
147 */
148 static int ejs_creds_get_realm(MprVarHandle eid, int argc, struct MprVar **argv)
149 {
150         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
151         
152         mpr_Return(eid, mprString(cli_credentials_get_realm(creds)));
153         return 0;
154 }
155
156
157 /*
158   set workstation
159 */
160 static int ejs_creds_set_workstation(MprVarHandle eid, int argc, char **argv)
161 {
162         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
163         if (argc != 1) {
164                 ejsSetErrorMsg(eid, "bad arguments to set_workstation");
165                 return -1;
166         }
167         
168         cli_credentials_set_workstation(creds, argv[0], CRED_SPECIFIED);
169         mpr_Return(eid, mprCreateBoolVar(True));
170         return 0;
171 }
172
173
174 /*
175   get workstation
176 */
177 static int ejs_creds_get_workstation(MprVarHandle eid, int argc, struct MprVar **argv)
178 {
179         struct cli_credentials *creds = ejs_creds_get_credentials(eid);
180         
181         mpr_Return(eid, mprString(cli_credentials_get_workstation(creds)));
182         return 0;
183 }
184
185
186 /*
187   initialise credentials ejs object
188 */
189 static int ejs_credentials_obj(struct MprVar *obj, struct cli_credentials *creds)
190 {
191         mprSetPtrChild(obj, "creds", creds);
192
193         /* setup our object methods */
194         mprSetCFunction(obj, "get_domain", ejs_creds_get_domain);
195         mprSetStringCFunction(obj, "set_domain", ejs_creds_set_domain);
196         mprSetCFunction(obj, "get_username", ejs_creds_get_username);
197         mprSetStringCFunction(obj, "set_username", ejs_creds_set_username);
198         mprSetCFunction(obj, "get_password", ejs_creds_get_password);
199         mprSetStringCFunction(obj, "set_password", ejs_creds_set_password);
200         mprSetCFunction(obj, "get_realm", ejs_creds_get_realm);
201         mprSetStringCFunction(obj, "set_realm", ejs_creds_set_realm);
202         mprSetCFunction(obj, "get_workstation", ejs_creds_get_workstation);
203         mprSetStringCFunction(obj, "set_workstation", ejs_creds_set_workstation);
204
205         return 0;
206 }
207
208
209 struct MprVar mprCredentials(struct cli_credentials *creds)
210 {
211         struct MprVar mpv = mprObject("credentials");
212
213         ejs_credentials_obj(&mpv, creds);
214         
215         return mpv;
216 }
217
218
219 /*
220   initialise credentials ejs object
221 */
222 static int ejs_credentials_init(MprVarHandle eid, int argc, struct MprVar **argv)
223 {
224         struct cli_credentials *creds;
225         struct MprVar *obj = mprInitObject(eid, "credentials", argc, argv);
226
227         creds = cli_credentials_init(mprMemCtx());
228         if (creds == NULL) {
229                 return -1;
230         }
231
232         return ejs_credentials_obj(obj, creds);
233 }
234
235 /*
236   initialise cmdline credentials ejs object
237 */
238 int ejs_credentials_cmdline(int eid, int argc, struct MprVar **argv)
239 {
240         struct MprVar *obj = mprInitObject(eid, "credentials", argc, argv);
241         return ejs_credentials_obj(obj, cmdline_credentials);
242 }
243
244
245 /*
246   setup C functions that be called from ejs
247 */
248 void smb_setup_ejs_credentials(void)
249 {
250         ejsDefineCFunction(-1, "credentials_init", ejs_credentials_init, NULL, MPR_VAR_SCRIPT_HANDLE);
251 }