Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lens for reading termcap-style databases #274

Merged
merged 1 commit into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions lenses/getcap.aug
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
(*
Module: Getcap
Parses generic termcap-style capability databases

Author: Matt Dainty <[email protected]>

About: Reference
- man 3 getcap
- man 5 login.conf
- man 5 printcap

Each line represents a record consisting of a number of ':'-separated fields
the first of which is the name or identifier for the record. The name can
optionally be split by '|' and each subsequent value is considered an alias
of the first. Records can be split across multiple lines with '\'.

See also the Rtadvd and Termcap modules which contain slightly more specific
grammars.

*)

module Getcap =
autoload xfm

(* Comments cannot have any leading characters *)
let comment = Util.comment_generic /#[ \t]*/ "# "

let nfield = /[^#:\\\\\t\n|][^:\\\\\t\n|]*/

(* field must not contain ':' *)
let cfield = /[a-zA-Z0-9-]+([%^$#\\]?@|[%^$#\\=]([^:\\\\^]|\\\\[0-7]{1,3}|\\\\[bBcCeEfFnNrRtT\\^]|\^.)*)?/

let csep = del /:([ \t]*\\\\\n[ \t]*:)?/ ":\\\n\t:"
let nsep = Util.del_str "|"
let name = [ label "name" . store nfield ]
let capability (re:regexp) = [ label "capability" . store re ]
let record (re:regexp) = [ label "record" . name . ( nsep . name )* . ( csep . capability re )* . Sep.colon . Util.eol ]

let lns = ( Util.empty | comment | record cfield )*

let filter = incl "/etc/login.conf"
. incl "/etc/printcap"
. Util.stdexcl

let xfm = transform lns filter

(* Local Variables: *)
(* mode: caml *)
(* End: *)
34 changes: 34 additions & 0 deletions lenses/rtadvd.aug
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(*
Module: Rtadvd
Parses rtadvd configuration file

Author: Matt Dainty <[email protected]>

About: Reference
- man 5 rtadvd.conf

Each line represents a record consisting of a number of ':'-separated fields
the first of which is the name or identifier for the record. The name can
optionally be split by '|' and each subsequent value is considered an alias
of the first. Records can be split across multiple lines with '\'.

*)

module Rtadvd =
autoload xfm

let empty = Util.empty

(* field must not contain ':' unless quoted *)
let cfield = /[a-zA-Z0-9-]+(#?@|#[0-9]+|=("[^"]*"|[^:"]*))?/

let lns = ( empty | Getcap.comment | Getcap.record cfield )*

let filter = incl "/etc/rtadvd.conf"
. Util.stdexcl

let xfm = transform lns filter

(* Local Variables: *)
(* mode: caml *)
(* End: *)
34 changes: 34 additions & 0 deletions lenses/termcap.aug
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(*
Module: Termcap
Parses termcap capability database

Author: Matt Dainty <[email protected]>

About: Reference
- man 5 termcap

Each line represents a record consisting of a number of ':'-separated fields
the first of which is the name or identifier for the record. The name can
optionally be split by '|' and each subsequent value is considered an alias
of the first. Records can be split across multiple lines with '\'.

*)

module Termcap =
autoload xfm

(* All termcap capabilities are two characters, optionally preceded by *)
(* upto two periods and the only types are boolean, numeric or string *)
let cfield = /\.{0,2}([a-zA-Z0-9]{2}|[@#%&*!][a-zA-Z0-9]|k;)(#?@|#[0-9]+|=([^:\\\\^]|\\\\[0-7]{3}|\\\\[:bBcCeEfFnNrRstT0\\^]|\^.)*)?/

let lns = ( Util.empty | Getcap.comment | Getcap.record cfield )*

let filter = incl "/etc/termcap"
. incl "/usr/share/misc/termcap"
. Util.stdexcl

let xfm = transform lns filter

(* Local Variables: *)
(* mode: caml *)
(* End: *)
122 changes: 122 additions & 0 deletions lenses/tests/test_getcap.aug
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
module Test_getcap =

(* Example from getcap(3) *)
let getcap = "example|an example of binding multiple values to names:\\
:foo%bar:foo^blah:foo@:\\
:abc%xyz:abc^frap:abc$@:\\
:tc=more:
"

test Getcap.lns get getcap =
{ "record"
{ "name" = "example" }
{ "name" = "an example of binding multiple values to names" }
{ "capability" = "foo%bar" }
{ "capability" = "foo^blah" }
{ "capability" = "foo@" }
{ "capability" = "abc%xyz" }
{ "capability" = "abc^frap" }
{ "capability" = "abc$@" }
{ "capability" = "tc=more" }
}

(* Taken from the standard /etc/login.conf *)
let login_conf = "# Default allowed authentication styles
auth-defaults:auth=passwd,skey:

# Default allowed authentication styles for authentication type ftp
auth-ftp-defaults:auth-ftp=passwd:

#
# The default values
# To alter the default authentication types change the line:
# :tc=auth-defaults:\\
# to be read something like: (enables passwd, \"myauth\", and activ)
# :auth=passwd,myauth,activ:\\
# Any value changed in the daemon class should be reset in default
# class.
#
default:\\
:path=/usr/bin /bin /usr/sbin /sbin /usr/X11R6/bin /usr/local/bin /usr/local/sbin:\\
:umask=022:\\
:datasize-max=512M:\\
:datasize-cur=512M:\\
:maxproc-max=256:\\
:maxproc-cur=128:\\
:openfiles-cur=512:\\
:stacksize-cur=4M:\\
:localcipher=blowfish,8:\\
:ypcipher=old:\\
:tc=auth-defaults:\\
:tc=auth-ftp-defaults:
"

test Getcap.lns get login_conf =
{ "#comment" = "Default allowed authentication styles" }
{ "record"
{ "name" = "auth-defaults" }
{ "capability" = "auth=passwd,skey" }
}
{ }
{ "#comment" = "Default allowed authentication styles for authentication type ftp" }
{ "record"
{ "name" = "auth-ftp-defaults" }
{ "capability" = "auth-ftp=passwd" }
}
{ }
{ }
{ "#comment" = "The default values" }
{ "#comment" = "To alter the default authentication types change the line:" }
{ "#comment" = ":tc=auth-defaults:\\" }
{ "#comment" = "to be read something like: (enables passwd, \"myauth\", and activ)" }
{ "#comment" = ":auth=passwd,myauth,activ:\\" }
{ "#comment" = "Any value changed in the daemon class should be reset in default" }
{ "#comment" = "class." }
{ }
{ "record"
{ "name" = "default" }
{ "capability" = "path=/usr/bin /bin /usr/sbin /sbin /usr/X11R6/bin /usr/local/bin /usr/local/sbin" }
{ "capability" = "umask=022" }
{ "capability" = "datasize-max=512M" }
{ "capability" = "datasize-cur=512M" }
{ "capability" = "maxproc-max=256" }
{ "capability" = "maxproc-cur=128" }
{ "capability" = "openfiles-cur=512" }
{ "capability" = "stacksize-cur=4M" }
{ "capability" = "localcipher=blowfish,8" }
{ "capability" = "ypcipher=old" }
{ "capability" = "tc=auth-defaults" }
{ "capability" = "tc=auth-ftp-defaults" }
}

(* Sample /etc/printcap *)
let printcap = "# $OpenBSD: printcap,v 1.1 2014/07/12 03:52:39 deraadt Exp $

lp|local line printer:\\
:lp=/dev/lp:sd=/var/spool/output:lf=/var/log/lpd-errs:

rp|remote line printer:\\
:lp=:rm=printhost:rp=lp:sd=/var/spool/output:lf=/var/log/lpd-errs:
"

test Getcap.lns get printcap =
{ "#comment" = "$OpenBSD: printcap,v 1.1 2014/07/12 03:52:39 deraadt Exp $" }
{ }
{ "record"
{ "name" = "lp" }
{ "name" = "local line printer" }
{ "capability" = "lp=/dev/lp" }
{ "capability" = "sd=/var/spool/output" }
{ "capability" = "lf=/var/log/lpd-errs" }
}
{ }
{ "record"
{ "name" = "rp" }
{ "name" = "remote line printer" }
{ "capability" = "lp=" }
{ "capability" = "rm=printhost" }
{ "capability" = "rp=lp" }
{ "capability" = "sd=/var/spool/output" }
{ "capability" = "lf=/var/log/lpd-errs" }
}

30 changes: 30 additions & 0 deletions lenses/tests/test_rtadvd.aug
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Test_rtadvd =

(* Example from rtadvd.conf(5) *)
let rtadvd_conf = "default:\\
:chlim#64:raflags#0:rltime#1800:rtime#0:retrans#0:\\
:pinfoflags=\"la\":vltime#2592000:pltime#604800:mtu#0:
ef0:\\
:addr=\"2001:db8:ffff:1000::\":prefixlen#64:tc=default:
"

test Rtadvd.lns get rtadvd_conf =
{ "record"
{ "name" = "default" }
{ "capability" = "chlim#64" }
{ "capability" = "raflags#0" }
{ "capability" = "rltime#1800" }
{ "capability" = "rtime#0" }
{ "capability" = "retrans#0" }
{ "capability" = "pinfoflags=\"la\"" }
{ "capability" = "vltime#2592000" }
{ "capability" = "pltime#604800" }
{ "capability" = "mtu#0" }
}
{ "record"
{ "name" = "ef0" }
{ "capability" = "addr=\"2001:db8:ffff:1000::\"" }
{ "capability" = "prefixlen#64" }
{ "capability" = "tc=default" }
}

Loading