--- embedaddon/mpd/src/console.c 2012/02/21 23:32:47 1.1.1.1 +++ embedaddon/mpd/src/console.c 2019/10/22 13:49:55 1.1.1.4 @@ -87,6 +87,11 @@ */ static const struct confinfo gConfList[] = { + { 0, CONSOLE_AUTH, "auth" }, + { 0, 0, NULL }, + }; + + static const struct confinfo sConfList[] = { { 0, CONSOLE_LOGGING, "logging" }, { 0, 0, NULL }, }; @@ -107,6 +112,7 @@ ConsoleInit(Console c) memset(&gConsole, 0, sizeof(gConsole)); ParseAddr(DEFAULT_CONSOLE_IP, &c->addr, ALLOW_IPV4|ALLOW_IPV6); c->port = DEFAULT_CONSOLE_PORT; + Enable(&c->options, CONSOLE_AUTH); SLIST_INIT(&c->sessions); @@ -169,6 +175,14 @@ ConsoleClose(Console c) return 0; } +void +ConsoleCancelCleanup(void *rwlock) +{ + pthread_rwlock_t p = (pthread_rwlock_t)rwlock; + + RWLOCK_UNLOCK(p); +} + /* * ConsoleStat() */ @@ -186,17 +200,20 @@ ConsoleStat(Context ctx, int ac, char *av[], void *arg Printf("\tIP-Address : %s\r\n", u_addrtoa(&c->addr,addrstr,sizeof(addrstr))); Printf("\tPort : %d\r\n", c->port); + pthread_cleanup_push(ConsoleCancelCleanup, c->lock); RWLOCK_RDLOCK(c->lock); Printf("Active sessions:\r\n"); SLIST_FOREACH(s, &c->sessions, next) { Printf("\tUsername: %s\tFrom: %s\r\n", s->user.username, u_addrtoa(&s->peer_addr,addrstr,sizeof(addrstr))); } - RWLOCK_UNLOCK(c->lock); + pthread_cleanup_pop(1); + Printf("Global options:\r\n"); + OptStat(ctx, &c->options, gConfList); if (cs) { Printf("This session options:\r\n"); - OptStat(ctx, &cs->options, gConfList); + OptStat(ctx, &cs->options, sConfList); } return 0; } @@ -233,7 +250,13 @@ ConsoleConnect(int type, void *cookie) cs->write = ConsoleSessionWrite; cs->writev = ConsoleSessionWriteV; cs->prompt = ConsoleSessionShowPrompt; - cs->state = STATE_USERNAME; + if (!Enabled(&c->options, CONSOLE_AUTH)) { + cs->state = STATE_AUTHENTIC; + strcpy(cs->user.username, "root"); + cs->context.priv = 2; + } else { + cs->state = STATE_USERNAME; + } cs->context.cs = cs; RWLOCK_WRLOCK(c->lock); SLIST_INSERT_HEAD(&c->sessions, cs, next); @@ -376,7 +399,7 @@ ConsoleSessionReadEvent(int type, void *cookie) goto out; Perror("CONSOLE: Error while reading"); } else { - if (cs->fd == 0) + if (cs->fd == 0 && isatty(cs->fd)) goto out; Log(LG_ERR, ("CONSOLE: Connection closed by peer")); } @@ -793,13 +816,19 @@ ConsoleSetCommand(Context ctx, int ac, char *av[], voi break; case SET_ENABLE: - if (cs) - EnableCommand(ac, av, &cs->options, gConfList); + if (cs) { + if (strcmp(av[0], "auth") != 0) + EnableCommand(ac, av, &cs->options, sConfList); + } else + EnableCommand(ac, av, &c->options, gConfList); break; case SET_DISABLE: - if (cs) - DisableCommand(ac, av, &cs->options, gConfList); + if (cs) { + if (strcmp(av[0], "auth") != 0) + DisableCommand(ac, av, &cs->options, sConfList); + } else + DisableCommand(ac, av, &c->options, gConfList); break; case SET_SELF: @@ -885,13 +914,14 @@ UserStat(Context ctx, int ac, char *av[], void *arg) ConsoleUser u; Printf("Configured users:\r\n"); + pthread_cleanup_push(ConsoleCancelCleanup, gUsersLock); RWLOCK_RDLOCK(gUsersLock); ghash_walk_init(gUsers, &walk); while ((u = ghash_walk_next(gUsers, &walk)) != NULL) { - Printf("\tUsername: %-15s Priv:%s\r\n", u->username, + Printf("\tUsername: %-15s Priv: %s\r\n", u->username, ((u->priv == 2)?"admin":((u->priv == 1)?"operator":"user"))); } - RWLOCK_UNLOCK(gUsersLock); + pthread_cleanup_pop(1); return 0; }