include-afsconfig-before-param-h-20010712
[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 extern char *rindex();
34
35
36 /*
37  * ---------------------- Private definitions ---------------------
38  */
39 #define uss_ptserver_MAX_SIZE   2048
40
41
42 /*
43  * ------------------------ Private globals -----------------------
44  */
45 static int initDone = 0;                /*Module initialized?*/
46
47
48 /*-----------------------------------------------------------------------
49  * static InitThisModule
50  *
51  * Description:
52  *      Set up this module, namely make the connection to the Protection
53  *      Server.
54  *
55  * Arguments:
56  *      None.
57  *
58  * Returns:
59  *      0 if everything went fine, or
60  *      lower-level error code otherwise.
61  *
62  * Environment:
63  *      This routine will only be called once.
64  *
65  * Side Effects:
66  *      As advertised.
67  *------------------------------------------------------------------------*/
68
69 static afs_int32 InitThisModule()
70
71 { /*InitThisModule*/
72
73     static char rn[] =
74         "uss_ptserver:InitThisModule";  /*Routine name*/
75     register afs_int32 code;                    /*Return code*/
76
77     /*
78      * Only once, guys.
79      */
80     if (initDone)
81         return(0);
82
83     /*
84      * Connect up with the Protection Server.
85      */
86 #ifdef USS_PTSERVER_DB
87     printf("%s: Initializing Protection Server: security=1, confdir = '%s', cell = '%s'\n",
88            rn, uss_ConfDir, uss_Cell);
89 #endif /* USS_PTSERVER_DB */
90     code = pr_Initialize(1,             /*Security level*/
91                          uss_ConfDir,   /*Config directory*/
92                          uss_Cell);     /*Cell to touch*/
93     if (code) {
94         com_err(uss_whoami, code,
95                 "while initializing Protection Server library");
96         return(code);
97     }
98
99     initDone = 1;
100     return(0);
101
102 } /*InitThisModule*/
103
104
105 /*-----------------------------------------------------------------------
106  * EXPORTED uss_ptserver_AddUser
107  *
108  * Environment:
109  *      The common DesiredUID variable, if non-zero, is the value
110  *      desired for the user's uid.
111  *
112  * Side Effects:
113  *      As advertised.
114  *------------------------------------------------------------------------*/
115
116 afs_int32 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",
128                 a_user);
129         if (id)
130             fprintf(stderr, "\t[Presetting uid to %d]\n", id);
131     }
132     
133     /*
134      * Make sure we're initialized before doing anything.
135      */
136     if (!initDone) {
137         code = InitThisModule();
138         if (code)
139             return(code);
140     }
141     
142     /*
143      * If this is a dry run, we still need to setup the uid before
144      * returning.
145      */
146     if (uss_DryRun) {
147         fprintf(stderr, "\t[Dry run - user %d not created]\n", 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
192                 if (mappedUserID != id) {
193                     fprintf(stderr,
194                             "%s: User '%s' already has id %d; won't assign id %d\n",
195                             uss_whoami, a_user, mappedUserID, id);
196                     return(PRIDEXIST);
197                 }
198         }
199         else {
200             /*
201              * Got a fatal error.
202              */
203             com_err(uss_whoami, code, "while accessing Protection Server");
204             return(code);
205         }
206     } /*Create the user's protection entry*/
207     
208     sprintf(a_uid, "%d", id);
209     if (uss_verbose)
210         fprintf(stderr, "The uid for user '%s' is %s\n", a_user, a_uid);
211     
212     /*
213      * Return sweetness & light.
214      */
215     return(0);
216
217 } /*uss_ptserver_AddUser*/
218
219
220 /*-----------------------------------------------------------------------
221  * EXPORTED uss_ptserver_DelUser
222  *
223  * Environment:
224  *      Nothing interesting.
225  *
226  * Side Effects:
227  *      As advertised.
228  *------------------------------------------------------------------------*/
229
230 afs_int32 uss_ptserver_DelUser(a_name)
231     char *a_name;
232
233 { /*uss_ptserver_DelUser*/
234
235     afs_int32 code;                             /*Various return codes*/
236     
237     /*
238      * Make sure we're initialized before doing anything.
239      */
240     if (!initDone) {
241         code = InitThisModule();
242         if (code)
243             return(code);
244     }
245
246     if (uss_DryRun) {
247         fprintf(stderr,
248                 "\t[Dry run - user '%s' not deleted from Protection DB]\n",
249                 a_name);
250         return(0);
251     }
252
253     if (uss_verbose)
254         fprintf(stderr, "Deleting user '%s' from the Protection DB\n",
255                 a_name);
256
257     /*
258      * Go ahead and delete the user.
259      */
260     code = pr_Delete(a_name);
261     if (code) {
262         if (code == PRNOENT)  {
263             /*
264              * There's no entry for that user in the Protection DB,
265              * so our job is done.
266              */
267             fprintf(stderr,
268                     "%s: Warning: User '%s' not found in Protection DB\n",
269                     uss_whoami, a_name);
270         } /*User not registered*/
271         else {
272             com_err(uss_whoami, code,
273                     "while deleting user from Protection DB");
274             return(code);
275         } /*Fatal PTS error*/
276     } /*Error in deletion*/
277     
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 uss_ptserver_XlateUser(a_user, a_uidP)
297     char *a_user;
298     afs_int32 *a_uidP;
299
300 { /*uss_ptserver_XlateUser*/
301
302     static char rn[] = "uss_ptserver_XlateUser"; /*Routine name*/
303     register afs_int32 code;                             /*Various return codes*/
304
305     if (uss_verbose)
306         fprintf(stderr, "Translating user '%s' via the Protection DB\n",
307                 a_user);
308
309     /*
310      * Make sure we're initialized before doing anything.
311      */
312     if (!initDone) {
313         code = InitThisModule();
314         if (code)
315             return(code);
316     }
317
318     /*
319      * Note: pr_SNameToId ONLY returns a non-zero error code
320      * for a major problem, like a network partition, so we
321      * have to explicitly check the ID returned against
322      * ANONYMOUSID, which is what we get when there is no
323      * ID known for the user name.
324      */
325     *a_uidP = 0;
326     code = pr_SNameToId(a_user, a_uidP);
327     if (code) {
328         com_err(uss_whoami, code, "while getting uid from Protection DB");
329         return(code);
330     }
331     if (*a_uidP == ANONYMOUSID) {
332         fprintf(stderr,
333                 "%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*/