sbonazzo / rpms / cyrus-sasl

Forked from rpms/cyrus-sasl 2 years ago
Clone

Blame SOURCES/sasl-checkpass.c

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