]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/ypldap/entries.c
sys/{x86,amd64}: remove one of doubled ;s
[FreeBSD/FreeBSD.git] / usr.sbin / ypldap / entries.c
1 /*      $OpenBSD: entries.c,v 1.3 2015/01/16 06:40:22 deraadt Exp $ */
2 /*      $FreeBSD$ */
3 /*
4  * Copyright (c) 2008 Pierre-Yves Ritschard <pyr@openbsd.org>
5  *
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.
9  *
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.
17  */
18
19 #include <sys/types.h>
20 #include <sys/param.h>
21 #include <sys/queue.h>
22 #include <sys/socket.h>
23 #include <sys/tree.h>
24
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
27
28 #include <errno.h>
29 #include <event.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <pwd.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <limits.h>
37
38 #include "ypldap.h"
39
40 void
41 flatten_entries(struct env *env)
42 {
43         size_t           wrlen;
44         size_t           len;
45         char            *linep;
46         char            *endp;
47         char            *tmp;
48         struct userent  *ue;
49         struct groupent *ge;
50
51         log_debug("flattening trees");
52         /*
53          * This takes all the line pointers in RB elements and
54          * concatenates them in a single string, to be able to
55          * implement next element lookup without tree traversal.
56          *
57          * An extra octet is alloced to make space for an additional NUL.
58          */
59         wrlen = env->sc_user_line_len;
60         if ((linep = calloc(1, env->sc_user_line_len + 1)) == NULL) {
61                 /*
62                  * XXX: try allocating a smaller chunk of memory
63                  */
64                 fatal("out of memory");
65         }
66         endp = linep;
67
68         RB_FOREACH(ue, user_name_tree, env->sc_user_names) {
69                 /*
70                  * we convert the first nul back to a column,
71                  * copy the string and then convert it back to a nul.
72                  */
73                 ue->ue_line[strlen(ue->ue_line)] = ':';
74                 log_debug("pushing line: %s", ue->ue_line);
75                 len = strlen(ue->ue_line) + 1;
76                 memcpy(endp, ue->ue_line, len);
77                 endp[strcspn(endp, ":")] = '\0';
78                 free(ue->ue_line);
79                 ue->ue_line = endp;
80                 endp += len;
81                 wrlen -= len;
82
83                 /*
84                  * To save memory strdup(3) the netid_line which originally used
85                  * LINE_WIDTH bytes
86                  */
87                 tmp = ue->ue_netid_line;
88                 ue->ue_netid_line = strdup(tmp);
89                 if (ue->ue_netid_line == NULL) {
90                         fatal("out of memory");
91                 }
92                 free(tmp);
93         }
94         env->sc_user_lines = linep;
95         log_debug("done pushing users");
96
97         wrlen = env->sc_group_line_len;
98         if ((linep = calloc(1, env->sc_group_line_len + 1)) == NULL) {
99                 /*
100                  * XXX: try allocating a smaller chunk of memory
101                  */
102                 fatal("out of memory");
103         }
104         endp = linep;
105         RB_FOREACH(ge, group_name_tree, env->sc_group_names) {
106                 /*
107                  * we convert the first nul back to a column,
108                  * copy the string and then convert it back to a nul.
109                  */
110                 ge->ge_line[strlen(ge->ge_line)] = ':';
111                 log_debug("pushing line: %s", ge->ge_line);
112                 len = strlen(ge->ge_line) + 1;
113                 memcpy(endp, ge->ge_line, len);
114                 endp[strcspn(endp, ":")] = '\0';
115                 free(ge->ge_line);
116                 ge->ge_line = endp;
117                 endp += len;
118                 wrlen -= len;
119         }
120         env->sc_group_lines = linep;
121         log_debug("done pushing groups");
122 }
123
124 int
125 userent_name_cmp(struct userent *ue1, struct userent *ue2)
126 {
127         return (strcmp(ue1->ue_line, ue2->ue_line));
128 }
129
130 int
131 userent_uid_cmp(struct userent *ue1, struct userent *ue2)
132 {
133         return (ue1->ue_uid - ue2->ue_uid);
134 }
135
136 int
137 groupent_name_cmp(struct groupent *ge1, struct groupent *ge2)
138 {
139         return (strcmp(ge1->ge_line, ge2->ge_line));
140 }
141
142 int
143 groupent_gid_cmp(struct groupent *ge1, struct groupent *ge2)
144 {
145         return (ge1->ge_gid - ge2->ge_gid);
146 }
147
148 RB_GENERATE(user_name_tree, userent, ue_name_node, userent_name_cmp);
149 RB_GENERATE(user_uid_tree, userent, ue_uid_node, userent_uid_cmp);
150 RB_GENERATE(group_name_tree, groupent, ge_name_node, groupent_name_cmp);
151 RB_GENERATE(group_gid_tree, groupent, ge_gid_node, groupent_gid_cmp);