1: /*************************************************************************
2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: aitcfg.c,v 1.15 2021/11/26 01:15:03 misho Exp $
7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
15: Copyright 2004 - 2021
16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
49: #pragma GCC visibility push(hidden)
50:
51: int cfg_Errno;
52: char cfg_Error[STRSIZ];
53:
54: int
55: cfg_Write(FILE *f, char *fmt, ...)
56: {
57: int ret = 0;
58: va_list lst;
59:
60: va_start(lst, fmt);
61: ret = vfprintf(f, fmt, lst);
62: va_end(lst);
63:
64: return ret;
65: }
66:
67: int
68: cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b)
69: {
70: int ret;
71:
72: assert(a && b);
73:
74: ret = ((AIT_KEY(&a->cfg_sec) << 15) | AIT_KEY(&a->cfg_attr)) -
75: ((AIT_KEY(&b->cfg_sec) << 15) | AIT_KEY(&b->cfg_attr));
76:
77: if (ret < 0)
78: return -1;
79: else if (ret > 0)
80: return 1;
81:
82: return ret;
83: }
84:
85: RB_GENERATE(tagRC, tagCfg, cfg_node, cfg_tree_cmp);
86:
87: #pragma GCC visibility pop
88:
89:
90: // cfg_GetErrno() Get error code of last operation
91: int
92: cfg_GetErrno()
93: {
94: return cfg_Errno;
95: }
96:
97: // cfg_GetError() Get error text of last operation
98: const char *
99: cfg_GetError()
100: {
101: return cfg_Error;
102: }
103:
104: // cfg_SetErr() Set error to variables for internal use!!!
105: void
106: cfg_SetErr(int eno, char *estr, ...)
107: {
108: va_list lst;
109:
110: cfg_Errno = eno;
111: memset(cfg_Error, 0, sizeof cfg_Error);
112: va_start(lst, estr);
113: vsnprintf(cfg_Error, sizeof cfg_Error, estr, lst);
114: va_end(lst);
115: }
116:
117:
118: /*
119: * cfgInitConfig() - Init config root
120: *
121: * return: NULL error or !=NULL allocated config root
122: */
123: cfg_root_t *
124: cfgInitConfig()
125: {
126: cfg_root_t *cfg = NULL;
127:
128: cfg = e_malloc(sizeof(cfg_root_t));
129: if (!cfg) {
130: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
131: return NULL;
132: } else
133: memset(cfg, 0, sizeof(cfg_root_t));
134:
135: pthread_mutex_init(&cfg->rc_mtx, NULL);
136:
137: TAILQ_INIT(cfg);
138: RB_INIT(cfg);
139: return cfg;
140: }
141:
142: /*
143: * cfgEndConfig() - Free resources & config root
144: *
145: * @pcfg = Config root
146: * return: none
147: */
148: void
149: cfgEndConfig(cfg_root_t **pcfg)
150: {
151: if (pcfg && *pcfg) {
152: cfgClearConfig(*pcfg);
153: pthread_mutex_destroy(&(*pcfg)->rc_mtx);
154: e_free(*pcfg);
155: *pcfg = NULL;
156: }
157: }
158:
159: /*
160: * cfgLoadConfig() - Load config from file
161: *
162: * @cfgName = Config filename
163: * @cfg = Config root
164: * return: -1 error or 0 ok
165: */
166: int
167: cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg)
168: {
169: FILE *f;
170: int ret;
171:
172: if (!cfgName || !cfg) {
173: cfg_SetErr(EINVAL, "Invalid parameter(s)");
174: return -1;
175: } else {
176: memset(cfg, 0, sizeof(cfg_root_t));
177:
178: pthread_mutex_init(&cfg->rc_mtx, NULL);
179:
180: TAILQ_INIT(cfg);
181: RB_INIT(cfg);
182: }
183:
184: f = fopen(cfgName, "r");
185: if (!f) {
186: LOGERR;
187: return -1;
188: }
189:
190: ret = cfgReadConfig(f, cfg);
191:
192: fclose(f);
193: return ret;
194: }
195:
196: /*
197: * cfgClearConfig() - Clear config and free resources
198: *
199: * @cfg = Config root
200: * return: none
201: */
202: void
203: cfgClearConfig(cfg_root_t * __restrict cfg)
204: {
205: struct tagCfg *av;
206:
207: if (!cfg)
208: return;
209:
210: CFG_RC_LOCK(cfg);
211: while ((av = TAILQ_FIRST(cfg))) {
212: TAILQ_REMOVE(cfg, av, cfg_next);
213:
214: AIT_FREE_VAL(&av->cfg_val);
215: AIT_FREE_VAL(&av->cfg_attr);
216: AIT_FREE_VAL(&av->cfg_sec);
217: e_free(av);
218: }
219: cfg->rbh_root = NULL;
220: CFG_RC_UNLOCK(cfg);
221: }
222:
223: /*
224: * cfgUnloadConfig() - Unload config from memory and destroy resources
225: *
226: * @cfg = Config root
227: * return: none
228: */
229: void
230: cfgUnloadConfig(cfg_root_t * __restrict cfg)
231: {
232: if (!cfg)
233: return;
234:
235: cfgClearConfig(cfg);
236: pthread_mutex_destroy(&cfg->rc_mtx);
237: }
238:
239: /*
240: * cfgCreateConfig() - Create config file from memory
241: *
242: * @csConfigName = New config filename
243: * @cfg = Config root
244: * @whitespace = Additional whitespace characters to file
245: * return: -1 error or 0 ok
246: */
247: int
248: cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace)
249: {
250: FILE *f;
251: int ret;
252:
253: if (!csConfigName || !cfg)
254: return -1;
255:
256: f = fopen(csConfigName, "w");
257: if (!f) {
258: LOGERR;
259: return -1;
260: }
261:
262: ret = cfgWriteConfig(f, cfg, whitespace);
263:
264: fclose(f);
265: return ret;
266: }
267:
268: /*
269: * cfgInitPasswd() - Init password root
270: *
271: * return: NULL error or !=NULL allocated password root
272: */
273: pwd_root_t *
274: cfgInitPasswd()
275: {
276: pwd_root_t *pwd = NULL;
277:
278: pwd = e_malloc(sizeof(pwd_root_t));
279: if (!pwd) {
280: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
281: return NULL;
282: } else
283: memset(pwd, 0, sizeof(pwd_root_t));
284:
285: pthread_mutex_init(&pwd->pwd_mtx, NULL);
286:
287: SLIST_INIT(pwd);
288: RB_INIT(pwd);
289: return 0;
290: }
291:
292: /*
293: * cfgEndPasswd() - Free resources & password root
294: *
295: * @ppwd = Password root
296: * return: none
297: */
298: void
299: cfgEndPasswd(pwd_root_t **ppwd)
300: {
301: if (ppwd && *ppwd) {
302: cfgClearPasswd(*ppwd);
303: pthread_mutex_destroy(&(*ppwd)->pwd_mtx);
304: e_free(*ppwd);
305: *ppwd = NULL;
306: }
307: }
308:
309: /*
310: * cfgLoadPasswd() - Load passwords from file
311: *
312: * @pwdName = Passwords filename
313: * @pwd = Password root
314: * return: -1 error or 0 ok
315: */
316: int
317: cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd)
318: {
319: FILE *f;
320: int ret;
321:
322: if (!pwdName || !pwd) {
323: cfg_SetErr(EINVAL, "Invalid parameter(s)");
324: return -1;
325: } else {
326: memset(pwd, 0, sizeof(pwd_root_t));
327:
328: pthread_mutex_init(&pwd->pwd_mtx, NULL);
329:
330: SLIST_INIT(pwd);
331: RB_INIT(pwd);
332: }
333:
334: f = fopen(pwdName, "r");
335: if (!f) {
336: LOGERR;
337: return -1;
338: }
339:
340: ret = cfgReadPasswd(f, pwd);
341:
342: fclose(f);
343: return ret;
344: }
345:
346: /*
347: * cfgClearPasswd() - Clear passwords and free resources
348: *
349: * @cfg = Password root
350: * return: none
351: */
352: void
353: cfgClearPasswd(pwd_root_t * __restrict pwd)
354: {
355: struct tagUser *p;
356:
357: if (!pwd)
358: return;
359:
360: PWD_LOCK(pwd);
361: while ((p = SLIST_FIRST(pwd))) {
362: SLIST_REMOVE_HEAD(pwd, usr_next);
363:
364: AIT_FREE_VAL(&p->usr_name);
365: AIT_FREE_VAL(&p->usr_pass);
366: AIT_FREE_VAL(&p->usr_uid);
367: AIT_FREE_VAL(&p->usr_gid);
368: AIT_FREE_VAL(&p->usr_class);
369: AIT_FREE_VAL(&p->usr_change);
370: AIT_FREE_VAL(&p->usr_expire);
371: AIT_FREE_VAL(&p->usr_realm);
372: AIT_FREE_VAL(&p->usr_home);
373: AIT_FREE_VAL(&p->usr_shell);
374: e_free(p);
375: }
376: pwd->rbh_root = NULL;
377: PWD_UNLOCK(pwd);
378: }
379:
380: /*
381: * cfgUnloadPasswd() - Unload passwords from memory and destroy resources
382: *
383: * @pwd = Password root
384: * return: none
385: */
386: void
387: cfgUnloadPasswd(pwd_root_t * __restrict pwd)
388: {
389: if (!pwd)
390: return;
391:
392: cfgClearPasswd(pwd);
393: pthread_mutex_destroy(&pwd->pwd_mtx);
394: }
395:
396: /*
397: * cfgCreatePasswd() - Create password file from memory
398: *
399: * @pwdName = New password filename
400: * @pwd = Password root
401: * return: -1 error or 0 ok
402: */
403: int
404: cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd)
405: {
406: FILE *f;
407: int ret;
408:
409: if (!pwdName || !pwd)
410: return -1;
411:
412: f = fopen(pwdName, "w");
413: if (!f) {
414: LOGERR;
415: return -1;
416: }
417:
418: ret = cfgWritePasswd(f, pwd);
419:
420: fclose(f);
421: return ret;
422: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>