Initial commit for Tizen
[profile/extras/shadow-utils.git] / lib / pwio.c
1 /*
2  * Copyright (c) 1990 - 1994, Julianne Frances Haugh
3  * Copyright (c) 1996 - 2000, Marek Michałkiewicz
4  * Copyright (c) 2001       , Michał Moskal
5  * Copyright (c) 2003 - 2005, Tomasz Kłoczko
6  * Copyright (c) 2007 - 2009, Nicolas François
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the copyright holders or contributors may not be used to
18  *    endorse or promote products derived from this software without
19  *    specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24  * PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT
25  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <config.h>
35
36 #ident "$Id: pwio.c 2783 2009-04-23 21:19:02Z nekral-guest $"
37
38 #include "prototypes.h"
39 #include "defines.h"
40 #include <pwd.h>
41 #include <stdio.h>
42 #include "commonio.h"
43 #include "pwio.h"
44
45 static /*@null@*/ /*@only@*/void *passwd_dup (const void *ent)
46 {
47         const struct passwd *pw = ent;
48
49         return __pw_dup (pw);
50 }
51
52 static void passwd_free (/*@out@*/ /*@only@*/void *ent)
53 {
54         struct passwd *pw = ent;
55
56         pw_free (pw);
57 }
58
59 static const char *passwd_getname (const void *ent)
60 {
61         const struct passwd *pw = ent;
62
63         return pw->pw_name;
64 }
65
66 static void *passwd_parse (const char *line)
67 {
68         return (void *) sgetpwent (line);
69 }
70
71 static int passwd_put (const void *ent, FILE * file)
72 {
73         const struct passwd *pw = ent;
74
75         return (putpwent (pw, file) == -1) ? -1 : 0;
76 }
77
78 static struct commonio_ops passwd_ops = {
79         passwd_dup,
80         passwd_free,
81         passwd_getname,
82         passwd_parse,
83         passwd_put,
84         fgets,
85         fputs,
86         NULL,                   /* open_hook */
87         NULL                    /* close_hook */
88 };
89
90 static struct commonio_db passwd_db = {
91         PASSWD_FILE,            /* filename */
92         &passwd_ops,            /* ops */
93         NULL,                   /* fp */
94 #ifdef WITH_SELINUX
95         NULL,                   /* scontext */
96 #endif
97         NULL,                   /* head */
98         NULL,                   /* tail */
99         NULL,                   /* cursor */
100         false,                  /* changed */
101         false,                  /* isopen */
102         false,                  /* locked */
103         false                   /* readonly */
104 };
105
106 int pw_setdbname (const char *filename)
107 {
108         return commonio_setname (&passwd_db, filename);
109 }
110
111 /*@observer@*/const char *pw_dbname (void)
112 {
113         return passwd_db.filename;
114 }
115
116 int pw_lock (void)
117 {
118         return commonio_lock (&passwd_db);
119 }
120
121 int pw_open (int mode)
122 {
123         return commonio_open (&passwd_db, mode);
124 }
125
126 /*@observer@*/ /*@null@*/const struct passwd *pw_locate (const char *name)
127 {
128         return commonio_locate (&passwd_db, name);
129 }
130
131 /*@observer@*/ /*@null@*/const struct passwd *pw_locate_uid (uid_t uid)
132 {
133         const struct passwd *pwd;
134
135         pw_rewind ();
136         while (   ((pwd = pw_next ()) != NULL)
137                && (pwd->pw_uid != uid)) {
138         }
139
140         return pwd;
141 }
142
143 int pw_update (const struct passwd *pw)
144 {
145         return commonio_update (&passwd_db, (const void *) pw);
146 }
147
148 int pw_remove (const char *name)
149 {
150         return commonio_remove (&passwd_db, name);
151 }
152
153 int pw_rewind (void)
154 {
155         return commonio_rewind (&passwd_db);
156 }
157
158 /*@observer@*/ /*@null@*/const struct passwd *pw_next (void)
159 {
160         return commonio_next (&passwd_db);
161 }
162
163 int pw_close (void)
164 {
165         return commonio_close (&passwd_db);
166 }
167
168 int pw_unlock (void)
169 {
170         return commonio_unlock (&passwd_db);
171 }
172
173 /*@null@*/struct commonio_entry *__pw_get_head (void)
174 {
175         return passwd_db.head;
176 }
177
178 void __pw_del_entry (const struct commonio_entry *ent)
179 {
180         commonio_del_entry (&passwd_db, ent);
181 }
182
183 struct commonio_db *__pw_get_db (void)
184 {
185         return &passwd_db;
186 }
187
188 static int pw_cmp (const void *p1, const void *p2)
189 {
190         uid_t u1, u2;
191
192         if ((*(struct commonio_entry **) p1)->eptr == NULL)
193                 return 1;
194         if ((*(struct commonio_entry **) p2)->eptr == NULL)
195                 return -1;
196
197         u1 = ((struct passwd *) (*(struct commonio_entry **) p1)->eptr)->pw_uid;
198         u2 = ((struct passwd *) (*(struct commonio_entry **) p2)->eptr)->pw_uid;
199
200         if (u1 < u2)
201                 return -1;
202         else if (u1 > u2)
203                 return 1;
204         else
205                 return 0;
206 }
207
208 /* Sort entries by UID */
209 int pw_sort ()
210 {
211         return commonio_sort (&passwd_db, pw_cmp);
212 }