FOSSology  4.4.0
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 = $GLOBALS['_SERVER']["{$EXT_AUTH_USER_KW}"];
49  if (isset($EXT_AUTH_USER) && !empty($EXT_AUTH_USER)) {
50  if ($GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_LOWERCASE_USER']) {
51  $EXT_AUTH_USER = strtolower($EXT_AUTH_USER);
52  }
53  $out['useAuthExternal'] = true;
54  $out['loginAuthExternal'] = $EXT_AUTH_USER;
55  $out['passwordAuthExternal'] = sha1($EXT_AUTH_USER);
56  $EXT_AUTH_MAIL_KW = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENV_MAIL'];
57  $out['emailAuthExternal'] = $GLOBALS['_SERVER']["{$EXT_AUTH_MAIL_KW}"];
58  $EXT_AUTH_DESC_KW = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENV_DESC'];
59  $out['descriptionAuthExternal'] = $GLOBALS['_SERVER']["{$EXT_AUTH_DESC_KW}"];
60  return $out;
61  }
62  }
63  return $out['useAuthExternal'] = false;
64 }
65 
75 function account_check(&$user, &$passwd, &$group = "")
76 {
77  global $SysConf;
78  $dbManager = $GLOBALS['container']->get('db.manager');
79  /* get username/passwd from ~/.fossology.rc */
80  $user_passwd_file = getenv("HOME") . "/.fossology.rc";
81  if (empty($user) && empty($passwd) && file_exists($user_passwd_file)) {
82  $user_passwd_array = parse_ini_file($user_passwd_file, true, INI_SCANNER_RAW);
83 
84  /* get username and password from conf file */
85  if (! empty($user_passwd_array) && ! empty($user_passwd_array['user'])) {
86  $user = $user_passwd_array['user'];
87  }
88  if (! empty($user_passwd_array) && ! empty($user_passwd_array['username'])) {
89  $user = $user_passwd_array['username'];
90  }
91  if (! empty($user_passwd_array) && ! empty($user_passwd_array['groupname'])) {
92  $group = $user_passwd_array['groupname'];
93  }
94  if (! empty($user_passwd_array) && ! empty($user_passwd_array['password'])) {
95  $passwd = $user_passwd_array['password'];
96  }
97  }
98  /* check if the user name/passwd is valid */
99  if (empty($user)) {
100  /*
101  * $uid_arr = posix_getpwuid(posix_getuid());
102  * $user = $uid_arr['name'];
103  */
104  echo "FATAL: You should add '--username USERNAME' when running OR add " .
105  "'username=USERNAME' in ~/.fossology.rc before running.\n";
106  exit(1);
107  }
108  if (empty($passwd)) {
109  echo "The user is: $user, please enter the password:\n";
110  system('stty -echo');
111  $passwd = trim(fgets(STDIN));
112  system('stty echo');
113  if (empty($passwd)) {
114  echo "You entered an empty password.\n";
115  }
116  }
117 
118  if (! empty($user)) {
119  $userDao = $GLOBALS['container']->get('dao.user');
120  try {
121  $row = $userDao->getUserAndDefaultGroupByUserName($user);
122  } catch (Exception $e) {
123  echo $e->getMessage(), "\n";
124  exit(1);
125  }
126  $userId = $row['user_pk'];
127  $SysConf['auth']['UserId'] = $userId;
128 
129  if (empty($group)) {
130  $group = $row['group_name'];
131  $groupId = $row['group_fk'];
132  } else {
133  $rowGroup = $dbManager->getSingleRow(
134  "SELECT group_pk
135  FROM group_user_member INNER JOIN groups ON groups.group_pk = group_user_member.group_fk
136  WHERE user_fk = $1 AND group_name = $2", array($userId, $group),
137  __METHOD__ . ".lookUpGroup");
138  if (false === $rowGroup) {
139  echo "User is not in group.\n";
140  exit(1);
141  }
142  $groupId = $rowGroup['group_pk'];
143  }
144  $SysConf['auth']['GroupId'] = $groupId;
145  if (empty($groupId)) {
146  echo "Group '$group' not found.\n";
147  exit(1);
148  }
149 
150  if (! empty($row['user_pass'])) {
151  $options = array('cost' => 10);
152  if (password_verify($passwd, $row['user_pass'])) {
153  if (password_needs_rehash($row['user_pass'], PASSWORD_DEFAULT, $options)) {
154  $newHash = password_hash($passwd, PASSWORD_DEFAULT, $options);
155  /* Update old hash with new hash */
156  update_password_hash($user, $newHash);
157  }
158  return true;
159  } else if (! empty($row['user_seed'])) {
160  $passwd_hash = sha1($row['user_seed'] . $passwd);
161  /* If verify with new hash fails check with the old hash */
162  if (strcmp($passwd_hash, $row['user_pass']) == 0) {
163  $newHash = password_hash($passwd, PASSWORD_DEFAULT, $options);
164  /* Update old hash with new hash */
165  update_password_hash($user, $newHash);
166  return true;
167  } else {
168  echo "User name or password is invalid.\n";
169  exit(1);
170  }
171  }
172  }
173  }
174  return $userId;
175 }
176 
186 function read_permission($upload, $user)
187 {
188  $ADMIN_PERMISSION = 10;
189  $dbManager = $GLOBALS['container']->get('db.manager');
190 
191  /* check if the user if the owner of this upload */
192  $row = $dbManager->getSingleRow(
193  "SELECT 1
194  FROM upload INNER JOIN users ON users.user_pk = upload.user_fk
195  WHERE users.user_name = $1 AND upload.upload_pk = $2",
196  array($user, $upload),
197  __METHOD__.".checkUpload"
198  );
199 
200  if (! empty($row)) {
201  /* user has permission */
202  return 1;
203  }
204 
205  /* check if the user is administrator */
206  $row = $dbManager->getSingleRow(
207  "SELECT 1
208  FROM users
209  WHERE user_name = $1 AND user_perm = $2",
210  array($user, $ADMIN_PERMISSION),
211  __METHOD__.".checkPerm"
212  );
213 
214  if (! empty($row)) {
215  /* user has permission */
216  return 1;
217  }
218 
219  /* user does not have permission */
220  return 0;
221 }
222 
228 {
229  $sysconfig = $GLOBALS['SysConf']['SYSCONFIG'];
230  if (! array_key_exists('PasswdPolicy', $sysconfig) ||
231  $sysconfig['PasswdPolicy'] == 'false') {
232  return false;
233  }
234  return true;
235 }
236 
242 {
243  $sysconfig = $GLOBALS['SysConf']['SYSCONFIG'];
244  if (! passwordPolicyEnabled()) {
245  return ".*";
246  }
247  $limit = "*";
248  $min = trim($sysconfig['PasswdPolicyMinChar']);
249  $max = trim($sysconfig['PasswdPolicyMaxChar']);
250  if (!empty($min) || !empty($max)) {
251  if (empty($min)) {
252  $min = 0;
253  }
254  $min = intval($min) < 0 ? 0 : $min;
255  $max = intval($max) < 0 ? 0 : $max;
256  $limit = '{' . $min . ",$max}";
257  }
258  $lookAhead = "";
259  $charset = "a-zA-Z\\d";
260  if ($sysconfig['PasswdPolicyLower'] == 'true') {
261  $lookAhead .= '(?=.*[a-z])';
262  }
263  if ($sysconfig['PasswdPolicyUpper'] == 'true') {
264  $lookAhead .= '(?=.*[A-Z])';
265  }
266  if ($sysconfig['PasswdPolicyDigit'] == 'true') {
267  $lookAhead .= '(?=.*\\d)';
268  }
269  $special = trim($sysconfig['PasswdPolicySpecial']);
270  if (!empty($special)) {
271  $lookAhead .= "(?=.*[$special])";
272  $charset .= $special;
273  $charset = '[' . $charset . ']';
274  } else {
275  $charset = '.'; // Allow any special character
276  }
277  return $lookAhead . $charset . $limit;
278 }
279 
285 {
286  $sysconfig = $GLOBALS['SysConf']['SYSCONFIG'];
287  if (! passwordPolicyEnabled()) {
288  return "No policy defined.";
289  }
290  $limit = "Any length.";
291  $min = trim($sysconfig['PasswdPolicyMinChar']);
292  $max = trim($sysconfig['PasswdPolicyMaxChar']);
293  if (!empty($min) || !empty($max)) {
294  if (empty($min)) {
295  $min = 0;
296  }
297  $limit = "Minimum $min";
298  if (!empty($max)) {
299  $limit .= ", maximum $max";
300  }
301  $limit .= " characters.";
302  }
303  $others = [];
304  if ($sysconfig['PasswdPolicyLower'] == 'true') {
305  $others[] = "lower case";
306  }
307  if ($sysconfig['PasswdPolicyUpper'] == 'true') {
308  $others[] = "upper case";
309  }
310  if ($sysconfig['PasswdPolicyDigit'] == 'true') {
311  $others[] = "digit";
312  }
313  if (!empty($others)) {
314  $others = "At least one " . join(", ", $others);
315  } else {
316  $others = "";
317  }
318  $special = trim($sysconfig['PasswdPolicySpecial']);
319  if (!empty($special)) {
320  if (!empty($others)) {
321  $others .= " and";
322  }
323  $others .= " one of <em>$special</em>";
324  }
325  return "$limit $others.";
326 }
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:75
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