FOSSology  4.4.0
Open Source License Compliance by Open Source Software
core-smauth.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 use Symfony\Component\HttpFoundation\Session\Session;
10 
11 define("TITLE_CORE_SMAUTH", _("SiteMinder_Login"));
12 
13 class core_smauth extends FO_Plugin
14 {
15  var $Name = "smauth";
16  var $Title = TITLE_CORE_SMAUTH;
17  var $Version = "1.0";
18  var $Dependency = array();
19  var $PluginLevel = 1000; /* make this run first! */
20  var $LoginFlag = 0;
21 
29  function Install()
30  {
31  global $PG_CONN;
32  if (empty($PG_CONN)) {
33  return (1);
34  } /* No DB */
35  return (0);
36  } // Install()
37 
42  function PostInitialize()
43  {
44  global $PG_CONN;
45  global $SysConf;
46 
47  if (siteminder_check() == -1) {
48  return;
49  }
50 
51  $UID = siteminder_check();
52 
53  global $container;
55  $session = $container->get('session');
56  $session->setName('Login');
57  $session->start();
58 
59  if (array_key_exists(Auth::USER_ID, $_SESSION)) {
60  $SysConf['auth'][Auth::USER_ID] = $_SESSION[Auth::USER_ID];
61  }
62  $Now = time();
63  /* Logins older than 60 secs/min * 480 min = 8 hr are auto-logout */
64  if (! empty($_SESSION['time']) && @$_SESSION['time'] + (60 * 480) < $Now) {
65  $_SESSION['User'] = NULL;
66  $_SESSION[Auth::USER_ID] = NULL;
67  $_SESSION[Auth::USER_LEVEL] = NULL;
68  $SysConf['auth'][Auth::USER_ID] = NULL;
69  $_SESSION['UserEmail'] = NULL;
70  $_SESSION['Folder'] = NULL;
71  $_SESSION['UiPref'] = NULL;
72  /* TODO: need to clear SiteMinder session */
73  }
74 
75  /* check db connection */
76  if (! $PG_CONN) {
77  DBconnect();
78  if (!$PG_CONN) {
79  echo "NO DB connection";
80  }
81  }
82 
83  /* Enable or disable plugins based on login status */
84  $Level = PLUGIN_DB_NONE;
85  if (@$_SESSION['User']) { //TODO: also need to check SiteMinder session
86  /* If you are logged in, then the default level is "Download". */
87  if ("X" . $_SESSION[Auth::USER_LEVEL] == "X") {
88  $Level = PLUGIN_DB_WRITE;
89  } else {
90  $Level = $_SESSION[Auth::USER_LEVEL];
91  }
92  /* Recheck the user in case he is suddenly blocked or changed. */
93  if (empty($_SESSION['time_check'])) {
94  $_SESSION['time_check'] = time() + (480 * 60);
95  }
96  if (time() >= @$_SESSION['time_check']) {
97  $sql = "SELECT * FROM users WHERE user_pk='" . @$_SESSION['UserId'] . "';";
98  $result = pg_query($PG_CONN, $sql);
99  DBCheckResult($result, $sql, __FILE__, __LINE__);
100  $R = pg_fetch_assoc($result);
101  pg_free_result($result);
102  $_SESSION['User'] = $R['user_name'];
103  $_SESSION['Folder'] = $R['root_folder_fk'];
104  $_SESSION[Auth::USER_LEVEL] = $R['user_perm'];
105  $_SESSION['UserEmail'] = $R['user_email'];
106  $_SESSION['UserEnote'] = $R['email_notify'];
107  if (empty($R['ui_preference'])) {
108  $_SESSION['UiPref'] = 'simple';
109  } else {
110  $_SESSION['UiPref'] = $R['ui_preference'];
111  }
112  $Level = $_SESSION[Auth::USER_LEVEL];
113  }
114  } else {
115  $this->CheckUser($UID);
116  $Level = $_SESSION[Auth::USER_LEVEL];
117  }
118 
119  /* Disable all plugins with >= $Level access */
120  plugin_disable($Level);
121 
122  $this->State = PLUGIN_STATE_READY;
123  } // PostInitialize()
124 
129  function CheckUser($Email)
130  {
131  global $PG_CONN;
132  global $SysConf;
133 
134  if (empty($Email)) {
135  return;
136  }
137  $Email = str_replace("'", "''", $Email); /* protect DB */
138  $FolderName = substr($Email, 0, strpos($Email,'@'));
139  $FolderName = trim($FolderName);
140  if (empty($FolderName)) {
141  return;
142  }
143  $FolderDes = "Folder created for " . $FolderName;
144 
145  /* Get default User description and bucketpool from sysconf table */
146  $UserDesc = 'null';
147  $BucketPool = 'null';
148  $sql = "SELECT typlen FROM pg_type where typname='sysconfig' limit 1";
149  $result = pg_query($PG_CONN, $sql);
150  DBCheckResult($result, $sql, __FILE__, __LINE__);
151  if (pg_num_rows($result) > 0) {
152  pg_free_result($result);
153  // Get UserDesc from sysconfig
154  $sql = "SELECT conf_value FROM sysconfig WHERE variablename = 'UserDesc';";
155  $result = pg_query($PG_CONN, $sql);
156  DBCheckResult($result, $sql, __FILE__, __LINE__);
157  $R = pg_fetch_assoc($result);
158  pg_free_result($result);
159  if (!empty($R['conf_value'])) {
160  $UserDesc = "'".$R['conf_value']."'";
161  }
162 
163  // Get BucketPool from sysconfig
164  $sql = "SELECT conf_value FROM sysconfig WHERE variablename = 'BucketPool';";
165  $result = pg_query($PG_CONN, $sql);
166  DBCheckResult($result, $sql, __FILE__, __LINE__);
167  $R = pg_fetch_assoc($result);
168  pg_free_result($result);
169  if (!empty($R['conf_value'])) {
170  $BucketPool = $R['conf_value'];
171  //Check if the bucketpool defined in sysconfig exist in bucketpool table
172  $sql = "SELECT bucketpool_pk FROM bucketpool WHERE bucketpool_pk=$BucketPool AND active='Y'";
173  $result = pg_query($PG_CONN, $sql);
174  DBCheckResult($result, $sql, __FILE__, __LINE__);
175  if (pg_num_rows($result) < 1) {
176  $BucketPool = 'null'; //didn't exist in bucketpool table, set it 'null'
177  }
178  } else {
179  /* if didn't define bucketpool from sycconf.And only a single bucketpool record, get bucketpool from bucketpool table. If more than one, set it null*/
180  $sql = "SELECT bucketpool_pk FROM bucketpool;";
181  $result = pg_query($PG_CONN, $sql);
182  DBCheckResult($result, $sql, __FILE__, __LINE__);
183  if (pg_num_rows($result) == 1) {
184  $R = pg_fetch_assoc($result);
185  if (!empty($R['bucketpool_pk'])) {
186  $BucketPool = $R['bucketpool_pk'];
187  }
188  } else {
189  $BucketPool = 'null';
190  }
191  }
192  pg_free_result($result);
193  } else {
194  pg_free_result($result);
195  }
196 
197  /* See if the user exists */
198  $sql = "SELECT * FROM users WHERE user_email = '$Email';";
199  $result = pg_query($PG_CONN, $sql);
200  DBCheckResult($result, $sql, __FILE__, __LINE__);
201  $R = pg_fetch_assoc($result);
202  pg_free_result($result);
203  if (empty($R['user_name'])) {
204  //check if folder name exists under the parent?
205  pg_exec("BEGIN;");
206  $sql = "SELECT * FROM folderlist WHERE name = '$FolderName' AND parent = '1' AND foldercontents_mode = '1';";
207  $result = pg_query($PG_CONN, $sql);
208  DBCheckResult($result, $sql, __FILE__, __LINE__);
209  $row = pg_fetch_assoc($result);
210  pg_free_result($result);
211  if (empty($row['name'])) {
212  //create folder for the user
213  $sql = "INSERT INTO folder (folder_name, folder_desc) VALUES ('$FolderName', '$FolderDes');";
214  $result = pg_query($PG_CONN, $sql);
215  DBCheckResult($result, $sql, __FILE__, __LINE__);
216  pg_free_result($result);
217  $sql = "SELECT folder_pk FROM folder WHERE folder_name='$FolderName' AND folder_desc = '$FolderDes';";
218  $result = pg_query($PG_CONN, $sql);
219  DBCheckResult($result, $sql, __FILE__, __LINE__);
220  $row = pg_fetch_assoc($result);
221  pg_free_result($result);
222  //print_r($row);
223  if (empty($row['folder_pk'])) {
224  return;
225  }
226  $FolderPk = $row['folder_pk'];
227  //echo $FolderPk;
228 
229  $sql = "INSERT INTO foldercontents (parent_fk,foldercontents_mode,child_id) VALUES ('1','1','$FolderPk');";
230  $result = pg_query($PG_CONN, $sql);
231  DBCheckResult($result, $sql, __FILE__, __LINE__);
232  pg_free_result($result);
233  } else {
234  $sql = "SELECT folder_pk FROM folder WHERE folder_name='$FolderName' AND folder_desc = '$FolderDes';";
235  $result = pg_query($PG_CONN, $sql);
236  DBCheckResult($result, $sql, __FILE__, __LINE__);
237  $row = pg_fetch_assoc($result);
238  pg_free_result($result);
239  //print_r($row);
240  if (empty($row['folder_pk'])) {
241  return;
242  }
243  $FolderPk = $row['folder_pk'];
244  //echo $FolderPk;
245  }
246 
247  //create user
248  add_user($Email,$UserDesc,null,null,5,$Email,y,"agent_bucket,agent_copyright",$FolderPk,$BucketPool);
249 
250  pg_exec("COMMIT;");
251  } /* no user */
252 
253  $sql = "SELECT * FROM users WHERE user_email = '$Email';";
254  $result = pg_query($PG_CONN, $sql);
255  DBCheckResult($result, $sql, __FILE__, __LINE__);
256  $R = pg_fetch_assoc($result);
257  pg_free_result($result);
258 
259  /* Check the email */
260  if (strcmp($Email, $R['user_email']) != 0) {
261  return;
262  }
263  /* If you make it here, then username and email were good! */
264  $_SESSION['User'] = $R['user_name'];
265  $_SESSION['UserId'] = $R['user_pk'];
266  $SysConf['auth'][Auth::USER_ID] = $R['user_pk'];
267  $_SESSION['UserEmail'] = $R['user_email'];
268  $_SESSION['UserEnote'] = $R['email_notify'];
269  if (empty($R['ui_preference'])) {
270  $_SESSION['UiPref'] = 'simple';
271  } else {
272  $_SESSION['UiPref'] = $R['ui_preference'];
273  }
274  $_SESSION['Folder'] = $R['root_folder_fk'];
275  $_SESSION['time_check'] = time() + (480 * 60);
276  /* No specified permission means ALL permission */
277  if ("X" . $R['user_perm'] == "X") {
278  $_SESSION[Auth::USER_LEVEL] = PLUGIN_DB_ADMIN;
279  } else {
280  $_SESSION[Auth::USER_LEVEL] = $R['user_perm'];
281  }
282  /* Check for the no-popup flag */
283  if (GetParm("nopopup", PARM_INTEGER) == 1) {
284  $_SESSION['NoPopup'] = 1;
285  } else {
286  $_SESSION['NoPopup'] = 0;
287  }
288  } // CheckUser()
289 
293  function Output()
294  {
295  global $SysConf;
296  if ($this->State != PLUGIN_STATE_READY) {
297  return;
298  }
299  $V = "";
300  if (siteminder_check() == -1) {
301  return;
302  }
303 
304  $UID = siteminder_check();
305 
306  if ($this->OutputType=="HTML") {
307  /* TODO:logout need to clear SiteMinder session */
308  $_SESSION['User'] = NULL;
309  $_SESSION[Auth::USER_ID] = NULL;
310  $SysConf['auth'][Auth::USER_ID] = NULL;
311  $_SESSION[Auth::USER_LEVEL] = NULL;
312  $_SESSION['UserEmail'] = NULL;
313  $_SESSION['Folder'] = NULL;
314  $_SESSION['UiPref'] = NULL;
315  $Uri = Traceback_uri() . "logout.html?" . random_int(0, getrandmax());
316  //$Uri = Traceback_uri() . "?mod=refresh&remod=default";
317  $V.= "<script language='javascript'>\n";
318  $V.= "window.open('$Uri','_top');\n";
319  $V.= "</script>\n";
320  }
321  if (!$this->OutputToStdout) {
322  return ($V);
323  }
324  print ($V);
325  return;
326  } // Output()
327 }
328 
329 $NewPlugin = new core_smauth();
330 $NewPlugin->Initialize();
This is the Plugin class. All plugins should:
Definition: FO_Plugin.php:57
PostInitialize()
This function is called before the plugin is used and after all plugins have been initialized....
Definition: FO_Plugin.php:230
Contains the constants and helpers for authentication of user.
Definition: Auth.php:24
Definition: state.hpp:16
Output()
generate the output for this plug-in
Install()
Only used during installation. This may be called multiple times. Used to ensure the DB has the right...
Definition: core-smauth.php:29
CheckUser($Email)
See if a username is valid.
siteminder_check()
Check if SiteMinder is enabled.
Definition: common-auth.php:22
DBconnect($sysconfdir, $options="", $exitOnFail=true)
Connect to database engine. This is a no-op if $PG_CONN already has a value.
Definition: common-db.php:33
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:187
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
const PARM_INTEGER
Definition: common-parm.php:14
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.
Definition: common-parm.php:46
add_user($User, $Desc, $Hash, $Perm, $Email, $Email_notify, $Upload_visibility, $agentList, $Folder, $default_bucketpool_fk='')
Add a user.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38
#define PLUGIN_DB_NONE
Plugin requires no DB permission.
Definition: libfossology.h:36
#define PLUGIN_DB_ADMIN
Plugin requires admin level permission on DB.
Definition: libfossology.h:39
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN