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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "system/passwd.h"
30 return a struct passwd as an object
32 static struct MprVar mpr_passwd(struct passwd *pwd)
36 return mprCreateUndefinedVar();
38 ret = mprObject("passwd");
40 mprSetVar(&ret, "pw_name", mprString(pwd->pw_name));
41 mprSetVar(&ret, "pw_passwd", mprString(pwd->pw_passwd));
42 mprSetVar(&ret, "pw_uid", mprCreateIntegerVar(pwd->pw_uid));
43 mprSetVar(&ret, "pw_gid", mprCreateIntegerVar(pwd->pw_gid));
44 mprSetVar(&ret, "pw_gecos", mprString(pwd->pw_gecos));
45 mprSetVar(&ret, "pw_dir", mprString(pwd->pw_dir));
46 mprSetVar(&ret, "pw_shell", mprString(pwd->pw_shell));
51 return a struct passwd as an object
53 static struct MprVar mpr_group(struct group *grp)
57 return mprCreateUndefinedVar();
59 ret = mprObject("group");
61 mprSetVar(&ret, "gr_name", mprString(grp->gr_name));
62 mprSetVar(&ret, "gr_passwd", mprString(grp->gr_passwd));
63 mprSetVar(&ret, "gr_gid", mprCreateIntegerVar(grp->gr_gid));
64 mprSetVar(&ret, "gr_mem", mprList("gr_mem", (const char **)grp->gr_mem));
71 var pw = getpwnam("root");
73 returns an object containing struct passwd entries
75 static int ejs_getpwnam(MprVarHandle eid, int argc, struct MprVar **argv)
77 /* validate arguments */
78 if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
79 ejsSetErrorMsg(eid, "getpwnam invalid arguments");
83 mpr_Return(eid, mpr_passwd(getpwnam(mprToString(argv[0]))));
91 returns an object containing struct passwd entries
93 static int ejs_getpwuid(MprVarHandle eid, int argc, struct MprVar **argv)
95 /* validate arguments */
96 if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
97 ejsSetErrorMsg(eid, "getpwuid invalid arguments");
100 mpr_Return(eid, mpr_passwd(getpwuid(mprToInt(argv[0]))));
106 var pw = getgrnam("users");
108 returns an object containing struct group entries
110 static int ejs_getgrnam(MprVarHandle eid, int argc, struct MprVar **argv)
112 /* validate arguments */
113 if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
114 ejsSetErrorMsg(eid, "getgrnam invalid arguments");
117 mpr_Return(eid, mpr_group(getgrnam(mprToString(argv[0]))));
123 var pw = getgrgid(0);
125 returns an object containing struct group entries
127 static int ejs_getgrgid(MprVarHandle eid, int argc, struct MprVar **argv)
129 /* validate arguments */
130 if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
131 ejsSetErrorMsg(eid, "getgrgid invalid arguments");
134 mpr_Return(eid, mpr_group(getgrgid(mprToInt(argv[0]))));
140 setup C functions that be called from ejs
142 void smb_setup_ejs_nss(void)
144 ejsDefineCFunction(-1, "getpwnam", ejs_getpwnam, NULL, MPR_VAR_SCRIPT_HANDLE);
145 ejsDefineCFunction(-1, "getpwuid", ejs_getpwuid, NULL, MPR_VAR_SCRIPT_HANDLE);
146 ejsDefineCFunction(-1, "getgrnam", ejs_getgrnam, NULL, MPR_VAR_SCRIPT_HANDLE);
147 ejsDefineCFunction(-1, "getgrgid", ejs_getgrgid, NULL, MPR_VAR_SCRIPT_HANDLE);