<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Feb 24, 2014 at 5:16 PM, Mike Miller <span dir="ltr"><<a href="mailto:mbmiller+l@gmail.com" target="_blank">mbmiller+l@gmail.com</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="">On Mon, 24 Feb 2014, Mike Miller wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
zip -r "$DIR".zip "$DIR" &>/dev/null<br>
</blockquote>
<br></div>
After all I wrote earlier, I forgot my zero option!  It should have been:<br>
<br>
zip -0r "$DIR".zip "$DIR" &>/dev/null</blockquote><div><br></div><div>Some other language will give you better handling for query string parameters and to safety check user inputs, but the script below might work. <br>

<br></div><div>The most immediate danger that comes to mind is that a user might request "../../../path/to/personal/files" and get whatever they want from your server, all zipped up neatly.  Other dangers like shellcode could exist too. <br>

<br>--<br></div><div>Michael <br></div><div><br><br></div><div><br>#!/bin/bash<br><br># Usage: <a href="http://localhost/cgi-bin/zip.sh?path=selectedDir">http://localhost/cgi-bin/zip.sh?path=selectedDir</a><br><br># Base dir for all photos<br>

BASEPATH="/fatty/Photos/2014"<br><br># This is a simple way to split the query string. Thanks SO!  <a href="http://stackoverflow.com/questions/3919755/how-to-parse-query-string-from-a-bash-cgi-script">http://stackoverflow.com/questions/3919755/how-to-parse-query-string-from-a-bash-cgi-script</a><br>

saveIFS=$IFS<br>IFS='=&'<br>param=($QUERY_STRING)<br>IFS=$saveIFS<br><br># Grab the requested directory. Assume that it's value 1<br>DIR=${param[1]}<br><br># Allowing a user to specify a path to zip and return to them is <br>

# a huge security vulnerability. I doubt this solves the problem<br># but it mitigates it slightly<br><br>REALPATH=$(readlink -m $BASEPATH/$DIR)<br><br>if [[ $BASEPATH =~ ^$REALPATH ]]<br>then<br>    # Someone requested a path that left the BASEPATH<br>

    echo -e "Content-type: text/plain\n"<br>    echo "$REALPATH is not within the allowed path!"<br>    exit<br>fi<br><br># Check if the requested directory exists<br>if [[ ! -d $REALPATH ]]<br>then<br>

    echo -e "Content-type: text/plain\n"<br>    echo "The requested directory doesn't exist"<br>    exit<br>fi<br><br><br># Make a temp file<br>TMPFILE=$(mktemp -u --suffix .zip)<br><br># Change to the parent of the requested directory<br>

cd $(dirname $REALPATH)<br><br><br># Zip the requested directory into the temp file<br><br>zip -0 --quiet -r $TMPFILE $(basename $REALPATH)<br><br># Bad exit from zip. Sad. <br>ZIPEXIT=$?<br>if [[ $ZIPEXIT -ne 0 ]]<br>then<br>

    echo -e "Content-type: text/plain\n"<br>    echo "Zip had a problem ($ZIPEXIT). Sorry."<br>    exit<br>fi<br><br># Get filesize<br>FILESIZE=$(wc -c $TMPFILE)<br><br>echo "Content-type: application/octet-stream"<br>

echo "Content-Disposition: attachment; filename='mydownload.zip'"<br>echo "Content-Length: $FILESIZE"<br>echo ""<br><br># Send it and remove it<br>cat $TMPFILE<br>rm $TMPFILE <br></div>

</div></div></div>