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