LCOV - code coverage report
Current view: top level - source4/lib/policy - gp_ini.c (source / functions) Hit Total Coverage
Test: coverage report for fix-15632 9995c5c2 Lines: 0 65 0.0 %
Date: 2024-04-13 12:30:31 Functions: 0 5 0.0 %

          Line data    Source code
       1             : 
       2             : /*
       3             :  *  Unix SMB/CIFS implementation.
       4             :  *  Group Policy Object Support
       5             :  *  Copyright (C) Wilco Baan Hofman 2010
       6             :  *
       7             :  *  This program is free software; you can redistribute it and/or modify
       8             :  *  it under the terms of the GNU General Public License as published by
       9             :  *  the Free Software Foundation; either version 3 of the License, or
      10             :  *  (at your option) any later version.
      11             :  *
      12             :  *  This program is distributed in the hope that it will be useful,
      13             :  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  *  GNU General Public License for more details.
      16             :  *
      17             :  *  You should have received a copy of the GNU General Public License
      18             :  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
      19             :  */
      20             : #include "includes.h"
      21             : #include "lib/util/samba_util.h"
      22             : #include "lib/policy/policy.h"
      23             : 
      24             : struct gp_parse_context {
      25             :         struct gp_ini_context *ini;
      26             :         int32_t cur_section;
      27             : };
      28             : 
      29             : 
      30           0 : static bool gp_add_ini_section(const char *name, void *callback_data)
      31             : {
      32           0 :         struct gp_parse_context *parse = callback_data;
      33           0 :         struct gp_ini_context *ini = parse->ini;
      34             : 
      35           0 :         ini->sections = talloc_realloc(ini, ini->sections, struct gp_ini_section, ini->num_sections+1);
      36           0 :         if (ini->sections == NULL) return false;
      37           0 :         ini->sections[ini->num_sections].name = talloc_strdup(ini, name);
      38           0 :         if (ini->sections[ini->num_sections].name == NULL) return false;
      39           0 :         parse->cur_section = ini->num_sections;
      40           0 :         ini->num_sections++;
      41             : 
      42           0 :         return true;
      43             : }
      44             : 
      45           0 : static bool gp_add_ini_param(const char *name, const char *value, void *callback_data)
      46             : {
      47           0 :         struct gp_parse_context *parse = callback_data;
      48           0 :         struct gp_ini_context *ini = parse->ini;
      49           0 :         struct gp_ini_section *section;
      50             : 
      51           0 :         if (parse->cur_section == -1) {
      52           0 :                 return false;
      53             :         }
      54             : 
      55           0 :         section = &ini->sections[parse->cur_section];
      56             : 
      57           0 :         section->params = talloc_realloc(ini, ini->sections[parse->cur_section].params, struct gp_ini_param, section->num_params+1);
      58           0 :         if (section->params == NULL) return false;
      59           0 :         section->params[section->num_params].name = talloc_strdup(ini, name);
      60           0 :         if (section->params[section->num_params].name == NULL) return false;
      61           0 :         section->params[section->num_params].value = talloc_strdup(ini, value);
      62           0 :         if (section->params[section->num_params].value == NULL) return false;
      63           0 :         section->num_params++;
      64             : 
      65           0 :         return true;
      66             : }
      67             : 
      68           0 : NTSTATUS gp_parse_ini(TALLOC_CTX *mem_ctx, struct gp_context *gp_ctx, const char *filename, struct gp_ini_context **ret)
      69             : {
      70           0 :         struct gp_parse_context parse;
      71           0 :         bool rv;
      72             : 
      73           0 :         parse.ini = talloc_zero(mem_ctx, struct gp_ini_context);
      74           0 :         NT_STATUS_HAVE_NO_MEMORY(parse.ini);
      75           0 :         parse.cur_section = -1;
      76             : 
      77           0 :         rv = pm_process(filename, gp_add_ini_section, gp_add_ini_param, &parse);
      78           0 :         if (!rv) {
      79           0 :                 DEBUG(0, ("Error while processing ini file %s\n", filename));
      80           0 :                 return NT_STATUS_UNSUCCESSFUL;
      81             :         }
      82             : 
      83           0 :         *ret = parse.ini;
      84           0 :         return NT_STATUS_OK;
      85             : }
      86             : 
      87           0 : NTSTATUS gp_get_ini_string(struct gp_ini_context *ini, const char *section, const char *name, char **ret)
      88             : {
      89           0 :         uint16_t i;
      90           0 :         int32_t cur_sec = -1;
      91           0 :         for (i = 0; i < ini->num_sections; i++) {
      92           0 :                 if (strcmp(ini->sections[i].name, section) == 0) {
      93           0 :                         cur_sec = i;
      94           0 :                         break;
      95             :                 }
      96             :         }
      97             : 
      98           0 :         if (cur_sec == -1) {
      99           0 :                 return NT_STATUS_NOT_FOUND;
     100             :         }
     101             : 
     102           0 :         for (i = 0; i < ini->sections[cur_sec].num_params; i++) {
     103           0 :                 if (strcmp(ini->sections[cur_sec].params[i].name, name) == 0) {
     104           0 :                         *ret = ini->sections[cur_sec].params[i].value;
     105           0 :                         return NT_STATUS_OK;
     106             :                 }
     107             :         }
     108           0 :         return NT_STATUS_NOT_FOUND;
     109             : }
     110             : 
     111           0 : NTSTATUS gp_get_ini_uint(struct gp_ini_context *ini, const char *section, const char *name, uint32_t *ret)
     112             : {
     113           0 :         uint16_t i;
     114           0 :         int32_t cur_sec = -1;
     115           0 :         for (i = 0; i < ini->num_sections; i++) {
     116           0 :                 if (strcmp(ini->sections[i].name, section) == 0) {
     117           0 :                         cur_sec = i;
     118           0 :                         break;
     119             :                 }
     120             :         }
     121             : 
     122           0 :         if (cur_sec == -1) {
     123           0 :                 return NT_STATUS_NOT_FOUND;
     124             :         }
     125             : 
     126           0 :         for (i = 0; i < ini->sections[cur_sec].num_params; i++) {
     127           0 :                 if (strcmp(ini->sections[cur_sec].params[i].name, name) == 0) {
     128           0 :                         *ret = atol(ini->sections[cur_sec].params[i].value);
     129           0 :                         return NT_STATUS_OK;
     130             :                 }
     131             :         }
     132           0 :         return NT_STATUS_NOT_FOUND;
     133             : }

Generated by: LCOV version 1.14