View | Details | Raw Unified | Return to bug 22495
Collapse All | Expand All

(-)univention-config-registry/include/univention/config.h (-4 / +20 lines)
Lines 35-43 Link Here
35
35
36
#include <stdio.h>
36
#include <stdio.h>
37
37
38
char* univention_config_get_string ( char *value );
38
/**
39
int   univention_config_get_int    ( char *value );
39
 * Retrieve value of config registry entry associated with key.
40
long  univention_config_get_long   ( char *value );
40
 * @return an allocated buffer containingt the value or NULL on errors or if not found.
41
int   univention_config_set_string ( char *key, char *value);
41
 */
42
char *univention_config_get_string(const char *key);
43
/**
44
 * Retrieve integer value of config registry entry associated with key.
45
 * @return an integer value of -1 on errors of if not found.
46
 */
47
int   univention_config_get_int   (const char *key);
48
/**
49
 * Retrieve integer value of config registry entry associated with key.
50
 * @return an integer value of -1 on errors of if not found.
51
 */
52
long  univention_config_get_long  (const char *key);
53
/**
54
 * Set config registry entry associated with key to new value.
55
 * @return 0 on success, -1 on internal errors.
56
 */
57
int   univention_config_set_string(const char *key, const char *value);
42
58
43
#endif
59
#endif
(-)univention-config-registry/lib/config.c (-89 / +47 lines)
Lines 47-176 Link Here
47
#define BASECONFIG_MAX_LINE 1024
47
#define BASECONFIG_MAX_LINE 1024
48
48
49
49
50
char* univention_config_get_string ( char *value )
50
char *univention_config_get_string(const char *key)
51
{
51
{
52
	FILE *file;
52
	FILE *file;
53
	char line[BASECONFIG_MAX_LINE];
53
	char line[BASECONFIG_MAX_LINE];
54
	char *nvalue;
54
	char *nvalue;
55
	int len;
55
	size_t len;
56
	char *ret = NULL;
56
57
57
	if( (file=fopen(BASECONFIG_FILE,"r")) == NULL )
58
	if ((file = fopen(BASECONFIG_FILE, "r")) == NULL)
58
	{
59
	{
59
		univention_debug(UV_DEBUG_CONFIG,UV_DEBUG_ERROR,"Error on opening \"%s\n",BASECONFIG_FILE);
60
		univention_debug(UV_DEBUG_CONFIG, UV_DEBUG_ERROR, "Error on opening \"%s\"", BASECONFIG_FILE);
60
		return NULL;
61
		return NULL;
61
	}
62
	}
62
	
63
	len = strlen(value);
64
63
65
	nvalue = calloc(len + 2 /* ':' + '\0'*/, sizeof(char));
64
	len = strlen(key);
66
	memcpy(nvalue, value, len);
65
	nvalue = malloc(len + 2 /* ':' + '\0'*/);
67
	nvalue[len] = ':';
66
	memcpy(nvalue, key, len);
67
	nvalue[len++] = ':';
68
	nvalue[len++] = '\0';
68
69
69
	while( fgets(line, BASECONFIG_MAX_LINE, file) != NULL )
70
	while (fgets(line, BASECONFIG_MAX_LINE, file) != NULL)
70
	{
71
	{
71
		if( !strncmp(line, nvalue, strlen(nvalue) ) )
72
		if (!strncmp(line, nvalue, len))
72
		{
73
		{
73
			fclose (file);
74
			ret = strndup(line + len, strlen(line) - len - 1 ); /* no newline */
74
			free (nvalue);
75
			goto done;
75
76
			return (char*)strndup(&(line[len+2]), strlen(line) - (len+2) - 1 );
77
																								/* no newline */
78
		}
76
		}
79
	}
77
	}
80
78
    univention_debug(UV_DEBUG_USERS, UV_DEBUG_INFO, "Did not find \"%s\"", key);
81
	fclose (file);
79
done:
82
80
	fclose(file);
83
    univention_debug(UV_DEBUG_USERS, UV_DEBUG_INFO,"Did not find \"%s\"\n",value);
84
85
	free(nvalue);
81
	free(nvalue);
86
	return NULL;
82
	return ret;
87
}
83
}
88
84
89
int univention_config_get_int(char *value)
85
int univention_config_get_int(const char *key)
90
{
86
{
91
	FILE *file;
87
	int ret = -1;
92
	char line[BASECONFIG_MAX_LINE];
88
	char *s = univention_config_get_string(key);
93
	char *s_var;
89
	if (s) {
94
	int var;
90
		ret = atoi(s);
95
91
		free(s);
96
	if( (file=fopen(BASECONFIG_FILE,"r")) == NULL )
97
	{
98
		univention_debug(UV_DEBUG_USERS,UV_DEBUG_ERROR,"Error on opening \"%s\n",BASECONFIG_FILE);
99
		return -1;
100
	}
101
102
	while( fgets(line, BASECONFIG_MAX_LINE, file) != NULL )
103
	{
104
		if( !strncmp(line, value, strlen(value) ) )
105
		{
106
			fclose (file);
107
			s_var=(char*)strndup(&(line[strlen(value)+2]), strlen(line) - (strlen(value)+2) - 1 );
108
			return atoi(s_var);
109
		}
110
	}
92
	}
111
93
	return ret;
112
	fclose (file);
113
114
    univention_debug(UV_DEBUG_USERS, UV_DEBUG_INFO,"Did not find \"%s\"\n",value);
115
116
	return -1;
117
}
94
}
118
95
119
long univention_config_get_long(char *value)
96
long univention_config_get_long(const char *key)
120
{
97
{
121
	FILE *file;
98
	long ret = -1;
122
	char line[BASECONFIG_MAX_LINE];
99
	char *s = univention_config_get_string(key);
123
	char *s_var;
100
	if (s) {
124
	long var;
101
		ret = atol(s);
125
102
		free(s);
126
	if( (file=fopen(BASECONFIG_FILE,"r")) == NULL )
127
	{
128
		univention_debug(UV_DEBUG_USERS,UV_DEBUG_ERROR,"Error on opening \"%s\n",BASECONFIG_FILE);
129
		return -1;
130
	}
131
132
	while( fgets(line, BASECONFIG_MAX_LINE, file) != NULL )
133
	{
134
		if( !strncmp(line, value, strlen(value) ) )
135
		{
136
			fclose (file);
137
			s_var=(char*)strndup(&(line[strlen(value)+2]), strlen(line) - (strlen(value)+2) - 1 );
138
			return atol(s_var);
139
		}
140
	}
103
	}
141
104
	return ret;
142
	fclose (file);
143
144
    univention_debug(UV_DEBUG_USERS, UV_DEBUG_INFO,"Did not find \"%s\"\n",value);
145
146
	return -1;
147
}
105
}
148
106
149
int univention_config_set_string(char *key, char *value)
107
int univention_config_set_string(const char *key, const char *value)
150
{
108
{
109
	size_t len;
151
	char *str;
110
	char *str;
152
	int pid, status;
111
	int pid, status;
153
112
154
	
113
	len = strlen(key) + strlen(value) + 2;
155
	str=malloc((strlen(key)+strlen(value)+2) * sizeof(char));
114
	str = malloc(len);
156
	strcpy(str, key);
115
	if (!str)
157
	strcat(str, "=");
116
		return -1;
158
	strcat(str,value);
117
	snprintf(str, len, "%s=%s", key, value);
159
118
160
	pid = fork();
119
	pid = fork();
161
	if (pid == -1)
120
	if (pid == -1)
162
		return -1;
121
		return -1;
163
	if (pid == 0) {
122
	if (pid == 0) {
164
		char *argv[5];
123
		/* child */
165
		argv[0] = "sh";
124
		char *argv[4];
166
		argv[1] = "-c";
125
		argv[0] = "univention-config-registry";
167
		argv[2] = "univention-config-registry";
126
		argv[1] = "set";
168
		argv[2] = "set";
127
		argv[2] = str;
169
		argv[3] = str;
128
		argv[3] = NULL;
170
		argv[4] = 0;
129
		execve("/usr/sbin/univention-config-registry", argv, NULL);
171
		execve("/bin/sh", argv, NULL);
172
		exit(127);
130
		exit(127);
173
	}
131
	}
132
	/* parent */
174
	do {
133
	do {
175
		if (waitpid(pid, &status, 0) == -1) {
134
		if (waitpid(pid, &status, 0) == -1) {
176
			if (errno != EINTR)
135
			if (errno != EINTR)
Lines 181-184 Link Here
181
140
182
	return 0;
141
	return 0;
183
}
142
}
184

Return to bug 22495