firewalld dbus接口使用指南

firewalld,一个基于动态区的iptables/nftables守护程序,自2009年左右开始开发,CentOS7基于 firewalld-0.6.3 , 发布于2018年10月11日。主要的开发人员是托马斯·沃纳,他目前为红帽公司工作。这是因为为Federal 18 的默认防火墙机制, 随后在 Rhel7 和 Centos 7 中使用。

firewalld相对于旧的 iptables-service 机制有许多优势。值得注意的是,它解决了 iptables 要求每次更改时重新启动防火墙的问题,从而中断了任何状态连接。它还提供了丰富的 D-Bus 方法、信号和属性。

这里并不是从firewalld操作使用方式来介绍firewalld,想反,是介绍 firewalld D-Bus API来检索信息或更改设置。

firewalld被配置为系统 D-Bus 服务,注意看 systemd file中的"Type=dbus"参数。

# cat /usr/lib/systemd/system/firewalld.service
[Unit]
Description=firewalld - dynamic firewall daemon
Before=network-pre.target
Wants=network-pre.target
After=dbus.service
After=polkit.service
Conflicts=iptables.service ip6tables.service ebtables.service ipset.service
Documentation=man:firewalld(1)[Service]
EnvironmentFile=-/etc/sysconfig/firewalld
ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS
ExecReload=/bin/kill -HUP $MAINPID
# supress to log debug and error output also to /var/log/messages
StandardOutput=null
StandardError=null
Type=dbus
BusName=org.fedoraproject.FirewallD1
KillMode=mixed[Install]
WantedBy=multi-user.target
Alias=dbus-org.fedoraproject.FirewallD1.service

实际上,手动运行 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid --debug 效果是一样的,这里的注册是通过dbus 高级API操作的。

此时由于已经了解到了,firewalld 服务 是基于D-Bus接口的,所以需要找到对应的 dbus interface

dbus-send --system --dest=org.freedesktop.DBus \--type=method_call --print-reply \/org/freedesktop/DBus org.freedesktop.DBus.ListNames | grep FirewallD

org.fedoraproject.FirewallD1 这个就是firewalld注册的dbus interface了。

dbus-send 命令可以向 D-Bus消息总线发送消息并显示该消息的返回结果。有两个众所周知的消息总线:system bus(Option -System) 和每个用户session bus( -session)。使用 firewall-cmd 也是通过 dbus interface 进行交互的。在使用dbus-send 时,必须指定其对应的消息接口 -dest,该参数是连接到对应总线上的接口名称,以将消息发送到对应的dbus firewalld-server进行对应iptables规则的翻译。

现在有了dbus接口,需要了解改接口支持的方法 methods,属性 properties,信号signals 等信息。

dbus-send --system --dest=org.fedoraproject.FirewallD1 --print-reply \/org/fedoraproject/FirewallD1 \org.freedesktop.DBus.Introspectable.Introspect

通过上述输出列出了通过防火墙 D-Bus 接口提供的所有方法、单一和属性。这是基于D-Bus DTD 的输出格式。所有 dbus服务都需要实现 org.freedesktop.DBus.Introspectable.Introspect 方法。

知道了 方法 属性 信号,就可以直接对firewalld进行一个操作了。现在开始第一个例子。获取默认zone。

# firewall-cmd --get-default-zonedbus-send --system --dest=org.fedoraproject.FirewallD1 \--print-reply --type=method_call \ /org/fedoraproject/FirewallD1 \org.fedoraproject.FirewallD1.getDefaultZone

通过dbus接口来检索区域列表

# firewall-cmd --get-zonesdbus-send --system \--dest=org.fedoraproject.FirewallD1 \--print-reply --type=method_call \ /org/fedoraproject/FirewallD1 \org.fedoraproject.FirewallD1.zone.getZones

最常用的命令:查看当前zone所有策略

# firewall-cmd --zone=public --list-alldbus-send --system \--dest=org.fedoraproject.FirewallD1 \--print-reply --type=method_call \/org/fedoraproject/FirewallD1 \org.fedoraproject.FirewallD1.getZoneSettings string:"public"

获得inerface的properties

其实这里在命令行根本用不到,但是在封装时却会可以用到。

dbus-send --system \--print-reply --dest=org.fedoraproject.FirewallD1 \/org/fedoraproject/FirewallD1 \org.freedesktop.DBus.Properties.GetAll string:"org.fedoraproject.FirewallD1"

还可以通过其他的接口来查看对应的属性值

dbus-send --system --print-reply
--dest=org.fedoraproject.FirewallD1 \/org/fedoraproject/FirewallD1 \org.freedesktop.DBus.Properties.Get \string:"org.fedoraproject.FirewallD1" \string:"version"# dbus-send --system --print-reply \--dest=org.fedoraproject.FirewallD1 \/org/fedoraproject/FirewallD1 org.freedesktop.DBus.Properties.Get \string:"org.fedoraproject.FirewallD1" \string:"interface_version"# dbus-send --system --print-reply \--dest=org.fedoraproject.FirewallD1 \/org/fedoraproject/FirewallD1 \org.freedesktop.DBus.Properties.Get \string:"org.fedoraproject.FirewallD1" \string:"state"# dbus-send --system --print-reply=literal \--dest=org.fedoraproject.FirewallD1 \/org/fedoraproject/FirewallD1 \org.freedesktop.DBus.Properties.Get \string:"org.fedoraproject.FirewallD1" \string:"state"

查询规则

查询接口

dbus-send --system \--dest=org.fedoraproject.FirewallD1 \--print-reply \--type=method_call \/org/fedoraproject/FirewallD1 \org.fedoraproject.FirewallD1.zone.getZoneOfInterface \string:"eth0"

创建一个新zone

dbus-send --session \--dest=org.freedesktop.DBus \--type=method_call \--print-reply /org/freedesktop/DBus  \org.fedoraproject.FirewallD1.config.addZone \string:"testapi"

获得一个zone的所有规则(zonesettings

dbus-send --system \--dest=org.fedoraproject.FirewallD1  \--type=method_call \--print-reply /org/fedoraproject/FirewallD1  \org.fedoraproject.FirewallD1.getZoneSettings \string:"public"

添加一个port

dbus-send --system \--dest=org.fedoraproject.FirewallD1 \--print-reply --type=method_call \/org/fedoraproject/FirewallD1 \org.fedoraproject.FirewallD1.zone.addPort \string:"public" \string:"81" \string:"tcp" \uint64:300

对应设置firewalld 面板所有属性的命令

firewall-cmd --zone=public --change-interface=eth0firewall-cmd --zone=public --add-masquerade
firewall-cmd --zone=public --add-forward-port=port=1122:proto=tcp:toport=22:toaddr=192.168.100.3
firewall-cmd --zone=public --add-forward-port=port=1122:proto=tcp:toport=22:toaddr=10.0.0.3firewall-cmd --add-protocol=tcp
firewall-cmd --add-protocol=udpfirewall-cmd --add-icmp-blocks=icmp
firewall-cmd --set-target=DROPfirewall-cmd --add-icmp-block=redirect
firewall-cmd --add-icmp-block=network-unknownfirewall-cmd --add-source-port=80/tcp
firewall-cmd --add-source-port=100/tcpfirewall-cmd --add-source=10.0.0.1
firewall-cmd --add-source=10.0.0.2firewall-cmd --add-rich-rule='rule family=ipv4 source address=192.168.1.101/32 service name=telnet limit value=1/m accept'firewall-cmd --add-icmp-block-inversionfirewall-cmd --new-zone=123 --permanen

执行远程命令

dbus接口支持远程命令的,通过dbus-send发送时,根据配置dbus的监听来完成远程的操作

DBUS_SESSION_BUS_ADDRESS=tcp:host=10.0.0.3,port=55557

根据上述,参考加上官方文档,了解如何通过D-Bus接口操作FirewallD,虽然此处是使用了 dbus-send,但是也可以通过 qt 或者 其他的来管理 基于 dbus api的应用了。

dbus客户端使用指南相关推荐

  1. sqoop2 java api实现_Sqoop2 Java客户端API指南

    原文连接:http://sqoop.apache.org/docs/1.99.6/ClientAPI.html Sqoop Java客户端API指南 这篇文章秒描述了额如何在外部应用中使用sqoop ...

  2. 极光小课堂 | 极光推送之 Android 客户端使用指南——基础篇

    " 本文中涉及到的所有代码现已在 Github 上开源,地址:https://github.com/xuexiangjys/JPushSample" 01 前言 - 极光推送是国内 ...

  3. Windows平台下Mediasoup客户端开发指南

    操作系统:Windows 10 IDE: Visual Studio 2019 GitHub:https://github.com/versatica/libmediasoupclient/ 官网文档 ...

  4. linux上网的解决方案-Dr.comlinux下的客户端使用指南

    这篇文章我贴了好多地方了,呵呵,希望新学期开学大家能上网了~ 用windows的也建议换成这个开源版本的for windows版,不会默认弹出学校的首页,并且支持共享上网` 如果你只是挂qq,上上网的 ...

  5. 用WinInet开发Internet客户端应用指南

    2019独角兽企业重金招聘Python工程师标准>>> 概述 一个Internet客户端程序的目的是通过Internet协议如:HTTP.FTP等来存取网络数据源(服务器)的信息.客 ...

  6. apollo local 模式_Java客户端使用指南 - 五、本地开发模式 - 《携程 Apollo v1.4 开发指南》 - 书栈网 · BookStack...

    五.本地开发模式 Apollo客户端还支持本地开发模式,这个主要用于当开发环境无法连接Apollo服务器的时候,比如在邮轮.飞机上做相关功能开发. 在本地开发模式下,Apollo只会从本地文件读取配置 ...

  7. apollo java客户端_02、携程Apollo Java客户端使用指南

    一. 客户端设计 image.png 上图简要描述了Apollo客户端的实现原理 1.客户端和服务端保持了一个长连接,从而能第一时间获得配置更新的推送.(通过Http Long Polling实现) ...

  8. Ubuntu安装SS及win10下客户端使用指南

    说明 该笔记仅用作学习交流使用 环境说明 服务端:Ubuntu4.04( x86_64  linux-3.13.0-160-generic)    客户端:win10 服务端安装 apt-get in ...

  9. linux syslog客户端,syslog服务器及客户端配置指南

    第一部分 syslog服务器配置 操作系统:windows server 2008 R2 syslog软件:KIWI syslog 1.安装完成后,不需要特殊的配置,在kiwi syslog serv ...

最新文章

  1. 火狐浏览器下DIV不能居中的解决办法
  2. Spark:获取dataframe某列最大值
  3. socket中的nagle算法
  4. sqlite3移植到arm linux
  5. 比特币可视化工具_这个比特币交易“可视化”网站,用一辆公交车带你“上车”...
  6. java 蓝桥杯算法训练 秘密行动
  7. Android 发送邮件信息,附带附件
  8. python中seaborn库_GitHub - a13544835729/python-seaborn: python seaborn库基础用法
  9. 视频码率,帧率和分辨率的联系与差别
  10. css border 圆角气泡案例
  11. littlevgl之roller 滚动轴控件
  12. si4438+efm32g210f128
  13. 2021年春季学期期末统一考试电子商务概论(农) 试题
  14. 面试官:聊聊二维码扫码登录的原理
  15. 遇到问题你的PIN不可用,请单击以重置和bitlocker恢复密匙
  16. ios html格式转换,如何使用HTML模版和iOS中的UIPrintPageRenderer来生成PDF文档
  17. 【数据库系统原理】数据库课内实验
  18. 酷我音乐linux版本,酷我音乐盒的 Gtk/Linux 实现 – v2.5 版本发布
  19. SpringBoot配置过滤器和拦截器
  20. Scratch少儿编程系列目录

热门文章

  1. Unity的VRTK捡拾物体学习笔记
  2. dateformat java_java中Dateformat类的详细使用(详解)
  3. 海思linux中编译,基于海思开发环境,交叉编译,安装tslib库
  4. 每日一书丨机器人数量增长不会减少人类就业机会,反会促增岗位数量?
  5. 一睹为快,推特NFT网红热议的Bored Ape是个啥?
  6. windows下符号的快捷输入
  7. 零基础入门产品经理(视频课程),有需要的请加微信或qq
  8. Eclipse syntax coloring java xml 语法 样式
  9. 我叫MT online刷精英方砖攻略
  10. 炮兵阵地(状态压缩DP)