r8635: make object inheritance with the builtin objects easy by allowing
[ira/wip.git] / source / scripting / ejs / smbcalls_nss.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    provide access to getpwnam() and related calls
5
6    Copyright (C) Andrew Tridgell 2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
24 #include "scripting/ejs/smbcalls.h"
25 #include "lib/appweb/ejs/ejs.h"
26 #include "system/passwd.h"
27
28
29 /*
30   return a struct passwd as an object
31 */
32 static struct MprVar mpr_passwd(struct passwd *pwd)
33 {
34         struct MprVar ret;
35         if (pwd == NULL) {
36                 return mprCreateUndefinedVar();
37         }
38         ret = mprObject("passwd");
39
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));
47         return ret;
48 }
49
50 /*
51   return a struct passwd as an object
52 */
53 static struct MprVar mpr_group(struct group *grp)
54 {
55         struct MprVar ret;
56         if (grp == NULL) {
57                 return mprCreateUndefinedVar();
58         }
59         ret = mprObject("group");
60
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));
65         return ret;
66 }
67
68
69 /*
70   usage:
71       var pw = nss.getpwnam("root");
72
73   returns an object containing struct passwd entries
74 */
75 static int ejs_getpwnam(MprVarHandle eid, int argc, struct MprVar **argv)
76 {
77         /* validate arguments */
78         if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
79                 ejsSetErrorMsg(eid, "getpwnam invalid arguments");
80                 return -1;
81         }
82
83         mpr_Return(eid, mpr_passwd(getpwnam(mprToString(argv[0]))));
84         return 0;
85 }
86
87 /*
88   usage:
89       var pw = nss.getpwuid(0);
90
91   returns an object containing struct passwd entries
92 */
93 static int ejs_getpwuid(MprVarHandle eid, int argc, struct MprVar **argv)
94 {
95         /* validate arguments */
96         if (argc != 1 || !mprVarIsNumber(argv[0]->type)) {
97                 ejsSetErrorMsg(eid, "getpwuid invalid arguments");
98                 return -1;
99         }
100         mpr_Return(eid, mpr_passwd(getpwuid(mprToInt(argv[0]))));
101         return 0;
102 }
103
104 /*
105   usage:
106       var pw = nss.getgrnam("users");
107
108   returns an object containing struct group entries
109 */
110 static int ejs_getgrnam(MprVarHandle eid, int argc, struct MprVar **argv)
111 {
112         /* validate arguments */
113         if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
114                 ejsSetErrorMsg(eid, "getgrnam invalid arguments");
115                 return -1;
116         }
117         mpr_Return(eid, mpr_group(getgrnam(mprToString(argv[0]))));
118         return 0;
119 }
120
121 /*
122   usage:
123       var pw = nss.getgrgid(0);
124
125   returns an object containing struct group entries
126 */
127 static int ejs_getgrgid(MprVarHandle eid, int argc, struct MprVar **argv)
128 {
129         /* validate arguments */
130         if (argc != 1 || argv[0]->type != MPR_TYPE_STRING) {
131                 ejsSetErrorMsg(eid, "getgrgid invalid arguments");
132                 return -1;
133         }
134         mpr_Return(eid, mpr_group(getgrgid(mprToInt(argv[0]))));
135         return 0;
136 }
137
138
139 /*
140   initialise nss ejs subsystem
141 */
142 static int ejs_nss_init(MprVarHandle eid, int argc, struct MprVar **argv)
143 {
144         struct MprVar *nss = mprInitObject(eid, "nss", argc, argv);
145
146         mprSetCFunction(nss, "getpwnam", ejs_getpwnam);
147         mprSetCFunction(nss, "getpwuid", ejs_getpwuid);
148         mprSetCFunction(nss, "getgrnam", ejs_getgrnam);
149         mprSetCFunction(nss, "getgrgid", ejs_getgrgid);
150
151         return 0;
152 }
153
154 /*
155   setup C functions that be called from ejs
156 */
157 void smb_setup_ejs_nss(void)
158 {
159         ejsDefineCFunction(-1, "nss_init", ejs_nss_init, NULL, MPR_VAR_SCRIPT_HANDLE);
160 }