2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /*******************************************************************************
27 ** $XConsortium: namesvc.c /main/3 1995/11/03 10:30:13 rswiston $
29 ** RESTRICTED CONFIDENTIAL INFORMATION:
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
39 ** Copyright 1993 Sun Microsystems, Inc. All rights reserved.
41 *******************************************************************************/
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. *
51 static char sccsid[] = "@(#)namesvc.c 1.5 94/11/07 Copyr 1993 Sun Microsystems, Inc.";
56 #include <rpcsvc/nis.h>
57 #include <rpcsvc/ypclnt.h>
58 #include <rpcsvc/yp_prot.h>
65 #define MAPNAME "mail.aliases"
70 static char *userscontext;
72 if (userscontext == NULL) {
73 if ((userscontext = cm_strdup(
74 (char *)getenv("USERSCONTEXT"))) == NULL)
75 userscontext = USERSCONTEXT;
82 get_principal(table, ent, udata)
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;
91 if (ec[PRINCIPALCOLNUM].ec_value.ec_value_len)
92 *principal = cm_strdup(ec[PRINCIPALCOLNUM].ec_value.ec_value_val);
93 else *principal = NULL;
98 cm_get_nis_location(name, calname, host)
103 nis_result *tres, *eres;
104 char cmtbl[NIS_MAXNAMELEN];
105 char indexname[NIS_MAXNAMELEN];
108 u_long tflag = EXPAND_NAME;
114 user = get_head(name, '@');
115 domain = get_tail(name, '@');
116 sprintf(cmtbl, "%s.%s.%s", USERMAP, user, get_userscontext());
119 strcat(cmtbl, domain);
122 tres = nis_lookup(cmtbl, tflag);
123 if (tres->status != NIS_SUCCESS) {
124 nis_freeresult(tres);
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);
133 eres = nis_list(indexname, tflag, 0, 0);
135 if (eres->status == NIS_SUCCESS || eres->status == NIS_CBRESULTS) {
136 nis_obj = NIS_RES_OBJECT(eres);
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) {
145 ec[LOCATIONCOLNUM].ec_value.ec_value_val);
156 nis_perror(tres->status, "nis_list");
159 nis_freeresult(tres);
160 nis_freeresult(eres);
165 netname2principal(netname)
169 char indexname[NIS_MAXNAMELEN];
171 u_long tflag = EXPAND_NAME;
172 char *principal = NULL;
174 domain = get_tail(netname, '@');
178 sprintf(indexname, "[%s=%s],%s.%s", AUTHNAMECOLNAME, netname,
180 eres = nis_list(indexname, tflag, get_principal, (void *)&principal);
181 if (eres->status != NIS_SUCCESS && eres->status != NIS_CBRESULTS)
183 nis_perror(eres->status, "nis_list");
184 nis_freeresult(eres);
189 test_cm_get_target(name, name2)
193 char *b, *tmp, *uname, *ptr, buffer[BUFSIZ];
195 Boolean domain = False;
199 f = fopen("/home2/curry/myds/src/cm2/testfile", "r");
200 tail = get_tail(name, '@');
201 if (tail != NULL && strcasecmp(tail, "eng") == 0) {
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, ' ');
212 ptr[strlen(ptr)-1] = 0;
213 *name2 = ckalloc(cm_strlen(name) + cm_strlen(ptr) + 7);
215 sprintf(*name2, "%s@%s.eng", uname, ptr);
217 sprintf(*name2, "%s@%s", uname, ptr);
218 free(uname); free(b);
229 /* principal.domain -> principal */
231 principal_to_user(principal)
234 return(get_head(principal, '.'));
237 /* principal.domain -> domain */
239 principal_to_domain(principal)
242 return(get_tail(principal, '.'));
245 new_entryobj(nobj, loc)
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;
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;
280 cm_set_nis_location(name, calname, loc)
285 nis_result *eres, *mres, *ares;
286 char cmtbl[NIS_MAXNAMELEN];
287 char indexname[NIS_MAXNAMELEN];
289 Lookup_stat res = l_ok;
290 u_long tflag = EXPAND_NAME;
291 nis_object *new_obj, *nis_obj;
295 user = get_head(name, '@');
296 domain = get_tail(name, '@');
297 sprintf(cmtbl, "%s.%s.%s", USERMAP, user, get_userscontext());
300 strcat(cmtbl, domain);
304 strcat(cmtbl, cm_get_local_domain());
307 eres = nis_lookup(cmtbl, tflag);
308 if (eres->status != NIS_SUCCESS) {
309 nis_freeresult(eres);
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);
319 eres = nis_list(indexname, tflag, 0, 0);
321 if (eres->status == NIS_SUCCESS || eres->status == NIS_CBRESULTS) {
322 nis_obj = NIS_RES_OBJECT(eres);
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 =
336 ec[LOCATIONCOLNUM].ec_value.ec_value_len =
338 mres = nis_modify_entry(indexname, new_obj, 0);
339 if (mres->status != NIS_SUCCESS) {
341 nis_perror(eres->status, "nis_modify_entry");
344 nis_destroy_object(new_obj);
348 nis_freeresult(mres);
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) {
358 nis_perror(eres->status, "nis_add_entry");
361 nis_destroy_object(nis_obj);
362 nis_freeresult(ares);
366 nis_perror(eres->status, "nis_list");
369 nis_freeresult(eres);
374 * user specification -> calendar_name@host[.domain]
378 cm_get_target(name, target)
391 test_cm_get_target(name, target);
394 /* assume user@domain and do nis+ lookup */
395 status = cm_get_nis_location(name, DEFAULT_CALNAME, &host);
397 if (status == l_ok) {
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.
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);
411 strcat(*target, ".");
412 strcat(*target, domain);
419 status = l_nis_error;
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.
436 cm_get_yptarget(name, name2)
440 int outlen, inlen, status=0;
441 char *save_name=NULL, *tmp=NULL, *tmp2=NULL, *ptr;
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)
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) {
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);