Annotation of libaitcfg/src/aitcfg.c, revision 1.16.6.2
1.2 misho 1: /*************************************************************************
2: * (C) 2008 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
1.16.6.2! misho 6: * $Id: aitcfg.c,v 1.16.6.1 2025/08/19 10:42:28 misho Exp $
1.2 misho 7: *
1.4 misho 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:
1.16.6.1 misho 15: Copyright 2004 - 2025
1.4 misho 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: */
1.1 misho 46: #include "global.h"
47:
48:
49: #pragma GCC visibility push(hidden)
50:
1.5 misho 51: int cfg_Errno;
52: char cfg_Error[STRSIZ];
53:
1.9 misho 54: int
1.8 misho 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:
1.9 misho 67: int
1.5 misho 68: cfg_tree_cmp(struct tagCfg *a, struct tagCfg *b)
69: {
70: int ret;
71:
72: assert(a && b);
73:
1.14 misho 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));
1.5 misho 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);
1.1 misho 86:
87: #pragma GCC visibility pop
88:
89:
1.5 misho 90: // cfg_GetErrno() Get error code of last operation
1.9 misho 91: int
1.5 misho 92: cfg_GetErrno()
93: {
94: return cfg_Errno;
95: }
96:
97: // cfg_GetError() Get error text of last operation
1.9 misho 98: const char *
1.5 misho 99: cfg_GetError()
100: {
101: return cfg_Error;
102: }
103:
104: // cfg_SetErr() Set error to variables for internal use!!!
1.9 misho 105: void
1.5 misho 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:
1.1 misho 118: /*
1.5 misho 119: * cfgInitConfig() - Init config root
120: *
1.12 misho 121: * return: NULL error or !=NULL allocated config root
1.5 misho 122: */
1.12 misho 123: cfg_root_t *
124: cfgInitConfig()
1.1 misho 125: {
1.12 misho 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));
1.1 misho 134:
1.5 misho 135: pthread_mutex_init(&cfg->rc_mtx, NULL);
136:
1.11 misho 137: TAILQ_INIT(cfg);
1.5 misho 138: RB_INIT(cfg);
1.12 misho 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: }
1.1 misho 157: }
158:
159: /*
1.16.6.1 misho 160: * cfgInitConfigExt() - Init existed config root
161: *
162: * @cfg = Config root
163: * return: NULL error or !=NULL inited config root
164: */
165: cfg_root_t *
166: cfgInitConfigExt(cfg_root_t * __restrict cfg)
167: {
1.16.6.2! misho 168: pthread_mutex_t mtx = { 0 };
! 169:
! 170: if (!TAILQ_EMPTY(cfg) || !RB_EMPTY(cfg) ||
! 171: memcmp(&cfg->rc_mtx, &mtx, sizeof mtx))
1.16.6.1 misho 172: cfgUnloadConfig(cfg);
173:
174: memset(cfg, 0, sizeof(cfg_root_t));
175:
176: pthread_mutex_init(&cfg->rc_mtx, NULL);
177:
178: TAILQ_INIT(cfg);
179: RB_INIT(cfg);
180: return cfg;
181: }
182:
183: /*
1.5 misho 184: * cfgLoadConfig() - Load config from file
185: *
186: * @cfgName = Config filename
187: * @cfg = Config root
188: * return: -1 error or 0 ok
189: */
190: int
191: cfgLoadConfig(const char *cfgName, cfg_root_t * __restrict cfg)
1.1 misho 192: {
193: FILE *f;
194: int ret;
1.16.6.2! misho 195: pthread_mutex_t mtx = { 0 };
1.1 misho 196:
1.5 misho 197: if (!cfgName || !cfg) {
198: cfg_SetErr(EINVAL, "Invalid parameter(s)");
1.1 misho 199: return -1;
1.12 misho 200: } else {
1.16.6.2! misho 201: if (!TAILQ_EMPTY(cfg) || !RB_EMPTY(cfg) ||
! 202: memcmp(&cfg->rc_mtx, &mtx, sizeof mtx))
1.16.6.1 misho 203: cfgUnloadConfig(cfg);
204:
1.15 misho 205: memset(cfg, 0, sizeof(cfg_root_t));
206:
1.12 misho 207: pthread_mutex_init(&cfg->rc_mtx, NULL);
208:
209: TAILQ_INIT(cfg);
210: RB_INIT(cfg);
211: }
1.1 misho 212:
1.5 misho 213: f = fopen(cfgName, "r");
214: if (!f) {
1.1 misho 215: LOGERR;
216: return -1;
217: }
218:
1.5 misho 219: ret = cfgReadConfig(f, cfg);
1.1 misho 220:
221: fclose(f);
222: return ret;
223: }
224:
225: /*
1.7 misho 226: * cfgClearConfig() - Clear config and free resources
1.5 misho 227: *
228: * @cfg = Config root
229: * return: none
230: */
231: void
1.7 misho 232: cfgClearConfig(cfg_root_t * __restrict cfg)
1.1 misho 233: {
1.5 misho 234: struct tagCfg *av;
1.1 misho 235:
1.5 misho 236: if (!cfg)
1.1 misho 237: return;
238:
1.5 misho 239: CFG_RC_LOCK(cfg);
1.11 misho 240: while ((av = TAILQ_FIRST(cfg))) {
241: TAILQ_REMOVE(cfg, av, cfg_next);
1.5 misho 242:
243: AIT_FREE_VAL(&av->cfg_val);
244: AIT_FREE_VAL(&av->cfg_attr);
245: AIT_FREE_VAL(&av->cfg_sec);
1.9 misho 246: e_free(av);
1.1 misho 247: }
1.5 misho 248: cfg->rbh_root = NULL;
249: CFG_RC_UNLOCK(cfg);
1.7 misho 250: }
251:
252: /*
253: * cfgUnloadConfig() - Unload config from memory and destroy resources
254: *
255: * @cfg = Config root
256: * return: none
257: */
258: void
259: cfgUnloadConfig(cfg_root_t * __restrict cfg)
260: {
261: if (!cfg)
262: return;
1.1 misho 263:
1.7 misho 264: cfgClearConfig(cfg);
1.5 misho 265: pthread_mutex_destroy(&cfg->rc_mtx);
1.1 misho 266: }
267:
1.3 misho 268: /*
1.5 misho 269: * cfgCreateConfig() - Create config file from memory
270: *
1.3 misho 271: * @csConfigName = New config filename
1.5 misho 272: * @cfg = Config root
273: * @whitespace = Additional whitespace characters to file
274: * return: -1 error or 0 ok
275: */
276: int
277: cfgCreateConfig(const char *csConfigName, cfg_root_t * __restrict cfg, int whitespace)
1.3 misho 278: {
279: FILE *f;
280: int ret;
281:
282: if (!csConfigName || !cfg)
283: return -1;
284:
1.5 misho 285: f = fopen(csConfigName, "w");
1.3 misho 286: if (!f) {
287: LOGERR;
288: return -1;
289: }
290:
1.16 misho 291: ret = cfgWriteConfigRaw(f, cfg, whitespace);
1.3 misho 292:
293: fclose(f);
294: return ret;
295: }
1.8 misho 296:
297: /*
298: * cfgInitPasswd() - Init password root
299: *
1.12 misho 300: * return: NULL error or !=NULL allocated password root
1.8 misho 301: */
1.12 misho 302: pwd_root_t *
303: cfgInitPasswd()
1.8 misho 304: {
1.12 misho 305: pwd_root_t *pwd = NULL;
306:
307: pwd = e_malloc(sizeof(pwd_root_t));
308: if (!pwd) {
309: cfg_SetErr(elwix_GetErrno(), "%s", elwix_GetError());
310: return NULL;
311: } else
312: memset(pwd, 0, sizeof(pwd_root_t));
1.8 misho 313:
314: pthread_mutex_init(&pwd->pwd_mtx, NULL);
315:
316: SLIST_INIT(pwd);
317: RB_INIT(pwd);
318: return 0;
319: }
320:
321: /*
1.12 misho 322: * cfgEndPasswd() - Free resources & password root
323: *
324: * @ppwd = Password root
325: * return: none
326: */
327: void
328: cfgEndPasswd(pwd_root_t **ppwd)
329: {
330: if (ppwd && *ppwd) {
331: cfgClearPasswd(*ppwd);
332: pthread_mutex_destroy(&(*ppwd)->pwd_mtx);
333: e_free(*ppwd);
334: *ppwd = NULL;
335: }
336: }
337:
338: /*
1.8 misho 339: * cfgLoadPasswd() - Load passwords from file
340: *
341: * @pwdName = Passwords filename
342: * @pwd = Password root
343: * return: -1 error or 0 ok
344: */
345: int
346: cfgLoadPasswd(const char *pwdName, pwd_root_t * __restrict pwd)
347: {
348: FILE *f;
349: int ret;
350:
351: if (!pwdName || !pwd) {
352: cfg_SetErr(EINVAL, "Invalid parameter(s)");
353: return -1;
1.12 misho 354: } else {
1.15 misho 355: memset(pwd, 0, sizeof(pwd_root_t));
356:
1.12 misho 357: pthread_mutex_init(&pwd->pwd_mtx, NULL);
358:
359: SLIST_INIT(pwd);
360: RB_INIT(pwd);
361: }
1.8 misho 362:
363: f = fopen(pwdName, "r");
364: if (!f) {
365: LOGERR;
366: return -1;
367: }
368:
369: ret = cfgReadPasswd(f, pwd);
370:
371: fclose(f);
372: return ret;
373: }
374:
375: /*
376: * cfgClearPasswd() - Clear passwords and free resources
377: *
378: * @cfg = Password root
379: * return: none
380: */
381: void
382: cfgClearPasswd(pwd_root_t * __restrict pwd)
383: {
384: struct tagUser *p;
385:
386: if (!pwd)
387: return;
388:
389: PWD_LOCK(pwd);
390: while ((p = SLIST_FIRST(pwd))) {
391: SLIST_REMOVE_HEAD(pwd, usr_next);
392:
393: AIT_FREE_VAL(&p->usr_name);
394: AIT_FREE_VAL(&p->usr_pass);
395: AIT_FREE_VAL(&p->usr_uid);
396: AIT_FREE_VAL(&p->usr_gid);
397: AIT_FREE_VAL(&p->usr_class);
398: AIT_FREE_VAL(&p->usr_change);
399: AIT_FREE_VAL(&p->usr_expire);
400: AIT_FREE_VAL(&p->usr_realm);
401: AIT_FREE_VAL(&p->usr_home);
402: AIT_FREE_VAL(&p->usr_shell);
1.9 misho 403: e_free(p);
1.8 misho 404: }
405: pwd->rbh_root = NULL;
406: PWD_UNLOCK(pwd);
407: }
408:
409: /*
410: * cfgUnloadPasswd() - Unload passwords from memory and destroy resources
411: *
412: * @pwd = Password root
413: * return: none
414: */
415: void
416: cfgUnloadPasswd(pwd_root_t * __restrict pwd)
417: {
418: if (!pwd)
419: return;
420:
421: cfgClearPasswd(pwd);
422: pthread_mutex_destroy(&pwd->pwd_mtx);
423: }
424:
425: /*
426: * cfgCreatePasswd() - Create password file from memory
427: *
428: * @pwdName = New password filename
429: * @pwd = Password root
430: * return: -1 error or 0 ok
431: */
432: int
433: cfgCreatePasswd(const char *pwdName, pwd_root_t * __restrict pwd)
434: {
435: FILE *f;
436: int ret;
437:
438: if (!pwdName || !pwd)
439: return -1;
440:
441: f = fopen(pwdName, "w");
442: if (!f) {
443: LOGERR;
444: return -1;
445: }
446:
447: ret = cfgWritePasswd(f, pwd);
448:
449: fclose(f);
450: return ret;
451: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>