1 (** fifowatcher.ml: Routines to handle non-persistent scripts *)
10 let backend_prefix = ref ""
11 let direct_fifo_table: (string,(string*string) option) Hashtbl.t = Hashtbl.create 1024
13 let rec list_check lst elt =
16 | car::cdr -> if (car==elt) then true else list_check cdr elt
21 (* vsys is activated when a client opens an in file *)
22 let connect_file fqp_in =
23 (* Do we care about this file? *)
25 Hashtbl.find direct_fifo_table fqp_in with _ -> fprintf logfd "[Alert] Access via unauthorized vsys entry: %s\n" fqp_in;flush logfd;None in
27 | Some(execpath,slice_name) ->
28 fprintf logfd "Executing %s for slice %s\n" execpath slice_name;flush logfd;
30 let len = String.length fqp_in in
31 let fqp = String.sub fqp_in 0 (len-3) in
32 let fqp_out = String.concat "." [fqp;"out"] in
34 try openfile fqp_in [O_RDONLY;O_NONBLOCK] 0o777 with
35 e->fprintf logfd "Error opening and connecting FIFO: %s\n" fqp_in;flush logfd;raise e
38 try openfile fqp_out [O_WRONLY;O_NONBLOCK] 0o777 with
39 _->fprintf logfd "%s Output pipe not open, using stdout in place of %s\n" slice_name fqp_out;flush logfd;stdout
41 try ignore(create_process execpath [|execpath;slice_name|] fifo_fdin fifo_fdout fifo_fdout) with e -> fprintf logfd "Error executing service: %s\n" execpath;flush logfd
46 (** Make a pair of fifo entries *)
47 let mkentry fqp abspath perm uname =
48 fprintf logfd "Making entry %s->%s\n" fqp abspath;flush logfd;
49 let fifoin=sprintf "%s.in" fqp in
50 let fifoout=sprintf "%s.out" fqp in
51 (try Unix.unlink fifoin with _ -> ());
52 (try Unix.unlink fifoout with _ -> ());
54 let infname =(sprintf "%s.in" fqp) in
55 let outfname =(sprintf "%s.out" fqp) in
56 Unix.mkfifo infname 0o666;
57 Unix.mkfifo outfname 0o666;
58 ( (* Make the user the owner of the pipes in a non-chroot environment *)
59 if (!Globals.nochroot) then
60 let pwentry = Unix.getpwnam uname in
61 Unix.chown infname pwentry.pw_uid pwentry.pw_gid;
62 Unix.chown outfname pwentry.pw_uid pwentry.pw_gid
66 e->fprintf logfd "Error creating FIFO: %s->%s. May be something wrong at the frontend.\n" fqp fifoout;flush logfd;Failed)
68 (** Open fifos for a session. SHOULD NOt shutdown vsys if the fifos don't exist *)
69 let openentry fqp backend_spec =
70 let fqp_in = String.concat "." [fqp;"in"] in
71 Hashtbl.replace direct_fifo_table fqp_in (Some(backend_spec))
73 (** Close fifos that just got removed *)
75 let fqp_in = String.concat "." [fqp;"in"] in
76 Hashtbl.remove direct_fifo_table fqp_in
78 let direct_fifo_handler dirname evlist fname =
79 printf "Received event %s %s\n" dirname fname;flush Pervasives.stdout;
80 let is_event = list_check evlist in
81 if (is_event Open) then
82 let fqp_in = String.concat "/" [dirname;fname] in
85 let add_dir_watch fqp =
86 Dirwatcher.add_watch fqp [S_Open] (Some(direct_fifo_handler))
88 let del_dir_watch fqp =
89 (* XXX Dirwatcher.del_watch fqp *)