1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

IPKG-WEB , bad coded ?!

Discussion in 'Tomato Firmware' started by ulyan, Jun 26, 2012.

  1. ulyan

    ulyan Networkin' Nut Member

    Hi again,

    In my journey with the tomato ipkg packages testing, I am testing now the ipkg-web one. I have it in my lighttpd web server. There are 2 files with cgi extension: sluginfo.cgi and package.cgi. The first one shows some info related to your router and the other is suposed to help you do some ipkg package management. Thing is it works, I mean it shows the page but no query works. Can this be bad coding or my problem is related to something else ?!

    I had to install perl (is this coded in perl ?!), and enable cgi module on lighttpd.

    Code:
    #!/bin/sh
    #
    # $Id: package.cgi 6240 2007-06-17 22:29:05Z marceln $
    #
     
    if [ "${BASH_CHECK}" != 1 -a -f /opt/bin/bash ]
    then
        BASH_CHECK=1; export BASH_CHECK
        /opt/bin/bash $0
        exit $$
    fi
     
    PROG=${0##*/}
    TMPFILE=/tmp/${PROG}.$$
     
    #
    # HTML Header
    #
    cat << EOF
    Content-type: text/html
     
    <!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">
     
    <head>
    <meta name="generator" content="CGI:shell" />
    <meta name="version" content="\$Id: package.cgi 6240 2007-06-17 22:29:05Z marceln $" />
    <title>ipkg web</title>
    <style type="text/css">
    h1, h2 {
      font-family: Arial, Helvetica, sans-serif;
      color: #004;
    }
     
    table {
      border-top: 1px solid #eee;
      border-right: 1px solid #eee;
      width: 100%;
    }
     
    th, td {
      padding: 2px 4px;
      border-left: 1px solid #eee;
      border-bottom: 1px solid #eee;
    }
     
    table a {
      background: #ddd;
      color: #004;
      text-decoration: none;
      margin: 1px;
      padding: 2px 4px;
      font-family: Arial, Helvetica, sans-serif;
      font-size: 75%;
    }
     
    table a.ins {
      background: #dfd;
      border-left: 1px solid #cec;
      border-bottom: 1px solid #cec;
    }
     
    table a.upd {
      background: #ddf;
      border-left: 1px solid #cce;
      border-bottom: 1px solid #cce;
    }
     
    table a.del {
      background: #fdd;
      border-left: 1px solid #ecc;
      border-bottom: 1px solid #ecc;
    }
    </style>
    </head>
     
    <body>
    <h1>The ipkg web frontend</h1>
    <form methode="GET">
    <table>
    <tr>
        <td>Sync packages</td>
        <td>
            <input type="radio" name="updatedb" value="n" checked>no</input>
            <input type="radio" name="updatedb" value="y">yes</input>
        </td>
    </tr>
    <tr>
        <td>Type:</td>
        <td>
            <select name="typefilter">
                <option selected value="none">NONE</option>
                <option value="update">Updates</option>
                <option value="installed">Installed</option>
                <option value="not">Not installed</option>
            </select>
        </td>
    </tr>
    <tr>
        <td>Filter</td>
        <td><input type="text" name="namefilter"></td>
    </tr>
    </table>
    <input type="submit" name="submit">&nbsp;<input type="reset">
    </form>
    EOF
     
    NAMEFILTER=.
     
    # QUERY_STRING=task=install&package=atk
    IFS='&'
    set -- ${QUERY_STRING}
    for i in $@
    do
        case ${i%%=*} in
            task)
                TASK="${i#*=}"
                ;;
            package)
                PACKAGE="${i#*=}"
                ;;
            updatedb)
                UPDATEDB="${i#*=}"
                ;;
            typefilter)
                TYPEFILTER="${i#*=}"
                ;;
            namefilter)
                if [ "${i#*=}" ]
                then
                    NAMEFILTER="${i#*=}"
                fi
                ;;
            submit)
                SUBMIT="${i#*=}"
                ;;
            *)
                echo "<b>ERROR</b>: Unkown option '${i}'"
                echo '</body>'
                echo '</html>'
                exit 1
                ;;
        esac
    done
     
    if [ ! -f /opt/bin/bash ]
    then
        echo "<h2>Please install bash</h2>"
        echo "<p>I recomend to install bash because this cgi script will"
        echo "run more than two times faster with bash</p>"
    fi
     
    if [ "${UPDATEDB}" = y ]
    then
        echo "<h2>Upgrading package list</h2>"
        echo '<pre>'
        ipkg update </dev/null
        echo '</pre>'
    fi
     
    if [ "${TASK}" = install ]
    then
        echo "<h2>Install ${PACKAGE}</h2>"
        echo '<pre>'
        ipkg -force-defaults install "${PACKAGE}" </dev/null
        echo '</pre>'
    fi
     
    if [ "${TASK}" = update ]
    then
        echo "<h2>Update ${PACKAGE}</h2>"
        echo '<pre>'
        ipkg -force-defaults upgrade "${PACKAGE}" </dev/null
        echo '</pre>'
    fi
     
    if [ "${TASK}" = delete ]
    then
        echo "<h2>Delete ${PACKAGE}</h2>"
        echo '<pre>'
        ipkg -force-defaults remove "${PACKAGE}" </dev/null
        echo '</pre>'
    fi
     
    if [ "${SUBMIT}" != ""  ]
    then
        echo '<h2>Package list</h2>'
        ipkg list_installed >${TMPFILE}
        trap "[ -f ${TMPFILE} ] && rm ${TMPFILE}" 0
     
        echo '<table border="1" cellpadding="0" cellspacing="0">'
        echo '<tr><th>task</th><th>Package</th><th>I-Ver</th><th>P-Ver</th><th>Comment</th><th>Delete</th></tr>'
        ipkg list | grep -- "${NAMEFILTER}" | while read line
        do
            INSTALLED=""
            NAME=${line%% - *}
            line=${line#* - }
            VERSION=${line%% - *}
            VERSION=${VERSION% -}
            COMMENT=${line#* - }
            if [ "$COMMENT" = "$line" ]
            then
                COMMENT='&nbsp;'
            fi
            instline=$(grep "^${NAME} - " ${TMPFILE})
            if [ "${instline}" != "" ]
            then
                INSTALLED=Y
                DEL="<a href='$PROG?task=delete&amp;package=${NAME}' class='del'>delete</a>"
                instline=${instline#* - }
                IVER=${instline%% - *}
                if [ "${IVER}" = "${VERSION}" ]
                then
                    TASK='&nbsp;'
                else
                    TASK="<a href='$PROG?task=update&amp;package=${NAME}' class='upd'>update</a>"
                fi
            else
                DEL='&nbsp;'
                IVER='&nbsp;'
                TASK="<a href='$PROG?task=install&amp;package=${NAME}' class='ins'>install</a>"
            fi
            SHOW=0
            case "${TYPEFILTER}" in
                none)
                    SHOW=1
                    ;;
                update)
                    [ "${INSTALLED}" ] && [ "${IVER}" != "${VERSION}" ] && SHOW=1
                    ;;
                installed)
                    [ "${INSTALLED}" ] && SHOW=1
                    ;;
                not)
                    [ -z "${INSTALLED}" ] && SHOW=1
                    ;;
                *)
                    echo "<b>ERROR</b>: Unkown typefilter '${TYPEFILTER}'"
                    echo '</body>'
                    echo '</html>'
                    exit 1
                    ;;
            esac
            [ "${SHOW}" = 1 ] && echo "<tr><td>${TASK}</td><td>${NAME}</td><td>${IVER}</td><td>${VERSION}</td><td>${COMMENT}</td><td>${DEL}</td></tr>"
        done
        echo '</table>'
    fi
     
    echo '</body>'
    echo '</html>'
    
    I see that he uses bash. I have it installed but is not by default, as I think you have to ask bash in this manner:
    Code:
    bash -c command
    Can this be the problem ? Anyone knows who coded this ? Is it gpl ? etc

    Captura de pantalla 2012-06-26 a les 13.48.01.png
     
  2. shibby20

    shibby20 Network Guru Member

  3. shibby20

    shibby20 Network Guru Member

    i will try integrade this into tomato :)
     
  4. Dragonken

    Dragonken Serious Server Member

    the /www is read-only..
    I am looking forward the integrated version :)
     
  5. Twincam

    Twincam Reformed Router Member

    I use Toastman's Tomato and I had similar issues. Noting what shibby20 said above, I have placed the following lines in the "WAN Up" script area.

    Code:
    # NB - Added 18/04/14 (Own script fix)
    #  Idea from here "http://www.linksysinfo.org/index.php?threads/ipkg-web-bad-coded.38195/"
    #
    sleep 10
    cp /opt/share/www/sluginfo.cgi /www/ext/cgi-bin
    cp /opt/share/www/package.cgi /www/ext/cgi-bin
    I don't even know whether the sleep is needed (but it seemed logical - assuming "WAN Up" execution follows execution of "Init" script). It seems to have fixed it and the links (checked remotely via Windows shortcuts) persist after the router reboots. There's probably a better solution but I don't know enough linux!

    I also installed lighttpd and bash. One of them (I don't know which) seemed to screw-up "nvram show" results when entered via the "System" menu (that's how I noticed). I removed both of them and this tool is great now! The second URL is http://192.168.1.1/ext/cgi-bin/sluginfo.cgi

    [​IMG]

    [​IMG]

    Latest script (with "How to" comments for me) after installing "Heartbleed-safe" v7505

    Code:
    # ------------------------------------------------------------------------------ #
    # NB - Added 20/04/14 - Make "ipkg web" scripts available (to Windows shortcuts)
    # ------------------------------------------------------------------------------ #
    #  Idea from here "http://www.linksysinfo.org/index.php?threads/ipkg-web-bad-coded.38195/"
    #
    #  Run "find / -name *.cgi" from SSH terminal to locate SOURCE files
    #  Copy "sluginfo.cgi" & "package.cgi" somewhere safe (I used "/opt") in case they get "lost" (during
    #   router reboot).
    #  If "lost", re-install "ipkg-web" (from SSH terminal "ipkg remove ipkg-web" followed by
    #   "ipkg install ipkg-web") and run "find / -name *.cgi" again.
    #  Resulting URLs:
    #   "http://http://192.168.1.1/ext/cgi-bin/package.cgi"
    #   "http://http://192.168.1.1/ext/cgi-bin/sluginfo.cgi"
    #
    sleep 10
    cp /opt/sluginfo.cgi /www/ext/cgi-bin
    cp /opt/package.cgi /www/ext/cgi-bin
     
    Last edited: Apr 22, 2014

Share This Page