Repository: valkey-io/valkey
File: src/networking.c (C)

Code before:
                    server.rdb_pipe_conns[i] = NULL;
                }
                if (server.rdb_pipe_conns[i]) still_alive++;
            }
            if (still_alive == 0) {
                serverLog(LL_NOTICE, "Diskless rdb transfer, last replica dropped, killing fork child.");
                killRDBChild();
            }
        }
        /* Only use shutdown when the fork is active and we are the parent. */
        if (server.child_type && !c->flag.repl_rdb_channel) {
            connShutdown(c->conn);
        } else if (c->flag.repl_rdb_channel) {
            shutdown(c->conn->fd, SHUT_RDWR);
        }
        connClose(c->conn);
        c->conn = NULL;
    }

    /* Remove from the list of pending writes if needed. */
    if (c->flag.pending_write) {
        if (c->io_write_state == CLIENT_IDLE) {
            listUnlinkNode(server.clients_pending_write, &c->clients_pending_write_node);
        } else {
            listUnlinkNode(server.clients_pending_io_write, &c->clients_pending_write_node);
        }
        c->flag.pending_write = 0;
    }

    /* Remove from the list of pending reads if needed. */
    serverAssert(c->io_read_state != CLIENT_PENDING_IO && c->io_write_state != CLIENT_PENDING_IO);
    if (c->flag.pending_read) {
        listUnlinkNode(server.clients_pending_io_read, &c->pending_read_list_node);
        c->flag.pending_read = 0;
    }


    /* When client was just unblocked because of a blocking operation,
     * remove it from the list of unblocked clients. */
    if (c->flag.unblocked) {
        ln = listSearchKey(server.unblocked_clients, c);
        serverAssert(ln != NULL);
        listDelNode(server.unblocked_clients, ln);
        c->flag.unblocked = 0;
    }

    /* Clear the tracking status. */
    if (c->flag.tracking) disableTracking(c);
}

/* Clear the client state to resemble a newly connected client. */

Review comment (bug):
```suggestion
            serverLog(LL_NOTICE, "TLS: Auto-authentication failed, user '%s' not found", username);
```
I wonder if this should go in the ACL log instead? This is basically a failed authentication attempt. That removes the concern about dropping user data to the disk and also includes a lot of useful information like IP.

Code after:
                        "mode option to 'no', and then restarting the server. "
                        "3) If you started the server manually just for testing, restart "
                        "it with the '--protected-mode no' option. "
                        "4) Set up an authentication password for the default user. "
                        "NOTE: You only need to do one of the above things in order for "
                        "the server to start accepting connections from the outside.\r\n";
            if (connWrite(c->conn, err, strlen(err)) == -1) {
                /* Nothing to do, Just to avoid the warning... */
            }
            server.stat_rejected_conn++;
            freeClientAsync(c);
            return;
        }
    }

    /* Auto-authenticate from cert_user field if set */
    sds username = connGetPeerUsername(conn);
    if (username != NULL) {
        user *u = ACLGetUserByName(username, sdslen(username));
        if (u) {
            c->user = u;
            c->flag.authenticated = true;
            serverLog(LL_VERBOSE, "TLS: Auto-authenticated client as %s",
                      server.hide_user_data_from_log ? "*redacted*" : u->name);
        } else {
            addACLLogEntry(c, ACL_INVALID_TLS_CERT_AUTH, ACL_LOG_CTX_TOPLEVEL, 0, username, NULL);
        }
        sdsfree(username);
    }

    server.stat_numconnections++;
    moduleFireServerEvent(VALKEYMODULE_EVENT_CLIENT_CHANGE, VALKEYMODULE_SUBEVENT_CLIENT_CHANGE_CONNECTED, c);
}

void acceptCommonHandler(connection *conn, struct ClientFlags flags, char *ip) {
    client *c;
    UNUSED(ip);

    char addr[CONN_ADDR_STR_LEN] = {0};
    char laddr[CONN_ADDR_STR_LEN] = {0};
    connFormatAddr(conn, addr, sizeof(addr), 1);
    connFormatAddr(conn, laddr, sizeof(addr), 0);

    if (connGetState(conn) != CONN_STATE_ACCEPTING) {
        serverLog(LL_VERBOSE, "Accepted client connection in error state: %s (addr=%s laddr=%s)",
                  connGetLastError(conn), addr, laddr);
        connClose(conn);
        return;
    }

    /* Limit the number of connections we take at the same time.