2 Unix SMB/CIFS implementation.
4 provide access to getpwnam() and related calls
6 Copyright (C) Andrew Tridgell 2005
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "scripting/ejs/smbcalls.h"
24 #include "lib/appweb/ejs/ejs.h"
25 #include "system/passwd.h"
29 return a struct passwd as an object
31 static struct MprVar mpr_passwd(struct passwd *pwd)
35 return mprCreateUndefinedVar();
37 ret = mprObject("passwd");
39 mprSetVar(&ret, "pw_name", mprString(pwd->pw_name));
40 mprSetVar(&ret, "pw_passwd", mprString(pwd->pw_passwd));
41 mprSetVar(&ret, "pw_uid", mprCreateIntegerVar(pwd->pw_uid));
42 mprSetVar(&ret, "pw_gid", mprCreateIntegerVar(pwd->pw_gid));
43 mprSetVar(&ret, "pw_gecos", mprString(pwd->pw_gecos));
44 mprSetVar(&ret, "pw_dir", mprString(pwd->pw_dir));
45 mprSetVar(&ret, "pw_shell", mprString(pwd->pw_shell));
50 return a struct passwd as an object
52 static struct MprVar mpr_group(struct group *grp)
56 return mprCreateUndefinedVar();
58 ret = mprObject("group");
60 mprSetVar(&ret, "gr_name", mprString(grp->gr_name));
61 mprSetVar(&ret, "gr_passwd", mprString(grp->gr_passwd));
62 mprSetVar(&ret, "gr_gid", mprCreateIntegerVar(grp->gr_gid));
63 mprSetVar(&ret, "gr_mem", mprList("gr_mem", (const char **)grp->gr_mem));
70 var pw = nss.getpwnam("root");
72 returns an object containing struct passwd entries
74 static int ejs_getpwnam(MprVarHandle eid, int argc, struct MprVar **argv)
76 /* validate arguments */
77 if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
78 ejsSetErrorMsg(eid, "getpwnam invalid arguments");
82 mpr_Return(eid, mpr_passwd(getpwnam(mprToString(argv[0]))));
88 var pw = nss.getpwuid(0);
90 returns an object containing struct passwd entries
92 static int ejs_getpwuid(MprVarHandle eid, int argc, struct MprVar **argv)
94 /* validate arguments */
95 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
96 ejsSetErrorMsg(eid, "getpwuid invalid arguments");
99 mpr_Return(eid, mpr_passwd(getpwuid(mprToInt(argv[0]))));
105 var pw = nss.getgrnam("users");
107 returns an object containing struct group entries
109 static int ejs_getgrnam(MprVarHandle eid, int argc, struct MprVar **argv)
111 /* validate arguments */
112 if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
113 ejsSetErrorMsg(eid, "getgrnam invalid arguments");
116 mpr_Return(eid, mpr_group(getgrnam(mprToString(argv[0]))));
122 var pw = nss.getgrgid(0);
124 returns an object containing struct group entries
126 static int ejs_getgrgid(MprVarHandle eid, int argc, struct MprVar **argv)
128 /* validate arguments */
129 if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
130 ejsSetErrorMsg(eid, "getgrgid invalid arguments");
133 mpr_Return(eid, mpr_group(getgrgid(mprToInt(argv[0]))));
139 initialise nss ejs subsystem
141 static int ejs_nss_init(MprVarHandle eid, int argc, struct MprVar **argv)
143 struct MprVar *nss = mprInitObject(eid, "nss", argc, argv);
145 mprSetCFunction(nss, "getpwnam", ejs_getpwnam);
146 mprSetCFunction(nss, "getpwuid", ejs_getpwuid);
147 mprSetCFunction(nss, "getgrnam", ejs_getgrnam);
148 mprSetCFunction(nss, "getgrgid", ejs_getgrgid);
154 setup C functions that be called from ejs
156 NTSTATUS smb_setup_ejs_nss(void)
158 ejsDefineCFunction(-1, "nss_init", ejs_nss_init, NULL, MPR_VAR_SCRIPT_HANDLE);