summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniil Baturin <daniil@vyos.io>2020-05-06 18:50:59 +0300
committerDaniil Baturin <daniil@vyos.io>2020-05-06 18:50:59 +0300
commitae68e5b11573b1b7dc0eba9d95c7ccbba1f1b23d (patch)
tree60151af2b20bc83118262a00a4c469934da24e88
parent02f2571c473d073a9370cf1dfaff9fac34470d63 (diff)
downloadvyos-utils-ae68e5b11573b1b7dc0eba9d95c7ccbba1f1b23d.tar.gz
vyos-utils-ae68e5b11573b1b7dc0eba9d95c7ccbba1f1b23d.zip
Initial import.
-rw-r--r--debian/changelog5
-rw-r--r--debian/compat1
-rw-r--r--debian/control15
-rw-r--r--debian/copyright35
-rwxr-xr-xdebian/rules20
-rw-r--r--debian/vyos-utils.install2
-rw-r--r--src/numeric.ml71
-rw-r--r--src/validate_value.ml42
8 files changed, 191 insertions, 0 deletions
diff --git a/debian/changelog b/debian/changelog
new file mode 100644
index 0000000..baa22da
--- /dev/null
+++ b/debian/changelog
@@ -0,0 +1,5 @@
+vyos-utils (0.0.1) unstable; urgency=medium
+
+ * Initial release.
+
+ -- Daniil Baturin <daniil@baturin.org> Wed, 06 May 2020 18:43:04 +0300
diff --git a/debian/compat b/debian/compat
new file mode 100644
index 0000000..ec63514
--- /dev/null
+++ b/debian/compat
@@ -0,0 +1 @@
+9
diff --git a/debian/control b/debian/control
new file mode 100644
index 0000000..4ae87ca
--- /dev/null
+++ b/debian/control
@@ -0,0 +1,15 @@
+Source: vyos-utils
+Section: contrib/net
+Priority: extra
+Maintainer: VyOS Package Maintainers <maintainers@vyos.net>
+Build-Depends: debhelper (>= 9),
+ quilt,
+ libpcre3-dev,
+Standards-Version: 3.9.6
+
+Package: vyos-utils
+Architecture: any
+Depends: ${shlibs:Depends},
+ ${misc:Depends}
+Description: VyOS utils for value validation and other things
+ VyOS utils for value validation and other things
diff --git a/debian/copyright b/debian/copyright
new file mode 100644
index 0000000..67cdfb6
--- /dev/null
+++ b/debian/copyright
@@ -0,0 +1,35 @@
+This package was debianized by Daniil Baturin <daniil@baturin.org> on
+Wed, 06 May 2020 18:31:27 +0300
+
+It's original content from the GIT repository <http://github.com/vyos/vyos-utils>
+
+Upstream Author:
+
+ <maintainers@vyos.net>
+
+Copyright:
+
+ Copyright (C) 2020 VyOS maintainers and contributors
+ All Rights Reserved.
+
+License:
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation; either version 2, or (at your option)
+any later version.
+
+This program is distributed in the hope that it will be useful, but
+WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+General Public License for more details.
+
+A copy of the GNU General Public License is available as
+`/usr/share/common-licenses/GPL' in the Debian GNU/Linux distribution
+or on the World Wide Web at `http://www.gnu.org/copyleft/gpl.html'.
+You can also obtain it by writing to the Free Software Foundation,
+Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
+MA 02110-1301, USA.
+
+The Debian packaging is (C) 2020, Daniil Baturin <daniil@baturin.org> and
+is licensed under the GPL, see above.
diff --git a/debian/rules b/debian/rules
new file mode 100755
index 0000000..b3ff1c6
--- /dev/null
+++ b/debian/rules
@@ -0,0 +1,20 @@
+#!/usr/bin/make -f
+
+DIR := debian/tmp
+
+%:
+ dh $@ --with quilt
+
+override_dh_auto_build:
+ eval `opam env`
+ mkdir -p _build
+ ocamlfind ocamlopt -o _build/numeric -package num -linkpkg src/numeric.ml
+ ocamlfind ocamlopt -o _build/validate-value -package pcre,unix -linkpkg src/validate_value.ml
+
+override_dh_auto_install:
+ mkdir -p $(DIR)/usr/libexec/vyos/validators
+ cp _build/numeric $(DIR)/usr/libexec/vyos/validators
+ cp _build/validate-value $(DIR)/usr/libexec/vyos/
+
+override_dh_auto_test:
+ echo "No tests yet"
diff --git a/debian/vyos-utils.install b/debian/vyos-utils.install
new file mode 100644
index 0000000..77ea559
--- /dev/null
+++ b/debian/vyos-utils.install
@@ -0,0 +1,2 @@
+usr/libexec/vyos/validators/numeric
+usr/libexec/vyos/validate-value
diff --git a/src/numeric.ml b/src/numeric.ml
new file mode 100644
index 0000000..12ea04c
--- /dev/null
+++ b/src/numeric.ml
@@ -0,0 +1,71 @@
+type options = {
+ positive: bool;
+ nonnegative: bool;
+ ranges: string list;
+}
+
+let default_opts = {
+ positive = false;
+ nonnegative = false;
+ ranges = []
+}
+
+let int_of_string = Big_int.big_int_of_string
+let int_of_string_opt = Big_int.big_int_of_string_opt
+let big = Big_int.big_int_of_int
+let (>=) = Big_int.ge_big_int
+let (<=) = Big_int.le_big_int
+
+let opts = ref default_opts
+
+let number_arg = ref ""
+
+let args = [
+ ("--non-negative", Arg.Unit (fun () -> opts := {!opts with nonnegative=true}), "Check if the number is non-negative (>= 0)");
+ ("--positive", Arg.Unit (fun () -> opts := {!opts with positive=true}), "Check if the number is positive (> 0)");
+ ("--range", Arg.String (fun s -> let optsv = !opts in opts := {optsv with ranges=(s :: optsv.ranges)}), "Check if the number is within a range (inclusive)");
+]
+let usage = Printf.sprintf "Usage: %s [OPTIONS] <number>" Sys.argv.(0)
+
+let () = if Array.length Sys.argv = 1 then (Arg.usage args usage; exit 1)
+let () = Arg.parse args (fun s -> number_arg := s) usage
+
+let check_nonnegative opts n =
+ if opts.nonnegative && (n < (big 0)) then
+ failwith "Number should be non-negative."
+
+let check_positive opts n =
+ if opts.positive && (n <= (big 0)) then
+ failwith "Number should be positive"
+
+let number_of_string s =
+ let n = int_of_string_opt s in
+ match n with
+ | Some n -> n
+ | None ->
+ Printf.ksprintf failwith "'%s' is not a valid integer number" s
+
+let range_of_string s =
+ let rs = String.split_on_char '-' s |> List.map String.trim |> List.map int_of_string_opt in
+ match rs with
+ | [Some l; Some r] -> (l, r)
+ | _ -> Printf.ksprintf failwith "'%s' is not a valid number range" s
+
+let check_ranges ranges n =
+ let in_range (l, r) n = (n >= l) && (n <= r) in
+ let res = List.fold_left (fun acc r -> acc || (in_range r n)) false ranges in
+ if not res then
+ Printf.ksprintf failwith "Number is not in any of allowed ranges"
+
+let () = try
+ let opts = !opts in
+ let n = number_of_string !number_arg in
+ check_nonnegative opts n;
+ check_positive opts n;
+ if opts.ranges <> [] then
+ let ranges = List.map range_of_string opts.ranges in
+ check_ranges ranges n
+with (Failure err) ->
+ print_endline err;
+ exit 1
+
diff --git a/src/validate_value.ml b/src/validate_value.ml
new file mode 100644
index 0000000..3af58e0
--- /dev/null
+++ b/src/validate_value.ml
@@ -0,0 +1,42 @@
+type check = Regex of string | Exec of string
+
+let validate_value value_constraint value =
+ match value_constraint with
+ | Regex s ->
+ (try
+ let _ = Pcre.exec ~pat:s value in true
+ with Not_found -> false)
+ | Exec c ->
+ (* XXX: Using Unix.system is a bad idea on multiple levels,
+ especially when the input comes directly from the user...
+ We should do something about it.
+ *)
+ let result = Unix.system (Printf.sprintf "%s %s" c value) in
+ match result with
+ | Unix.WEXITED 0 -> true
+ | Unix.WEXITED 127 ->
+ let () = Printf.printf "Could not execute validator %s" c in
+ false
+ | _ -> false
+
+let value = ref ""
+
+let checks = ref []
+
+let args = [
+ ("--regex", Arg.String (fun s -> checks := (Regex s) :: !checks), "Check the value against a regex");
+ ("--exec", Arg.String (fun s -> checks := (Exec s) :: !checks), "Check the value against an external command");
+ ("--value", Arg.String (fun s -> value := s), "Value to check");
+]
+let usage = Printf.sprintf "Usage: %s [OPTIONS] <number>" Sys.argv.(0)
+
+let () = Arg.parse args (fun _ -> ()) usage
+
+let _ =
+ let value = !value in
+ let checks = !checks in
+ match checks with
+ | [] -> exit 0
+ | _ ->
+ List.iter (fun c -> if (validate_value c value) then exit 0 else ()) checks;
+ exit 1