530b11557e5bfd1c9b9ee5fb19bf3d2c78efa514
[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 extern int errno;
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 InitThisModule()
69
70 { /*InitThisModule*/
71
72     static char rn[] =
73         "uss_ptserver:InitThisModule";  /*Routine name*/
74     register afs_int32 code;                    /*Return code*/
75
76     /*
77      * Only once, guys.
78      */
79     if (initDone)
80         return(0);
81
82     /*
83      * Connect up with the Protection Server.
84      */
85 #ifdef USS_PTSERVER_DB
86     printf("%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 uss_ptserver_AddUser(a_user, a_uid)
116     char *a_user;
117     char *a_uid;
118
119 { /*uss_ptserver_AddUser*/
120
121     afs_int32 code;                             /*Various return codes*/
122     afs_int32 id = uss_DesiredUID;              /*ID desired for user, if any*/
123     afs_int32 mappedUserID;                     /*ID user already has*/
124     
125     if (uss_verbose) {
126         fprintf(stderr, "Adding user '%s' to the Protection DB\n",
127                 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", uss_DesiredUID);
147         sprintf(a_uid, "%d", uss_DesiredUID);
148         return(0);
149     }
150
151     /*
152      * Go ahead and create the user.
153      */
154     code = pr_CreateUser(a_user, &id);
155     if (code) {
156         if (code == PREXIST || code == PRIDEXIST)  {
157             if (code == PREXIST)
158                 fprintf(stderr,
159                         "%s: Warning: '%s' already in the Protection DB\n",
160                         uss_whoami, a_user);
161             else
162                 fprintf(stderr,
163                         "%s: Warning: Id '%d' already in Protection DB\n",
164                         uss_whoami, id);
165
166             /*
167              * Make sure the user name given matches the id that has
168              * already been registered with the Protection Server.
169              *
170              * Note: pr_SNameToId ONLY returns a non-zero error code
171              * for a major problem, like a network partition, so we
172              * have to explicitly check the ID returned against
173              * ANONYMOUSID, which is what we get when there is no
174              * ID known for the user name.
175              */
176             mappedUserID = id;
177             if (code = pr_SNameToId(a_user, &mappedUserID)) {
178                 com_err(uss_whoami, code,
179                         "while getting uid from Protection Server");
180                 return(code);
181             }
182             if (mappedUserID == ANONYMOUSID) {
183                 fprintf(stderr,
184                         "%s: User '%s' unknown, yet given id (%d) already has a mapping!\n",
185                         uss_whoami, a_user, id);
186                 return(PRIDEXIST);
187             }
188             if (id == 0)
189                 id = mappedUserID;
190             else
191                 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         }
198         else {
199             /*
200              * Got a fatal error.
201              */
202             com_err(uss_whoami, code, "while accessing Protection Server");
203             return(code);
204         }
205     } /*Create the user's protection entry*/
206     
207     sprintf(a_uid, "%d", id);
208     if (uss_verbose)
209         fprintf(stderr, "The uid for user '%s' is %s\n", a_user, a_uid);
210     
211     /*
212      * Return sweetness & light.
213      */
214     return(0);
215
216 } /*uss_ptserver_AddUser*/
217
218
219 /*-----------------------------------------------------------------------
220  * EXPORTED uss_ptserver_DelUser
221  *
222  * Environment:
223  *      Nothing interesting.
224  *
225  * Side Effects:
226  *      As advertised.
227  *------------------------------------------------------------------------*/
228
229 afs_int32 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     } /*Error in deletion*/
276     
277     /*
278      * Return sweetness & light.
279      */
280     return(0);
281
282 } /*uss_ptserver_DelUser*/
283
284
285 /*-----------------------------------------------------------------------
286  * EXPORTED uss_ptserver_XlateUser
287  *
288  * Environment:
289  *      Nothing interesting.
290  *
291  * Side Effects:
292  *      As advertised.
293  *------------------------------------------------------------------------*/
294
295 afs_int32 uss_ptserver_XlateUser(a_user, a_uidP)
296     char *a_user;
297     afs_int32 *a_uidP;
298
299 { /*uss_ptserver_XlateUser*/
300
301     static char rn[] = "uss_ptserver_XlateUser"; /*Routine name*/
302     register afs_int32 code;                             /*Various return codes*/
303
304     if (uss_verbose)
305         fprintf(stderr, "Translating user '%s' via the Protection DB\n",
306                 a_user);
307
308     /*
309      * Make sure we're initialized before doing anything.
310      */
311     if (!initDone) {
312         code = InitThisModule();
313         if (code)
314             return(code);
315     }
316
317     /*
318      * Note: pr_SNameToId ONLY returns a non-zero error code
319      * for a major problem, like a network partition, so we
320      * have to explicitly check the ID returned against
321      * ANONYMOUSID, which is what we get when there is no
322      * ID known for the user name.
323      */
324     *a_uidP = 0;
325     code = pr_SNameToId(a_user, a_uidP);
326     if (code) {
327         com_err(uss_whoami, code, "while getting uid from Protection DB");
328         return(code);
329     }
330     if (*a_uidP == ANONYMOUSID) {
331         fprintf(stderr,
332                 "%s: No entry for user '%s' in the Protection DB\n",
333                 uss_whoami, a_user);
334         return(code);
335     }
336
337     /*
338      * Return sweetness & light.
339      */
340 #ifdef USS_PTSERVER_DB
341     printf("%s: User '%s' maps to uid %d\n", rn, a_user, *a_uidP);
342 #endif /* USS_PTSERVER_DB */
343     return(0);
344
345 } /*uss_ptserver_XlateUser*/