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