File: internalsecret.c

package info (click to toggle)
redis 5%3A8.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,916 kB
  • sloc: ansic: 217,142; tcl: 51,874; sh: 4,625; perl: 4,214; cpp: 3,568; python: 3,165; makefile: 2,055; ruby: 639; javascript: 30; csh: 7
file content (154 lines) | stat: -rw-r--r-- 5,723 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "redismodule.h"
#include <errno.h>

int InternalAuth_GetInternalSecret(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
    REDISMODULE_NOT_USED(argv);
    REDISMODULE_NOT_USED(argc);

    /* NOTE: The internal secret SHOULD NOT be exposed by any module. This is
    done for testing purposes only. */
    size_t len;
    const char *secret = RedisModule_GetInternalSecret(ctx, &len);
    if(secret) {
        RedisModule_ReplyWithStringBuffer(ctx, secret, len);
    } else {
        RedisModule_ReplyWithError(ctx, "ERR no internal secret available");
    }
    return REDISMODULE_OK;
}

int InternalAuth_InternalCommand(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
    REDISMODULE_NOT_USED(argv);
    REDISMODULE_NOT_USED(argc);

    RedisModule_ReplyWithSimpleString(ctx, "OK");
    return REDISMODULE_OK;
}

typedef enum {
    RM_CALL_REGULAR = 0,
    RM_CALL_WITHUSER = 1,
    RM_CALL_WITHDETACHEDCLIENT = 2,
    RM_CALL_REPLICATED = 3
} RMCallMode;

int call_rm_call(RedisModuleCtx *ctx, RedisModuleString **argv, int argc, RMCallMode mode) {
    if(argc < 2){
        return RedisModule_WrongArity(ctx);
    }
    RedisModuleCallReply *rep = NULL;
    RedisModuleCtx *detached_ctx = NULL;
    const char* cmd = RedisModule_StringPtrLen(argv[1], NULL);

    switch (mode) {
        case RM_CALL_REGULAR:
            // Regular call, with the unrestricted user.
            rep = RedisModule_Call(ctx, cmd, "vE", argv + 2, argc - 2);
            break;
        case RM_CALL_WITHUSER:
            // Simply call the command with the current client.
            rep = RedisModule_Call(ctx, cmd, "vCE", argv + 2, argc - 2);
            break;
        case RM_CALL_WITHDETACHEDCLIENT:
            // Use a context created with the thread-safe-context API
            detached_ctx = RedisModule_GetThreadSafeContext(NULL);
            if(!detached_ctx){
                RedisModule_ReplyWithError(ctx, "ERR failed to create detached context");
                return REDISMODULE_ERR;
            }
            // Dispatch the command with the detached context
            rep = RedisModule_Call(detached_ctx, cmd, "vCE", argv + 2, argc - 2);
            break;
        case RM_CALL_REPLICATED:
            rep = RedisModule_Call(ctx, cmd, "vE", argv + 2, argc - 2);
    }

    if(!rep) {
        char err[100];
        switch (errno) {
            case EACCES:
                RedisModule_ReplyWithError(ctx, "ERR NOPERM");
                break;
            case ENOENT:
                RedisModule_ReplyWithError(ctx, "ERR unknown command");
                break;
            default:
                snprintf(err, sizeof(err) - 1, "ERR errno=%d", errno);
                RedisModule_ReplyWithError(ctx, err);
                break;
        }
    } else {
        RedisModule_ReplyWithCallReply(ctx, rep);
        RedisModule_FreeCallReply(rep);
        if (mode == RM_CALL_REPLICATED)
            RedisModule_ReplicateVerbatim(ctx);
    }

    if (mode == RM_CALL_WITHDETACHEDCLIENT) {
        RedisModule_FreeThreadSafeContext(detached_ctx);
    }

    return REDISMODULE_OK;
}

int internal_rmcall(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
    return call_rm_call(ctx, argv, argc, RM_CALL_REGULAR);
}

int noninternal_rmcall(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
    return call_rm_call(ctx, argv, argc, RM_CALL_REGULAR);
}

int noninternal_rmcall_withuser(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
    return call_rm_call(ctx, argv, argc, RM_CALL_WITHUSER);
}

int noninternal_rmcall_detachedcontext_withuser(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
    return call_rm_call(ctx, argv, argc, RM_CALL_WITHDETACHEDCLIENT);
}

int internal_rmcall_replicated(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
    return call_rm_call(ctx, argv, argc, RM_CALL_REPLICATED);
}

/* This function must be present on each Redis module. It is used in order to
 * register the commands into the Redis server. */
int RedisModule_OnLoad(RedisModuleCtx *ctx, RedisModuleString **argv, int argc) {
    REDISMODULE_NOT_USED(argv);
    REDISMODULE_NOT_USED(argc);

    if (RedisModule_Init(ctx,"testinternalsecret",1,REDISMODULE_APIVER_1)
        == REDISMODULE_ERR) return REDISMODULE_ERR;

    /* WARNING: A module should NEVER expose the internal secret - this is for
     * testing purposes only. */
    if (RedisModule_CreateCommand(ctx,"internalauth.getinternalsecret",
        InternalAuth_GetInternalSecret,"",0,0,0) == REDISMODULE_ERR)
        return REDISMODULE_ERR;

    if (RedisModule_CreateCommand(ctx,"internalauth.internalcommand",
        InternalAuth_InternalCommand,"internal",0,0,0) == REDISMODULE_ERR)
        return REDISMODULE_ERR;

    if (RedisModule_CreateCommand(ctx,"internalauth.internal_rmcall",
        internal_rmcall,"write internal",0,0,0) == REDISMODULE_ERR)
        return REDISMODULE_ERR;

    if (RedisModule_CreateCommand(ctx,"internalauth.noninternal_rmcall",
        noninternal_rmcall,"write",0,0,0) == REDISMODULE_ERR)
        return REDISMODULE_ERR;

    if (RedisModule_CreateCommand(ctx,"internalauth.noninternal_rmcall_withuser",
        noninternal_rmcall_withuser,"write",0,0,0) == REDISMODULE_ERR)
        return REDISMODULE_ERR;

    if (RedisModule_CreateCommand(ctx,"internalauth.noninternal_rmcall_detachedcontext_withuser",
        noninternal_rmcall_detachedcontext_withuser,"write",0,0,0) == REDISMODULE_ERR)
        return REDISMODULE_ERR;

    if (RedisModule_CreateCommand(ctx,"internalauth.internal_rmcall_replicated",
        internal_rmcall_replicated,"write internal",0,0,0) == REDISMODULE_ERR)
        return REDISMODULE_ERR;

    return REDISMODULE_OK;
}