aboutsummaryrefslogtreecommitdiff
path: root/captain/config.f90
blob: 2d67adf1013ce9abc0265264c06b2ea7321fb225 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
! Copyright (c) 2021 Approximatrix, LLC <support@approximatrix.com>
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in
! all copies or substantial portions of the Software.
!
! The Software shall be used for Good, not Evil.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.

module config
implicit none

    character(*), parameter::TEMPLATE_DIRECTORY_VARIABLE = "template-directory"
    character(1024)::template_directory
    
    character(*), parameter::DATABASE_VARIABLE = "database"
    character(1024)::database_filename
    
    character(*), parameter::LOGFILE_VARIABLE = "log-filename"
    character(1024)::log_filename
    
    character(*), parameter::LOGLEVEL_VARIABLE = "log-level"
    integer::loglevel = 3
    
    character(*), parameter::PROJECT_NAME_VARIABLE = "project"
    character(32)::project

    character(*), parameter::DESCRIPTION_VARIABLE = "description"
    character(1024)::description
    
    character(*), parameter::PUBLIC_CERT_VARIABLE = "public-cert"
    character(1024)::pubcert
    
    character(*), parameter::PRIVATE_CERT_VARIABLE = "private-cert"
    character(1024)::privcert
    
    character(*), parameter::RELEASE_DIRECTORY_VARIABLE = "release-directory"
    character(1024)::release_dir
    
    character(*), parameter::UPLOAD_DIRECTORY_VARIABLE = "upload-directory"
    character(1024)::upload_dir
    
    character(*), parameter::RESULTS_DIRECTORY_VARIABLE = "results-directory"
    character(1024)::results_dir
    
    character(*), parameter::STATIC_DIRECTORY_VARIABLE = "static-directory"
    character(1024)::static_dir
    
    character(*), parameter::INSTRUCTIONS_DIRECTORY_VARIABLE = "instructions-directory"
    character(1024)::instructions_dir
    
    character(*), parameter::SCRIPT_DIRECTORY_VARIABLE = "script-directory"
    character(1024)::script_dir
    
    character(*), parameter::TEMP_DIRECTORY_VARIABLE = "temp-directory"
    character(1024)::temp_dir = "/tmp"
    
    character(*), parameter::SALT_VARIABLE = "security-salt"
    character(1024)::app_salt
    
    character(*), parameter::PERMISSIONS_FILE_VARIABLE = "permissions_file"
    character(1024)::perm_filename
    
    integer, parameter::MAX_PERMISSIONS = 16
    
    type::permissions
        character(len=40), dimension(MAX_PERMISSIONS)::k
        integer, dimension(MAX_PERMISSIONS)::v
        
        contains
        
        procedure :: load => load_permissions
        procedure :: get => get_permission
        
    end type permissions
    
    type(permissions)::global_permissions
    
contains
    
    subroutine get_variable(str, v)
    implicit none
    
        character(*), intent(in)::str
        character(*), intent(out)::v
        
        integer::i
        
        v = " "
        
        i = index(str, '=')
        if(i > 0) then
            i = i - 1 ! Remove the equal sign
            v = adjustl(str(1:i))
        end if
    
    end subroutine get_variable
    
    subroutine get_value(str, v)
    implicit none
    
        character(*), intent(in)::str
        character(*), intent(out)::v
        
        integer::i,n
        
        v = " "
        
        i = index(str, '=')
        n = len_trim(str)
        if(i > 0) then
            v = adjustl(str(i+1:n))
        end if
    
    end subroutine get_value
    
    subroutine assign_config(cvariable, cvalue)
    use utilities, only: set_temporary_directory
    implicit none
    
        character(*), intent(in)::cvariable, cvalue
        
        if(cvariable == TEMPLATE_DIRECTORY_VARIABLE) then
            template_directory = cvalue
            
        else if(cvariable == DATABASE_VARIABLE) then
            database_filename = cvalue
        
        else if(cvariable == PROJECT_NAME_VARIABLE) then
            project = cvalue
        
        else if(cvariable == DESCRIPTION_VARIABLE) then
            description = cvalue
        
        else if(cvariable == PUBLIC_CERT_VARIABLE) then
            pubcert = cvalue
            
        else if(cvariable == PRIVATE_CERT_VARIABLE) then
            privcert = cvalue
        
        else if(cvariable == LOGFILE_VARIABLE) then
            log_filename = cvalue
            
        else if(cvariable == RELEASE_DIRECTORY_VARIABLE) then
            release_dir = cvalue
            
        else if(cvariable == UPLOAD_DIRECTORY_VARIABLE) then
            upload_dir = cvalue
            
        else if(cvariable == RESULTS_DIRECTORY_VARIABLE) then
            results_dir = cvalue
            
        else if(cvariable == STATIC_DIRECTORY_VARIABLE) then
            static_dir = cvalue
            
        else if(cvariable == SCRIPT_DIRECTORY_VARIABLE) then
            script_dir = cvalue
            
        else if(cvariable == INSTRUCTIONS_DIRECTORY_VARIABLE) then
            instructions_dir = cvalue
        
        else if(cvariable == LOGLEVEL_VARIABLE) then
            read(cvalue, '(I3)') loglevel
            
        else if(cvariable == TEMP_DIRECTORY_VARIABLE) then
            call set_temporary_directory(cvalue)
        
        else if(cvariable == SALT_VARIABLE) then
            app_salt = trim(cvalue)
            
        else if(cvariable == PERMISSIONS_FILE_VARIABLE) then
            perm_filename = trim(cvalue)
        
        end if
        
    end subroutine assign_config
    
    subroutine load_configuration(filename)
    implicit none
        
        character(*), intent(in)::filename
        integer::unit_number, istatus
        character(1024)::line, cvalue
        character(64)::cvariable
        integer::i
        
        open(newunit=unit_number, file=trim(filename), status='old', &
             action="read", iostat=istatus)
             
        read(unit_number, '(A)', iostat=istatus) line
        do while(istatus == 0)
            
            if(len_trim(line) > 0 .and. line(1:1) /= '#') then
            
                call get_variable(line, cvariable)
                call get_value(line, cvalue)

                call assign_config(trim(cvariable), trim(cvalue))
            
            end if
            
            read(unit_number, '(A)', iostat=istatus) line
        end do
             
        close(unit_number)
        
        if(len_trim(perm_filename) > 0) then
            if(perm_filename(1:1) /= "/") then
                i = index(filename, "/", back=.true.)
                if(i > 1) then
                    perm_filename = filename(1:i)//trim(perm_filename)
                end if
            end if
            call global_permissions%load(perm_filename)
        end if
        
    end subroutine load_configuration
    
    subroutine template_filepath(x, res)
    use utilities, only: combine_paths
    implicit none
    
        character(*), intent(in)::x
        character(*), intent(out)::res
    
        call combine_paths(template_directory, x, res)
        
    end subroutine template_filepath

    subroutine load_permissions(self, filename)
    implicit none
    
        class(permissions), intent(out)::self
        character(*), intent(in)::filename
        
        integer::unit_number, istatus
        character(1024)::line, cvalue
        character(64)::cvariable
        integer::i
        
        self%k = " "
        
        open(newunit=unit_number, file=trim(filename), status='old', &
             action="read", iostat=istatus)
        
        i = 1
             
        read(unit_number, '(A)', iostat=istatus) line
        do while(istatus == 0 .and. i <= MAX_PERMISSIONS)
            
            if(len_trim(line) > 0 .and. line(1:1) /= '#') then
            
                call get_variable(line, cvariable)
                call get_value(line, cvalue)

                self%k(i) = cvariable
                read(cvalue, *, iostat=istatus) self%v(i)
                
                i = i + 1
            end if
            
            read(unit_number, '(A)', iostat=istatus) line
        end do
        
        close(unit_number)
        
    end subroutine load_permissions
    
    pure function get_permission(self, key)
    use auth_levels, only: AUTH_ADMIN_USER
    implicit none
    
        class(permissions), intent(in)::self
        character(len=*), intent(in)::key
        integer::get_permission
        
        integer::i
        
        get_permission = AUTH_ADMIN_USER
        
        do i = 1, MAX_PERMISSIONS
            if(trim(self%k(i)) == trim(key)) then
                get_permission = self%v(i)
                exit
            end if
        end do
        
    end function get_permission
    
end module config