summary refs log tree commit diff
path: root/nixos/modules/services/networking/xinetd.nix
blob: 2f527ab156aa79b6aaeca9e8be8e24947bf94700 (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
{ config, lib, pkgs, ... }:

with lib;

let

  cfg = config.services.xinetd;

  configFile = pkgs.writeText "xinetd.conf"
    ''
      defaults
      {
        log_type       = SYSLOG daemon info
        log_on_failure = HOST
        log_on_success = PID HOST DURATION EXIT
        ${cfg.extraDefaults}
      }

      ${concatMapStrings makeService cfg.services}
    '';

  makeService = srv:
    ''
      service ${srv.name}
      {
        protocol    = ${srv.protocol}
        ${optionalString srv.unlisted "type        = UNLISTED"}
        ${optionalString (srv.flags != "") "flags = ${srv.flags}"}
        socket_type = ${if srv.protocol == "udp" then "dgram" else "stream"}
        ${if srv.port != 0 then "port        = ${toString srv.port}" else ""}
        wait        = ${if srv.protocol == "udp" then "yes" else "no"}
        user        = ${srv.user}
        server      = ${srv.server}
        ${optionalString (srv.serverArgs != "") "server_args = ${srv.serverArgs}"}
        ${srv.extraConfig}
      }
    '';

in

{

  ###### interface

  options = {

    services.xinetd.enable = mkEnableOption "the xinetd super-server daemon";

    services.xinetd.extraDefaults = mkOption {
      default = "";
      type = types.lines;
      description = ''
        Additional configuration lines added to the default section of xinetd's configuration.
      '';
    };

    services.xinetd.services = mkOption {
      default = [];
      description = ''
        A list of services provided by xinetd.
      '';

      type = with types; listOf (submodule ({

        options = {

          name = mkOption {
            type = types.str;
            example = "login";
            description = "Name of the service.";
          };

          protocol = mkOption {
            type = types.str;
            default = "tcp";
            description =
              "Protocol of the service.  Usually <literal>tcp</literal> or <literal>udp</literal>.";
          };

          port = mkOption {
            type = types.int;
            default = 0;
            example = 123;
            description = "Port number of the service.";
          };

          user = mkOption {
            type = types.str;
            default = "nobody";
            description = "User account for the service";
          };

          server = mkOption {
            type = types.str;
            example = "/foo/bin/ftpd";
            description = "Path of the program that implements the service.";
          };

          serverArgs = mkOption {
            type = types.separatedString " ";
            default = "";
            description = "Command-line arguments for the server program.";
          };

          flags = mkOption {
            type = types.str;
            default = "";
            description = "";
          };

          unlisted = mkOption {
            type = types.bool;
            default = false;
            description = ''
              Whether this server is listed in
              <filename>/etc/services</filename>.  If so, the port
              number can be omitted.
            '';
          };

          extraConfig = mkOption {
            type = types.lines;
            default = "";
            description = "Extra configuration-lines added to the section of the service.";
          };

        };

      }));

    };

  };


  ###### implementation

  config = mkIf cfg.enable {
    systemd.services.xinetd = {
      description = "xinetd server";
      after = [ "network.target" ];
      wantedBy = [ "multi-user.target" ];
      path = [ pkgs.xinetd ];
      script = "exec xinetd -syslog daemon -dontfork -stayalive -f ${configFile}";
    };
  };
}