FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-auth.php
Go to the documentation of this file.
1 <?php
2 /***********************************************************
3  Copyright (C) 2011-2015 Hewlett-Packard Development Company, L.P.
4  Copyright (C) 2015 Siemens AG
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License version 2.1 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this library; if not, write to the Free Software Foundation, Inc.0
17  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  ***********************************************************/
19 
33 function siteminder_check()
34 {
35  // $IDEnvVar = 'HPPF_AUTH_UID'; // for example for PingIdentity
36  $IDEnvVar = 'HTTP_SMUNIVERSALID';
37  if (isset($_SERVER[$IDEnvVar])) {
38  $SEA = $_SERVER[$IDEnvVar];
39  return $SEA;
40  }
41  return(-1);
42 } // siteminder_check()
43 
51 {
52  $EXT_AUTH_ENABLE = false;
53  if (array_key_exists('EXT_AUTH', $GLOBALS['SysConf'])) {
54  if (array_key_exists('CONF_EXT_AUTH_ENABLE', $GLOBALS['SysConf']['EXT_AUTH'])) {
55  $EXT_AUTH_ENABLE = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENABLE'];
56  }
57  }
58  if ($EXT_AUTH_ENABLE) {
59  $EXT_AUTH_USER_KW = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENV_USER'];
60  $EXT_AUTH_USER = $GLOBALS['_SERVER']["{$EXT_AUTH_USER_KW}"];
61  if (isset($EXT_AUTH_USER) && !empty($EXT_AUTH_USER)) {
62  if ($GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_LOWERCASE_USER']) {
63  $EXT_AUTH_USER = strtolower($EXT_AUTH_USER);
64  }
65  $out['useAuthExternal'] = true;
66  $out['loginAuthExternal'] = $EXT_AUTH_USER;
67  $out['passwordAuthExternal'] = sha1($EXT_AUTH_USER);
68  $EXT_AUTH_MAIL_KW = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENV_MAIL'];
69  $out['emailAuthExternal'] = $GLOBALS['_SERVER']["{$EXT_AUTH_MAIL_KW}"];
70  $EXT_AUTH_DESC_KW = $GLOBALS['SysConf']['EXT_AUTH']['CONF_EXT_AUTH_ENV_DESC'];
71  $out['descriptionAuthExternal'] = $GLOBALS['_SERVER']["{$EXT_AUTH_DESC_KW}"];
72  return $out;
73  }
74  }
75  return $out['useAuthExternal'] = false;
76 }
77 
87 function account_check(&$user, &$passwd, &$group = "")
88 {
89  global $SysConf;
90  $dbManager = $GLOBALS['container']->get('db.manager');
91  /* get username/passwd from ~/.fossology.rc */
92  $user_passwd_file = getenv("HOME") . "/.fossology.rc";
93  if (empty($user) && empty($passwd) && file_exists($user_passwd_file)) {
94  $user_passwd_array = parse_ini_file($user_passwd_file, true, INI_SCANNER_RAW);
95 
96  /* get username and password from conf file */
97  if (! empty($user_passwd_array) && ! empty($user_passwd_array['user'])) {
98  $user = $user_passwd_array['user'];
99  }
100  if (! empty($user_passwd_array) && ! empty($user_passwd_array['username'])) {
101  $user = $user_passwd_array['username'];
102  }
103  if (! empty($user_passwd_array) && ! empty($user_passwd_array['groupname'])) {
104  $group = $user_passwd_array['groupname'];
105  }
106  if (! empty($user_passwd_array) && ! empty($user_passwd_array['password'])) {
107  $passwd = $user_passwd_array['password'];
108  }
109  }
110  /* check if the user name/passwd is valid */
111  if (empty($user)) {
112  /*
113  * $uid_arr = posix_getpwuid(posix_getuid());
114  * $user = $uid_arr['name'];
115  */
116  echo "FATAL: You should add '--username USERNAME' when running OR add " .
117  "'username=USERNAME' in ~/.fossology.rc before running.\n";
118  exit(1);
119  }
120  if (empty($passwd)) {
121  echo "The user is: $user, please enter the password:\n";
122  system('stty -echo');
123  $passwd = trim(fgets(STDIN));
124  system('stty echo');
125  if (empty($passwd)) {
126  echo "You entered an empty password.\n";
127  }
128  }
129 
130  if (! empty($user)) {
131  $userDao = $GLOBALS['container']->get('dao.user');
132  try {
133  $row = $userDao->getUserAndDefaultGroupByUserName($user);
134  } catch (Exception $e) {
135  echo $e->getMessage(), "\n";
136  exit(1);
137  }
138  $userId = $row['user_pk'];
139  $SysConf['auth']['UserId'] = $userId;
140 
141  if (empty($group)) {
142  $group = $row['group_name'];
143  $groupId = $row['group_fk'];
144  } else {
145  $rowGroup = $dbManager->getSingleRow(
146  "SELECT group_pk
147  FROM group_user_member INNER JOIN groups ON groups.group_pk = group_user_member.group_fk
148  WHERE user_fk = $1 AND group_name = $2", array($userId, $group),
149  __METHOD__ . ".lookUpGroup");
150  if (false === $rowGroup) {
151  echo "User is not in group.\n";
152  exit(1);
153  }
154  $groupId = $rowGroup['group_pk'];
155  }
156  $SysConf['auth']['GroupId'] = $groupId;
157  if (empty($groupId)) {
158  echo "Group '$group' not found.\n";
159  exit(1);
160  }
161 
162  if (! empty($row['user_seed']) && ! empty($row['user_pass'])) {
163  $passwd_hash = sha1($row['user_seed'] . $passwd);
164  if (strcmp($passwd_hash, $row['user_pass']) != 0) {
165  echo "User name or password is invalid.\n";
166  exit(1);
167  }
168  }
169  }
170  return $userId;
171 }
172 
182 function read_permission($upload, $user)
183 {
184  $ADMIN_PERMISSION = 10;
185  $dbManager = $GLOBALS['container']->get('db.manager');
186 
187  /* check if the user if the owner of this upload */
188  $row = $dbManager->getSingleRow(
189  "SELECT 1
190  FROM upload INNER JOIN users ON users.user_pk = upload.user_fk
191  WHERE users.user_name = $1 AND upload.upload_pk = $2",
192  array($user, $upload),
193  __METHOD__.".checkUpload"
194  );
195 
196  if (! empty($row)) {
197  /* user has permission */
198  return 1;
199  }
200 
201  /* check if the user is administrator */
202  $row = $dbManager->getSingleRow(
203  "SELECT 1
204  FROM users
205  WHERE user_name = $1 AND user_perm = $2",
206  array($user, $ADMIN_PERMISSION),
207  __METHOD__.".checkPerm"
208  );
209 
210  if (! empty($row)) {
211  /* user has permission */
212  return 1;
213  }
214 
215  /* user does not have permission */
216  return 0;
217 }
218 
account_check(&$user, &$passwd, &$group="")
check if this account is correct
Definition: common-auth.php:87
siteminder_check()
Check if SiteMinder is enabled.
Definition: common-auth.php:33
read_permission($upload, $user)
Check if the user has the permission to read the copyright/license/etc information of this upload...
auth_external_check()
Check if the external HTTP authentication is enabled. The mapping variables should be configured in f...
Definition: common-auth.php:50
char * trim(char *ptext)
Trimming whitespace.
Definition: fossconfig.c:695