View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2006, 2010
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Alexander Wolf-Reber, IBM, a.wolf-reber@de.ibm.com
12   * 
13   * Change History
14   * Flag       Date        Prog         Description
15   * ------------------------------------------------------------------------------- 
16   * 1516242    2006-07-05  lupusalex    Support of OpenPegasus local authentication
17   * 1565892    2006-11-28  lupusalex    Make SBLIM client JSR48 compliant
18   * 1710066    2007-04-30  lupsualex    LocalAuth fails for z/OS Pegasus
19   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
20   * 2204488 	  2008-10-28  raman_arora  Fix code to remove compiler warnings
21   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
22   * 2763216    2009-04-14  blaschke-oss Code cleanup: visible spelling/grammar errors
23   * 3027618    2010-07-14  blaschke-oss Close files/readers in finally blocks
24   */
25  package org.metricshub.wbem.sblim.cimclient.internal.http;
26  
27  /*-
28   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
29   * WBEM Java Client
30   * ჻჻჻჻჻჻
31   * Copyright 2023 - 2025 MetricsHub
32   * ჻჻჻჻჻჻
33   * Licensed under the Apache License, Version 2.0 (the "License");
34   * you may not use this file except in compliance with the License.
35   * You may obtain a copy of the License at
36   *
37   *      http://www.apache.org/licenses/LICENSE-2.0
38   *
39   * Unless required by applicable law or agreed to in writing, software
40   * distributed under the License is distributed on an "AS IS" BASIS,
41   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
42   * See the License for the specific language governing permissions and
43   * limitations under the License.
44   * ╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱
45   */
46  
47  import java.io.BufferedReader;
48  import java.io.File;
49  import java.io.FileInputStream;
50  import java.io.FileReader;
51  import java.io.IOException;
52  import java.io.InputStreamReader;
53  import java.net.URI;
54  import java.util.logging.Level;
55  import org.metricshub.wbem.sblim.cimclient.internal.logging.LogAndTraceBroker;
56  import org.metricshub.wbem.sblim.cimclient.internal.logging.Messages;
57  import org.metricshub.wbem.sblim.cimclient.internal.util.WBEMConstants;
58  
59  /**
60   * Implements OpenPegasus local authentication
61   */
62  public class PegasusLocalAuthInfo extends AuthorizationInfo {
63  	private boolean iChallenged = false;
64  
65  	/**
66  	 * Default ctor.
67  	 */
68  	public PegasusLocalAuthInfo() {
69  		super();
70  	}
71  
72  	/*
73  	 * (non-Javadoc)
74  	 *
75  	 * @see
76  	 * org.sblim.cimclient.internal.http.AuthorizationInfo#updateAuthenticationInfo
77  	 * (org.sblim.cimclient.internal.http.Challenge, java.net.URI,
78  	 * java.lang.String)
79  	 */
80  	/**
81  	 * @param challenge
82  	 * @param authenticate
83  	 * @param url
84  	 * @param requestMethod
85  	 */
86  	@Override
87  	public void updateAuthenticationInfo(Challenge challenge, String authenticate, URI url, String requestMethod) {
88  		this.iChallenged = true;
89  		this.iResponse = authenticate;
90  	}
91  
92  	/*
93  	 * (non-Javadoc)
94  	 *
95  	 * @see java.lang.Object#toString()
96  	 */
97  	@Override
98  	public String toString() {
99  		if (this.iChallenged && this.iResponse != null && this.iResponse.startsWith("Local ")) {
100 			String fileName = "";
101 			BufferedReader in = null;
102 
103 			try {
104 				fileName = this.iResponse.substring(7, this.iResponse.length() - 1);
105 
106 				if (fileName.length() == 0) throw new IOException("No local authorization file specified");
107 
108 				File authorizationFile = new File(fileName);
109 
110 				if (!authorizationFile.canRead()) throw new IOException("Local authorization file not accessible");
111 
112 				in =
113 					WBEMConstants.Z_OS.equals(System.getProperty(WBEMConstants.OS_NAME))
114 						? new BufferedReader(
115 							new InputStreamReader(new FileInputStream(authorizationFile), WBEMConstants.ISO_8859_1)
116 						)
117 						: new BufferedReader(new FileReader(authorizationFile));
118 
119 				StringBuffer buffer = new StringBuffer();
120 				String line;
121 
122 				while (true) {
123 					line = in.readLine();
124 					if (line == null) break;
125 					buffer.append(line);
126 				}
127 
128 				StringBuffer header = new StringBuffer();
129 				header.append("Local \"");
130 				header.append(getCredentials().getUserName());
131 				header.append(':');
132 				header.append(fileName);
133 				header.append(':');
134 				header.append(buffer);
135 				header.append('"');
136 
137 				return header.toString();
138 			} catch (IOException e) {
139 				LogAndTraceBroker logger = LogAndTraceBroker.getBroker();
140 				logger.trace(Level.FINER, "Exception while reading OpenPegasus local authorization file", e);
141 				logger.message(Messages.HTTP_PEGASUS_LOCAL_AUTH_READ, fileName);
142 			} finally {
143 				if (in != null) {
144 					try {
145 						in.close();
146 					} catch (IOException e) {
147 						LogAndTraceBroker
148 							.getBroker()
149 							.trace(Level.FINER, "Exception while closing OpenPegasus local authorization file", e);
150 					}
151 				}
152 			}
153 		}
154 
155 		return "Local \"" + getCredentials().getUserName() + "\"";
156 	}
157 
158 	@Override
159 	public String getHeaderFieldName() {
160 		return "PegasusAuthorization";
161 	}
162 
163 	@Override
164 	public boolean isSentOnFirstRequest() {
165 		return true;
166 	}
167 
168 	@Override
169 	public boolean isKeptAlive() {
170 		return true;
171 	}
172 }