1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file user_list.c User handling for para_server. */
14 #include "user_list.h"
16 INITIALIZED_LIST_HEAD(user_list);
19 * Wrapper for fgets(3).
21 * Unlike fgets(3), an integer value is returned. On success, this function
22 * returns 1. On errors, -E_FGETS is returned. A zero return value indicates an
23 * end of file condition.
25 static int xfgets(char *line, int size, FILE *f)
28 if (fgets(line, size, f))
35 PARA_ERROR_LOG("%s\n", strerror(errno));
43 * Fill the list of users known to para_server.
45 * Populates a linked list of all users in \a user_list_file. Returns on
46 * success, calls exit() on errors.
48 static void populate(char *user_list_file)
50 int ret = -E_USERLIST;
51 FILE *file_ptr = fopen(user_list_file, "r");
58 /* keyword, name, key, perms */
59 char w[255], n[255], k[255], p[255], tmp[4][255];
61 struct asymmetric_key *pubkey;
63 ret = xfgets(line, sizeof(line), file_ptr);
66 if (sscanf(line,"%200s %200s %200s %200s", w, n, k, p) < 3)
68 if (strcmp(w, "user"))
70 PARA_DEBUG_LOG("found entry for user %s\n", n);
71 ret = get_public_key(k, &pubkey);
73 PARA_NOTICE_LOG("skipping entry for user %s: %s\n", n,
78 * In order to encrypt len := CHALLENGE_SIZE + 2 * SESSION_KEY_LEN
79 * bytes using RSA_public_encrypt() with EME-OAEP padding mode,
80 * RSA_size(rsa) must be greater than len + 41. So ignore keys
81 * which are too short. For details see RSA_public_encrypt(3).
83 if (ret <= CHALLENGE_SIZE + 2 * SESSION_KEY_LEN + 41) {
84 PARA_WARNING_LOG("public key %s too short (%d)\n",
86 free_public_key(pubkey);
89 u = para_malloc(sizeof(*u));
90 u->name = para_strdup(n);
93 num = sscanf(p, "%200[A-Z_],%200[A-Z_],%200[A-Z_],%200[A-Z_]",
94 tmp[0], tmp[1], tmp[2], tmp[3]);
95 PARA_DEBUG_LOG("found %i perm entries\n", num);
98 if (!strcmp(tmp[num], "VSS_READ"))
100 else if (!strcmp(tmp[num], "VSS_WRITE"))
101 u->perms |= VSS_WRITE;
102 else if (!strcmp(tmp[num], "AFS_READ"))
103 u->perms |= AFS_READ;
104 else if (!strcmp(tmp[num], "AFS_WRITE"))
105 u->perms |= AFS_WRITE;
106 else /* unknown permission */
107 PARA_WARNING_LOG("ignoring unknown permission: %s\n",
110 para_list_add(&u->node, &user_list);
116 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
121 * Initialize the list of users allowed to connect to para_server.
123 * \param user_list_file The file containing access information.
125 * If this function is called for the second time, the contents of the
126 * previous call are discarded, i.e. the user list is reloaded.
128 void user_list_init(char *user_list_file)
130 struct user *u, *tmp;
132 list_for_each_entry_safe(u, tmp, &user_list, node) {
135 free_public_key(u->pubkey);
138 populate(user_list_file);
142 * Lookup a user in the user list.
144 * \param name The name of the user.
146 * \return A pointer to the corresponding user struct if the user was found, \p
149 const struct user *user_list_lookup(const char *name)
151 const struct user *u;
152 list_for_each_entry(u, &user_list, node) {
153 if (strcmp(u->name, name))