summaryrefslogtreecommitdiff
path: root/services/samba/ejsnet.esp
blob: 00424f2cae430c028608abbef07c087ac1c7b872 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<%

/*
 * Copyright (C)  Rafal Szczesniak 2007
 */

/* JSON-RPC mappings to the libnet functions */

jsonrpc_include("resources.esp");


function _NetContext(params, error)
{
  var hostName, credParams, credentials;
  var resName = "netCtx";

  credentials = session.authinfo.credentials;

  if (params.length > 0)
  {
    /* first expected argument is host name that this context is going
       to be attached to */
    hostName = params[0];
    if (typeof(hostName) != "string" || hostName == "")
    {
      error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		     "hostName parameter is expected to be a valid non zero-length string");
      return error;
    }

    resName = "netCtx[" + hostName + "]";
    
    /* check whether credentials are supplied as well */
    if (params.length > 1)
    {
      /* create user specified credentials object */
      credParams = params[1];
      if (typeof(credParams) != "object")
      {
        error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		       "credentials parameter is expected to be an object");
	return error;
      }

      if (typeof(credParams.domain) != "string")
      {
	error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		       "a valid string is expected in credentials.domain");
	return error;
      }
      
      if (typeof(credParams.username) != "string")
      {
	error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		       "a valid string is expected in credentials.username");
	return error;
      }
    
      if (typeof(credParams.username) != "string")
      {
	error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		       "a valid string is expected in credentials.password");
	return error;
      }
  
      credentials = credentials_init();
      credentials.set_domain(credParams.domain);
      credentials.set_username(credParams.username);
      credentials.set_password(credParams.password);
    }
  }

  /* was this NetContext created yet ? */
  var resId = session.resources.find(resName, error);
  if (resId != undefined)
  {
    /* yes, return its resource id */
    return resId;
  }

  /* no, create the new context and assign it a resource id */
  var netCtx = NetContext(credentials);
  resId = session.resources.set(netCtx, resName, error);
  return resId;
}
jsonrpc.method.NetContext = _NetContext;


function _NetContextCreds(params, error)
{
  if (params.length != 1)
  {
    error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
                   "missing resource id parameter");
    return error;
  }

  var resId = params[0];
  if (typeof(resId) != "number")
  {
    error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		   "an integer parameter is required");
    return error;
  }
  
  var netCtx = session.resources.get(resId, error);
  if (typeof(netCtx) != "object")
  {
    return null;
  }

  var creds = netCtx.credentials;
  var result = new Object();
  result.username = creds.get_username();
  result.domain   = creds.get_domain();

  return result;
}
jsonrpc.method.NetContextCreds = _NetContextCreds;


function _UserMgr(params, error)
{
  var domainName = null, netCtxId;
  var resId = -1;
  var resName = "usrCtx";
  var netCtx = undefined;
  var usrCtx = undefined;

  if (params.length == 0)
  {
    error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		   "NetContext parameter missing");
    return error;
  }

  /* checking NetContext parameter */
  netCtxId = params[0];
  if (netCtxId == undefined || typeof(netCtxId) != "number")
  {
    error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		   "NetContext parameter is expected to be a number");
  }
  
  netCtx = session.resources.get(netCtxId, error);
  if (netCtx == undefined || typeof(netCtx) != "object")
  {
    error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		   "Incorrect NetContext passed");
    return error;
  }

  if (params.length > 1)
  {
    domainName = params[1];
    if (domainName == undefined || typeof(domainName) != "string")
    {
      error.setError(jsonrpc.Constant.ServerError.ParameterMismatch,
		     "domainName parameter is expected to be a valid non zero-length string");
      return error;
    }
  }
  
  if (domainName == "")
  {
    usrCtx = netCtx.UserMgr();
  }
  else
  {
    usrCtx = netCtx.UserMgr(domainName);
  }

  resId = session.resources.set(usrCtx, resName, error);
  return resId;
}
jsonrpc.method.UserMgr = _UserMgr;

%>