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