Blame SOURCES/sasl-checkpass.c

b9abc1
#include <errno.h>
b9abc1
#include <getopt.h>
b9abc1
#include <stdio.h>
b9abc1
#include <stdlib.h>
b9abc1
b9abc1
#include "sasl.h"
b9abc1
#ifdef SASL2
b9abc1
static int main_requested_sasl_version = 2;
b9abc1
#else
b9abc1
static int main_requested_sasl_version = 1;
b9abc1
#endif
b9abc1
b9abc1
static int main_verbose = 0;
b9abc1
b9abc1
static int
b9abc1
my_getopt(void *context, const char *plugin_name,
b9abc1
	  const char *option, const char **result, unsigned *len)
b9abc1
{
b9abc1
	if (result) {
b9abc1
		*result = NULL;
b9abc1
		if (strcmp(option, "pwcheck_method") == 0) {
b9abc1
			*result = "saslauthd";
b9abc1
		}
b9abc1
		if (strcmp(option, "saslauthd_version") == 0) {
b9abc1
			switch (main_requested_sasl_version) {
b9abc1
			case 1:
b9abc1
				*result = "1";
b9abc1
				break;
b9abc1
			case 2:
b9abc1
				*result = "2";
b9abc1
				break;
b9abc1
			default:
b9abc1
#ifdef SASL2
b9abc1
				*result = "2";
b9abc1
#else
b9abc1
				*result = "1";
b9abc1
#endif
b9abc1
				break;
b9abc1
			}
b9abc1
		}
b9abc1
		if (main_verbose) {
b9abc1
			fprintf(stderr, "Getopt plugin=%s%s%s/option=%s%s%s -> ",
b9abc1
				plugin_name ? "\"" : "",
b9abc1
				plugin_name ? plugin_name : "(null)",
b9abc1
				plugin_name ? "\"" : "",
b9abc1
				option ? "\"" : "",
b9abc1
				option ? option : "(null)",
b9abc1
				option ? "\"" : "");
b9abc1
			fprintf(stderr, "'%s'.\n", *result ? *result : "");
b9abc1
		}
b9abc1
	}
b9abc1
	if (len) {
b9abc1
		*len = 0;
b9abc1
	}
b9abc1
	return 0;
b9abc1
}
b9abc1
b9abc1
int
b9abc1
main(int argc, char **argv)
b9abc1
{
b9abc1
	const char *user, *realm, *passwd, *service, *mechs, **globals, *err;
b9abc1
	int c, ret;
b9abc1
	sasl_callback_t callbacks[] = {
b9abc1
		{SASL_CB_GETOPT, my_getopt, NULL},
b9abc1
		{SASL_CB_LIST_END},
b9abc1
	};
b9abc1
	sasl_conn_t *connection;
b9abc1
	char hostname[512];
b9abc1
	char fulluser[512]; /* XXX: may overflow */
b9abc1
b9abc1
	user = realm = passwd = service = "";
b9abc1
	strcpy(hostname, "localhost");
b9abc1
	gethostname(hostname, sizeof(hostname));
b9abc1
b9abc1
	while ((c = getopt(argc, argv, "u:r:p:s:h:12v")) != -1) {
b9abc1
		switch (c) {
b9abc1
		case 'u':
b9abc1
			user = optarg;
b9abc1
			break;
b9abc1
		case 'r':
b9abc1
			realm = optarg;
b9abc1
			break;
b9abc1
		case 'p':
b9abc1
			passwd = optarg;
b9abc1
			break;
b9abc1
		case 's':
b9abc1
			service = optarg;
b9abc1
			break;
b9abc1
		case 'h':
b9abc1
			strncpy(hostname, optarg, sizeof(hostname) - 1);
b9abc1
			hostname[sizeof(hostname) - 1] = '\0';
b9abc1
			break;
b9abc1
		case '1':
b9abc1
			main_requested_sasl_version = 1;
b9abc1
			break;
b9abc1
		case '2':
b9abc1
			main_requested_sasl_version = 2;
b9abc1
			break;
b9abc1
		case 'v':
b9abc1
			main_verbose++;
b9abc1
			break;
b9abc1
		default:
b9abc1
			printf("Usage: %s [-v] [-1] [-2] "
b9abc1
			       "[-h hostname] "
b9abc1
			       "[-u user] "
b9abc1
			       "[-r realm] "
b9abc1
			       "[-p password] "
b9abc1
			       "[-s service] "
b9abc1
			       "\n", argv[0]);
b9abc1
			return 2;
b9abc1
			break;
b9abc1
		}
b9abc1
	}
b9abc1
	if ((strlen(user) == 0) || (strlen(passwd) == 0)) {
b9abc1
		printf("Usage: %s [-v] [-1] [-2] "
b9abc1
		       "[-h hostname] "
b9abc1
		       "[-u user] "
b9abc1
		       "[-r realm] "
b9abc1
		       "[-p password] "
b9abc1
		       "[-s service] "
b9abc1
		       "\n", argv[0]);
b9abc1
		return 2;
b9abc1
	}
b9abc1
	if (realm && (strlen(realm) > 0)) {
b9abc1
		sprintf(fulluser, "%s@%s", user, realm);
b9abc1
	} else {
b9abc1
		sprintf(fulluser, "%s", user);
b9abc1
	}
b9abc1
b9abc1
	ret = sasl_server_init(callbacks,
b9abc1
			       strlen(service) ? service : "sasl-checkpass");
b9abc1
	if (ret != SASL_OK) {
b9abc1
		fprintf(stderr, "Error in sasl_server_init(): %s\n",
b9abc1
			sasl_errstring(ret, NULL, NULL));
b9abc1
	}
b9abc1
b9abc1
	connection = NULL;
b9abc1
	ret = sasl_server_new(strlen(service) ? service : "sasl-checkpass",
b9abc1
			      hostname,
b9abc1
			      NULL,
b9abc1
#ifdef SASL2
b9abc1
			      NULL,
b9abc1
			      NULL,
b9abc1
#endif
b9abc1
			      callbacks,
b9abc1
			      0,
b9abc1
			      &connection);
b9abc1
	if (ret != SASL_OK) {
b9abc1
		fprintf(stderr, "Error in sasl_server_new(): %s\n",
b9abc1
			sasl_errstring(ret, NULL, NULL));
b9abc1
	}
b9abc1
b9abc1
	err = NULL;
b9abc1
	ret = sasl_checkpass(connection,
b9abc1
			     fulluser, strlen(fulluser),
b9abc1
			     passwd, strlen(passwd)
b9abc1
#ifndef SASL2
b9abc1
			     , &err
b9abc1
#endif
b9abc1
			     );
b9abc1
	switch (ret) {
b9abc1
	case SASL_OK:
b9abc1
		printf("OK\n");
b9abc1
		break;
b9abc1
	default:
b9abc1
		printf("NO: %d", ret);
b9abc1
		switch (ret) {
b9abc1
		case SASL_FAIL:
b9abc1
			err = "generic failure";
b9abc1
			break;
b9abc1
		case SASL_BADAUTH:
b9abc1
			err = "authentication failure";
b9abc1
			break;
b9abc1
		default:
b9abc1
			err = NULL;
b9abc1
			break;
b9abc1
		}
b9abc1
		if (err) {
b9abc1
			printf(" (%s)", err);
b9abc1
		}
b9abc1
		printf("\n");
b9abc1
		break;
b9abc1
	}
b9abc1
	return ret;
b9abc1
}