Browse Source

Logging for ejabberd authentication is now done via syslog

tags/3.6
Michael 1 year ago
parent
commit
d2dc61c246
2 changed files with 41 additions and 62 deletions
  1. 0
    1
      doc/htconfig.md
  2. 41
    61
      include/auth_ejabberd.php

+ 0
- 1
doc/htconfig.md View File

@@ -19,7 +19,6 @@ Example: To set the directory value please add this line to your .htconfig.php:
19 19
 
20 20
 ## jabber ##
21 21
 * **debug** (Boolean) - Enable debug level for the jabber account synchronisation.
22
-* **logfile** - Logfile for the jabber account synchronisation.
23 22
 
24 23
 ## system ##
25 24
 

+ 41
- 61
include/auth_ejabberd.php View File

@@ -33,6 +33,7 @@
33 33
  */
34 34
 
35 35
 use Friendica\App;
36
+use Friendica\Core\Config;
36 37
 
37 38
 if (sizeof($_SERVER["argv"]) == 0)
38 39
 	die();
@@ -58,42 +59,30 @@ require_once("include/dba.php");
58 59
 dba::connect($db_host, $db_user, $db_pass, $db_data);
59 60
 unset($db_host, $db_user, $db_pass, $db_data);
60 61
 
61
-// the logfile to which to write, should be writeable by the user which is running the server
62
-$sLogFile = get_config('jabber','logfile');
63
-
64
-// set true to debug if needed
65
-$bDebug	= get_config('jabber','debug');
66
-
67
-$oAuth = new exAuth($sLogFile, $bDebug);
62
+$oAuth = new exAuth();
68 63
 
69 64
 class exAuth {
70
-	private $sLogFile;
71 65
 	private $bDebug;
72 66
 
73
-	private $rLogFile;
74
-
75 67
 	/**
76 68
 	 * @brief Create the class and do the authentification studd
77 69
 	 *
78
-	 * @param string $sLogFile The logfile name
79 70
 	 * @param boolean $bDebug Debug mode
80 71
 	 */
81
-	public function __construct($sLogFile, $bDebug) {
72
+	public function __construct() {
82 73
 		// setter
83
-		$this->sLogFile 	= $sLogFile;
84
-		$this->bDebug		= $bDebug;
74
+		$this->bDebug = (int)Config::get('jabber', 'debug');
75
+
85 76
 
86
-		// Open the logfile if the logfile name is defined
87
-		if ($this->sLogFile != '')
88
-			$this->rLogFile = fopen($this->sLogFile, "a") || die("Error opening log file: ". $this->sLogFile);
77
+		openlog('auth_ejabberd', LOG_PID, LOG_USER);
89 78
 
90
-		$this->writeLog("[exAuth] start");
79
+		$this->writeLog(LOG_NOTICE, "start");
91 80
 
92
-		// We are connected to the SQL server and are having a log file.
81
+		// We are connected to the SQL server.
93 82
 		do {
94 83
 			// Quit if the database connection went down
95 84
 			if (!dba::connected()) {
96
-				$this->writeDebugLog("[debug] the database connection went down");
85
+				$this->writeLog(LOG_ERR, "the database connection went down");
97 86
 				return;
98 87
 			}
99 88
 
@@ -103,13 +92,13 @@ class exAuth {
103 92
 
104 93
 			// No data? Then quit
105 94
 			if ($iLength == 0) {
106
-				$this->writeDebugLog("[debug] we got no data");
95
+				$this->writeLog(LOG_ERR, "we got no data, quitting");
107 96
 				return;
108 97
 			}
109 98
 
110 99
 			// Fetching the data
111 100
 			$sData = fgets(STDIN, $iLength + 1);
112
-			$this->writeDebugLog("[debug] received data: ". $sData);
101
+			$this->writeLog(LOG_DEBUG, "received data: ". $sData);
113 102
 			$aCommand = explode(":", $sData);
114 103
 			if (is_array($aCommand)) {
115 104
 				switch ($aCommand[0]) {
@@ -123,17 +112,17 @@ class exAuth {
123 112
 						break;
124 113
 					case "setpass":
125 114
 						// We don't accept the setting of passwords here
126
-						$this->writeLog("[exAuth] setpass command disabled");
115
+						$this->writeLog(LOG_NOTICE, "setpass command disabled");
127 116
 						fwrite(STDOUT, pack("nn", 2, 0));
128 117
 						break;
129 118
 					default:
130 119
 						// We don't know the given command
131
-						$this->writeLog("[exAuth] unknown command ". $aCommand[0]);
120
+						$this->writeLog(LOG_NOTICE, "unknown command ". $aCommand[0]);
132 121
 						fwrite(STDOUT, pack("nn", 2, 0));
133 122
 						break;
134 123
 				}
135 124
 			} else {
136
-				$this->writeDebugLog("[debug] invalid command string");
125
+				$this->writeLog(LOG_NOTICE, "invalid command string ".$sData);
137 126
 				fwrite(STDOUT, pack("nn", 2, 0));
138 127
 			}
139 128
 		} while (true);
@@ -149,19 +138,19 @@ class exAuth {
149 138
 
150 139
 		// Check if there is a username
151 140
 		if (!isset($aCommand[1])) {
152
-			$this->writeLog("[exAuth] invalid isuser command, no username given");
141
+			$this->writeLog(LOG_NOTICE, "invalid isuser command, no username given");
153 142
 			fwrite(STDOUT, pack("nn", 2, 0));
154 143
 			return;
155 144
 		}
156 145
 
157 146
 		// Now we check if the given user is valid
158 147
 		$sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
159
-		$this->writeDebugLog("[debug] checking isuser for ". $sUser."@".$aCommand[2]);
160 148
 
161 149
 		// Does the hostname match? So we try directly
162 150
 		if ($a->get_hostname() == $aCommand[2]) {
151
+			$this->writeLog(LOG_INFO, "internal user check for ". $sUser."@".$aCommand[2]);
163 152
 			$sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
164
-			$this->writeDebugLog("[debug] using query ". $sQuery);
153
+			$this->writeLog(LOG_DEBUG, "using query ". $sQuery);
165 154
 			$r = q($sQuery);
166 155
 			$found = dbm::is_result($r);
167 156
 		} else {
@@ -175,11 +164,11 @@ class exAuth {
175 164
 
176 165
 		if ($found) {
177 166
 			// The user is okay
178
-			$this->writeLog("[exAuth] valid user: ". $sUser);
167
+			$this->writeLog(LOG_NOTICE, "valid user: ". $sUser);
179 168
 			fwrite(STDOUT, pack("nn", 2, 1));
180 169
 		} else {
181 170
 			// The user isn't okay
182
-			$this->writeLog("[exAuth] invalid user: ". $sUser);
171
+			$this->writeLog(LOG_WARNING, "invalid user: ". $sUser);
183 172
 			fwrite(STDOUT, pack("nn", 2, 0));
184 173
 		}
185 174
 	}
@@ -195,6 +184,8 @@ class exAuth {
195 184
 	 */
196 185
 	private function check_user($host, $user, $ssl) {
197 186
 
187
+		$this->writeLog(LOG_INFO, "external user check for ".$user."@".$host);
188
+
198 189
 		$url = ($ssl ? "https":"http")."://".$host."/noscrape/".$user;
199 190
 
200 191
 		$data = z_fetch_url($url);
@@ -222,30 +213,31 @@ class exAuth {
222 213
 
223 214
 		// check user authentication
224 215
 		if (sizeof($aCommand) != 4) {
225
-			$this->writeLog("[exAuth] invalid auth command, data missing");
216
+			$this->writeLog(LOG_NOTICE, "invalid auth command, data missing");
226 217
 			fwrite(STDOUT, pack("nn", 2, 0));
227 218
 			return;
228 219
 		}
229 220
 
230 221
 		// We now check if the password match
231 222
 		$sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
232
-		$this->writeDebugLog("[debug] doing auth for ".$sUser."@".$aCommand[2]);
233 223
 
234 224
 		// Does the hostname match? So we try directly
235 225
 		if ($a->get_hostname() == $aCommand[2]) {
226
+			$this->writeLog(LOG_INFO, "internal auth for ".$sUser."@".$aCommand[2]);
227
+
236 228
 			$sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
237
-			$this->writeDebugLog("[debug] using query ". $sQuery);
229
+			$this->writeLog(LOG_DEBUG, "using query ". $sQuery);
238 230
 			if ($oResult = q($sQuery)) {
239 231
 				$uid = $oResult[0]["uid"];
240 232
 				$Error = ($oResult[0]["password"] != hash('whirlpool',$aCommand[3]));
241 233
 			} else {
242
-				$this->writeLog("[MySQL] invalid query: ". $sQuery);
234
+				$this->writeLog(LOG_WARNING, "invalid query: ". $sQuery);
243 235
 				$Error = true;
244 236
 				$uid = -1;
245 237
 			}
246 238
 			if ($Error) {
247 239
 				$oConfig = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = 'xmpp' AND `k`='password' LIMIT 1;", intval($uid));
248
-				$this->writeLog("[exAuth] got password ".$oConfig[0]["v"]);
240
+				$this->writeLog(LOG_INFO, "check against alternate password for ".$sUser."@".$aCommand[2]);
249 241
 				$Error = ($aCommand[3] != $oConfig[0]["v"]);
250 242
 			}
251 243
 		} else {
@@ -258,10 +250,10 @@ class exAuth {
258 250
 		}
259 251
 
260 252
 		if ($Error) {
261
-			$this->writeLog("[exAuth] authentification failed for user ".$sUser."@". $aCommand[2]);
253
+			$this->writeLog(LOG_WARNING, "authentification failed for user ".$sUser."@". $aCommand[2]);
262 254
 			fwrite(STDOUT, pack("nn", 2, 0));
263 255
 		} else {
264
-			$this->writeLog("[exAuth] authentificated user ".$sUser."@".$aCommand[2]);
256
+			$this->writeLog(LOG_NOTICE, "authentificated user ".$sUser."@".$aCommand[2]);
265 257
 			fwrite(STDOUT, pack("nn", 2, 1));
266 258
 		}
267 259
 	}
@@ -277,8 +269,6 @@ class exAuth {
277 269
 	 * @return boolean Are the credentials okay?
278 270
 	 */
279 271
 	private function check_credentials($host, $user, $password, $ssl) {
280
-		$this->writeDebugLog("[debug] check credentials for user ".$user." on ".$host);
281
-
282 272
 		$url = ($ssl ? "https":"http")."://".$host."/api/account/verify_credentials.json";
283 273
 
284 274
 		$ch = curl_init();
@@ -295,39 +285,29 @@ class exAuth {
295 285
 		$http_code = $curl_info["http_code"];
296 286
 		curl_close($ch);
297 287
 
298
-		$this->writeDebugLog("[debug] got HTTP code ".$http_code);
288
+		$this->writeLog(LOG_INFO, "external auth for ".$user."@".$host." returned ".$http_code);
299 289
 
300 290
 		return ($http_code == 200);
301 291
 	}
302 292
 
303 293
 	/**
304
-	 * @brief write data to the logfile
294
+	 * @brief write data to the syslog
305 295
 	 *
306
-	 * @param string $sMessage The logfile message
296
+	 * @param integer $loglevel The syslog loglevel
297
+	 * @param string $sMessage The syslog message
307 298
 	 */
308
-	private function writeLog($sMessage) {
309
-		if (is_resource($this->rLogFile))
310
-			fwrite($this->rLogFile, date("r")." ".getmypid()." ".$sMessage."\n");
311
-	}
312
-
313
-	/**
314
-	 * @brief write debug data to the logfile
315
-	 *
316
-	 * @param string $sMessage The logfile message
317
-	 */
318
-	private function writeDebugLog($sMessage) {
319
-		if ($this->bDebug)
320
-			$this->writeLog($sMessage);
299
+	private function writeLog($loglevel, $sMessage) {
300
+		if (!$this->bDebug && ($loglevel >= LOG_DEBUG)) {
301
+			return;
302
+		}
303
+		syslog($loglevel, $sMessage);
321 304
 	}
322 305
 
323 306
 	/**
324
-	 * @brief destroy the class
307
+	 * @brief destroy the class, close the syslog connection.
325 308
 	 */
326 309
 	public function __destruct() {
327
-		// close the log file
328
-		$this->writeLog("[exAuth] stop");
329
-
330
-		if (is_resource($this->rLogFile))
331
-			fclose($this->rLogFile);
310
+		$this->writeLog(LOG_NOTICE, "stop");
311
+		closelog();
332 312
 	}
333 313
 }

Loading…
Cancel
Save