summaryrefslogtreecommitdiff
path: root/source4/scripting/ejs/smbcalls_creds.c
blob: 45da5895e42fbd5e9e300c39e5dab182cf6295c0 (plain)
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
/* 
   Unix SMB/CIFS implementation.

   provide hooks credentials calls

   Copyright (C) Andrew Tridgell 2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "scripting/ejs/smbcalls.h"
#include "lib/appweb/ejs/ejs.h"
#include "lib/cmdline/popt_common.h"

/*
  helper function to get the local objects credentials ptr
*/
static struct cli_credentials *ejs_creds_get_credentials(int eid)
{
	struct cli_credentials *creds = mprGetThisPtr(eid, "creds");
	if (creds == NULL) {
		ejsSetErrorMsg(eid, "NULL ejs credentials");
	}
	return creds;
}

/*
  get a domain
*/
static int ejs_creds_get_domain(MprVarHandle eid, int argc, struct MprVar **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);

	mpr_Return(eid, mprString(cli_credentials_get_domain(creds)));
	return 0;
}


/*
  set a domain
*/
static int ejs_creds_set_domain(MprVarHandle eid, int argc, char **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);
	if (argc != 1) {
		ejsSetErrorMsg(eid, "bad arguments to set_domain");
		return -1;
	}

	cli_credentials_set_domain(creds, argv[0], CRED_SPECIFIED);
	mpr_Return(eid, mprCreateBoolVar(True));
	return 0;
}


/*
  get a username
*/
static int ejs_creds_get_username(MprVarHandle eid, int argc, struct MprVar **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);

	mpr_Return(eid, mprString(cli_credentials_get_username(creds)));
	return 0;
}


/*
  set a username
*/
static int ejs_creds_set_username(MprVarHandle eid, int argc, char **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);
	if (argc != 1) {
		ejsSetErrorMsg(eid, "bad arguments to set_username");
		return -1;
	}

	cli_credentials_set_username(creds, argv[0], CRED_SPECIFIED);
	mpr_Return(eid, mprCreateBoolVar(True));
	return 0;
}


/*
  get user password
*/
static int ejs_creds_get_password(MprVarHandle eid, int argc, struct MprVar **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);
	
	mpr_Return(eid, mprString(cli_credentials_get_password(creds)));
	return 0;
}


/*
  set user password
*/
static int ejs_creds_set_password(MprVarHandle eid, int argc, char **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);
	if (argc != 1) {
		ejsSetErrorMsg(eid, "bad arguments to set_password");
		return -1;
	}

	cli_credentials_set_password(creds, argv[0], CRED_SPECIFIED);
	mpr_Return(eid, mprCreateBoolVar(True));
	return 0;
}


/*
  set realm
*/
static int ejs_creds_set_realm(MprVarHandle eid, int argc, char **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);
	if (argc != 1) {
		ejsSetErrorMsg(eid, "bad arguments to set_realm");
		return -1;
	}

	cli_credentials_set_realm(creds, argv[0], CRED_SPECIFIED);
	mpr_Return(eid, mprCreateBoolVar(True));
	return 0;
}


/*
  get realm
*/
static int ejs_creds_get_realm(MprVarHandle eid, int argc, struct MprVar **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);
	
	mpr_Return(eid, mprString(cli_credentials_get_realm(creds)));
	return 0;
}


/*
  set workstation
*/
static int ejs_creds_set_workstation(MprVarHandle eid, int argc, char **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);
	if (argc != 1) {
		ejsSetErrorMsg(eid, "bad arguments to set_workstation");
		return -1;
	}
	
	cli_credentials_set_workstation(creds, argv[0], CRED_SPECIFIED);
	mpr_Return(eid, mprCreateBoolVar(True));
	return 0;
}


/*
  get workstation
*/
static int ejs_creds_get_workstation(MprVarHandle eid, int argc, struct MprVar **argv)
{
	struct cli_credentials *creds = ejs_creds_get_credentials(eid);
	
	mpr_Return(eid, mprString(cli_credentials_get_workstation(creds)));
	return 0;
}


/*
  initialise credentials ejs object
*/
static int ejs_credentials_obj(MprVarHandle eid, int argc, struct MprVar **argv, struct cli_credentials *creds)
{
	struct MprVar *obj = mprInitObject(eid, "credentials", argc, argv);

	mprSetPtrChild(obj, "creds", creds);

	/* setup our object methods */
	mprSetCFunction(obj, "get_domain", ejs_creds_get_domain);
	mprSetStringCFunction(obj, "set_domain", ejs_creds_set_domain);
	mprSetCFunction(obj, "get_username", ejs_creds_get_username);
	mprSetStringCFunction(obj, "set_username", ejs_creds_set_username);
	mprSetCFunction(obj, "get_password", ejs_creds_get_password);
	mprSetStringCFunction(obj, "set_password", ejs_creds_set_password);
	mprSetCFunction(obj, "get_realm", ejs_creds_get_realm);
	mprSetStringCFunction(obj, "set_realm", ejs_creds_set_realm);
	mprSetCFunction(obj, "get_workstation", ejs_creds_get_workstation);
	mprSetStringCFunction(obj, "set_workstation", ejs_creds_set_workstation);

	return 0;
}


/*
  initialise credentials ejs object
*/
static int ejs_credentials_init(MprVarHandle eid, int argc, struct MprVar **argv)
{
	struct cli_credentials *creds;

	creds = cli_credentials_init(mprMemCtx());
	if (creds == NULL) {
		return -1;
	}

	return ejs_credentials_obj(eid, argc, argv, creds);
}

/*
  initialise cmdline credentials ejs object
*/
int ejs_credentials_cmdline(int eid, int argc, struct MprVar **argv)
{
	return ejs_credentials_obj(eid, argc, argv, cmdline_credentials);
}


/*
  setup C functions that be called from ejs
*/
void smb_setup_ejs_credentials(void)
{
	ejsDefineCFunction(-1, "credentials_init", ejs_credentials_init, NULL, MPR_VAR_SCRIPT_HANDLE);
}