Für unixartige Betriebssysteme stellt die Bibliothek „libpcap“ (packet capture library) Funktionen zur Verfügung, um den Datenverkehr, welcher über die Netzwerkschnittstellen läuft, mitzuschneiden und zu analysieren. Das JavaScript Modul „pcap“ stellt die entsprechenden Bindungen (engl. Bindings) für diese Bibliothek zur Verfügung. Diese können dann mit Node.js verwendet werden.

Problem

Diese Bibliothek wird gerne in Node.js Modulen, wie z. B. dem Amazon Dashbutton Adapter für ioBroker, verwendet. Da die libpcap Bibliothek auf Funktionen des Betriebssystems zugreift, kann es zu folgender Fehlermeldung kommen, wenn der Code ausgeführt wird.

Error: socket: Operation not permitted

Der Benutzer, durch den der Code ausgeführt wurde, besitzt anscheinend nicht die notwendigen Rechte.

Am Beispiel des Amazon Dashbutton Adapters, würde dann folgende Fehlermeldung im Log auftauchen.

error   at Adapter. (/opt/iobroker/node_modules/iobroker.amazon-dash/main.js:50:29)
error   at Object.exports.createSession (/opt/iobroker/node_modules/iobroker.amazon-dash/node_modules/pcap/pcap.js:123:12)
error   at new PcapSession (/opt/iobroker/node_modules/iobroker.amazon-dash/node_modules/pcap/pcap.js:49:39)
error   Error: socket: Operation not permitted
error   uncaught exception: socket: Operation not permitted

Lösung

Um das Problem zu lösen, müssen die Dateisystem „Capabilities“ für das Programm „node“ angepasst werden. Dies kann durch das Kommandozeilen-Tool „setcap“ erreicht werden.
Hierzu muss folgender Befehl in der Kommandozeile ausgeführt werden.

sudo setcap 'cap_net_raw,cap_net_admin+eip' $(readlink -f $(which node))

Mit dem Tool „getcap“ kann geprüft werden, ob alles korrekt gesetzt wurde. Hierzu muss folgender Befehl in der Kommandozeile ausgeführt werden.

sudo getcap $(readlink -f $(which node))

Dies Ausgabe sollte dann z. B. so aussehen:

/usr/bin/nodejs = cap_net_admin,cap_net_raw+eip

Hier noch eine kurze Erklärung aus der Dokumentation

 In many distributions you can use the 'setcap' utility to add capabilities to individual files.
 CAP_NET_RAW: 
       Allow use of RAW and PACKET sockets.
 CAP_NET_ADMIN:
       Allow various network-related operations (e.g. setting privileged socket options, enabling multicasting, setting promiscuous mode, interface configuration, modifying routing tables).
       The “+eip” means you’re adding the file capability sets Effective, Inherited and Permitted.
       
       Each thread has three capability sets containing zero or more of the above capabilities: 
		 The three file capability sets are:
             e: Effective  -  The capabilities used by the kernel to perform permission checks for the thread. 
             i: Inherited  -  The capabilities preserved across an execve(2). A child created via fork(2) inherits copies of its parent's capability sets. See below for a discussion of the treatment of capabilities during exec(). Using capset(2), a thread may manipulate its own capability sets, or, if it has the CAP_SETPCAP capability, those of a thread in another process.
             p: Permitted  -  The capabilities that the thread may assume (i.e., a limiting superset for the effective and inheritable sets). If a thread drops a capability from its permitted set, it can never re-acquire that capability (unless it exec()s a set-user-ID-root program).


Prüfung

Um zu prüfen, ob das pcap Module korrekt funktioniert, kann die im Module mitgelieferte Bespieldatei „network_grep.js“ über die Kommandozeile ausgeführt werden. Diese Datei befindet sich z. B. im Ordner „node_modules“ unter pcap/examples/network_grep.js

Beispiel Befehl:

node /opt/iobroker/node_modules/pcap/examples/network_grep.js

Hier sollte dann folgender Fehler NICHT mehr auftreten:

/opt/iobroker/node_modules/pcap/pcap.js:49
        this.link_type = this.session.open_live(this.device_name, this.filter, this.buffer_size, this.outfile, packet_ready, this.is_monitor);

Error: socket: Operation not permitted
    at new PcapSession (/opt/iobroker/node_modules/pcap/pcap.js:49:39)
    at Object.exports.createSession (/opt/iobroker/node_modules/pcap/pcap.js:123:12)
    at Object.<anonymous> (/opt/iobroker/node_modules/pcap/examples/network_grep.js:2:25)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:427:7)
Node „pcap“ Module – Error: „socket: Operation not permitted“ – Fehler wenn PcapSession geöffnet wird

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert