예제 #1
0
int
main(int argc, char *argv[])
{
	char *user, *locn;
	int i, infosystem;
	
	/* since DS works for most infosystems, make it the default */
	//infosystem = INFO_NETINFO;
	infosystem = INFO_DIRECTORYSERVICES;
	user = NULL;
	locn = NULL;
	
	for (i = 1; i < argc; i++)
	{
		if (!strcmp(argv[i], "-i"))
		{
			if (++i >= argc)
			{
				fprintf(stderr, "no argument for -i option\n");
				usage();
			}

			if (!strcmp(argv[i], "NetInfo")) infosystem = INFO_NETINFO;
			else if (!strcmp(argv[i], "netinfo")) infosystem = INFO_NETINFO;
			else if (!strcmp(argv[i], "File")) infosystem = INFO_FILE;
			else if (!strcmp(argv[i], "file")) infosystem = INFO_FILE;
			else if (!strcmp(argv[i], "NIS")) infosystem = INFO_NIS;
			else if (!strcmp(argv[i], "nis")) infosystem = INFO_NIS;
			else if (!strcmp(argv[i], "YP")) infosystem = INFO_NIS;
			else if (!strcmp(argv[i], "yp")) infosystem = INFO_NIS;
			else if (!strcasecmp(argv[i], "opendirectory")) infosystem = INFO_DIRECTORYSERVICES;
			else
			{
				fprintf(stderr, "unknown info system \"%s\"\n", argv[i]);
				usage();
			}
		}

		else if (!strcmp(argv[i], "-l"))
		{
			if (++i >= argc)
			{
				fprintf(stderr, "no argument for -l option\n");
				usage();
			}
			locn = argv[i];
		}
		else if (user == NULL) user = argv[i];
		else usage();
	}

	if (user == NULL)
	{
	    	/*
		 * Verify that the login name exists.
		 * lukeh 24 Dec 1997
		 */
		if ((user = getlogin()) == NULL)
		{
			fprintf(stderr, "you don't have a login name\n");
			exit(1);
		}
	}
	
	switch (infosystem)
	{
		case INFO_NETINFO:
			netinfo_passwd(user, locn);
			break;
		case INFO_FILE:
			file_passwd(user, locn);
			break;
		case INFO_NIS:
			nis_passwd(user, locn);
			break;
		case INFO_DIRECTORYSERVICES:
			ds_passwd(user, locn);
			break;
	}

	exit(0);
}
예제 #2
0
int
main(int argc, char *argv[])
{
	char* user = NULL;
	char* locn = NULL;
	char* auth = NULL;
	int infosystem, ch;
	int free_user = 0;
	
#ifdef INFO_PAM
	infosystem = INFO_PAM;
#else
#ifdef INFO_OPEN_DIRECTORY
	infosystem = INFO_OPEN_DIRECTORY;
#else
	infosystem = INFO_FILE;
#endif
#endif

#ifdef INFO_OPEN_DIRECTORY
	/* PAM is the default infosystem, but we still want to use OpenDirectory directly when run by root */
	if (0 == getuid())
		infosystem = INFO_OPEN_DIRECTORY;
#endif

	while ((ch = getopt(argc, argv, "i:l:u:")) != -1)
		switch(ch) {
		case 'i':
			if (!strcasecmp(optarg, "file")) {
				infosystem = INFO_FILE;
#ifdef INFO_NIS
			} else if (!strcasecmp(optarg, "NIS")) {
				infosystem = INFO_NIS;
			} else if (!strcasecmp(optarg, "YP")) {
				infosystem = INFO_NIS;
#endif
#ifdef INFO_OPEN_DIRECTORY
			} else if (!strcasecmp(optarg, "opendirectory")) {
				infosystem = INFO_OPEN_DIRECTORY;
#endif
#ifdef INFO_PAM
			} else if (!strcasecmp(optarg, "PAM")) {
				infosystem = INFO_PAM;
#endif
			} else {
				fprintf(stderr, "%s: Unknown info system \'%s\'.\n",
					progname, optarg);
				usage();
			}
			break;
		case 'l':
			locn = optarg;
			break;
		case 'u':
			auth = optarg;
			break;
		case '?':
		default:
			usage();
			break;
	}
	argc -= optind;
	argv += optind;

	if (argc > 1) {
		usage();
	} else if (argc == 1) {
		user = argv[0];
	}

#ifdef INFO_PAM
	if (INFO_PAM == infosystem && NULL != locn)
		usage();
#endif

	if (user == NULL)
	{
		/*
		 * Verify that the login name exists.
		 * lukeh 24 Dec 1997
		 */
		 
		/* getlogin() is the wrong thing to use here because it returns the wrong user after su */
		/* sns 5 Jan 2005 */
		
		struct passwd * userRec = getpwuid(getuid());
		if (userRec != NULL && userRec->pw_name != NULL) {
			/* global static mem is volatile; must strdup */
			user = strdup(userRec->pw_name);
			free_user = 1;
		}
		
		if (user == NULL)
		{
			fprintf(stderr, "you don't have a login name\n");
			exit(1);
		}
	}
	
	switch (infosystem)
	{
		case INFO_FILE:
			file_passwd(user, locn);
			break;
#ifdef INFO_NIS
		case INFO_NIS:
			nis_passwd(user, locn);
			break;
#endif
#ifdef INFO_OPEN_DIRECTORY
		case INFO_OPEN_DIRECTORY:
			od_passwd(user, locn, auth);
			break;
#endif
#ifdef INFO_PAM
		case INFO_PAM:
			pam_passwd(user);
			break;
#endif
	}
	
	if (free_user == 1)
		free(user);
	
	exit(0);
}