1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
---|
2 | /* ***** BEGIN LICENSE BLOCK ***** |
---|
3 | * Version: NPL 1.1/GPL 2.0/LGPL 2.1 |
---|
4 | * |
---|
5 | * The contents of this file are subject to the Netscape Public License |
---|
6 | * Version 1.1 (the "License"); you may not use this file except in |
---|
7 | * compliance with the License. You may obtain a copy of the License at |
---|
8 | * http://www.mozilla.org/NPL/ |
---|
9 | * |
---|
10 | * Software distributed under the License is distributed on an "AS IS" basis, |
---|
11 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
---|
12 | * for the specific language governing rights and limitations under the |
---|
13 | * License. |
---|
14 | * |
---|
15 | * The Original Code is mozilla.org code. |
---|
16 | * |
---|
17 | * The Initial Developer of the Original Code is |
---|
18 | * Netscape Communications Corporation. |
---|
19 | * Portions created by the Initial Developer are Copyright (C) 1999-2000 |
---|
20 | * the Initial Developer. All Rights Reserved. |
---|
21 | * |
---|
22 | * Contributor(s): |
---|
23 | * |
---|
24 | * |
---|
25 | * Alternatively, the contents of this file may be used under the terms of |
---|
26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
---|
27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
---|
28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
---|
29 | * of those above. If you wish to allow use of your version of this file only |
---|
30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
---|
31 | * use your version of this file under the terms of the NPL, indicate your |
---|
32 | * decision by deleting the provisions above and replace them with the notice |
---|
33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
---|
34 | * the provisions above, a recipient may use your version of this file under |
---|
35 | * the terms of any one of the NPL, the GPL or the LGPL. |
---|
36 | * |
---|
37 | * ***** END LICENSE BLOCK ***** */ |
---|
38 | |
---|
39 | /* The privileged system principal. */ |
---|
40 | |
---|
41 | #include "nscore.h" |
---|
42 | #include "nsSystemPrincipal.h" |
---|
43 | #include "nsIComponentManager.h" |
---|
44 | #include "nsIServiceManager.h" |
---|
45 | #include "nsIURL.h" |
---|
46 | #include "nsCOMPtr.h" |
---|
47 | #include "nsXPIDLString.h" |
---|
48 | #include "nsReadableUtils.h" |
---|
49 | #include "nsCRT.h" |
---|
50 | #include "nsString.h" |
---|
51 | |
---|
52 | |
---|
53 | NS_INTERFACE_MAP_BEGIN(nsSystemPrincipal) |
---|
54 | NS_INTERFACE_MAP_ENTRY(nsIPrincipal) |
---|
55 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISerializable, nsIPrincipal) |
---|
56 | NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIPrincipal) |
---|
57 | NS_INTERFACE_MAP_ENTRY(nsISubsumingPrincipal) |
---|
58 | NS_INTERFACE_MAP_ENTRY(nsIPrincipalObsolete) |
---|
59 | NS_IMPL_QUERY_CLASSINFO(nsSystemPrincipal) |
---|
60 | NS_INTERFACE_MAP_END |
---|
61 | |
---|
62 | NS_IMPL_CI_INTERFACE_GETTER3(nsSystemPrincipal, |
---|
63 | nsISubsumingPrincipal, |
---|
64 | nsIPrincipal, |
---|
65 | nsISerializable) |
---|
66 | |
---|
67 | NS_IMETHODIMP_(nsrefcnt) |
---|
68 | nsSystemPrincipal::AddRef() |
---|
69 | { |
---|
70 | NS_PRECONDITION(PRInt32(mJSPrincipals.refcount) >= 0, "illegal refcnt"); |
---|
71 | nsrefcnt count = PR_AtomicIncrement((PRInt32 *)&mJSPrincipals.refcount); |
---|
72 | NS_LOG_ADDREF(this, count, "nsSystemPrincipal", sizeof(*this)); |
---|
73 | return count; |
---|
74 | } |
---|
75 | |
---|
76 | NS_IMETHODIMP_(nsrefcnt) |
---|
77 | nsSystemPrincipal::Release() |
---|
78 | { |
---|
79 | NS_PRECONDITION(0 != mJSPrincipals.refcount, "dup release"); |
---|
80 | nsrefcnt count = PR_AtomicDecrement((PRInt32 *)&mJSPrincipals.refcount); |
---|
81 | NS_LOG_RELEASE(this, count, "nsSystemPrincipal"); |
---|
82 | if (count == 0) { |
---|
83 | NS_DELETEXPCOM(this); |
---|
84 | } |
---|
85 | |
---|
86 | return count; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | /////////////////////////////////////// |
---|
91 | // Methods implementing nsIPrincipal // |
---|
92 | /////////////////////////////////////// |
---|
93 | |
---|
94 | NS_IMETHODIMP |
---|
95 | nsSystemPrincipal::GetPreferences(char** aPrefName, char** aID, |
---|
96 | char** aGrantedList, char** aDeniedList) |
---|
97 | { |
---|
98 | // The system principal should never be streamed out |
---|
99 | *aPrefName = nsnull; |
---|
100 | *aID = nsnull; |
---|
101 | *aGrantedList = nsnull; |
---|
102 | *aDeniedList = nsnull; |
---|
103 | |
---|
104 | return NS_ERROR_FAILURE; |
---|
105 | } |
---|
106 | |
---|
107 | NS_IMETHODIMP |
---|
108 | nsSystemPrincipal::Equals(nsIPrincipal *other, PRBool *result) |
---|
109 | { |
---|
110 | *result = (other == this); |
---|
111 | return NS_OK; |
---|
112 | } |
---|
113 | |
---|
114 | NS_IMETHODIMP |
---|
115 | nsSystemPrincipal::Subsumes(nsIPrincipal *other, PRBool *result) |
---|
116 | { |
---|
117 | *result = PR_TRUE; |
---|
118 | return NS_OK; |
---|
119 | } |
---|
120 | |
---|
121 | NS_IMETHODIMP |
---|
122 | nsSystemPrincipal::GetHashValue(PRUint32 *result) |
---|
123 | { |
---|
124 | *result = NS_PTR_TO_INT32(this); |
---|
125 | return NS_OK; |
---|
126 | } |
---|
127 | |
---|
128 | NS_IMETHODIMP |
---|
129 | nsSystemPrincipal::CanEnableCapability(const char *capability, |
---|
130 | PRInt16 *result) |
---|
131 | { |
---|
132 | // System principal can enable all capabilities. |
---|
133 | *result = nsIPrincipal::ENABLE_GRANTED; |
---|
134 | return NS_OK; |
---|
135 | } |
---|
136 | |
---|
137 | NS_IMETHODIMP |
---|
138 | nsSystemPrincipal::SetCanEnableCapability(const char *capability, |
---|
139 | PRInt16 canEnable) |
---|
140 | { |
---|
141 | return NS_ERROR_FAILURE; |
---|
142 | } |
---|
143 | |
---|
144 | |
---|
145 | NS_IMETHODIMP |
---|
146 | nsSystemPrincipal::IsCapabilityEnabled(const char *capability, |
---|
147 | void *annotation, |
---|
148 | PRBool *result) |
---|
149 | { |
---|
150 | *result = PR_TRUE; |
---|
151 | return NS_OK; |
---|
152 | } |
---|
153 | |
---|
154 | NS_IMETHODIMP |
---|
155 | nsSystemPrincipal::EnableCapability(const char *capability, void **annotation) |
---|
156 | { |
---|
157 | *annotation = nsnull; |
---|
158 | return NS_OK; |
---|
159 | } |
---|
160 | |
---|
161 | NS_IMETHODIMP |
---|
162 | nsSystemPrincipal::RevertCapability(const char *capability, void **annotation) |
---|
163 | { |
---|
164 | *annotation = nsnull; |
---|
165 | return NS_OK; |
---|
166 | } |
---|
167 | |
---|
168 | NS_IMETHODIMP |
---|
169 | nsSystemPrincipal::DisableCapability(const char *capability, void **annotation) |
---|
170 | { |
---|
171 | // Can't disable the capabilities of the system principal. |
---|
172 | // XXX might be handy to be able to do so! |
---|
173 | *annotation = nsnull; |
---|
174 | return NS_ERROR_FAILURE; |
---|
175 | } |
---|
176 | |
---|
177 | NS_IMETHODIMP |
---|
178 | nsSystemPrincipal::GetURI(nsIURI** aURI) |
---|
179 | { |
---|
180 | *aURI = nsnull; |
---|
181 | return NS_OK; |
---|
182 | } |
---|
183 | |
---|
184 | NS_IMETHODIMP |
---|
185 | nsSystemPrincipal::GetOrigin(char** aOrigin) |
---|
186 | { |
---|
187 | *aOrigin = ToNewCString(NS_LITERAL_CSTRING("[System]")); |
---|
188 | return *aOrigin ? NS_OK : NS_ERROR_OUT_OF_MEMORY; |
---|
189 | } |
---|
190 | |
---|
191 | NS_IMETHODIMP |
---|
192 | nsSystemPrincipal::GetCertificateID(char** aID) |
---|
193 | { |
---|
194 | *aID = nsnull; |
---|
195 | return NS_OK; |
---|
196 | } |
---|
197 | |
---|
198 | NS_IMETHODIMP |
---|
199 | nsSystemPrincipal::GetCommonName(char** aName) |
---|
200 | { |
---|
201 | *aName = nsnull; |
---|
202 | return NS_OK; |
---|
203 | } |
---|
204 | |
---|
205 | NS_IMETHODIMP |
---|
206 | nsSystemPrincipal::SetCommonName(const char* aName) |
---|
207 | { |
---|
208 | return NS_OK; |
---|
209 | } |
---|
210 | |
---|
211 | NS_IMETHODIMP |
---|
212 | nsSystemPrincipal::GetHasCertificate(PRBool* aResult) |
---|
213 | { |
---|
214 | *aResult = PR_FALSE; |
---|
215 | return NS_OK; |
---|
216 | } |
---|
217 | |
---|
218 | NS_IMETHODIMP |
---|
219 | nsSystemPrincipal::GetDomain(nsIURI** aDomain) |
---|
220 | { |
---|
221 | *aDomain = nsnull; |
---|
222 | return NS_OK; |
---|
223 | } |
---|
224 | |
---|
225 | NS_IMETHODIMP |
---|
226 | nsSystemPrincipal::SetDomain(nsIURI* aDomain) |
---|
227 | { |
---|
228 | return NS_OK; |
---|
229 | } |
---|
230 | |
---|
231 | NS_IMETHODIMP |
---|
232 | nsSystemPrincipal::GetSecurityPolicy(void** aSecurityPolicy) |
---|
233 | { |
---|
234 | *aSecurityPolicy = nsnull; |
---|
235 | return NS_OK; |
---|
236 | } |
---|
237 | |
---|
238 | NS_IMETHODIMP |
---|
239 | nsSystemPrincipal::SetSecurityPolicy(void* aSecurityPolicy) |
---|
240 | { |
---|
241 | return NS_OK; |
---|
242 | } |
---|
243 | |
---|
244 | NS_IMETHODIMP |
---|
245 | nsSystemPrincipal::GetJSPrincipals(JSContext *cx, JSPrincipals **jsprin) |
---|
246 | { |
---|
247 | NS_PRECONDITION(mJSPrincipals.nsIPrincipalPtr, "mJSPrincipals is uninitalized!"); |
---|
248 | |
---|
249 | JSPRINCIPALS_HOLD(nsnull, &mJSPrincipals); |
---|
250 | *jsprin = &mJSPrincipals; |
---|
251 | return NS_OK; |
---|
252 | } |
---|
253 | |
---|
254 | /////////////////////////////////////////////// |
---|
255 | // Methods implementing nsIPrincipalObsolete // |
---|
256 | /////////////////////////////////////////////// |
---|
257 | |
---|
258 | NS_IMETHODIMP |
---|
259 | nsSystemPrincipal::ToString(char **aResult) |
---|
260 | { |
---|
261 | return nsSystemPrincipal::GetOrigin(aResult); |
---|
262 | } |
---|
263 | |
---|
264 | NS_IMETHODIMP |
---|
265 | nsSystemPrincipal::ToUserVisibleString(char **aResult) |
---|
266 | { |
---|
267 | return nsSystemPrincipal::GetOrigin(aResult); |
---|
268 | } |
---|
269 | |
---|
270 | NS_IMETHODIMP |
---|
271 | nsSystemPrincipal::Equals(nsIPrincipalObsolete *aOther, PRBool *aResult) |
---|
272 | { |
---|
273 | *aResult = (aOther == this); |
---|
274 | return NS_OK; |
---|
275 | } |
---|
276 | |
---|
277 | NS_IMETHODIMP |
---|
278 | nsSystemPrincipal::HashValue(PRUint32 *aResult) |
---|
279 | { |
---|
280 | *aResult = NS_PTR_TO_INT32(this); |
---|
281 | return NS_OK; |
---|
282 | } |
---|
283 | |
---|
284 | NS_IMETHODIMP |
---|
285 | nsSystemPrincipal::GetJSPrincipals(JSPrincipals **aResult) |
---|
286 | { |
---|
287 | *aResult = &mJSPrincipals; |
---|
288 | JSPRINCIPALS_HOLD(nsnull, *aResult); |
---|
289 | return NS_OK; |
---|
290 | } |
---|
291 | |
---|
292 | ////////////////////////////////////////// |
---|
293 | // Methods implementing nsISerializable // |
---|
294 | ////////////////////////////////////////// |
---|
295 | |
---|
296 | NS_IMETHODIMP |
---|
297 | nsSystemPrincipal::Read(nsIObjectInputStream* aStream) |
---|
298 | { |
---|
299 | // no-op: CID is sufficient to identify the mSystemPrincipal singleton |
---|
300 | return NS_OK; |
---|
301 | } |
---|
302 | |
---|
303 | NS_IMETHODIMP |
---|
304 | nsSystemPrincipal::Write(nsIObjectOutputStream* aStream) |
---|
305 | { |
---|
306 | // no-op: CID is sufficient to identify the mSystemPrincipal singleton |
---|
307 | return NS_OK; |
---|
308 | } |
---|
309 | |
---|
310 | ///////////////////////////////////////////// |
---|
311 | // Constructor, Destructor, initialization // |
---|
312 | ///////////////////////////////////////////// |
---|
313 | |
---|
314 | nsSystemPrincipal::nsSystemPrincipal() |
---|
315 | { |
---|
316 | } |
---|
317 | |
---|
318 | nsresult |
---|
319 | nsSystemPrincipal::Init() |
---|
320 | { |
---|
321 | return mJSPrincipals.Init(this, "[System Principal]"); |
---|
322 | } |
---|
323 | |
---|
324 | nsSystemPrincipal::~nsSystemPrincipal(void) |
---|
325 | { |
---|
326 | } |
---|