waffle

user and group backend daemon
git clone git://git.2f30.org/waffle
Log | Files | Refs | LICENSE

backend.c (1789B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <stddef.h>
      4 #include <stdlib.h>
      5 #include "waffle.h"
      6 
      7 static struct {
      8 	char *name;
      9 	int enabled;
     10 	struct backend_ops *ops;
     11 } backends[] = {
     12 	{ .name = "dummy", .enabled = 0, .ops = &dummy_ops },
     13 	{ .name = "ldap",  .enabled = 0, .ops = &ldap_ops  },
     14 };
     15 
     16 int
     17 backends_init(void)
     18 {
     19 	size_t i;
     20 	int r = -1;
     21 
     22 	for (i = 0; i < LEN(backends); i++) {
     23 		if (!backends[i].ops->init()) {
     24 			backends[i].enabled = 1;
     25 			r = 0;
     26 		}
     27 	}
     28 	return r;
     29 }
     30 
     31 void
     32 backends_term(void)
     33 {
     34 	size_t i;
     35 
     36 	for (i = 0; i < LEN(backends); i++) {
     37 		if (backends[i].enabled) {
     38 			backends[i].ops->term();
     39 			backends[i].enabled = 0;
     40 		}
     41 	}
     42 }
     43 
     44 int
     45 backends_pwbyname(const char *key, struct passwd *pw)
     46 {
     47 	size_t i;
     48 
     49 	for (i = 0; i < LEN(backends); i++) {
     50 		if (!backends[i].enabled)
     51 			continue;
     52 		if (!backends[i].ops->pwbyname(key, pw))
     53 			return 0;
     54 	}
     55 	return -1;
     56 }
     57 
     58 int
     59 backends_pwbyuid(const char *key, struct passwd *pw)
     60 {
     61 	char *end;
     62 	uid_t uid;
     63 	size_t i;
     64 
     65 	errno = 0;
     66 	uid = strtol(key, &end, 10);
     67 	if (*end || errno)
     68 		return -1;
     69 	for (i = 0; i < LEN(backends); i++) {
     70 		if (!backends[i].enabled)
     71 			continue;
     72 		if (!backends[i].ops->pwbyuid(uid, pw))
     73 			return 0;
     74 	}
     75 	return -1;
     76 }
     77 
     78 int
     79 backends_grbyname(const char *key, struct group *gr)
     80 {
     81 	size_t i;
     82 
     83 	for (i = 0; i < LEN(backends); i++) {
     84 		if (!backends[i].enabled)
     85 			continue;
     86 		if (!backends[i].ops->grbyname(key, gr))
     87 			return 0;
     88 	}
     89 	return -1;
     90 }
     91 
     92 int
     93 backends_grbygid(const char *key, struct group *gr)
     94 {
     95 	char *end;
     96 	gid_t gid;
     97 	size_t i;
     98 
     99 	errno = 0;
    100 	gid = strtol(key, &end, 10);
    101 	if (*end || errno)
    102 		return -1;
    103 	for (i = 0; i < LEN(backends); i++) {
    104 		if (!backends[i].enabled)
    105 			continue;
    106 		if (!backends[i].ops->grbygid(gid, gr))
    107 			return 0;
    108 	}
    109 	return -1;
    110 }