Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement template addition for replacing keys in profile files
Implement template addition to pass templates as key value pairs as cmd line parameters to replace the keys in read profile file lines to allow more customization and flexibility. This adds a new file called template.c which contains all the functionality. Motivation for this is to have the possibility to create profile files where a specific key exists that can be customized per application that us being run with firejail. For example, application name can be used in a D-Bus name that is requested to be owned to avoid collision with other applications requiring the same base name. This can be passed directly then to firejail as a cmd line parameter when starting the application. All templates are to be given via cmd line parameters in format: --template=KEY:VALUE The keys and values are stored in a single linked list within template.c, which is free'd when the keys in all read profile file (including included profiles) lines have been replaced. Each key can exist only once, existing hardcoded macros in addition to XDG cannot be overwritten. In any of these is violated firejail exits. Key cannot start with a digit and must contain alphanumeric chars only. Each value must conform to following rules: - length is < 255 (D-Bus name length) - can contain alphabetical (a-zA-Z), integer (0-9) and '_./' chars but no '..' In order to use the same DBUS_MAX_NAME_LENGTH it is moved from dbus.c to firejail.h. When processing the profile file lines the template keys are expected to be written as other macros, ${TEMPLATE_KEY}. Template cannot be in the beginning of the line. If the read line contains other internal macros they are not replaced as they are processed later with more strict and specific checks. It is known that using strtok_r() and doing the tokenization in two steps, first by '$' and then by '{}' invalid definitions such as ${{TEMPLATE_KEY2}} will pass the checks. The process of replacing the keys can be described as follows to ease the understanding of the code: 1. "whitelist ${HOME}/${key1}/path/to/${key2}.somewhere" tokens are: a: whitelist b: {HOME}/ c: {key1}/path/to/ d: {key2}.somewhere 2. Keys in the first token 'a' are ignored, it is the start of new str 3. Tokens 'b', 'c' and 'd' are passed to process_key_value 4. Each of the template keys are replaced with corresponding values, as ${HOME} is internal macro it is not replaced but added as is. Only the first items in tokens, 'key1' and 'key2' are considered as proper keys to have the values replaced, the remains are just added to the str. 5. Resulting string would be then: "whitelist ${HOME}/value1/path/to/value2.somewhere" In order to avoid unnecessary duplication of each read profile line the line is first checked to have at least one template key. If the template key is not found firejail will exit with an error.
- Loading branch information