reindent-20030715
[openafs.git] / src / uss / uss_ptserver.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  *      Implementation of basic procedures for the AFS user account
12  *      facility.
13  */
14
15 /*
16  * --------------------- Required definitions ---------------------
17  */
18 #include <afsconfig.h>
19 #include <afs/param.h>
20
21 RCSID
22     ("$Header$");
23
24 #include "uss_ptserver.h"       /*Module interface */
25 #include <afs/ptclient.h>       /*Protection Server client interface */
26 #include <afs/pterror.h>        /*Protection Server error codes */
27 #include <afs/com_err.h>        /*Error code xlation */
28
29
30 #undef USS_PTSERVER_DB
31
32 extern int line;
33
34
35 /*
36  * ---------------------- Private definitions ---------------------
37  */
38 #define uss_ptserver_MAX_SIZE   2048
39
40
41 /*
42  * ------------------------ Private globals -----------------------
43  */
44 static int initDone = 0;        /*Module initialized? */
45
46
47 /*-----------------------------------------------------------------------
48  * static InitThisModule
49  *
50  * Description:
51  *      Set up this module, namely make the connection to the Protection
52  *      Server.
53  *
54  * Arguments:
55  *      None.
56  *
57  * Returns:
58  *      0 if everything went fine, or
59  *      lower-level error code otherwise.
60  *
61  * Environment:
62  *      This routine will only be called once.
63  *
64  * Side Effects:
65  *      As advertised.
66  *------------------------------------------------------------------------*/
67
68 static afs_int32
69 InitThisModule()
70 {                               /*InitThisModule */
71
72     static char rn[] = "uss_ptserver:InitThisModule";   /*Routine name */
73     register afs_int32 code;    /*Return code */
74
75     /*
76      * Only once, guys.
77      */
78     if (initDone)
79         return (0);
80
81     /*
82      * Connect up with the Protection Server.
83      */
84 #ifdef USS_PTSERVER_DB
85     printf
86         ("%s: Initializing Protection Server: security=1, confdir = '%s', cell = '%s'\n",
87          rn, uss_ConfDir, uss_Cell);
88 #endif /* USS_PTSERVER_DB */
89     code = pr_Initialize(1,     /*Security level */
90                          uss_ConfDir,   /*Config directory */
91                          uss_Cell);     /*Cell to touch */
92     if (code) {
93         com_err(uss_whoami, code,
94                 "while initializing Protection Server library");
95         return (code);
96     }
97
98     initDone = 1;
99     return (0);
100
101 }                               /*InitThisModule */
102
103
104 /*-----------------------------------------------------------------------
105  * EXPORTED uss_ptserver_AddUser
106  *
107  * Environment:
108  *      The common DesiredUID variable, if non-zero, is the value
109  *      desired for the user's uid.
110  *
111  * Side Effects:
112  *      As advertised.
113  *------------------------------------------------------------------------*/
114
115 afs_int32
116 uss_ptserver_AddUser(a_user, a_uid)
117      char *a_user;
118      char *a_uid;
119
120 {                               /*uss_ptserver_AddUser */
121
122     afs_int32 code;             /*Various return codes */
123     afs_int32 id = uss_DesiredUID;      /*ID desired for user, if any */
124     afs_int32 mappedUserID;     /*ID user already has */
125
126     if (uss_verbose) {
127         fprintf(stderr, "Adding user '%s' to the Protection DB\n", a_user);
128         if (id)
129             fprintf(stderr, "\t[Presetting uid to %d]\n", id);
130     }
131
132     /*
133      * Make sure we're initialized before doing anything.
134      */
135     if (!initDone) {
136         code = InitThisModule();
137         if (code)
138             return (code);
139     }
140
141     /*
142      * If this is a dry run, we still need to setup the uid before
143      * returning.
144      */
145     if (uss_DryRun) {
146         fprintf(stderr, "\t[Dry run - user %d not created]\n",
147                 uss_DesiredUID);
148         sprintf(a_uid, "%d", uss_DesiredUID);
149         return (0);
150     }
151
152     /*
153      * Go ahead and create the user.
154      */
155     code = pr_CreateUser(a_user, &id);
156     if (code) {
157         if (code == PREXIST || code == PRIDEXIST) {
158             if (code == PREXIST)
159                 fprintf(stderr,
160                         "%s: Warning: '%s' already in the Protection DB\n",
161                         uss_whoami, a_user);
162             else
163                 fprintf(stderr,
164                         "%s: Warning: Id '%d' already in Protection DB\n",
165                         uss_whoami, id);
166
167             /*
168              * Make sure the user name given matches the id that has
169              * already been registered with the Protection Server.
170              *
171              * Note: pr_SNameToId ONLY returns a non-zero error code
172              * for a major problem, like a network partition, so we
173              * have to explicitly check the ID returned against
174              * ANONYMOUSID, which is what we get when there is no
175              * ID known for the user name.
176              */
177             mappedUserID = id;
178             if (code = pr_SNameToId(a_user, &mappedUserID)) {
179                 com_err(uss_whoami, code,
180                         "while getting uid from Protection Server");
181                 return (code);
182             }
183             if (mappedUserID == ANONYMOUSID) {
184                 fprintf(stderr,
185                         "%s: User '%s' unknown, yet given id (%d) already has a mapping!\n",
186                         uss_whoami, a_user, id);
187                 return (PRIDEXIST);
188             }
189             if (id == 0)
190                 id = mappedUserID;
191             else if (mappedUserID != id) {
192                 fprintf(stderr,
193                         "%s: User '%s' already has id %d; won't assign id %d\n",
194                         uss_whoami, a_user, mappedUserID, id);
195                 return (PRIDEXIST);
196             }
197         } else {
198             /*
199              * Got a fatal error.
200              */
201             com_err(uss_whoami, code, "while accessing Protection Server");
202             return (code);
203         }
204     }
205     /*Create the user's protection entry */
206     sprintf(a_uid, "%d", id);
207     if (uss_verbose)
208         fprintf(stderr, "The uid for user '%s' is %s\n", a_user, a_uid);
209
210     /*
211      * Return sweetness & light.
212      */
213     return (0);
214
215 }                               /*uss_ptserver_AddUser */
216
217
218 /*-----------------------------------------------------------------------
219  * EXPORTED uss_ptserver_DelUser
220  *
221  * Environment:
222  *      Nothing interesting.
223  *
224  * Side Effects:
225  *      As advertised.
226  *------------------------------------------------------------------------*/
227
228 afs_int32
229 uss_ptserver_DelUser(a_name)
230      char *a_name;
231
232 {                               /*uss_ptserver_DelUser */
233
234     afs_int32 code;             /*Various return codes */
235
236     /*
237      * Make sure we're initialized before doing anything.
238      */
239     if (!initDone) {
240         code = InitThisModule();
241         if (code)
242             return (code);
243     }
244
245     if (uss_DryRun) {
246         fprintf(stderr,
247                 "\t[Dry run - user '%s' not deleted from Protection DB]\n",
248                 a_name);
249         return (0);
250     }
251
252     if (uss_verbose)
253         fprintf(stderr, "Deleting user '%s' from the Protection DB\n",
254                 a_name);
255
256     /*
257      * Go ahead and delete the user.
258      */
259     code = pr_Delete(a_name);
260     if (code) {
261         if (code == PRNOENT) {
262             /*
263              * There's no entry for that user in the Protection DB,
264              * so our job is done.
265              */
266             fprintf(stderr,
267                     "%s: Warning: User '%s' not found in Protection DB\n",
268                     uss_whoami, a_name);
269         } /*User not registered */
270         else {
271             com_err(uss_whoami, code,
272                     "while deleting user from Protection DB");
273             return (code);
274         }                       /*Fatal PTS error */
275     }
276
277     /*Error in deletion */
278     /*
279      * Return sweetness & light.
280      */
281     return (0);
282
283 }                               /*uss_ptserver_DelUser */
284
285
286 /*-----------------------------------------------------------------------
287  * EXPORTED uss_ptserver_XlateUser
288  *
289  * Environment:
290  *      Nothing interesting.
291  *
292  * Side Effects:
293  *      As advertised.
294  *------------------------------------------------------------------------*/
295
296 afs_int32
297 uss_ptserver_XlateUser(a_user, a_uidP)
298      char *a_user;
299      afs_int32 *a_uidP;
300
301 {                               /*uss_ptserver_XlateUser */
302
303     static char rn[] = "uss_ptserver_XlateUser";        /*Routine name */
304     register afs_int32 code;    /*Various return codes */
305
306     if (uss_verbose)
307         fprintf(stderr, "Translating user '%s' via the Protection DB\n",
308                 a_user);
309
310     /*
311      * Make sure we're initialized before doing anything.
312      */
313     if (!initDone) {
314         code = InitThisModule();
315         if (code)
316             return (code);
317     }
318
319     /*
320      * Note: pr_SNameToId ONLY returns a non-zero error code
321      * for a major problem, like a network partition, so we
322      * have to explicitly check the ID returned against
323      * ANONYMOUSID, which is what we get when there is no
324      * ID known for the user name.
325      */
326     *a_uidP = 0;
327     code = pr_SNameToId(a_user, a_uidP);
328     if (code) {
329         com_err(uss_whoami, code, "while getting uid from Protection DB");
330         return (code);
331     }
332     if (*a_uidP == ANONYMOUSID) {
333         fprintf(stderr, "%s: No entry for user '%s' in the Protection DB\n",
334                 uss_whoami, a_user);
335         return (code);
336     }
337
338     /*
339      * Return sweetness & light.
340      */
341 #ifdef USS_PTSERVER_DB
342     printf("%s: User '%s' maps to uid %d\n", rn, a_user, *a_uidP);
343 #endif /* USS_PTSERVER_DB */
344     return (0);
345
346 }                               /*uss_ptserver_XlateUser */