about summary refs log tree commit diff
path: root/nixos/modules/services/networking/jigasi.nix
blob: e701689031b142ea180300138db583aa5cbd2e9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
{ config, lib, pkgs, ... }:

with lib;

let
  cfg = config.services.jigasi;
  homeDirName = "jigasi-home";
  stateDir = "/tmp";
  sipCommunicatorPropertiesFile = "${stateDir}/${homeDirName}/sip-communicator.properties";
  sipCommunicatorPropertiesFileUnsubstituted = "${pkgs.jigasi}/etc/jitsi/jigasi/sip-communicator.properties";
in
{
  options.services.jigasi = with types; {
    enable = mkEnableOption "Jitsi Gateway to SIP - component of Jitsi Meet";

    xmppHost = mkOption {
      type = str;
      example = "localhost";
      description = ''
        Hostname of the XMPP server to connect to.
      '';
    };

    xmppDomain = mkOption {
      type = nullOr str;
      example = "meet.example.org";
      description = ''
        Domain name of the XMMP server to which to connect as a component.

        If null, <option>xmppHost</option> is used.
      '';
    };

    componentPasswordFile = mkOption {
      type = str;
      example = "/run/keys/jigasi-component";
      description = ''
        Path to file containing component secret.
      '';
    };

    userName = mkOption {
      type = str;
      default = "callcontrol";
      description = ''
        User part of the JID for XMPP user connection.
      '';
    };

    userDomain = mkOption {
      type = str;
      example = "internal.meet.example.org";
      description = ''
        Domain part of the JID for XMPP user connection.
      '';
    };

    userPasswordFile = mkOption {
      type = str;
      example = "/run/keys/jigasi-user";
      description = ''
        Path to file containing password for XMPP user connection.
      '';
    };

    bridgeMuc = mkOption {
      type = str;
      example = "jigasibrewery@internal.meet.example.org";
      description = ''
        JID of the internal MUC used to communicate with Videobridges.
      '';
    };

    defaultJvbRoomName = mkOption {
      type = str;
      default = "";
      example = "siptest";
      description = ''
        Name of the default JVB room that will be joined if no special header is included in SIP invite.
      '';
    };

    environmentFile = mkOption {
      type = types.nullOr types.path;
      default = null;
      description = ''
        File containing environment variables to be passed to the jigasi service,
        in which secret tokens can be specified securely by defining values for
        <literal>JIGASI_SIPUSER</literal>,
        <literal>JIGASI_SIPPWD</literal>,
        <literal>JIGASI_SIPSERVER</literal> and
        <literal>JIGASI_SIPPORT</literal>.
      '';
    };

    config = mkOption {
      type = attrsOf str;
      default = { };
      example = literalExpression ''
        {
          "org.jitsi.jigasi.auth.URL" = "XMPP:jitsi-meet.example.com";
        }
      '';
      description = ''
        Contents of the <filename>sip-communicator.properties</filename> configuration file for jigasi.
      '';
    };
  };

  config = mkIf cfg.enable {
    services.jicofo.config = {
      "org.jitsi.jicofo.jigasi.BREWERY" = "${cfg.bridgeMuc}";
    };

    services.jigasi.config = mapAttrs (_: v: mkDefault v) {
      "org.jitsi.jigasi.BRIDGE_MUC" = cfg.bridgeMuc;
    };

    users.groups.jitsi-meet = {};

    systemd.services.jigasi = let
      jigasiProps = {
        "-Dnet.java.sip.communicator.SC_HOME_DIR_LOCATION" = "${stateDir}";
        "-Dnet.java.sip.communicator.SC_HOME_DIR_NAME" = "${homeDirName}";
        "-Djava.util.logging.config.file" = "${pkgs.jigasi}/etc/jitsi/jigasi/logging.properties";
      };
    in
    {
      description = "Jitsi Gateway to SIP";
      wantedBy = [ "multi-user.target" ];
      after = [ "network.target" ];

      preStart = ''
        [ -f "${sipCommunicatorPropertiesFile}" ] && rm -f "${sipCommunicatorPropertiesFile}"
        mkdir -p "$(dirname ${sipCommunicatorPropertiesFile})"
        temp="${sipCommunicatorPropertiesFile}.unsubstituted"

        export DOMAIN_BASE="${cfg.xmppDomain}"
        export JIGASI_XMPP_PASSWORD=$(cat "${cfg.userPasswordFile}")
        export JIGASI_DEFAULT_JVB_ROOM_NAME="${cfg.defaultJvbRoomName}"

        # encode the credentials to base64
        export JIGASI_SIPPWD=$(echo -n "$JIGASI_SIPPWD" | base64 -w 0)
        export JIGASI_XMPP_PASSWORD_BASE64=$(cat "${cfg.userPasswordFile}" | base64 -w 0)

        cp "${sipCommunicatorPropertiesFileUnsubstituted}" "$temp"
        chmod 644 "$temp"
        cat <<EOF >>"$temp"
        net.java.sip.communicator.impl.protocol.sip.acc1403273890647.SERVER_PORT=$JIGASI_SIPPORT
        net.java.sip.communicator.impl.protocol.sip.acc1403273890647.PREFERRED_TRANSPORT=udp
        EOF
        chmod 444 "$temp"

        # Replace <<$VAR_NAME>> from example config to $VAR_NAME for environment substitution
        sed -i -E \
          's/<<([^>]+)>>/\$\1/g' \
          "$temp"

        sed -i \
          's|\(net\.java\.sip\.communicator\.impl\.protocol\.jabber\.acc-xmpp-1\.PASSWORD=\).*|\1\$JIGASI_XMPP_PASSWORD_BASE64|g' \
          "$temp"

        sed -i \
          's|\(#\)\(org.jitsi.jigasi.DEFAULT_JVB_ROOM_NAME=\).*|\2\$JIGASI_DEFAULT_JVB_ROOM_NAME|g' \
          "$temp"

        ${pkgs.envsubst}/bin/envsubst \
          -o "${sipCommunicatorPropertiesFile}" \
          -i "$temp"

        # Set the brewery room name
        sed -i \
          's|\(net\.java\.sip\.communicator\.impl\.protocol\.jabber\.acc-xmpp-1\.BREWERY=\).*|\1${cfg.bridgeMuc}|g' \
          "${sipCommunicatorPropertiesFile}"
        sed -i \
          's|\(org\.jitsi\.jigasi\.ALLOWED_JID=\).*|\1${cfg.bridgeMuc}|g' \
          "${sipCommunicatorPropertiesFile}"


        # Disable certificate verification for self-signed certificates
        sed -i \
          's|\(# \)\(net.java.sip.communicator.service.gui.ALWAYS_TRUST_MODE_ENABLED=true\)|\2|g' \
          "${sipCommunicatorPropertiesFile}"
      '';

      restartTriggers = [
        config.environment.etc."jitsi/jigasi/sip-communicator.properties".source
      ];
      environment.JAVA_SYS_PROPS = concatStringsSep " " (mapAttrsToList (k: v: "${k}=${toString v}") jigasiProps);

      script = ''
        ${pkgs.jigasi}/bin/jigasi \
          --host="${cfg.xmppHost}" \
          --domain="${if cfg.xmppDomain == null then cfg.xmppHost else cfg.xmppDomain}" \
          --secret="$(cat ${cfg.componentPasswordFile})" \
          --user_name="${cfg.userName}" \
          --user_domain="${cfg.userDomain}" \
          --user_password="$(cat ${cfg.userPasswordFile})" \
          --configdir="${stateDir}" \
          --configdirname="${homeDirName}"
      '';

      serviceConfig = {
        Type = "exec";

        DynamicUser = true;
        User = "jigasi";
        Group = "jitsi-meet";

        CapabilityBoundingSet = "";
        NoNewPrivileges = true;
        ProtectSystem = "strict";
        ProtectHome = true;
        PrivateTmp = true;
        PrivateDevices = true;
        ProtectHostname = true;
        ProtectKernelTunables = true;
        ProtectKernelModules = true;
        ProtectControlGroups = true;
        RestrictAddressFamilies = [ "AF_INET" "AF_INET6" "AF_UNIX" ];
        RestrictNamespaces = true;
        LockPersonality = true;
        RestrictRealtime = true;
        RestrictSUIDSGID = true;
        StateDirectory = baseNameOf stateDir;
        EnvironmentFile = cfg.environmentFile;
      };
    };

    environment.etc."jitsi/jigasi/sip-communicator.properties".source =
      mkDefault "${sipCommunicatorPropertiesFile}";
    environment.etc."jitsi/jigasi/logging.properties".source =
      mkDefault "${stateDir}/logging.properties-journal";
  };

  meta.maintainers = lib.teams.jitsi.members;
}