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