iptables.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package iptables
  15. import (
  16. "bytes"
  17. "fmt"
  18. "io"
  19. "net"
  20. "os/exec"
  21. "regexp"
  22. "strconv"
  23. "strings"
  24. "syscall"
  25. )
  26. // Adds the output of stderr to exec.ExitError
  27. type Error struct {
  28. exec.ExitError
  29. cmd exec.Cmd
  30. msg string
  31. exitStatus *int //for overriding
  32. }
  33. func (e *Error) ExitStatus() int {
  34. if e.exitStatus != nil {
  35. return *e.exitStatus
  36. }
  37. return e.Sys().(syscall.WaitStatus).ExitStatus()
  38. }
  39. func (e *Error) Error() string {
  40. return fmt.Sprintf("running %v: exit status %v: %v", e.cmd.Args, e.ExitStatus(), e.msg)
  41. }
  42. // IsNotExist returns true if the error is due to the chain or rule not existing
  43. func (e *Error) IsNotExist() bool {
  44. if e.ExitStatus() != 1 {
  45. return false
  46. }
  47. msgNoRuleExist := "Bad rule (does a matching rule exist in that chain?).\n"
  48. msgNoChainExist := "No chain/target/match by that name.\n"
  49. return strings.Contains(e.msg, msgNoRuleExist) || strings.Contains(e.msg, msgNoChainExist)
  50. }
  51. // Protocol to differentiate between IPv4 and IPv6
  52. type Protocol byte
  53. const (
  54. ProtocolIPv4 Protocol = iota
  55. ProtocolIPv6
  56. )
  57. type IPTables struct {
  58. path string
  59. proto Protocol
  60. hasCheck bool
  61. hasWait bool
  62. waitSupportSecond bool
  63. hasRandomFully bool
  64. v1 int
  65. v2 int
  66. v3 int
  67. mode string // the underlying iptables operating mode, e.g. nf_tables
  68. timeout int // time to wait for the iptables lock, default waits forever
  69. }
  70. // Stat represents a structured statistic entry.
  71. type Stat struct {
  72. Packets uint64 `json:"pkts"`
  73. Bytes uint64 `json:"bytes"`
  74. Target string `json:"target"`
  75. Protocol string `json:"prot"`
  76. Opt string `json:"opt"`
  77. Input string `json:"in"`
  78. Output string `json:"out"`
  79. Source *net.IPNet `json:"source"`
  80. Destination *net.IPNet `json:"destination"`
  81. Options string `json:"options"`
  82. }
  83. type option func(*IPTables)
  84. func IPFamily(proto Protocol) option {
  85. return func(ipt *IPTables) {
  86. ipt.proto = proto
  87. }
  88. }
  89. func Timeout(timeout int) option {
  90. return func(ipt *IPTables) {
  91. ipt.timeout = timeout
  92. }
  93. }
  94. // New creates a new IPTables configured with the options passed as parameter.
  95. // For backwards compatibility, by default always uses IPv4 and timeout 0.
  96. // i.e. you can create an IPv6 IPTables using a timeout of 5 seconds passing
  97. // the IPFamily and Timeout options as follow:
  98. // ip6t := New(IPFamily(ProtocolIPv6), Timeout(5))
  99. func New(opts ...option) (*IPTables, error) {
  100. ipt := &IPTables{
  101. proto: ProtocolIPv4,
  102. timeout: 0,
  103. }
  104. for _, opt := range opts {
  105. opt(ipt)
  106. }
  107. path, err := exec.LookPath(getIptablesCommand(ipt.proto))
  108. if err != nil {
  109. return nil, err
  110. }
  111. ipt.path = path
  112. vstring, err := getIptablesVersionString(path)
  113. if err != nil {
  114. return nil, fmt.Errorf("could not get iptables version: %v", err)
  115. }
  116. v1, v2, v3, mode, err := extractIptablesVersion(vstring)
  117. if err != nil {
  118. return nil, fmt.Errorf("failed to extract iptables version from [%s]: %v", vstring, err)
  119. }
  120. ipt.v1 = v1
  121. ipt.v2 = v2
  122. ipt.v3 = v3
  123. ipt.mode = mode
  124. checkPresent, waitPresent, waitSupportSecond, randomFullyPresent := getIptablesCommandSupport(v1, v2, v3)
  125. ipt.hasCheck = checkPresent
  126. ipt.hasWait = waitPresent
  127. ipt.waitSupportSecond = waitSupportSecond
  128. ipt.hasRandomFully = randomFullyPresent
  129. return ipt, nil
  130. }
  131. // New creates a new IPTables for the given proto.
  132. // The proto will determine which command is used, either "iptables" or "ip6tables".
  133. func NewWithProtocol(proto Protocol) (*IPTables, error) {
  134. return New(IPFamily(proto), Timeout(0))
  135. }
  136. // Proto returns the protocol used by this IPTables.
  137. func (ipt *IPTables) Proto() Protocol {
  138. return ipt.proto
  139. }
  140. // Exists checks if given rulespec in specified table/chain exists
  141. func (ipt *IPTables) Exists(table, chain string, rulespec ...string) (bool, error) {
  142. if !ipt.hasCheck {
  143. return ipt.existsForOldIptables(table, chain, rulespec)
  144. }
  145. cmd := append([]string{"-t", table, "-C", chain}, rulespec...)
  146. err := ipt.run(cmd...)
  147. eerr, eok := err.(*Error)
  148. switch {
  149. case err == nil:
  150. return true, nil
  151. case eok && eerr.ExitStatus() == 1:
  152. return false, nil
  153. default:
  154. return false, err
  155. }
  156. }
  157. // Insert inserts rulespec to specified table/chain (in specified pos)
  158. func (ipt *IPTables) Insert(table, chain string, pos int, rulespec ...string) error {
  159. cmd := append([]string{"-t", table, "-I", chain, strconv.Itoa(pos)}, rulespec...)
  160. return ipt.run(cmd...)
  161. }
  162. // Append appends rulespec to specified table/chain
  163. func (ipt *IPTables) Append(table, chain string, rulespec ...string) error {
  164. cmd := append([]string{"-t", table, "-A", chain}, rulespec...)
  165. return ipt.run(cmd...)
  166. }
  167. // AppendUnique acts like Append except that it won't add a duplicate
  168. func (ipt *IPTables) AppendUnique(table, chain string, rulespec ...string) error {
  169. exists, err := ipt.Exists(table, chain, rulespec...)
  170. if err != nil {
  171. return err
  172. }
  173. if !exists {
  174. return ipt.Append(table, chain, rulespec...)
  175. }
  176. return nil
  177. }
  178. // Delete removes rulespec in specified table/chain
  179. func (ipt *IPTables) Delete(table, chain string, rulespec ...string) error {
  180. cmd := append([]string{"-t", table, "-D", chain}, rulespec...)
  181. return ipt.run(cmd...)
  182. }
  183. func (ipt *IPTables) DeleteIfExists(table, chain string, rulespec ...string) error {
  184. exists, err := ipt.Exists(table, chain, rulespec...)
  185. if err == nil && exists {
  186. err = ipt.Delete(table, chain, rulespec...)
  187. }
  188. return err
  189. }
  190. // List rules in specified table/chain
  191. func (ipt *IPTables) List(table, chain string) ([]string, error) {
  192. args := []string{"-t", table, "-S", chain}
  193. return ipt.executeList(args)
  194. }
  195. // List rules (with counters) in specified table/chain
  196. func (ipt *IPTables) ListWithCounters(table, chain string) ([]string, error) {
  197. args := []string{"-t", table, "-v", "-S", chain}
  198. return ipt.executeList(args)
  199. }
  200. // ListChains returns a slice containing the name of each chain in the specified table.
  201. func (ipt *IPTables) ListChains(table string) ([]string, error) {
  202. args := []string{"-t", table, "-S"}
  203. result, err := ipt.executeList(args)
  204. if err != nil {
  205. return nil, err
  206. }
  207. // Iterate over rules to find all default (-P) and user-specified (-N) chains.
  208. // Chains definition always come before rules.
  209. // Format is the following:
  210. // -P OUTPUT ACCEPT
  211. // -N Custom
  212. var chains []string
  213. for _, val := range result {
  214. if strings.HasPrefix(val, "-P") || strings.HasPrefix(val, "-N") {
  215. chains = append(chains, strings.Fields(val)[1])
  216. } else {
  217. break
  218. }
  219. }
  220. return chains, nil
  221. }
  222. // '-S' is fine with non existing rule index as long as the chain exists
  223. // therefore pass index 1 to reduce overhead for large chains
  224. func (ipt *IPTables) ChainExists(table, chain string) (bool, error) {
  225. err := ipt.run("-t", table, "-S", chain, "1")
  226. eerr, eok := err.(*Error)
  227. switch {
  228. case err == nil:
  229. return true, nil
  230. case eok && eerr.ExitStatus() == 1:
  231. return false, nil
  232. default:
  233. return false, err
  234. }
  235. }
  236. // Stats lists rules including the byte and packet counts
  237. func (ipt *IPTables) Stats(table, chain string) ([][]string, error) {
  238. args := []string{"-t", table, "-L", chain, "-n", "-v", "-x"}
  239. lines, err := ipt.executeList(args)
  240. if err != nil {
  241. return nil, err
  242. }
  243. appendSubnet := func(addr string) string {
  244. if strings.IndexByte(addr, byte('/')) < 0 {
  245. if strings.IndexByte(addr, '.') < 0 {
  246. return addr + "/128"
  247. }
  248. return addr + "/32"
  249. }
  250. return addr
  251. }
  252. ipv6 := ipt.proto == ProtocolIPv6
  253. rows := [][]string{}
  254. for i, line := range lines {
  255. // Skip over chain name and field header
  256. if i < 2 {
  257. continue
  258. }
  259. // Fields:
  260. // 0=pkts 1=bytes 2=target 3=prot 4=opt 5=in 6=out 7=source 8=destination 9=options
  261. line = strings.TrimSpace(line)
  262. fields := strings.Fields(line)
  263. // The ip6tables verbose output cannot be naively split due to the default "opt"
  264. // field containing 2 single spaces.
  265. if ipv6 {
  266. // Check if field 6 is "opt" or "source" address
  267. dest := fields[6]
  268. ip, _, _ := net.ParseCIDR(dest)
  269. if ip == nil {
  270. ip = net.ParseIP(dest)
  271. }
  272. // If we detected a CIDR or IP, the "opt" field is empty.. insert it.
  273. if ip != nil {
  274. f := []string{}
  275. f = append(f, fields[:4]...)
  276. f = append(f, " ") // Empty "opt" field for ip6tables
  277. f = append(f, fields[4:]...)
  278. fields = f
  279. }
  280. }
  281. // Adjust "source" and "destination" to include netmask, to match regular
  282. // List output
  283. fields[7] = appendSubnet(fields[7])
  284. fields[8] = appendSubnet(fields[8])
  285. // Combine "options" fields 9... into a single space-delimited field.
  286. options := fields[9:]
  287. fields = fields[:9]
  288. fields = append(fields, strings.Join(options, " "))
  289. rows = append(rows, fields)
  290. }
  291. return rows, nil
  292. }
  293. // ParseStat parses a single statistic row into a Stat struct. The input should
  294. // be a string slice that is returned from calling the Stat method.
  295. func (ipt *IPTables) ParseStat(stat []string) (parsed Stat, err error) {
  296. // For forward-compatibility, expect at least 10 fields in the stat
  297. if len(stat) < 10 {
  298. return parsed, fmt.Errorf("stat contained fewer fields than expected")
  299. }
  300. // Convert the fields that are not plain strings
  301. parsed.Packets, err = strconv.ParseUint(stat[0], 0, 64)
  302. if err != nil {
  303. return parsed, fmt.Errorf(err.Error(), "could not parse packets")
  304. }
  305. parsed.Bytes, err = strconv.ParseUint(stat[1], 0, 64)
  306. if err != nil {
  307. return parsed, fmt.Errorf(err.Error(), "could not parse bytes")
  308. }
  309. _, parsed.Source, err = net.ParseCIDR(stat[7])
  310. if err != nil {
  311. return parsed, fmt.Errorf(err.Error(), "could not parse source")
  312. }
  313. _, parsed.Destination, err = net.ParseCIDR(stat[8])
  314. if err != nil {
  315. return parsed, fmt.Errorf(err.Error(), "could not parse destination")
  316. }
  317. // Put the fields that are strings
  318. parsed.Target = stat[2]
  319. parsed.Protocol = stat[3]
  320. parsed.Opt = stat[4]
  321. parsed.Input = stat[5]
  322. parsed.Output = stat[6]
  323. parsed.Options = stat[9]
  324. return parsed, nil
  325. }
  326. // StructuredStats returns statistics as structured data which may be further
  327. // parsed and marshaled.
  328. func (ipt *IPTables) StructuredStats(table, chain string) ([]Stat, error) {
  329. rawStats, err := ipt.Stats(table, chain)
  330. if err != nil {
  331. return nil, err
  332. }
  333. structStats := []Stat{}
  334. for _, rawStat := range rawStats {
  335. stat, err := ipt.ParseStat(rawStat)
  336. if err != nil {
  337. return nil, err
  338. }
  339. structStats = append(structStats, stat)
  340. }
  341. return structStats, nil
  342. }
  343. func (ipt *IPTables) executeList(args []string) ([]string, error) {
  344. var stdout bytes.Buffer
  345. if err := ipt.runWithOutput(args, &stdout); err != nil {
  346. return nil, err
  347. }
  348. rules := strings.Split(stdout.String(), "\n")
  349. // strip trailing newline
  350. if len(rules) > 0 && rules[len(rules)-1] == "" {
  351. rules = rules[:len(rules)-1]
  352. }
  353. for i, rule := range rules {
  354. rules[i] = filterRuleOutput(rule)
  355. }
  356. return rules, nil
  357. }
  358. // NewChain creates a new chain in the specified table.
  359. // If the chain already exists, it will result in an error.
  360. func (ipt *IPTables) NewChain(table, chain string) error {
  361. return ipt.run("-t", table, "-N", chain)
  362. }
  363. const existsErr = 1
  364. // ClearChain flushed (deletes all rules) in the specified table/chain.
  365. // If the chain does not exist, a new one will be created
  366. func (ipt *IPTables) ClearChain(table, chain string) error {
  367. err := ipt.NewChain(table, chain)
  368. eerr, eok := err.(*Error)
  369. switch {
  370. case err == nil:
  371. return nil
  372. case eok && eerr.ExitStatus() == existsErr:
  373. // chain already exists. Flush (clear) it.
  374. return ipt.run("-t", table, "-F", chain)
  375. default:
  376. return err
  377. }
  378. }
  379. // RenameChain renames the old chain to the new one.
  380. func (ipt *IPTables) RenameChain(table, oldChain, newChain string) error {
  381. return ipt.run("-t", table, "-E", oldChain, newChain)
  382. }
  383. // DeleteChain deletes the chain in the specified table.
  384. // The chain must be empty
  385. func (ipt *IPTables) DeleteChain(table, chain string) error {
  386. return ipt.run("-t", table, "-X", chain)
  387. }
  388. func (ipt *IPTables) ClearAndDeleteChain(table, chain string) error {
  389. exists, err := ipt.ChainExists(table, chain)
  390. if err != nil || !exists {
  391. return err
  392. }
  393. err = ipt.run("-t", table, "-F", chain)
  394. if err == nil {
  395. err = ipt.run("-t", table, "-X", chain)
  396. }
  397. return err
  398. }
  399. func (ipt *IPTables) ClearAll() error {
  400. return ipt.run("-F")
  401. }
  402. func (ipt *IPTables) DeleteAll() error {
  403. return ipt.run("-X")
  404. }
  405. // ChangePolicy changes policy on chain to target
  406. func (ipt *IPTables) ChangePolicy(table, chain, target string) error {
  407. return ipt.run("-t", table, "-P", chain, target)
  408. }
  409. // Check if the underlying iptables command supports the --random-fully flag
  410. func (ipt *IPTables) HasRandomFully() bool {
  411. return ipt.hasRandomFully
  412. }
  413. // Return version components of the underlying iptables command
  414. func (ipt *IPTables) GetIptablesVersion() (int, int, int) {
  415. return ipt.v1, ipt.v2, ipt.v3
  416. }
  417. // run runs an iptables command with the given arguments, ignoring
  418. // any stdout output
  419. func (ipt *IPTables) run(args ...string) error {
  420. return ipt.runWithOutput(args, nil)
  421. }
  422. // runWithOutput runs an iptables command with the given arguments,
  423. // writing any stdout output to the given writer
  424. func (ipt *IPTables) runWithOutput(args []string, stdout io.Writer) error {
  425. args = append([]string{ipt.path}, args...)
  426. if ipt.hasWait {
  427. args = append(args, "--wait")
  428. if ipt.timeout != 0 && ipt.waitSupportSecond {
  429. args = append(args, strconv.Itoa(ipt.timeout))
  430. }
  431. } else {
  432. fmu, err := newXtablesFileLock()
  433. if err != nil {
  434. return err
  435. }
  436. ul, err := fmu.tryLock()
  437. if err != nil {
  438. syscall.Close(fmu.fd)
  439. return err
  440. }
  441. defer ul.Unlock()
  442. }
  443. var stderr bytes.Buffer
  444. cmd := exec.Cmd{
  445. Path: ipt.path,
  446. Args: args,
  447. Stdout: stdout,
  448. Stderr: &stderr,
  449. }
  450. if err := cmd.Run(); err != nil {
  451. switch e := err.(type) {
  452. case *exec.ExitError:
  453. return &Error{*e, cmd, stderr.String(), nil}
  454. default:
  455. return err
  456. }
  457. }
  458. return nil
  459. }
  460. // getIptablesCommand returns the correct command for the given protocol, either "iptables" or "ip6tables".
  461. func getIptablesCommand(proto Protocol) string {
  462. if proto == ProtocolIPv6 {
  463. return "ip6tables"
  464. } else {
  465. return "iptables"
  466. }
  467. }
  468. // Checks if iptables has the "-C" and "--wait" flag
  469. func getIptablesCommandSupport(v1 int, v2 int, v3 int) (bool, bool, bool, bool) {
  470. return iptablesHasCheckCommand(v1, v2, v3), iptablesHasWaitCommand(v1, v2, v3), iptablesWaitSupportSecond(v1, v2, v3), iptablesHasRandomFully(v1, v2, v3)
  471. }
  472. // getIptablesVersion returns the first three components of the iptables version
  473. // and the operating mode (e.g. nf_tables or legacy)
  474. // e.g. "iptables v1.3.66" would return (1, 3, 66, legacy, nil)
  475. func extractIptablesVersion(str string) (int, int, int, string, error) {
  476. versionMatcher := regexp.MustCompile(`v([0-9]+)\.([0-9]+)\.([0-9]+)(?:\s+\((\w+))?`)
  477. result := versionMatcher.FindStringSubmatch(str)
  478. if result == nil {
  479. return 0, 0, 0, "", fmt.Errorf("no iptables version found in string: %s", str)
  480. }
  481. v1, err := strconv.Atoi(result[1])
  482. if err != nil {
  483. return 0, 0, 0, "", err
  484. }
  485. v2, err := strconv.Atoi(result[2])
  486. if err != nil {
  487. return 0, 0, 0, "", err
  488. }
  489. v3, err := strconv.Atoi(result[3])
  490. if err != nil {
  491. return 0, 0, 0, "", err
  492. }
  493. mode := "legacy"
  494. if result[4] != "" {
  495. mode = result[4]
  496. }
  497. return v1, v2, v3, mode, nil
  498. }
  499. // Runs "iptables --version" to get the version string
  500. func getIptablesVersionString(path string) (string, error) {
  501. cmd := exec.Command(path, "--version")
  502. var out bytes.Buffer
  503. cmd.Stdout = &out
  504. err := cmd.Run()
  505. if err != nil {
  506. return "", err
  507. }
  508. return out.String(), nil
  509. }
  510. // Checks if an iptables version is after 1.4.11, when --check was added
  511. func iptablesHasCheckCommand(v1 int, v2 int, v3 int) bool {
  512. if v1 > 1 {
  513. return true
  514. }
  515. if v1 == 1 && v2 > 4 {
  516. return true
  517. }
  518. if v1 == 1 && v2 == 4 && v3 >= 11 {
  519. return true
  520. }
  521. return false
  522. }
  523. // Checks if an iptables version is after 1.4.20, when --wait was added
  524. func iptablesHasWaitCommand(v1 int, v2 int, v3 int) bool {
  525. if v1 > 1 {
  526. return true
  527. }
  528. if v1 == 1 && v2 > 4 {
  529. return true
  530. }
  531. if v1 == 1 && v2 == 4 && v3 >= 20 {
  532. return true
  533. }
  534. return false
  535. }
  536. //Checks if an iptablse version is after 1.6.0, when --wait support second
  537. func iptablesWaitSupportSecond(v1 int, v2 int, v3 int) bool {
  538. if v1 > 1 {
  539. return true
  540. }
  541. if v1 == 1 && v2 >= 6 {
  542. return true
  543. }
  544. return false
  545. }
  546. // Checks if an iptables version is after 1.6.2, when --random-fully was added
  547. func iptablesHasRandomFully(v1 int, v2 int, v3 int) bool {
  548. if v1 > 1 {
  549. return true
  550. }
  551. if v1 == 1 && v2 > 6 {
  552. return true
  553. }
  554. if v1 == 1 && v2 == 6 && v3 >= 2 {
  555. return true
  556. }
  557. return false
  558. }
  559. // Checks if a rule specification exists for a table
  560. func (ipt *IPTables) existsForOldIptables(table, chain string, rulespec []string) (bool, error) {
  561. rs := strings.Join(append([]string{"-A", chain}, rulespec...), " ")
  562. args := []string{"-t", table, "-S"}
  563. var stdout bytes.Buffer
  564. err := ipt.runWithOutput(args, &stdout)
  565. if err != nil {
  566. return false, err
  567. }
  568. return strings.Contains(stdout.String(), rs), nil
  569. }
  570. // counterRegex is the regex used to detect nftables counter format
  571. var counterRegex = regexp.MustCompile(`^\[([0-9]+):([0-9]+)\] `)
  572. // filterRuleOutput works around some inconsistencies in output.
  573. // For example, when iptables is in legacy vs. nftables mode, it produces
  574. // different results.
  575. func filterRuleOutput(rule string) string {
  576. out := rule
  577. // work around an output difference in nftables mode where counters
  578. // are output in iptables-save format, rather than iptables -S format
  579. // The string begins with "[0:0]"
  580. //
  581. // Fixes #49
  582. if groups := counterRegex.FindStringSubmatch(out); groups != nil {
  583. // drop the brackets
  584. out = out[len(groups[0]):]
  585. out = fmt.Sprintf("%s -c %s %s", out, groups[1], groups[2])
  586. }
  587. return out
  588. }