FOSSology  4.7.0-rc1
Open Source License Compliance by Open Source Software
common-auth.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2015 Hewlett-Packard Development Company, L.P.
4  SPDX-FileCopyrightText: © 2015 Siemens AG
5 
6  SPDX-License-Identifier: LGPL-2.1-only
7 */
8 
22 function siteminder_check()
23 {
24  // $IDEnvVar = 'HPPF_AUTH_UID'; // for example for PingIdentity
25  $IDEnvVar = 'HTTP_SMUNIVERSALID';
26  if (isset($_SERVER[$IDEnvVar])) {
27  return $_SERVER[$IDEnvVar];
28  }
29  return(-1);
30 } // siteminder_check()
31 
39 {
40  $EXT_AUTH_ENABLE = false;
41  if (array_key_exists('EXT_AUTH', $GLOBALS['SysConf'])) {
42  if (array_key_exists('CONF_EXT_AUTH_ENABLE', $GLOBALS['SysConf']['EXT_AUTH'])) {
43  $EXT_AUTH_ENABLE = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENABLE'];
44  }
45  }
46  if ($EXT_AUTH_ENABLE) {
47  $EXT_AUTH_USER_KW = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENV_USER'];
48  $EXT_AUTH_USER = null;
49  if (isset($GLOBALS['_SERVER']["{$EXT_AUTH_USER_KW}"])) {
50  $EXT_AUTH_USER = $GLOBALS['_SERVER']["{$EXT_AUTH_USER_KW}"];
51  }
52  if (isset($EXT_AUTH_USER) && !empty($EXT_AUTH_USER)) {
53  if ($GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_LOWERCASE_USER']) {
54  $EXT_AUTH_USER = strtolower($EXT_AUTH_USER);
55  }
56  $out['useAuthExternal'] = true;
57  $out['loginAuthExternal'] = $EXT_AUTH_USER;
58  $out['passwordAuthExternal'] = sha1($EXT_AUTH_USER);
59  $EXT_AUTH_MAIL_KW = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENV_MAIL'];
60  $out['emailAuthExternal'] = isset($GLOBALS['_SERVER']["{$EXT_AUTH_MAIL_KW}"]) ? $GLOBALS['_SERVER']["{$EXT_AUTH_MAIL_KW}"] : '';
61  $EXT_AUTH_DESC_KW = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENV_DESC'];
62  $out['descriptionAuthExternal'] = isset($GLOBALS['_SERVER']["{$EXT_AUTH_DESC_KW}"]) ? $GLOBALS['_SERVER']["{$EXT_AUTH_DESC_KW}"] : '';
63  return $out;
64  }
65  }
66  return $out['useAuthExternal'] = false;
67 }
68 
78 function account_check(&$user, &$passwd, &$group = "")
79 {
80  global $SysConf;
81  $dbManager = $GLOBALS['container']->get('db.manager');
82  /* get username/passwd from ~/.fossology.rc */
83  $user_passwd_file = getenv("HOME") . "/.fossology.rc";
84  if (empty($user) && empty($passwd) && file_exists($user_passwd_file)) {
85  $user_passwd_array = parse_ini_file($user_passwd_file, true, INI_SCANNER_RAW);
86 
87  /* get username and password from conf file */
88  if (! empty($user_passwd_array) && ! empty($user_passwd_array['user'])) {
89  $user = $user_passwd_array['user'];
90  }
91  if (! empty($user_passwd_array) && ! empty($user_passwd_array['username'])) {
92  $user = $user_passwd_array['username'];
93  }
94  if (! empty($user_passwd_array) && ! empty($user_passwd_array['groupname'])) {
95  $group = $user_passwd_array['groupname'];
96  }
97  if (! empty($user_passwd_array) && ! empty($user_passwd_array['password'])) {
98  $passwd = $user_passwd_array['password'];
99  }
100  }
101  /* check if the user name/passwd is valid */
102  if (empty($user)) {
103  /*
104  * $uid_arr = posix_getpwuid(posix_getuid());
105  * $user = $uid_arr['name'];
106  */
107  echo "FATAL: You should add '--username USERNAME' when running OR add " .
108  "'username=USERNAME' in ~/.fossology.rc before running.\n";
109  exit(1);
110  }
111  if (empty($passwd)) {
112  echo "The user is: $user, please enter the password:\n";
113  system('stty -echo');
114  $passwd = trim(fgets(STDIN));
115  system('stty echo');
116  if (empty($passwd)) {
117  echo "You entered an empty password.\n";
118  }
119  }
120 
121  if (! empty($user)) {
122  $userDao = $GLOBALS['container']->get('dao.user');
123  try {
124  $row = $userDao->getUserAndDefaultGroupByUserName($user);
125  } catch (Exception $e) {
126  echo $e->getMessage(), "\n";
127  exit(1);
128  }
129  $userId = $row['user_pk'];
130  $SysConf['auth']['UserId'] = $userId;
131 
132  if (empty($group)) {
133  $group = $row['group_name'];
134  $groupId = $row['group_fk'];
135  } else {
136  $rowGroup = $dbManager->getSingleRow(
137  "SELECT group_pk
138  FROM group_user_member INNER JOIN groups ON groups.group_pk = group_user_member.group_fk
139  WHERE user_fk = $1 AND group_name = $2", array($userId, $group),
140  __METHOD__ . ".lookUpGroup");
141  if (false === $rowGroup) {
142  echo "User is not in group.\n";
143  exit(1);
144  }
145  $groupId = $rowGroup['group_pk'];
146  }
147  $SysConf['auth']['GroupId'] = $groupId;
148  if (empty($groupId)) {
149  echo "Group '$group' not found.\n";
150  exit(1);
151  }
152 
153  if (! empty($row['user_pass'])) {
154  $options = array('cost' => 10);
155  if (password_verify($passwd, $row['user_pass'])) {
156  if (password_needs_rehash($row['user_pass'], PASSWORD_DEFAULT, $options)) {
157  $newHash = password_hash($passwd, PASSWORD_DEFAULT, $options);
158  /* Update old hash with new hash */
159  update_password_hash($user, $newHash);
160  }
161  return true;
162  } else if (! empty($row['user_seed'])) {
163  $passwd_hash = sha1($row['user_seed'] . $passwd);
164  /* If verify with new hash fails check with the old hash */
165  if (strcmp($passwd_hash, $row['user_pass']) == 0) {
166  $newHash = password_hash($passwd, PASSWORD_DEFAULT, $options);
167  /* Update old hash with new hash */
168  update_password_hash($user, $newHash);
169  return true;
170  } else {
171  echo "User name or password is invalid.\n";
172  exit(1);
173  }
174  }
175  }
176  }
177  return $userId;
178 }
179 
189 function read_permission($upload, $user)
190 {
191  $ADMIN_PERMISSION = 10;
192  $dbManager = $GLOBALS['container']->get('db.manager');
193 
194  /* check if the user if the owner of this upload */
195  $row = $dbManager->getSingleRow(
196  "SELECT 1
197  FROM upload INNER JOIN users ON users.user_pk = upload.user_fk
198  WHERE users.user_name = $1 AND upload.upload_pk = $2",
199  array($user, $upload),
200  __METHOD__.".checkUpload"
201  );
202 
203  if (! empty($row)) {
204  /* user has permission */
205  return 1;
206  }
207 
208  /* check if the user is administrator */
209  $row = $dbManager->getSingleRow(
210  "SELECT 1
211  FROM users
212  WHERE user_name = $1 AND user_perm = $2",
213  array($user, $ADMIN_PERMISSION),
214  __METHOD__.".checkPerm"
215  );
216 
217  if (! empty($row)) {
218  /* user has permission */
219  return 1;
220  }
221 
222  /* user does not have permission */
223  return 0;
224 }
225 
231 {
232  $sysconfig = $GLOBALS['SysConf']['SYSCONFIG'];
233  if (! array_key_exists('PasswdPolicy', $sysconfig) ||
234  $sysconfig['PasswdPolicy'] == 'false') {
235  return false;
236  }
237  return true;
238 }
239 
245 {
246  $sysconfig = $GLOBALS['SysConf']['SYSCONFIG'];
247  if (! passwordPolicyEnabled()) {
248  return ".*";
249  }
250  $limit = "*";
251  $min = trim($sysconfig['PasswdPolicyMinChar']);
252  $max = trim($sysconfig['PasswdPolicyMaxChar']);
253  if (!empty($min) || !empty($max)) {
254  if (empty($min)) {
255  $min = 0;
256  }
257  $min = intval($min) < 0 ? 0 : $min;
258  $max = intval($max) < 0 ? 0 : $max;
259  $limit = '{' . $min . ",$max}";
260  }
261  $lookAhead = "";
262  $charset = "a-zA-Z\\d";
263  if ($sysconfig['PasswdPolicyLower'] == 'true') {
264  $lookAhead .= '(?=.*[a-z])';
265  }
266  if ($sysconfig['PasswdPolicyUpper'] == 'true') {
267  $lookAhead .= '(?=.*[A-Z])';
268  }
269  if ($sysconfig['PasswdPolicyDigit'] == 'true') {
270  $lookAhead .= '(?=.*\\d)';
271  }
272  $special = trim($sysconfig['PasswdPolicySpecial']);
273  if (!empty($special)) {
274  $lookAhead .= "(?=.*[$special])";
275  $charset .= $special;
276  $charset = '[' . $charset . ']';
277  } else {
278  $charset = '.'; // Allow any special character
279  }
280  return $lookAhead . $charset . $limit;
281 }
282 
288 {
289  $sysconfig = $GLOBALS['SysConf']['SYSCONFIG'];
290  if (! passwordPolicyEnabled()) {
291  return "No policy defined.";
292  }
293  $limit = "Any length.";
294  $min = trim($sysconfig['PasswdPolicyMinChar']);
295  $max = trim($sysconfig['PasswdPolicyMaxChar']);
296  if (!empty($min) || !empty($max)) {
297  if (empty($min)) {
298  $min = 0;
299  }
300  $limit = "Minimum $min";
301  if (!empty($max)) {
302  $limit .= ", maximum $max";
303  }
304  $limit .= " characters.";
305  }
306  $others = [];
307  if ($sysconfig['PasswdPolicyLower'] == 'true') {
308  $others[] = "lower case";
309  }
310  if ($sysconfig['PasswdPolicyUpper'] == 'true') {
311  $others[] = "upper case";
312  }
313  if ($sysconfig['PasswdPolicyDigit'] == 'true') {
314  $others[] = "digit";
315  }
316  if (!empty($others)) {
317  $others = "At least one " . join(", ", $others);
318  } else {
319  $others = "";
320  }
321  $special = trim($sysconfig['PasswdPolicySpecial']);
322  if (!empty($special)) {
323  if (!empty($others)) {
324  $others .= " and";
325  }
326  $others .= " one of <em>$special</em>";
327  }
328  return "$limit $others.";
329 }
generate_password_policy_string()
passwordPolicyEnabled()
read_permission($upload, $user)
Check if the user has the permission to read the copyright/license/etc information of this upload.
siteminder_check()
Check if SiteMinder is enabled.
Definition: common-auth.php:22
generate_password_policy()
account_check(&$user, &$passwd, &$group="")
check if this account is correct
Definition: common-auth.php:78
auth_external_check()
Check if the external HTTP authentication is enabled. The mapping variables should be configured in f...
Definition: common-auth.php:38
update_password_hash($User, $Hash)
Update user password hash.
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:690