Blame SOURCES/sasl-checkpass.c

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