php調用shell腳本安全,從PHP調用的shell腳本問題

TLDR;

我有一個shell腳本,從命令行運行時工作正常,但如果從PHP腳本中調用(通過Web訪問)則不行.

在這兩種情況下,主叫用戶都是www-data.

線路失敗是這樣的:

openssl genrsa -des3 -out certs/$PCODE.key -passout env:PASSPHRASE 2048

為什么會這樣?我該怎么調試呢?

全文

我有以下腳本,它是this gist的略微修改版本,用于生成自簽名SSL證書.

當我從終端運行它作為www-data時,它可以正常工作并生成密鑰文件,CSR和SSL證書文件.但是當我從PHP腳本中調用腳本時,它會輸出錯誤并且不會生成任何文件.是什么導致失敗?我該怎么調試呢?

從終端:

me@machine$sudo su www-data

www-data@machine$./gencert.sh acme

www-data will generate an SSL cert for acme.dev

Command after line 32 executed oK

Passphrase expoted as I7gOnWxWd0hOk38Zu ... FbxL3K3Rzlv

Generating RSA private key, 2048 bit long modulus

..............................................+++

.................+++

e is 65537 (0x10001)

Command after line 49 executed oK

Command after line 54 executed oK

Command after line 65 executed oK

writing RSA key

Command after line 69 executed oK

Signature ok

subject=/C=IR/ST=Alborz/.../emailAddress=noreply@acme.dev

Getting Private key

Command after line 74 executed oK

產生的文件:

> certs / acme.key.org

> certs / acme.key

> certs / acme.csr

> certs / acme.crt

來自PHP:

$r = `/var/www/testbench/pm/shell/gencert.sh acme`;

echo $r;

沒有生成文件,輸出如下:

www-data will generate an SSL cert for acme.dev

Command after line 32 executed oK

Passphrase expoted as 1Fd1seZoe2XF ... oSmQFJdVpdwOeTo2CK5VjLxp

Error. Return value = 1 after line 49

返回1的行是這樣的:

openssl genrsa -des3 -out certs / $PCODE.key -passout env:PASSPHRASE 2048

這是修改后的shell腳本:

#!/bin/bash

# Bash shell script for generating self-signed certs. Run this in a folder, as it

# generates a few files. Large portions of this script were taken from the

# following artcile:

#

# http://usrportage.de/archives/919-Batch-generating-SSL-certificates.html

# https://deliciousbrains.com/ssl-certificate-authority-for-local-https-development/

# Additional alterations by: Brad Landers

# Date: 2012-01-27

# Script accepts a single argument, the fqdn for the cert

PCODE="$1"

if [ -z "$PCODE" ]; then

echo "Usage: $(basename $0) "

exit 11

fi

THE_USER="$(whoami)"

echo "$THE_USER will generate an SSL cert for $PCODE.dev"

fail_if_error() {

[ $1 != 0 ] && {

echo -n "Error. Return value = $1 after line $LASTLINE"

unset PASSPHRASE

exit 10

}

echo "Command after line $LASTLINE executed oK"

}

# Generate a passphrase

LASTLINE="${LINENO}"

export PASSPHRASE=$(head -c 500 /dev/urandom | tr -dc a-z0-9A-Z | head -c 128; echo)

fail_if_error $?

echo -n "Passphrase expoted as "

printenv PASSPHRASE

# Certificate details; replace items in angle brackets with your own info

subj="

C=IR

ST=Alborz

O=ACME

localityName=Karaj

commonName=*.$PCODE.dev

organizationalUnitName=WebAdmin

emailAddress=noreply@$PCODE.dev

"

LASTLINE="${LINENO}"

# Generate the server private key

openssl genrsa -des3 -out certs/$PCODE.key -passout env:PASSPHRASE 2048

fail_if_error $?

LASTLINE="${LINENO}"

# Generate the CSR

openssl req \

-new \

-batch \

-subj "$(echo -n "$subj" | tr "\n" "/")" \

-key certs/$PCODE.key \

-out certs/$PCODE.csr \

-passin env:PASSPHRASE

fail_if_error $?

LASTLINE="${LINENO}"

cp certs/$PCODE.key certs/$PCODE.key.org

fail_if_error $?

LASTLINE="${LINENO}"

# Strip the password so we don't have to type it every time we restart Apache

openssl rsa -in certs/$PCODE.key.org -out certs/$PCODE.key -passin env:PASSPHRASE

fail_if_error $?

LASTLINE="${LINENO}"

# Generate the cert (good for 10 years)

openssl x509 -req -days 3650 -in certs/$PCODE.csr -signkey certs/$PCODE.key -out certs/$PCODE.crt

fail_if_error $?

解決方法:

要執行的命令具有相對路徑,例如:certs / $PCODE.key.當您執行命令時(在這種情況下通過反引號操作符),路徑相對于PHP進程的當前工作目錄進行擴展.這很少(如果有的話)與命令shell使用的路徑相同.

要調試這個,你可以使用strace擴展你的實際命令,例如:strace openssl ….這將為你提供相當大的診斷功能,接近最后,你會看到EPERM的內容.

要解決此問題,您可以在PHP中使用chdir來設置當前工作目錄,也可以在腳本中使用cd,或者您的腳本可以使用絕對路徑.我更喜歡后者.

標簽:php,shell,command-line,sh

來源: https://codeday.me/bug/20190627/1305955.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378541.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378541.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378541.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

linux 運維基礎問題_Linux基礎能力問題和解答

linux 運維基礎問題This section contains Aptitude Questions and Answers on Linux Basics. 本節包含有關Linux基礎知識的 Aptitude問答。 1) There are the following statements that are given below, which of them are correct about Linux? Linux is system software…

JS 獲取瀏覽器信息,給出友情提示,避免部分兼容性問題

最近在做webform,瀏覽器兼容是個問題,這里我收集了一些獲取瀏覽器信息的資料,可以給一些用戶使用時,提示瀏覽器版本過低,讓升級版本用. 這樣會給開發的我們,省下很多用來調試兼容性的時間和精力. 本人就是這樣想的 ~  檢測瀏覽器及版本使用 JavaScript 檢測關于訪問者的瀏覽器…

兩欄 三欄的css

三欄格局 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns"http://www.w3.org/1999/xhtml" xml:lang"zh" lang"zh"><head pro…

06-機器學習(Haar+Adaboost實現人臉、人眼檢測)

機器學習是什么? 機器學習訓練樣本特征分類器&#xff0c;通過讓機器學習的方式&#xff0c;來達到某種功能的過程 深度學習是什么&#xff1f; 深度學習海量的學習樣本人工神經網絡 機器學習需要&#xff1a;樣本、特征、分類器、對訓練后的數據進行預測或檢驗 人臉樣本haar…

php xml表格形式輸出,PHP XML如何輸出nice格式

這里是代碼&#xff1a;$doc new DomDocument(1.0);// create root node$root $doc->createElement(root);$root $doc->appendChild($root);$signed_values array(a > eee, b > sd, c > df);// process one row at a timeforeach ($signed_values as $key &…

Opencv實戰【3】——圖像修復與圖像銳化(darling in the franxx)

目錄前言圖像修復圖像銳化darling in the franxx圖片總結前言 前天&#xff0c;在群里看見有人發了這張表情包&#xff1a; 感覺女主有點好看&#xff0c;然后問室友是啥番劇&#xff08;darling in the franxx&#xff09;&#xff0c;然后就去補番了&#xff0c;然后從晚上…

python 示例_Python date isoweekday()方法與示例

python 示例Python date.isoweekday()方法 (Python date.isoweekday() Method) date.isoweekday() method is used to manipulate objects of date class of module datetime. date.isoweekday()方法用于處理模塊日期時間的日期類的對象。 It uses a date class object and r…

07-機器學習(Hog+SVM實現小獅子識別)

一、SVM支持向量機 什么是SVM支持向量機&#xff1f; SVM支持向量機本質仍是一個分類器&#xff0c;其核心為尋求一個最優超平面最終實現分類&#xff0c;實現分類問題 在尋求超平面的時候有多種方式&#xff0c;可以使用若干條直線或曲線進行分類&#xff0c;這里使用的是直線…

Net Remoting基礎篇

一、Remoting基礎 什么是Remoting&#xff0c;簡而言之&#xff0c;我們可以將其看作是一種分布式處理方式。從微軟的產品角度來看&#xff0c;可以說Remoting就是DCOM的一種升 級&#xff0c;它改善了很多功能&#xff0c;并極好的融合到.Net平臺下。Microsoft .NET Remoting …

Maven3.0.5代理nexus

Nexus簡介 Nexus是Sonatype推出的強大Maven倉庫管理器產品&#xff0c;要比以前TSS上介紹的Artifactory要好使用的多&#xff0c;也是一個拆箱即用的Java App&#xff0c;內嵌Jetty容器和Java Wrapper做Windows服務&#xff0c;安裝簡單到解壓然后雙擊install即可。更詳細的幫助…

8253譯碼電路設計以及初始化編程講解

先驗知識回顧&#xff1a;知識點不清晰的時候可以查詢相關知識點。 https://blog.csdn.net/qq_42604176/article/details/105810973 需掌握的主要知識點 1、譯碼電路設計 2、初始化編程 例題1 在以 8086構成的最大方式系統中&#xff0c;有一片8254的端口地址分別為301H、3…

java安卓寫文件路徑,如何使用gradle作為構建系統,平臺Android配置Protobuf(Java)文件的輸出路徑?...

我正在努力解決以下問題&#xff1a;我有2個基于maven的java項目和1個基于gradle的Android項目 . 布局如下&#xff1a;Workspace/├── MavenProj1/├── MavenProj2/├── AndroidGradleProject1/├── Protos/所有這些的包結構很常見&#xff0c;比方說 com.example.* 所…

Java System類exit()方法及示例

系統類exit()方法 (System class exit() method) exit() method is available in java.lang package. exit()方法在java.lang包中可用。 exit() method is used to exit the currently running JVM (Java Virtual Machine). exit()方法用于退出當前正在運行的JVM(Java虛擬機)。…

基于圖像處理的數碼印花噴墨墨滴形狀規范的研究(Python+OpenCV+Mysql)

大體思路&#xff1a;由于墨滴的不同參數會對墨滴的形態產生一定的影響&#xff0c;故如果通過研究墨滴的形態則通過海量的數據就可以大概確定墨滴的各項參數指標的范圍。通過OpenCV對墨滴的噴出的形狀進行圖像處理&#xff0c;對墨滴圖像進行一系列的分析&#xff0c;通過一系…

ASP.NET 主題(Themes)FAQ

1、主題是什么 主題由一組元素組成&#xff1a;外觀、級聯樣式表 (CSS)、圖像和其他資源。主題將至少包含外觀。主題是在網站或 Web 服務器上的特殊目錄中定義的。主題是一組Web Control的屬性設置的集合&#xff0c;提供一種簡單的方法設置控件的樣式屬性。 主題只在Web Contr…

Head First HTML與CSS、XHTML++筆記(第四章 WEB鎮之旅 第五章 認識媒體)

第四章 鏈接&#xff08;詳解<a>元素&#xff09; 目標錨 在目標位置 <h2><a id"chai">contentTest</a></h2> 在需要鏈接位置 <a href"index.html#chai">See</a> 鏈接到自身的目標錨 <a href"#top"…

Opencv實戰【4】——圖片動漫化處理

博主聯系方式&#xff1a; QQ:1540984562 微信&#xff1a;wxid_nz49532kbh9u22 QQ交流群&#xff1a;750313950 目錄動漫化風格的特點處理手段代碼實現效果總結動漫化風格的特點 &#xff08;1&#xff09;動漫中的細節相對少&#xff1b; &#xff08;2&#xff09;動漫中的邊…

nextshort_Java掃描儀的nextShort()方法與示例

nextshort掃描器類的nextShort()方法 (Scanner Class nextShort() method) Syntax: 句法&#xff1a; public short nextShort();public short nextShort(int rad);nextShort() method is available in java.util package. nextShort()方法在java.util包中可用。 nextShort() …

php 生成css文件怎么打開,php生成html文件的多種步驟介紹

//定義日期函數functiongetdatetime(){$datetimegetdate();$strReturn$datetime["year"]."-";$strReturn$strReturn.$datetime["mon"]."-";$strReturn$strReturn.$datetime["mday"];return$strReturn;}//定義時間函數(文件名…

08-KNN手寫數字識別

標簽下載地址 文件內容備注train-images-idx3-ubyte.gz訓練集圖片&#xff1a;55000張訓練圖片&#xff0c;5000張驗證圖片train-labels-idx1-ubyte.gz訓練集圖片對應的數字標簽t10k-images-idx3-ubyte.gz測試集圖片&#xff1a;10000張圖片t表示test&#xff0c;測試圖片&…