r8318: added a bunch more ejs calls.
authorAndrew Tridgell <tridge@samba.org>
Mon, 11 Jul 2005 09:19:50 +0000 (09:19 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:20:06 +0000 (13:20 -0500)
  getgr*()
  getpw*()
  strlower()
  strupper()
  IfaceList()

source/scripting/ejs/config.mk
source/scripting/ejs/mprutil.c
source/scripting/ejs/smbcalls.c
source/scripting/ejs/smbcalls.h
source/scripting/ejs/smbcalls_nss.c [new file with mode: 0644]
source/scripting/ejs/smbcalls_string.c [new file with mode: 0644]
source/scripting/libjs/base.js

index 7afcacbdf57c5a17cf7490d54b2c504ccb168c2b..5e061cb2a35d99a1ab087c99e32e7b19207a56ff 100644 (file)
@@ -20,6 +20,8 @@ OBJ_FILES = \
                scripting/ejs/smbcalls_rpc.o \
                scripting/ejs/smbcalls_auth.o \
                scripting/ejs/smbcalls_options.o \
+               scripting/ejs/smbcalls_nss.o \
+               scripting/ejs/smbcalls_string.o \
                scripting/ejs/mprutil.o
 REQUIRED_SUBSYSTEMS = AUTH EJS LIBBASIC EJSRPC MESSAGING
 # End SUBSYSTEM SMBCALLS
index 6b5c02c48dd5e33e037f8638c5b01ee8679ba8ab..a9cf71b15de5840c6d73d0d022dadbfe7a51ab7f 100644 (file)
@@ -114,6 +114,18 @@ struct MprVar mprList(const char *name, const char **list)
        return var;
 }
 
+/*
+  construct a MprVar from a string, using NULL if needed
+*/
+struct MprVar mprString(const char *s)
+{
+       struct MprVar var;
+       if (s == NULL) {
+               return mprCreatePtrVar(NULL, "NULL");
+       }
+       return mprCreateStringVar(s, 1);
+}
+
 /*
   construct a string MprVar from a lump of data
 */
index 328bd14ab5e91e0b76a16cad32c4c3082bbb1417..5972518036743fb9f1426dda9257604675a5a708 100644 (file)
@@ -64,6 +64,20 @@ static int ejs_typeof(MprVarHandle eid, int argc, struct MprVar **argv)
        return 0;
 }
 
+/*
+  return the list of configured network interfaces
+*/
+static int ejs_IfaceList(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       int i, count = iface_count();
+       struct MprVar ret = mprCreateObjVar("interfaces", MPR_DEFAULT_HASH_SIZE);
+       for (i=0;i<count;i++) {
+               mprAddArray(&ret, i, mprString(iface_n_ip(i)));
+       }
+       mpr_Return(eid, ret);
+       return 0;       
+}
+
 
 /*
   libinclude() allows you to include js files using a search path specified
@@ -123,8 +137,11 @@ void smb_setup_ejs_functions(void)
        smb_setup_ejs_rpc();
        smb_setup_ejs_auth();
        smb_setup_ejs_options();
+       smb_setup_ejs_nss();
+       smb_setup_ejs_string();
 
        ejsDefineCFunction(-1, "typeof", ejs_typeof, NULL, MPR_VAR_SCRIPT_HANDLE);
+       ejsDefineCFunction(-1, "IfaceList", ejs_IfaceList, NULL, MPR_VAR_SCRIPT_HANDLE);
        ejsDefineStringCFunction(-1, "libinclude", ejs_libinclude, NULL, MPR_VAR_SCRIPT_HANDLE);
 }
 
index 5bd7d4e448f2f8569f0817fd9888306cb7b9962f..57242fac0b9eccf0c87350989217f1a2bab8730f 100644 (file)
@@ -26,6 +26,3 @@ void mpr_Return(int eid, struct MprVar);
 NTSTATUS mprSetVar(struct MprVar *v, const char *name, struct MprVar val);
 NTSTATUS mprGetVar(struct MprVar **v, const char *name);
 void mprAddArray(struct MprVar *var, int i, struct MprVar v);
-
-
-
diff --git a/source/scripting/ejs/smbcalls_nss.c b/source/scripting/ejs/smbcalls_nss.c
new file mode 100644 (file)
index 0000000..1eef1ef
--- /dev/null
@@ -0,0 +1,148 @@
+/* 
+   Unix SMB/CIFS implementation.
+
+   provide access to getpwnam() and related 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/ejs/ejs.h"
+#include "system/passwd.h"
+
+
+/*
+  return a struct passwd as an object
+*/
+static struct MprVar mpr_passwd(struct passwd *pwd)
+{
+       struct MprVar ret;
+       if (pwd == NULL) {
+               return mprCreateUndefinedVar();
+       }
+       ret = mprCreateObjVar("passwd", MPR_DEFAULT_HASH_SIZE);
+
+       mprSetVar(&ret, "pw_name",   mprString(pwd->pw_name));
+       mprSetVar(&ret, "pw_passwd", mprString(pwd->pw_passwd));
+       mprSetVar(&ret, "pw_uid",    mprCreateIntegerVar(pwd->pw_uid));
+       mprSetVar(&ret, "pw_gid",    mprCreateIntegerVar(pwd->pw_gid));
+       mprSetVar(&ret, "pw_gecos",  mprString(pwd->pw_gecos));
+       mprSetVar(&ret, "pw_dir",    mprString(pwd->pw_dir));
+       mprSetVar(&ret, "pw_shell",  mprString(pwd->pw_shell));
+       return ret;
+}
+
+/*
+  return a struct passwd as an object
+*/
+static struct MprVar mpr_group(struct group *grp)
+{
+       struct MprVar ret;
+       if (grp == NULL) {
+               return mprCreateUndefinedVar();
+       }
+       ret = mprCreateObjVar("group", MPR_DEFAULT_HASH_SIZE);
+
+       mprSetVar(&ret, "gr_name",   mprString(grp->gr_name));
+       mprSetVar(&ret, "gr_passwd", mprString(grp->gr_passwd));
+       mprSetVar(&ret, "gr_gid",    mprCreateIntegerVar(grp->gr_gid));
+       mprSetVar(&ret, "gr_mem",    mprList("gr_mem", (const char **)grp->gr_mem));
+       return ret;
+}
+
+
+/*
+  usage:
+      var pw = getpwnam("root");
+
+  returns an object containing struct passwd entries
+*/
+static int ejs_getpwnam(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       /* validate arguments */
+       if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
+               ejsSetErrorMsg(eid, "getpwnam invalid arguments");
+               return -1;
+       }
+
+       mpr_Return(eid, mpr_passwd(getpwnam(mprToString(argv[0]))));
+       return 0;
+}
+
+/*
+  usage:
+      var pw = getpwuid(0);
+
+  returns an object containing struct passwd entries
+*/
+static int ejs_getpwuid(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       /* validate arguments */
+       if (argc != 1 || argv[0]->type != MPR_TYPE_INT) {
+               ejsSetErrorMsg(eid, "getpwuid invalid arguments");
+               return -1;
+       }
+       mpr_Return(eid, mpr_passwd(getpwuid(mprToInt(argv[0]))));
+       return 0;
+}
+
+/*
+  usage:
+      var pw = getgrnam("users");
+
+  returns an object containing struct group entries
+*/
+static int ejs_getgrnam(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       /* validate arguments */
+       if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
+               ejsSetErrorMsg(eid, "getgrnam invalid arguments");
+               return -1;
+       }
+       mpr_Return(eid, mpr_group(getgrnam(mprToString(argv[0]))));
+       return 0;
+}
+
+/*
+  usage:
+      var pw = getgrgid(0);
+
+  returns an object containing struct group entries
+*/
+static int ejs_getgrgid(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       /* validate arguments */
+       if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
+               ejsSetErrorMsg(eid, "getgrgid invalid arguments");
+               return -1;
+       }
+       mpr_Return(eid, mpr_group(getgrgid(mprToInt(argv[0]))));
+       return 0;
+}
+
+
+/*
+  setup C functions that be called from ejs
+*/
+void smb_setup_ejs_nss(void)
+{
+       ejsDefineCFunction(-1, "getpwnam", ejs_getpwnam, NULL, MPR_VAR_SCRIPT_HANDLE);
+       ejsDefineCFunction(-1, "getpwuid", ejs_getpwuid, NULL, MPR_VAR_SCRIPT_HANDLE);
+       ejsDefineCFunction(-1, "getgrnam", ejs_getgrnam, NULL, MPR_VAR_SCRIPT_HANDLE);
+       ejsDefineCFunction(-1, "getgrgid", ejs_getgrgid, NULL, MPR_VAR_SCRIPT_HANDLE);
+}
diff --git a/source/scripting/ejs/smbcalls_string.c b/source/scripting/ejs/smbcalls_string.c
new file mode 100644 (file)
index 0000000..7817de1
--- /dev/null
@@ -0,0 +1,70 @@
+/* 
+   Unix SMB/CIFS implementation.
+
+   provide access to string functions
+
+   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/ejs/ejs.h"
+#include "system/passwd.h"
+
+/*
+  usage:
+      var s = strlower("UPPER");
+*/
+static int ejs_strlower(MprVarHandle eid, int argc, char **argv)
+{
+       char *s;
+       if (argc != 1) {
+               ejsSetErrorMsg(eid, "strlower invalid arguments");
+               return -1;
+       }
+       s = strlower_talloc(mprMemCtx(), argv[0]);
+       mpr_Return(eid, mprString(s));
+       talloc_free(s);
+       return 0;
+}
+
+/*
+  usage:
+      var s = strupper("lower");
+*/
+static int ejs_strupper(MprVarHandle eid, int argc, char **argv)
+{
+       char *s;
+       if (argc != 1) {
+               ejsSetErrorMsg(eid, "strupper invalid arguments");
+               return -1;
+       }
+       s = strupper_talloc(mprMemCtx(), argv[0]);
+       mpr_Return(eid, mprString(s));
+       talloc_free(s);
+       return 0;
+}
+
+
+/*
+  setup C functions that be called from ejs
+*/
+void smb_setup_ejs_string(void)
+{
+       ejsDefineStringCFunction(-1, "strlower", ejs_strlower, NULL, MPR_VAR_SCRIPT_HANDLE);
+       ejsDefineStringCFunction(-1, "strupper", ejs_strupper, NULL, MPR_VAR_SCRIPT_HANDLE);
+}
index 504cd82259701260eacb2871594fed8beccd6f25..f5498789c590f18c0a41121f2a219be9a53d3fb0 100644 (file)
@@ -50,4 +50,3 @@ function check_array_zero(a)
                assert(a[i] == 0);
        }
 }
-