1 /*******************************************************************************
2 
3     Arguments extension to refuse startup of the process if run as root.
4 
5     Copyright:
6         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
7         All rights reserved.
8 
9     License:
10         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
11         Alternatively, this file may be distributed under the terms of the Tango
12         3-Clause BSD License (see LICENSE_BSD.txt for details).
13 
14 *******************************************************************************/
15 
16 module ocean.util.app.ext.RefuseRootExt;
17 
18 
19 
20 import ocean.meta.types.Qualifiers;
21 import ocean.util.app.ext.model.IArgumentsExtExtension;
22 import core.sys.posix.unistd;
23 
24 
25 /*******************************************************************************
26 
27     Arguments extension that refuses to start if the program is run as root.
28     Behavior can be overridden by specifying --asroot
29 
30 *******************************************************************************/
31 
32 class RefuseRootExt : IArgumentsExtExtension
33 {
34     /***************************************************************************
35 
36         Order doesn't matter, so return anything less than default app 0
37 
38         Returns:
39             the extension order
40 
41     ***************************************************************************/
42 
43     override int order ()
44     {
45         return -1;
46     }
47 
48     /***************************************************************************
49 
50         Function executed when command line arguments are set up (before
51         parsing).
52 
53         Params:
54             app = application instance
55             args = command line arguments instance
56 
57     ***************************************************************************/
58 
59     override void setupArgs ( IApplication app, Arguments args )
60     {
61         args("asroot").params(0).help("Run as root");
62     }
63 
64 
65     /***************************************************************************
66 
67         Function executed after parsing the command line arguments.
68 
69         This function is only called if the arguments are valid so far.
70 
71         Params:
72             app = application instance
73             args = command line arguments instance
74 
75         Returns:
76             string with an error message if validation failed, null otherwise
77 
78     ***************************************************************************/
79 
80     override cstring validateArgs ( IApplication app, Arguments args )
81     {
82         if ( getuid() == 0 && !args.exists("asroot"))
83         {
84             return "Won't run as root! (use --asroot if you really need to do this)";
85         }
86         else
87         {
88             return null;
89         }
90     }
91 
92 
93     /***************************************************************************
94 
95         Unused IArgumentsExtExtension methods.
96 
97         We just need to provide an "empty" implementation to satisfy the
98         interface.
99 
100     ***************************************************************************/
101 
102     override void processArgs ( IApplication app, Arguments args )
103     {
104         // Unused
105     }
106 
107     override void preValidateArgs ( IApplication app, Arguments args )
108     {
109         // Unused
110     }
111 }