1 /* $OpenBSD: entries.c,v 1.3 2015/01/16 06:40:22 deraadt Exp $ */
4 * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
41 flatten_entries(struct env *env)
50 log_debug("flattening trees");
52 * This takes all the line pointers in RB elements and
53 * concatenates them in a single string, to be able to
54 * implement next element lookup without tree traversal.
56 * An extra octet is alloced to make space for an additional NUL.
58 if ((linep = calloc(1, env->sc_user_line_len + 1)) == NULL) {
60 * XXX: try allocating a smaller chunk of memory
62 fatal("out of memory");
66 RB_FOREACH(ue, user_name_tree, env->sc_user_names) {
68 * we convert the first nul back to a column,
69 * copy the string and then convert it back to a nul.
71 ue->ue_line[strlen(ue->ue_line)] = ':';
72 log_debug("pushing line: %s", ue->ue_line);
73 len = strlen(ue->ue_line) + 1;
74 memcpy(endp, ue->ue_line, len);
75 endp[strcspn(endp, ":")] = '\0';
81 * To save memory strdup(3) the netid_line which originally used
84 tmp = ue->ue_netid_line;
85 ue->ue_netid_line = strdup(tmp);
86 if (ue->ue_netid_line == NULL) {
87 fatal("out of memory");
91 env->sc_user_lines = linep;
92 log_debug("done pushing users");
94 if ((linep = calloc(1, env->sc_group_line_len + 1)) == NULL) {
96 * XXX: try allocating a smaller chunk of memory
98 fatal("out of memory");
101 RB_FOREACH(ge, group_name_tree, env->sc_group_names) {
103 * we convert the first nul back to a column,
104 * copy the string and then convert it back to a nul.
106 ge->ge_line[strlen(ge->ge_line)] = ':';
107 log_debug("pushing line: %s", ge->ge_line);
108 len = strlen(ge->ge_line) + 1;
109 memcpy(endp, ge->ge_line, len);
110 endp[strcspn(endp, ":")] = '\0';
115 env->sc_group_lines = linep;
116 log_debug("done pushing groups");
120 userent_name_cmp(struct userent *ue1, struct userent *ue2)
122 return (strcmp(ue1->ue_line, ue2->ue_line));
126 userent_uid_cmp(struct userent *ue1, struct userent *ue2)
128 return (ue1->ue_uid - ue2->ue_uid);
132 groupent_name_cmp(struct groupent *ge1, struct groupent *ge2)
134 return (strcmp(ge1->ge_line, ge2->ge_line));
138 groupent_gid_cmp(struct groupent *ge1, struct groupent *ge2)
140 return (ge1->ge_gid - ge2->ge_gid);
143 RB_GENERATE(user_name_tree, userent, ue_name_node, userent_name_cmp);
144 RB_GENERATE(user_uid_tree, userent, ue_uid_node, userent_uid_cmp);
145 RB_GENERATE(group_name_tree, groupent, ge_name_node, groupent_name_cmp);
146 RB_GENERATE(group_gid_tree, groupent, ge_gid_node, groupent_gid_cmp);