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