Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
9.  NIS+ Programming Guide NIS+ API  Previous   Contents   Next 
   
 

NIS+ Sample Program

This sample program performs the following tasks:

  • Determines the local principal and local domain

  • Looks up the local directory object

  • Creates a directory called foo under the local domain

  • Creates the groups_dir and org_dir directories under domain foo

  • Creates a group object admins.foo

  • Adds the local principal to the admins group

  • Creates a table under org_dir.foo

  • Adds two entries to the org_dir.foo table

  • Retrieves and displays the new membership list of the admins group

  • Lists the namespace under the foo domain using callbacks

  • Lists the contents of the table created using callbacks

  • Cleans up all the objects that were created by removing the following:

    • The local principal from the admins group

    • The admins group

    • The entries in the table followed by the table

    • The groups_dir and org_dir directory objects

    • The foo directory object

The example program is not a typical application. In a normal situation the directories and tables would be created or removed through the command line interface, and applications would manipulate NIS+ entry objects.

Unsupported Macros

The sample program uses unsupported macros that are defined in the file <rpcsvc/nis.h>. These macros are not public APIs and can change or disappear in the future. They are used for illustration purposes only and if you choose to use them, you do so at your own risk. The macros used are:

  • NIS_RES_OBJECT

  • ENTRY_VAL

  • DEFAULT_RIGHTS

Functions Used in the Example

The use of the following NIS+ C API functions is illustrated through this example.

nis_add()
nis_add_entry()
nis_addmember()
nis_creategroup()
nis_destroygroup()
nis_domain_of()
nis_freeresult()
nis_leaf_of()
nis_list()
nis_local_directory()
nis_local_principal()
nis_lookup()
nis_mkdir()
nis_perror()
nis_remove()
nis_remove_entry()
nis_removemember()

Program Compilation

The NIS+ principal running this application has permission to create directory objects in the local domain. The program is compiled by typing:

% cc -o example.c example -lnsl

It is invoked by typing:

% example [dir]

where dir is the NIS+ directory in which the program creates all the NIS+ objects. Specifying no directory argument causes the objects to be created in the parent directory of the local domain. Note that for the call to nis_lookup(), a space and the name of the local domain are appended to the string that names the directory. The argument is the name of the NIS+ directory in which to create the NIS+ objects. The principal running this program should have create permission in the directory.

The following code example shows the routine is called by main() to create directory objects.


Example 9-1 NIS+ Routine to Create Directory Objects

void
dir_create (dir_name, dirobj)
	nis_name			dir_name;
	nis_object			*dirobj;
{
	nis_result 	*cres;
	nis_error			err;

	printf ("\n Adding Directory %s to namespace ... \n",
dir_name);
	cres = nis_add (dir_name, dirobj);

	if (cres->status != NIS_SUCCESS) {
		nis_perror (cres->status, "unable to add directory foo.");
		exit (1);
	}

	(void) nis_freeresult (cres);

	/*
	 * NOTE: you need to do a nis_mkdir to create the table to
store the
	 * contents of the directory you are creating.
	 */
	err = nis_mkdir (dir_name,

						dirobj->DI_data.do_servers.do_servers_val);
	if (err != NIS_SUCCESS) {
		(void) nis_remove (dir_name, 0);

		nis_perror (err,
				"unable to create table for  directory object foo.");
		exit (1);
	}
}

 
 
 
  Previous   Contents   Next