Blame SOURCES/sasl-checkpass.c

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