1#!/usr/bin/env php 2<?php 3/*********************************************************** 4 Copyright (C) 2019 Siemens AG 5 Author: Gaurav Mishra <mishra.gaurav@siemens.com> 6 7 This program is free software; you can redistribute it and/or 8 modify it under the terms of the GNU General Public License 9 version 2 as published by the Free Software Foundation. 10 11 This program is distributed in the hope that it will be useful, 12 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 GNU General Public License for more details. 15 16 You should have received a copy of the GNU General Public License along 17 with this program; if not, write to the Free Software Foundation, Inc., 18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 ***********************************************************/ 20 21use Fossology\Lib\Db\DbManager; 22 23require_once 'dbmigrate_3.5-3.6.php'; 24require_once 'fossinit-common.php'; 25 26/** 27 * @file 28 * @brief Migrate DB to for Software Heritage by calculating SHA256 of pfiles 29 * tables. 30 */ 31 32/** 33 * @brief Check if current user can access conf files. 34 * 35 * Check if current user is root or fossy 36 * @returns True if user can access the files, false otherwise. 37 */ 38function checkCorrectUser() 39{ 40 $processUser = posix_getpwuid(posix_geteuid()); 41 if ($processUser['uid'] !== 0 && $processUser['name'] != "{$PROJECTUSER}") { 42 return false; 43 } 44 return true; 45} 46 47function explainPfileUsage() 48{ 49 global $argv; 50 51 $usage = "Usage: " . basename($argv[0]) . " [options] 52 Calculate the SHA256 hash of files pointed by pfile table. Options are: 53 -c path to fossology configuration files 54 -d {database name} default is 'fossology' 55 -h this help usage"; 56 print "$usage\n"; 57 exit(0); 58} 59 60/* Condition must be satisfied */ 61if (!checkCorrectUser()) { 62 echo "Error: " . basename($argv[0]) . " must either run by root or {$PROJECTUSER} " . 63 "user!\n"; 64 exit(-2); 65} 66 67/* Note: php 5 getopt() ignores options not specified in the function call, so add 68 * dummy options in order to catch invalid options. 69 */ 70$AllPossibleOpts = "abc:d:efghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 71 72/* defaults */ 73$DatabaseName = "fossology"; 74$sysconfdir = "{$SYSCONFDIR}"; 75$force = true; 76 77/* command-line options */ 78$Options = getopt($AllPossibleOpts); 79foreach($Options as $optKey => $optVal) 80{ 81 switch($optKey) 82 { 83 case 'c': /* set SYSCONFIDR */ 84 $sysconfdir = $optVal; 85 break; 86 case 'd': /* optional database name */ 87 $DatabaseName = $optVal; 88 break; 89 case 'h': /* help */ 90 explainPfileUsage(); 91 break; 92 default: 93 echo "Invalid Option \"$optKey\".\n"; 94 explainUsage(); 95 } 96} 97 98/* Set SYSCONFDIR and set global (for backward compatibility) */ 99$SysConf = bootstrap($sysconfdir); 100$SysConf["DBCONF"]["dbname"] = $DatabaseName; 101$GLOBALS["SysConf"] = array_merge($GLOBALS["SysConf"], $SysConf); 102$projectGroup = $SysConf['DIRECTORIES']['PROJECTGROUP'] ?: 'fossy'; 103$gInfo = posix_getgrnam($projectGroup); 104posix_setgid($gInfo['gid']); 105$groups = `groups`; 106if (!preg_match("/\s$projectGroup\s/",$groups) && (posix_getgid() != $gInfo['gid'])) 107{ 108 print "FATAL: You must be in group '$projectGroup'.\n"; 109 exit(1); 110} 111 112require_once("$MODDIR/vendor/autoload.php"); 113require_once("$MODDIR/lib/php/common-db.php"); 114require_once("$MODDIR/lib/php/common-container.php"); 115require_once("$MODDIR/lib/php/common-sysconfig.php"); 116 117/* Initialize global system configuration variables $SysConfig[] */ 118ConfigInit($SYSCONFDIR, $SysConf); 119 120$dbManager = $GLOBALS["container"]->get("db.manager"); 121 122updatePfileSha256($dbManager, $force); 123