[dss-developer] ds3

Hofmann, Christoph christoph.hofmann at aizo.com
Sun Apr 17 15:07:19 CEST 2011


Hallo Dieter,

> Da komme ich nicht sehr weit, nämlich bis zur Fehlermeldung "HTTP
> request failed! HTTP/1.0 401 Unauthorized".
> 
> Ich bräuchte ein Beispiel, um auf diesem Weg weiter zu kommen.

menno, alles muss man selber machen.
Nachdem ich nun das ein oder andere Stündchen meines Sonntags verbastelt habe, hier eine Lösung bis zum erfolgreichen Ausliefern des Cookies.

Folgendes ist zu beachten: Um eine https-Verbindung herzustellen, muss das ssl:// Protokoll bei fsockopen angegeben werden. PHP unterstützt das nur, wenn openssl integriert ist (phpinfo muss openssl enabled ausgeben). Das ist daher unbedingt sicherzustellen.
Weiterhin unterstützt der dSS11 nur die Digest Authentifizierungsmethode, was die Geschichte eine Stufe komplexer macht. Aber auch das geht in PHP relativ einfach.

Nachfolgend mein Beispiel:

----------------------------------------

<?php

function dSS11Login($url, $username, $password)
{

    $urlfrag = parse_url($url);

    if (!$fp=fsockopen("ssl://".$urlfrag['host'], 443, $errno, $errstr, 15))
        return false;

    //first do the non-authenticated header so that the server
    //sends back a 401 error containing its nonce and opaque
    $out = "GET ".$urlfrag['path']."?".$urlfrag['query']." HTTP/1.1\r\n";
    $out .= "Host: ".$urlfrag['host']."\r\n";
    $out .= "Connection: Close\r\n\r\n";

    fwrite($fp, $out);

    //read the reply and look for the WWW-Authenticate element
    while (!feof($fp))
    {
        $line=fgets($fp, 512);

        if (strpos($line,"WWW-Authenticate:")!==false)
            $authline=trim(substr($line,18));
    }

    fclose($fp);

    //split up the WWW-Authenticate string to find digest-realm and nonce
    $authlinearr=explode(",",$authline);
    $autharr=array();

    foreach ($authlinearr as $el)
    {
        $elarr=explode("=",$el);
        //the substr here is used to remove the double quotes from the values
        $autharr[trim($elarr[0])]=substr($elarr[1],1,strlen($elarr[1])-2);
    }

    foreach ($autharr as $k=>$v)
        echo("$k ==> $v\r\n");

    //these are all the vals required from the server
    $nonce=$autharr['nonce'];
    $drealm=$autharr['Digest realm'];

    //client nonce can be anything since this authentication session is not going to be persistent
    //likewise for the cookie - just call it MyCookie
    $cnonce="sausages";

    //calculate the hashes of A1 and A2 as described in RFC 2617
    $a1="$username:$drealm:$password";$a2="GET:".$urlfrag['path'];
    $ha1=md5($a1);$ha2=md5($a2);

    //calculate the response hash as described in RFC 2617
    $concat = $ha1.':'.$nonce.':00000001:'.$cnonce.':auth:'.$ha2;
    $response=md5($concat);

    //put together the Authorization Request Header
    $out = "GET ".$urlfrag['path']."?".$urlfrag['query']." HTTP/1.1\r\n";
    $out .= "Host: ".$urlfrag['host']."\r\n";
    $out .= "Connection: Close\r\n";
    $out .= "Cookie: cookie=MyCookie\r\n";
    $out .= "Authorization: Digest username=\"$username\", realm=\"$drealm\", qop=\"auth\", algorithm=\"MD5\", uri=\"".$urlfrag['path']."\", nonce=\"$nonce\", nc=00000001, cnonce=\"$cnonce\", response=\"$response\"\r\n\r\n";

    if (!$fp=fsockopen("ssl://".$urlfrag['host'], 443, $errno, $errstr, 15))
        return false;

    fwrite($fp, $out);

    $str="";
    //read in a string which is the contents of the required file
    while (!feof($fp))
    {
        $str.=fgets($fp, 512);
    }

    fclose($fp);

    echo $str;
}

dSS11Login("https://192.168.1.100/json/system/login?user=dssadmin&password=dssadmin", "dssadmin", "dssadmin");

?>

----------------------------------------

Bitte nicht vergessen die richtige IP einzusetzen. Die ersten beiden dssadmin sind das Login für die API, die hinteren beiden dssadmin sind das Login für https. Solltest Du die bei Dir geändert haben, musst Du dort natürlich die richtigen Credentials eintragen.
In der letzten Ausgabe findest Du dann das Cookie, welches natürlich weiter zu verarbeiten ist und bei den nachfolgenden Aufrufen mit übergeben werden muss.


Grüße
Christoph


--
Christoph Hofmann
aizo ag, Deutschland - http://www.aizo.ag





More information about the dss-developer mailing list