death to register
[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
22 #include "uss_ptserver.h"       /*Module interface */
23 #include <afs/ptclient.h>       /*Protection Server client interface */
24 #include <afs/pterror.h>        /*Protection Server error codes */
25 #include <afs/ptuser.h>
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
68 InitThisModule(void)
69 {                               /*InitThisModule */
70
71 #ifdef USS_PTSERVER_DB
72     static char rn[] = "uss_ptserver:InitThisModule";   /*Routine name */
73 #endif
74     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
87         ("%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         afs_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
117 uss_ptserver_AddUser(char *a_user, char *a_uid)
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", a_user);
126         if (id)
127             fprintf(stderr, "\t[Presetting uid to %d]\n", id);
128     }
129
130     /*
131      * Make sure we're initialized before doing anything.
132      */
133     if (!initDone) {
134         code = InitThisModule();
135         if (code)
136             return (code);
137     }
138
139     /*
140      * If this is a dry run, we still need to setup the uid before
141      * returning.
142      */
143     if (uss_DryRun) {
144         fprintf(stderr, "\t[Dry run - user %d not created]\n",
145                 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                 afs_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 if (mappedUserID != id) {
190                 fprintf(stderr,
191                         "%s: User '%s' already has id %d; won't assign id %d\n",
192                         uss_whoami, a_user, mappedUserID, id);
193                 return (PRIDEXIST);
194             }
195         } else {
196             /*
197              * Got a fatal error.
198              */
199             afs_com_err(uss_whoami, code, "while accessing Protection Server");
200             return (code);
201         }
202     }
203     /*Create the user's protection entry */
204     sprintf(a_uid, "%d", id);
205     if (uss_verbose)
206         fprintf(stderr, "The uid for user '%s' is %s\n", a_user, a_uid);
207
208     /*
209      * Return sweetness & light.
210      */
211     return (0);
212
213 }                               /*uss_ptserver_AddUser */
214
215
216 /*-----------------------------------------------------------------------
217  * EXPORTED uss_ptserver_DelUser
218  *
219  * Environment:
220  *      Nothing interesting.
221  *
222  * Side Effects:
223  *      As advertised.
224  *------------------------------------------------------------------------*/
225
226 afs_int32
227 uss_ptserver_DelUser(char *a_name)
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             afs_com_err(uss_whoami, code,
268                     "while deleting user from Protection DB");
269             return (code);
270         }                       /*Fatal PTS error */
271     }
272
273     /*Error in deletion */
274     /*
275      * Return sweetness & light.
276      */
277     return (0);
278
279 }                               /*uss_ptserver_DelUser */
280
281
282 /*-----------------------------------------------------------------------
283  * EXPORTED uss_ptserver_XlateUser
284  *
285  * Environment:
286  *      Nothing interesting.
287  *
288  * Side Effects:
289  *      As advertised.
290  *------------------------------------------------------------------------*/
291
292 afs_int32
293 uss_ptserver_XlateUser(char *a_user, afs_int32 *a_uidP)
294 {                               /*uss_ptserver_XlateUser */
295 #ifdef USS_PTSERVER_DB
296     static char rn[] = "uss_ptserver_XlateUser";        /*Routine name */
297 #endif
298     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         afs_com_err(uss_whoami, code, "while getting uid from Protection DB");
324         return (code);
325     }
326     if (*a_uidP == ANONYMOUSID) {
327         fprintf(stderr, "%s: No entry for user '%s' in the Protection DB\n",
328                 uss_whoami, a_user);
329         return (code);
330     }
331
332     /*
333      * Return sweetness & light.
334      */
335 #ifdef USS_PTSERVER_DB
336     printf("%s: User '%s' maps to uid %d\n", rn, a_user, *a_uidP);
337 #endif /* USS_PTSERVER_DB */
338     return (0);
339
340 }                               /*uss_ptserver_XlateUser */