Introduce mprLpCtx() similar to mprMemCtx() for loadparm_context used by
[jelmer/samba4-debian.git] / source / scripting / ejs / smbcalls_sys.c
index b8382db296a9d9fcd3f53cdedc6f5956383cfb8f..00599a55bc70211dcc0619b95e2110e355009790 100644 (file)
@@ -7,7 +7,7 @@
    
    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
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
@@ -16,8 +16,7 @@
    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.
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #include "includes.h"
 #include "lib/ldb/include/ldb.h"
 #include "system/time.h"
 #include "system/network.h"
-#include "netif/netif.h"
+#include "lib/socket/netif.h"
 
 /*
   return the list of configured network interfaces
 */
 static int ejs_sys_interfaces(MprVarHandle eid, int argc, struct MprVar **argv)
 {
-       int i, count = iface_count();
+       int i, count;
        struct MprVar ret = mprArray("interfaces");
+       struct interface *ifaces;
+
+       load_interfaces(NULL, lp_interfaces(mprLpCtx()), &ifaces);
+
+       count = iface_count(ifaces);
        for (i=0;i<count;i++) {
-               mprAddArray(&ret, i, mprString(iface_n_ip(i)));
+               mprAddArray(&ret, i, mprString(iface_n_ip(ifaces, i)));
        }
+
+       talloc_free(ifaces);
        mpr_Return(eid, ret);
        return 0;       
 }
@@ -57,6 +63,22 @@ static int ejs_sys_hostname(MprVarHandle eid, int argc, struct MprVar **argv)
 }
 
 
+/*
+  return current time as seconds and microseconds
+*/
+static int ejs_sys_gettimeofday(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       struct timeval tv = timeval_current();
+        struct MprVar v = mprObject("timeval");
+       struct MprVar sec = mprCreateIntegerVar(tv.tv_sec);
+       struct MprVar usec = mprCreateIntegerVar(tv.tv_usec);
+
+        mprCreateProperty(&v, "sec", &sec);
+        mprCreateProperty(&v, "usec", &usec);
+       mpr_Return(eid, v);
+       return 0;
+}
+
 /*
   return current time as a 64 bit nttime value
 */
@@ -85,6 +107,35 @@ static int ejs_sys_unix2nttime(MprVarHandle eid, int argc, struct MprVar **argv)
        return 0;
 }
 
+/*
+  return the GMT time represented by the struct tm argument, as a time_t value
+*/
+static int ejs_sys_gmmktime(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       struct MprVar *o;
+       struct tm tm;
+       if (argc != 1 || !mprVarIsObject(argv[0]->type)) {
+               ejsSetErrorMsg(eid, "sys_gmmktime invalid arguments");
+               return -1;
+       }
+
+        o = argv[0];
+#define TM_EL(n) tm.n = mprVarToNumber(mprGetProperty(o, #n, NULL))
+       TM_EL(tm_sec);
+       TM_EL(tm_min);
+       TM_EL(tm_hour);
+       TM_EL(tm_mday);
+       TM_EL(tm_mon);
+       TM_EL(tm_year);
+       TM_EL(tm_wday);
+       TM_EL(tm_yday);
+       TM_EL(tm_isdst);
+#undef TM_EL        
+
+       mpr_Return(eid, mprCreateIntegerVar(mktime(&tm)));
+       return 0;
+}
+
 /*
   return the given time as a gmtime structure
 */
@@ -97,6 +148,58 @@ static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
                ejsSetErrorMsg(eid, "sys_gmtime invalid arguments");
                return -1;
        }
+       t = (time_t) mprVarToNumber(argv[0]);
+       tm = gmtime(&t);
+       if (tm == NULL) {
+               mpr_Return(eid, mprCreateUndefinedVar());
+               return 0;
+       }
+       ret = mprObject("gmtime");
+#define TM_EL(n) mprSetVar(&ret, #n, mprCreateIntegerVar(tm->n))
+       TM_EL(tm_sec);
+       TM_EL(tm_min);
+       TM_EL(tm_hour);
+       TM_EL(tm_mday);
+       TM_EL(tm_mon);
+       TM_EL(tm_year);
+       TM_EL(tm_wday);
+       TM_EL(tm_yday);
+       TM_EL(tm_isdst);
+#undef TM_EL
+
+       mpr_Return(eid, ret);
+       return 0;
+}
+
+/*
+  return the given NT time as a time_t value
+*/
+static int ejs_sys_nttime2unix(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       time_t t;
+       struct MprVar v;
+       if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
+               ejsSetErrorMsg(eid, "sys_ntgmtime invalid arguments");
+               return -1;
+       }
+       t = nt_time_to_unix(mprVarToNumber(argv[0]));
+       v = mprCreateNumberVar(t);
+       mpr_Return(eid, v);
+        return 0;
+}
+
+/*
+  return the given NT time as a gmtime structure
+*/
+static int ejs_sys_ntgmtime(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       time_t t;
+       struct MprVar ret;
+       struct tm *tm;
+       if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
+               ejsSetErrorMsg(eid, "sys_ntgmtime invalid arguments");
+               return -1;
+       }
        t = nt_time_to_unix(mprVarToNumber(argv[0]));
        tm = gmtime(&t);
        if (tm == NULL) {
@@ -114,6 +217,7 @@ static int ejs_sys_gmtime(MprVarHandle eid, int argc, struct MprVar **argv)
        TM_EL(tm_wday);
        TM_EL(tm_yday);
        TM_EL(tm_isdst);
+#undef TM_EL
 
        mpr_Return(eid, ret);
        return 0;
@@ -197,7 +301,7 @@ static int ejs_sys_file_load(MprVarHandle eid, int argc, char **argv)
 */
 static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv)
 {
-       BOOL ret;
+       bool ret;
        if (argc != 2) {
                ejsSetErrorMsg(eid, "sys_file_save invalid arguments");
                return -1;
@@ -207,6 +311,33 @@ static int ejs_sys_file_save(MprVarHandle eid, int argc, char **argv)
        return 0;
 }
 
+/*
+  mkdir()
+  usage:
+     ok = sys.mkdir(dirname, mode);
+*/
+static int ejs_sys_mkdir(MprVarHandle eid, int argc, struct MprVar **argv)
+{
+       bool ret;
+       char *name;
+       if (argc != 2) {
+               ejsSetErrorMsg(eid, "sys_mkdir invalid arguments, need mkdir(dirname, mode)");
+               return -1;
+       }
+       if (!mprVarIsString(argv[0]->type)) {
+               ejsSetErrorMsg(eid, "sys_mkdir dirname not a string");
+               return -1;
+       }
+       if (!mprVarIsNumber(argv[1]->type)) {
+               ejsSetErrorMsg(eid, "sys_mkdir mode not a number");
+               return -1;
+       }
+       mprVarToString(&name, 0, NULL, argv[0]);
+       ret = mkdir(name, mprVarToNumber(argv[1]));
+       mpr_Return(eid, mprCreateBoolVar(ret == 0));
+       return 0;
+}
+
 
 /*
   return fields of a stat() call
@@ -332,10 +463,15 @@ static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv)
        mprSetCFunction(obj, "interfaces", ejs_sys_interfaces);
        mprSetCFunction(obj, "hostname", ejs_sys_hostname);
        mprSetCFunction(obj, "nttime", ejs_sys_nttime);
+       mprSetCFunction(obj, "gettimeofday", ejs_sys_gettimeofday);
        mprSetCFunction(obj, "unix2nttime", ejs_sys_unix2nttime);
+       mprSetCFunction(obj, "gmmktime", ejs_sys_gmmktime);
        mprSetCFunction(obj, "gmtime", ejs_sys_gmtime);
+       mprSetCFunction(obj, "nttime2unix", ejs_sys_nttime2unix);
+       mprSetCFunction(obj, "ntgmtime", ejs_sys_ntgmtime);
        mprSetCFunction(obj, "ldaptime", ejs_sys_ldaptime);
        mprSetCFunction(obj, "httptime", ejs_sys_httptime);
+       mprSetCFunction(obj, "mkdir", ejs_sys_mkdir);
        mprSetStringCFunction(obj, "unlink", ejs_sys_unlink);
        mprSetStringCFunction(obj, "file_load", ejs_sys_file_load);
        mprSetStringCFunction(obj, "file_save", ejs_sys_file_save);
@@ -351,7 +487,8 @@ static int ejs_sys_init(MprVarHandle eid, int argc, struct MprVar **argv)
 /*
   setup C functions that be called from ejs
 */
-void smb_setup_ejs_system(void)
+NTSTATUS smb_setup_ejs_system(void)
 {
        ejsDefineCFunction(-1, "sys_init", ejs_sys_init, NULL, MPR_VAR_SCRIPT_HANDLE);
+       return NT_STATUS_OK;
 }