Initial import of the CDE 2.1.30 sources from the Open Group.
[oweals/cde.git] / cde / programs / dtcm / dtcm / namesvc.c
1 /*******************************************************************************
2 **
3 **  namesvc.c
4 **
5 **  $XConsortium: namesvc.c /main/3 1995/11/03 10:30:13 rswiston $
6 **
7 **  RESTRICTED CONFIDENTIAL INFORMATION:
8 **
9 **  The information in this document is subject to special
10 **  restrictions in a confidential disclosure agreement between
11 **  HP, IBM, Sun, USL, SCO and Univel.  Do not distribute this
12 **  document outside HP, IBM, Sun, USL, SCO, or Univel without
13 **  Sun's specific written approval.  This document and all copies
14 **  and derivative works thereof must be returned or destroyed at
15 **  Sun's request.
16 **
17 **  Copyright 1993 Sun Microsystems, Inc.  All rights reserved.
18 **
19 *******************************************************************************/
20
21 /*                                                                      *
22  * (c) Copyright 1993, 1994 Hewlett-Packard Company                     *
23  * (c) Copyright 1993, 1994 International Business Machines Corp.       *
24  * (c) Copyright 1993, 1994 Sun Microsystems, Inc.                      *
25  * (c) Copyright 1993, 1994 Novell, Inc.                                *
26  */
27
28 #ifndef lint
29 static  char sccsid[] = "@(#)namesvc.c 1.5 94/11/07 Copyr 1993 Sun Microsystems, Inc.";
30 #endif
31
32 #if 0
33
34 #include <rpcsvc/nis.h>
35 #include <rpcsvc/ypclnt.h>
36 #include <rpcsvc/yp_prot.h>
37 #include "util.h"
38 #include "namesvc.h"
39 #include "gettext.h"
40
41 extern int debug;
42
43 #define MAPNAME "mail.aliases"
44
45 static char *
46 get_userscontext()
47 {
48         static char *userscontext;
49
50         if (userscontext == NULL) {
51                 if ((userscontext = cm_strdup(
52                         (char *)getenv("USERSCONTEXT"))) == NULL)
53                         userscontext = USERSCONTEXT;
54         }
55         return(userscontext);
56 }
57
58
59 static int
60 get_principal(table, ent, udata)
61 char *table;
62 nis_object *ent;
63 void *udata;
64 {
65         char **principal = (char **)udata;
66         entry_col *ec = ent->EN_data.en_cols.en_cols_val;
67         int ncol = ent->EN_data.en_cols.en_cols_len;
68
69         if (ec[PRINCIPALCOLNUM].ec_value.ec_value_len)
70                 *principal = cm_strdup(ec[PRINCIPALCOLNUM].ec_value.ec_value_val);
71         else    *principal = NULL;
72         return(1);
73 }
74
75 extern Lookup_stat
76 cm_get_nis_location(name, calname, host)
77 char *name;
78 char *calname;
79 char **host;
80 {
81         nis_result *tres, *eres;
82         char cmtbl[NIS_MAXNAMELEN];
83         char indexname[NIS_MAXNAMELEN];
84         char *user, *domain;
85         Lookup_stat res;
86         u_long tflag = EXPAND_NAME;
87         entry_col *ec;
88         int ncol;
89         nis_object *nis_obj;
90
91         *host = NULL;
92         user = get_head(name, '@');
93         domain = get_tail(name, '@');
94         sprintf(cmtbl, "%s.%s.%s", USERMAP, user, get_userscontext());
95         if (domain) {
96                 strcat(cmtbl, ".");
97                 strcat(cmtbl, domain);
98         }
99
100         tres = nis_lookup(cmtbl, tflag);
101         if (tres->status != NIS_SUCCESS) {
102                 nis_freeresult(tres);
103                 return(l_no_table);
104         }
105
106         sprintf(indexname, "[%s=%s],%s.", NAMECOLNAME, calname,
107                 tres->objects.objects_val[0].zo_name);
108         if (*(tres->objects.objects_val[0].zo_domain) != '.')
109                 strcat(indexname, tres->objects.objects_val[0].zo_domain);
110
111         eres = nis_list(indexname, tflag, 0, 0);
112
113         if (eres->status == NIS_SUCCESS || eres->status == NIS_CBRESULTS) {
114                 nis_obj = NIS_RES_OBJECT(eres);
115                 if (nis_obj == NULL)  
116                         res = l_nis_error;
117                 else {
118                         ec = nis_obj->EN_data.en_cols.en_cols_val;
119                         ncol = nis_obj->EN_data.en_cols.en_cols_len;
120                         if (ncol >= NUMTBLCOLS && ec != NULL &&
121                                 ec[LOCATIONCOLNUM].ec_value.ec_value_len) {
122                                 *host = cm_strdup(
123                                         ec[LOCATIONCOLNUM].ec_value.ec_value_val);
124                                 res = l_ok;
125                         }
126                         else {  
127                                 *host = NULL;
128                                 res = l_nis_error;
129                         }
130                 }
131         }
132         else {
133                 if (debug)
134                         nis_perror(tres->status, "nis_list");
135                 res = l_nis_error;
136         }
137         nis_freeresult(tres);
138         nis_freeresult(eres);
139         return(res);
140 }
141
142 extern char *
143 netname2principal(netname)
144 char *netname;
145 {
146         nis_result *eres;
147         char indexname[NIS_MAXNAMELEN];
148         char *domain;
149         u_long tflag = EXPAND_NAME;
150         char *principal = NULL;
151
152         domain = get_tail(netname, '@');
153         if (domain == NULL)
154                 return(NULL);
155
156         sprintf(indexname, "[%s=%s],%s.%s", AUTHNAMECOLNAME, netname,
157                         CREDDIR, domain);
158         eres = nis_list(indexname, tflag, get_principal, (void *)&principal);
159         if (eres->status != NIS_SUCCESS && eres->status != NIS_CBRESULTS)
160                 if (debug)
161                         nis_perror(eres->status, "nis_list");
162         nis_freeresult(eres);
163         return(principal);
164 }
165
166 Lookup_stat
167 test_cm_get_target(name, name2)
168         char *name, **name2;
169 {
170         FILE *f;
171         char    *b, *tmp, *uname, *ptr, buffer[BUFSIZ];
172         char *tail;
173         Boolean domain = False;
174
175         *name2 = NULL;
176
177         f = fopen("/home2/curry/myds/src/cm2/testfile", "r");
178         tail = get_tail(name, '@');
179         if (tail != NULL && strcasecmp(tail, "eng") == 0) {
180                 domain = True;
181                 free(tail);
182         }
183         uname = cm_target2name(name);
184         while (fgets(buffer, BUFSIZ, f)) {
185                 b = (char*)cm_strdup(buffer);
186                 tmp = (char*)strtok(b, " ");
187                 if (strcmp(uname, tmp) == 0) {
188                         ptr = (char*)strrchr(buffer, ' ');
189                         ptr++;
190                         ptr[strlen(ptr)-1] = 0;
191                         *name2 = ckalloc(cm_strlen(name) + cm_strlen(ptr) + 7);
192                         if (domain) 
193                                 sprintf(*name2, "%s@%s.eng", uname, ptr);       
194                         else
195                                 sprintf(*name2, "%s@%s", uname, ptr);
196                         free(uname); free(b);
197                         fclose(f);
198                         return l_ok; 
199                 }
200                 free(b);
201         }
202         fclose(f);
203         free(uname);
204         return l_other;
205 }
206
207 /* principal.domain -> principal */
208 extern char *
209 principal_to_user(principal)
210 char *principal;
211 {
212         return(get_head(principal, '.'));
213 }
214
215 /* principal.domain -> domain */
216 extern char *
217 principal_to_domain(principal)
218 char *principal;
219 {
220         return(get_tail(principal, '.'));
221 }
222 static void
223 new_entryobj(nobj, loc)
224         nis_object *nobj;
225         char    *loc;
226 {
227         entry_col *ec;
228         entry_obj *eo;
229
230         nobj->zo_data.zo_type = ENTRY_OBJ;
231         eo = &(nobj->EN_data);
232         eo->en_cols.en_cols_len = NUMTBLCOLS;
233         eo->en_type = "cm_location_tbl";
234         ec = (entry_col*)ckalloc(sizeof(entry_col) * NUMTBLCOLS);
235         eo->en_cols.en_cols_val = ec;
236         ec[NAMECOLNUM].ec_value.ec_value_len = cm_strlen(DEFAULT_CALNAME)+1; 
237         ec[NAMECOLNUM].ec_value.ec_value_val = DEFAULT_CALNAME;
238         ec[NAMECOLNUM].ec_flags = 0;
239         ec[LOCATIONCOLNUM].ec_value.ec_value_len = cm_strlen(loc)+1;
240         ec[LOCATIONCOLNUM].ec_value.ec_value_val = cm_strdup(loc); 
241         ec[LOCATIONCOLNUM].ec_flags = 0;
242 }
243 static nis_object*
244 new_nisobj()
245 {
246         nis_object *nobj;
247
248         nobj = (nis_object*)ckalloc(sizeof(nis_object));
249         nobj->zo_owner = nis_local_principal();
250         nobj->zo_group = nis_local_group();
251         nobj->zo_access = DEFAULT_RIGHTS; 
252         nobj->zo_ttl = 43200;
253         
254         return nobj;
255 }
256
257 extern Lookup_stat
258 cm_set_nis_location(name, calname, loc)
259 char *name;
260 char *calname;
261 char *loc;
262 {
263         nis_result *eres, *mres, *ares;
264         char cmtbl[NIS_MAXNAMELEN];
265         char indexname[NIS_MAXNAMELEN];
266         char *user, *domain;
267         Lookup_stat res = l_ok;
268         u_long tflag = EXPAND_NAME;
269         nis_object *new_obj, *nis_obj;
270         entry_col *ec;
271         int ncol;
272
273         user = get_head(name, '@');
274         domain = get_tail(name, '@');
275         sprintf(cmtbl, "%s.%s.%s", USERMAP, user, get_userscontext());
276         if (domain) {
277                 strcat(cmtbl, ".");
278                 strcat(cmtbl, domain);
279         }
280         else {
281                 strcat(cmtbl, ".");
282                 strcat(cmtbl, cm_get_local_domain());
283         }
284
285         eres = nis_lookup(cmtbl, tflag);
286         if (eres->status != NIS_SUCCESS) {
287                 nis_freeresult(eres);
288                 return(l_no_table);
289         }
290
291         sprintf(indexname, "[%s=%s],%s.", NAMECOLNAME, calname,
292                 eres->objects.objects_val[0].zo_name);
293         if (*(eres->objects.objects_val[0].zo_domain) != '.')
294                 strcat(indexname, eres->objects.objects_val[0].zo_domain);
295         nis_freeresult(eres);
296
297         eres = nis_list(indexname, tflag, 0, 0);
298
299         if (eres->status == NIS_SUCCESS || eres->status == NIS_CBRESULTS) {
300                 nis_obj = NIS_RES_OBJECT(eres);
301                 if (nis_obj == NULL) 
302                         res = l_nis_error;
303                 else {
304                         ncol = nis_obj->EN_data.en_cols.en_cols_len;
305                         ec = nis_obj->EN_data.en_cols.en_cols_val;
306                         if (ncol >= NUMTBLCOLS && ec != NULL &&
307                                 ec[LOCATIONCOLNUM].ec_value.ec_value_len) {
308                                 new_obj = nis_clone_object(nis_obj, NULL);
309                                 ec = new_obj->EN_data.en_cols.en_cols_val;
310                                 ec[LOCATIONCOLNUM].ec_flags = EN_MODIFIED;
311                                 free(ec[LOCATIONCOLNUM].ec_value.ec_value_val);
312                                 ec[LOCATIONCOLNUM].ec_value.ec_value_val = 
313                                         cm_strdup(loc);
314                                 ec[LOCATIONCOLNUM].ec_value.ec_value_len = 
315                                         cm_strlen(loc)+1;
316                                 mres = nis_modify_entry(indexname, new_obj, 0);
317                                 if (mres->status != NIS_SUCCESS) {
318                                         if (debug)
319                                                 nis_perror(eres->status, "nis_modify_entry");  
320                                         res = l_nis_error;
321                                 }
322                                 nis_destroy_object(new_obj);
323                         }
324                         else 
325                                 res = l_nis_error;
326                         nis_freeresult(mres);
327                 }
328         }
329         else if (eres->status == NIS_NOTFOUND) {
330                 /* table exists but no entry; add new entry to NIS+ cm table*/
331                 nis_obj = (nis_object*)new_nisobj();
332                 new_entryobj(nis_obj, loc);
333                 ares = nis_add_entry(cmtbl, nis_obj, 0);
334                 if (ares->status != NIS_SUCCESS) {
335                         if (debug)
336                                 nis_perror(eres->status, "nis_add_entry");
337                         res = l_nis_error;
338                 }
339                 nis_destroy_object(nis_obj);
340                 nis_freeresult(ares);
341         }
342         else {
343                 if (debug)
344                         nis_perror(eres->status, "nis_list");
345                 res = l_nis_error;
346         }
347         nis_freeresult(eres);
348
349         return(res);
350 }
351 /*
352  * user specification -> calendar_name@host[.domain]
353  */
354
355 extern void
356 cm_get_target(name, target)
357 char *name;
358 char **target;
359 {
360         char *user, *domain;
361         int status=0;
362         char *host;
363
364         *target = NULL;
365  
366         if (name == NULL)
367                 return(l_other);
368
369         test_cm_get_target(name, target);
370         return;
371
372         /* assume user@domain and do nis+ lookup */
373         status = cm_get_nis_location(name, DEFAULT_CALNAME, &host);
374
375         if (status == l_ok) {
376                 /*
377                  * Nis+ table exists.
378                  * If we cannot find the location, default to local host.
379                  * Otherwise, build the calendar name. For now,
380                  * calendar name is the user login name.
381                  */
382                 if (host) {
383                         *target = (char *)ckalloc(cm_strlen(name) +
384                                         cm_strlen(host) + 3);
385                         user = get_head(name, '@');
386                         domain = get_tail(name, '@');
387                         sprintf(*target, "%s@%s", user, host);
388                         if (domain) {
389                                 strcat(*target, ".");
390                                 strcat(*target, domain);
391                         }
392                         free(user);
393                         free(domain);
394                         free(host);
395                 } else  {
396                         *target = NULL;
397                         status  = l_nis_error;  
398                 }
399         }
400         else {
401                 /*
402                  * Assume nis+ is not use, or no entry in table.
403                  * name = user@host[.domain]
404                  * If host is not specified, default to local host.
405                  */
406                 *target = NULL;
407         }
408         return(status);
409 }
410
411 #define YP_SUCCESS 0
412
413 extern int
414 cm_get_yptarget(name, name2)
415         char *name, **name2;
416 {
417         char *domain;
418         int outlen, inlen, status=0;
419         char *save_name=NULL, *tmp=NULL, *tmp2=NULL, *ptr;
420
421         *name2 = NULL;
422         domain = cm_get_local_domain();
423         inlen = cm_strlen(name)+1;
424         status = yp_match(domain, MAPNAME, name, inlen, &tmp, &outlen);
425         if (status != YP_SUCCESS)
426                 return YP_SUCCESS;
427         /* Name was mapped successfully, so continue; may be more than 1 */
428         /* Lots of white space in map for some dumb reason */
429         while (status == YP_SUCCESS) {
430                 save_name = tmp;
431                 /* get rid of beginning white space */
432                 while (tmp != NULL && *tmp == ' ') tmp++;
433                 /* get rid of trailing white space */
434                 for (ptr = tmp+cm_strlen(tmp)-1;
435                          ptr != NULL && *ptr == ' '; *ptr = 0, ptr--);
436                 inlen = cm_strlen(tmp)+1;
437                 status = yp_match(domain, MAPNAME, tmp, inlen, &tmp2, &outlen);
438                 /* no more maps so return value */
439                 if (status != YP_SUCCESS)
440                         *name2 = cm_strdup(tmp);
441                 else
442                         tmp = tmp2;
443                 free(save_name);
444         }
445         return 1;
446 }
447 #endif