Пример #1
0
int
main(int argc, char **argv)
{
	const char optstr[] = ":admopstuxg:l:";
	int i;

#ifdef	__GLIBC__
	putenv("POSIXLY_CORRECT=1");
#endif
	progname = basename(argv[0]);
	while ((i = getopt(argc, argv, optstr)) != EOF) {
		switch (i) {
		case 'a':
			aflag = 1;
			break;
		case 'd':
			dflag = 1;
			break;
		case 'm':
			mflag = 1;
			break;
		case 'o':
			oflag = 1;
			break;
		case 'p':
			pflag = 1;
			break;
		case 's':
			sflag = 1;
			break;
		case 't':
			tflag = 1;
			break;
		case 'u':
			uflag = 1;
			break;
		case 'x':
			xflag = 1;
			break;
		case 'g':
			selectgrp(optarg);
			break;
		case 'l':
			selectuser(optarg);
			break;
		default:
			usage();
		}
	}
	if (argv[optind])
		usage();
	if (sflag && uflag)
		sflag = uflag = 0;
	readpasswd();
	readshadow();
	readgroup();
	userdel();
	report();
	return errcnt;
}
Пример #2
0
int auth_pwd_client(int s, bin_addr *proxy)
{
    u_char buf[640];
    int  r, ret, done;
    FILE *fp = NULL;
    struct user_pass up;

    ret = -1;
    done = 0;
    /* get username/password */
    if (pwdfile != NULL) {
        setreuid(PROCUID, 0);
        fp = fopen(pwdfile, "r");
        setreuid(0, PROCUID);
    }

    if ( fp != NULL ) {
        r = readpasswd(fp, proxy, &up);
        fclose(fp);
        if ( r == 0 ) { /* readpasswd gets match */
            if ( up.ulen >= 1 && up.ulen <= 255
                    && up.plen >= 1 && up.plen <= 255 ) {
                /* build auth data */
                buf[0] = 0x01;
                buf[1] = up.ulen & 0xff;
                memcpy(&buf[2], up.user, up.ulen);
                buf[2+up.ulen] = up.plen & 0xff;
                memcpy(&buf[2+up.ulen+1], up.pass, up.plen);
                done++;
            }
        }
    }
    if (! done) {
        /* build fake auth data */
        /* little bit BAD idea */
        buf[0] = 0x01;
        buf[1] = 0x01;
        buf[2] = ' ';
        buf[3] = 0x01;
        buf[4] = ' ';
        up.ulen = up.plen = 1;
    }

    r = timerd_write(s, buf, 3+up.ulen+up.plen, TIMEOUTSEC);
    if (r < 3+up.ulen+up.plen) {
        /* cannot write */
        goto err_ret;
    }

    /* get server reply */
    r = timerd_read(s, buf, 2, TIMEOUTSEC, 0);
    if (r < 2) {
        /* cannot read */
        goto err_ret;
    }
    if (buf[0] == 0x01 && buf[1] == 0) {
        /* username/passwd auth succeded */
        ret = 0;
    }
err_ret:

    /* erace uname and passwd storage */
    memset(&up, 0, sizeof(struct user_pass));
    return(ret);
}
Пример #3
0
int
main(int argc, char *argv[])
{
	const char *host_default = "127.0.0.1";

	static int op = 0;
	char *host = host_default;
	char *user = NULL;

	static struct option long_options[] = {
		{"host",     required_argument, 0, 'h'},
		{"user",     required_argument, 0, 'u'},
		{"register", no_argument,     &op,   1},
		{"send",     no_argument,     &op,   2},
		{"receive",  no_argument,     &op,   3},
		{"delete",   no_argument,     &op,   4},
		{0, 0, 0, 0}
	};
	int option_index = 0;
	int c;

	while (1) {
		c = getopt_long(argc, argv, "hu", long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
			case 0:
				break;
			case 'h':
				host = optarg;
				break;
			case 'u':
				user = optarg;
				break;
			default:
				abort();
		}
	}

	if (user == NULL)
		user = getpwuid(getuid())->pw_name;

	int rc;
	const char *passwd;

	if (op > 0)
		printf("Working as %s@%s.\n", user, host);

	switch (op) {
		case 1:
			// register
			chat_register(host, user);
			break;
		case 2:
			// send
			if (argc - optind < 2) {
				printf("Please specify recipient and message.\n");
				exit(EXIT_FAILURE);
			}
			char *recipient = argv[optind];
			char *message = argv[optind+1];
			rc = chat_sendmsg(host, recipient, user, message);
			printf("Send %s.\n", rc == 0 ? "OK" : "ERROR");
			break;
		case 3:
			// receive
			passwd = readpasswd();
			if (passwd == NULL) {
				printf("Can't find passwd file. Please register first.\n");
				exit(EXIT_FAILURE);
			}
			const char *result = chat_recvmsg(host, user, passwd);
			if (result == NULL || strlen(result) == 0)
				printf("No messages for %s.\n", user);
			else
				printf("%s", result);
			break;
		case 4:
			// delete
			passwd = readpasswd();
			if (passwd == NULL) {
				printf("Can't find passwd file. Please register first.\n");
				exit(EXIT_FAILURE);
			}
			rc = chat_delmsg(host, user, passwd);
			printf("Delete %s.\n", rc == 0 ? "OK" : "ERROR");
			break;

	}
	exit(EXIT_SUCCESS);
}