1 /*******************************************************************************
5 ** $XConsortium: namesvc.c /main/3 1995/11/03 10:30:13 rswiston $
7 ** RESTRICTED CONFIDENTIAL INFORMATION:
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
17 ** Copyright 1993 Sun Microsystems, Inc. All rights reserved.
19 *******************************************************************************/
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. *
29 static char sccsid[] = "@(#)namesvc.c 1.5 94/11/07 Copyr 1993 Sun Microsystems, Inc.";
34 #include <rpcsvc/nis.h>
35 #include <rpcsvc/ypclnt.h>
36 #include <rpcsvc/yp_prot.h>
43 #define MAPNAME "mail.aliases"
48 static char *userscontext;
50 if (userscontext == NULL) {
51 if ((userscontext = cm_strdup(
52 (char *)getenv("USERSCONTEXT"))) == NULL)
53 userscontext = USERSCONTEXT;
60 get_principal(table, ent, udata)
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;
69 if (ec[PRINCIPALCOLNUM].ec_value.ec_value_len)
70 *principal = cm_strdup(ec[PRINCIPALCOLNUM].ec_value.ec_value_val);
71 else *principal = NULL;
76 cm_get_nis_location(name, calname, host)
81 nis_result *tres, *eres;
82 char cmtbl[NIS_MAXNAMELEN];
83 char indexname[NIS_MAXNAMELEN];
86 u_long tflag = EXPAND_NAME;
92 user = get_head(name, '@');
93 domain = get_tail(name, '@');
94 sprintf(cmtbl, "%s.%s.%s", USERMAP, user, get_userscontext());
97 strcat(cmtbl, domain);
100 tres = nis_lookup(cmtbl, tflag);
101 if (tres->status != NIS_SUCCESS) {
102 nis_freeresult(tres);
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);
111 eres = nis_list(indexname, tflag, 0, 0);
113 if (eres->status == NIS_SUCCESS || eres->status == NIS_CBRESULTS) {
114 nis_obj = NIS_RES_OBJECT(eres);
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) {
123 ec[LOCATIONCOLNUM].ec_value.ec_value_val);
134 nis_perror(tres->status, "nis_list");
137 nis_freeresult(tres);
138 nis_freeresult(eres);
143 netname2principal(netname)
147 char indexname[NIS_MAXNAMELEN];
149 u_long tflag = EXPAND_NAME;
150 char *principal = NULL;
152 domain = get_tail(netname, '@');
156 sprintf(indexname, "[%s=%s],%s.%s", AUTHNAMECOLNAME, netname,
158 eres = nis_list(indexname, tflag, get_principal, (void *)&principal);
159 if (eres->status != NIS_SUCCESS && eres->status != NIS_CBRESULTS)
161 nis_perror(eres->status, "nis_list");
162 nis_freeresult(eres);
167 test_cm_get_target(name, name2)
171 char *b, *tmp, *uname, *ptr, buffer[BUFSIZ];
173 Boolean domain = False;
177 f = fopen("/home2/curry/myds/src/cm2/testfile", "r");
178 tail = get_tail(name, '@');
179 if (tail != NULL && strcasecmp(tail, "eng") == 0) {
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, ' ');
190 ptr[strlen(ptr)-1] = 0;
191 *name2 = ckalloc(cm_strlen(name) + cm_strlen(ptr) + 7);
193 sprintf(*name2, "%s@%s.eng", uname, ptr);
195 sprintf(*name2, "%s@%s", uname, ptr);
196 free(uname); free(b);
207 /* principal.domain -> principal */
209 principal_to_user(principal)
212 return(get_head(principal, '.'));
215 /* principal.domain -> domain */
217 principal_to_domain(principal)
220 return(get_tail(principal, '.'));
223 new_entryobj(nobj, loc)
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;
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;
258 cm_set_nis_location(name, calname, loc)
263 nis_result *eres, *mres, *ares;
264 char cmtbl[NIS_MAXNAMELEN];
265 char indexname[NIS_MAXNAMELEN];
267 Lookup_stat res = l_ok;
268 u_long tflag = EXPAND_NAME;
269 nis_object *new_obj, *nis_obj;
273 user = get_head(name, '@');
274 domain = get_tail(name, '@');
275 sprintf(cmtbl, "%s.%s.%s", USERMAP, user, get_userscontext());
278 strcat(cmtbl, domain);
282 strcat(cmtbl, cm_get_local_domain());
285 eres = nis_lookup(cmtbl, tflag);
286 if (eres->status != NIS_SUCCESS) {
287 nis_freeresult(eres);
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);
297 eres = nis_list(indexname, tflag, 0, 0);
299 if (eres->status == NIS_SUCCESS || eres->status == NIS_CBRESULTS) {
300 nis_obj = NIS_RES_OBJECT(eres);
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 =
314 ec[LOCATIONCOLNUM].ec_value.ec_value_len =
316 mres = nis_modify_entry(indexname, new_obj, 0);
317 if (mres->status != NIS_SUCCESS) {
319 nis_perror(eres->status, "nis_modify_entry");
322 nis_destroy_object(new_obj);
326 nis_freeresult(mres);
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) {
336 nis_perror(eres->status, "nis_add_entry");
339 nis_destroy_object(nis_obj);
340 nis_freeresult(ares);
344 nis_perror(eres->status, "nis_list");
347 nis_freeresult(eres);
352 * user specification -> calendar_name@host[.domain]
356 cm_get_target(name, target)
369 test_cm_get_target(name, target);
372 /* assume user@domain and do nis+ lookup */
373 status = cm_get_nis_location(name, DEFAULT_CALNAME, &host);
375 if (status == l_ok) {
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.
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);
389 strcat(*target, ".");
390 strcat(*target, domain);
397 status = l_nis_error;
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.
414 cm_get_yptarget(name, name2)
418 int outlen, inlen, status=0;
419 char *save_name=NULL, *tmp=NULL, *tmp2=NULL, *ptr;
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)
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) {
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);