| 1 | /* Copyright (C) 2011 Omnibond, LLC
|
|---|
| 2 | Configuration file functions */
|
|---|
| 3 |
|
|---|
| 4 | #include <Windows.h>
|
|---|
| 5 | #include <stdlib.h>
|
|---|
| 6 | #include <stdio.h>
|
|---|
| 7 | #include <string.h>
|
|---|
| 8 | #include <ctype.h>
|
|---|
| 9 |
|
|---|
| 10 | #include "client-service.h"
|
|---|
| 11 | #include "config.h"
|
|---|
| 12 | #include "user-cache.h"
|
|---|
| 13 |
|
|---|
| 14 | extern struct qhash_table user_cache;
|
|---|
| 15 |
|
|---|
| 16 | static FILE *open_config_file(char *error_msg,
|
|---|
| 17 | unsigned int error_msg_len)
|
|---|
| 18 | {
|
|---|
| 19 | FILE *f = NULL;
|
|---|
| 20 | char *file_name = NULL, exe_path[MAX_PATH], *p;
|
|---|
| 21 | DWORD ret = 0, malloc_flag = FALSE;
|
|---|
| 22 |
|
|---|
| 23 | /* environment variable overrides */
|
|---|
| 24 | if (!(file_name = getenv("ORANGEFS_CONFIG_FILE")))
|
|---|
| 25 | {
|
|---|
| 26 | /* look for file in exe directory */
|
|---|
| 27 | ret = GetModuleFileName(NULL, exe_path, MAX_PATH);
|
|---|
| 28 | if (ret)
|
|---|
| 29 | {
|
|---|
| 30 | p = strrchr(exe_path, '\\');
|
|---|
| 31 | if (p)
|
|---|
| 32 | *p = '\0';
|
|---|
| 33 |
|
|---|
| 34 | file_name = (char *) malloc(MAX_PATH);
|
|---|
| 35 | malloc_flag = TRUE;
|
|---|
| 36 | strncpy(file_name, exe_path, MAX_PATH-14);
|
|---|
| 37 | strcat(file_name, "\\orangefs.cfg");
|
|---|
| 38 |
|
|---|
| 39 | ret = 0;
|
|---|
| 40 | }
|
|---|
| 41 | else
|
|---|
| 42 | {
|
|---|
| 43 | ret = GetLastError();
|
|---|
| 44 | _snprintf(error_msg, error_msg_len, "GetModuleFileName failed: %u\n", ret);
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | /* open config file */
|
|---|
| 49 | if (ret == 0)
|
|---|
| 50 | f = fopen(file_name, "r");
|
|---|
| 51 |
|
|---|
| 52 | if (f == NULL)
|
|---|
| 53 | _snprintf(error_msg, error_msg_len, "Fatal: could not open configuration file %s\n",
|
|---|
| 54 | file_name == NULL ? "(null)" : file_name);
|
|---|
| 55 |
|
|---|
| 56 | if (malloc_flag)
|
|---|
| 57 | free(file_name);
|
|---|
| 58 |
|
|---|
| 59 | return f;
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | static void close_config_file(FILE *f)
|
|---|
| 63 | {
|
|---|
| 64 | fclose(f);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | /* parse line in format: <[domain\]user name> <uid>:<gid> */
|
|---|
| 68 | int parse_user()
|
|---|
| 69 | {
|
|---|
| 70 | char *token, *p;
|
|---|
| 71 | char user_name[256];
|
|---|
| 72 | char uid[16], gid[16];
|
|---|
| 73 | int i, ret = 0;
|
|---|
| 74 | PVFS_credentials credentials;
|
|---|
| 75 |
|
|---|
| 76 | /* assume current string being parsed */
|
|---|
| 77 | token = strtok(NULL, " \t");
|
|---|
| 78 |
|
|---|
| 79 | if (token)
|
|---|
| 80 | {
|
|---|
| 81 | /* copy user name */
|
|---|
| 82 | strncpy(user_name, token, 256);
|
|---|
| 83 |
|
|---|
| 84 | token = strtok(NULL, " \t");
|
|---|
| 85 | if (token)
|
|---|
| 86 | {
|
|---|
| 87 | uid[0] = gid[0] = '\0';
|
|---|
| 88 | i = 0;
|
|---|
| 89 | p = token;
|
|---|
| 90 | while (*p && *p != ':' && i < 15)
|
|---|
| 91 | {
|
|---|
| 92 | if (isdigit(*p))
|
|---|
| 93 | {
|
|---|
| 94 | uid[i++] = *p++;
|
|---|
| 95 | }
|
|---|
| 96 | else
|
|---|
| 97 | {
|
|---|
| 98 | ret = 1;
|
|---|
| 99 | break;
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|
| 102 | uid[i] = '\0';
|
|---|
| 103 | if (ret == 0)
|
|---|
| 104 | {
|
|---|
| 105 | if (*p == ':')
|
|---|
| 106 | p++;
|
|---|
| 107 | i = 0;
|
|---|
| 108 | while(*p && i < 15)
|
|---|
| 109 | {
|
|---|
| 110 | if (isdigit(*p))
|
|---|
| 111 | {
|
|---|
| 112 | gid[i++] = *p++;
|
|---|
| 113 | }
|
|---|
| 114 | else
|
|---|
| 115 | {
|
|---|
| 116 | ret = 1;
|
|---|
| 117 | break;
|
|---|
| 118 | }
|
|---|
| 119 | }
|
|---|
| 120 | gid[i] = '\0';
|
|---|
| 121 | }
|
|---|
| 122 | }
|
|---|
| 123 | else
|
|---|
| 124 | {
|
|---|
| 125 | ret = 1;
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | else
|
|---|
| 129 | {
|
|---|
| 130 | ret = 1;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | if (ret == 0)
|
|---|
| 134 | ret = !(strlen(uid) > 0 && strlen(gid) > 0);
|
|---|
| 135 |
|
|---|
| 136 | if (ret == 0)
|
|---|
| 137 | {
|
|---|
| 138 | /* add user to cache with no expiration */
|
|---|
| 139 | credentials.uid = atoi(uid);
|
|---|
| 140 | credentials.gid = atoi(gid);
|
|---|
| 141 |
|
|---|
| 142 | add_user(user_name, &credentials, 0);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | return ret;
|
|---|
| 146 | }
|
|---|
| 147 |
|
|---|
| 148 | int get_config(PORANGEFS_OPTIONS options,
|
|---|
| 149 | char *error_msg,
|
|---|
| 150 | unsigned int error_msg_len)
|
|---|
| 151 | {
|
|---|
| 152 | FILE *config_file;
|
|---|
| 153 | char line[256], copy[256], *token;
|
|---|
| 154 | int ret = 0;
|
|---|
| 155 |
|
|---|
| 156 | config_file = open_config_file(error_msg, error_msg_len);
|
|---|
| 157 | if (config_file == NULL)
|
|---|
| 158 | /* config file is required */
|
|---|
| 159 | return 1;
|
|---|
| 160 |
|
|---|
| 161 | /* parse options from the file */
|
|---|
| 162 | while (!feof(config_file))
|
|---|
| 163 | {
|
|---|
| 164 | line[0] = '\0';
|
|---|
| 165 | fgets(line, 256, config_file);
|
|---|
| 166 |
|
|---|
| 167 | /* remove \n */
|
|---|
| 168 | if (strlen(line) > 0 && line[strlen(line)-1] == '\n')
|
|---|
| 169 | line[strlen(line)-1] = '\0';
|
|---|
| 170 |
|
|---|
| 171 | /* check line -- # used for comments */
|
|---|
| 172 | if (strlen(line) > 0 && line[0] != '#')
|
|---|
| 173 | {
|
|---|
| 174 | /* make a copy */
|
|---|
| 175 | strncpy(copy, line, 256);
|
|---|
| 176 | /* parse line */
|
|---|
| 177 | token = strtok(copy, " \t");
|
|---|
| 178 | if (token == NULL)
|
|---|
| 179 | continue;
|
|---|
| 180 |
|
|---|
| 181 | if (!stricmp(token, "mount"))
|
|---|
| 182 | {
|
|---|
| 183 | /* copy the remaining portion of the line
|
|---|
| 184 | as the mount point */
|
|---|
| 185 | /*
|
|---|
| 186 | p = line + strlen(token);
|
|---|
| 187 | while (*p && (*p == ' ' || *p == '\t'))
|
|---|
| 188 | p++;
|
|---|
| 189 | if (*p)
|
|---|
| 190 | */
|
|---|
| 191 | token = strtok(NULL, " \t");
|
|---|
| 192 | strncpy(options->mount_point, token, MAX_PATH);
|
|---|
| 193 | }
|
|---|
| 194 | else if (!stricmp(token, "threads"))
|
|---|
| 195 | {
|
|---|
| 196 | /*
|
|---|
| 197 | p = line + strlen(token);
|
|---|
| 198 | while (*p && (*p == ' ' || *p == '\t'))
|
|---|
| 199 | p++;
|
|---|
| 200 | if (*p)
|
|---|
| 201 | */
|
|---|
| 202 | token = strtok(NULL, " \t");
|
|---|
| 203 | options->threads = atoi(token);
|
|---|
| 204 | }
|
|---|
| 205 | else if (!stricmp(token, "user-mode"))
|
|---|
| 206 | {
|
|---|
| 207 | token = strtok(NULL, " \t");
|
|---|
| 208 | if (token == NULL)
|
|---|
| 209 | {
|
|---|
| 210 | _snprintf(error_msg, error_msg_len, "user-mode option must be list, certificate, or ldap\n");
|
|---|
| 211 | ret = 1;
|
|---|
| 212 | goto get_config_exit;
|
|---|
| 213 | }
|
|---|
| 214 | if (!stricmp(token, "list"))
|
|---|
| 215 | {
|
|---|
| 216 | options->user_mode = USER_MODE_LIST;
|
|---|
| 217 | }
|
|---|
| 218 | else if (!stricmp(token, "certificate"))
|
|---|
| 219 | {
|
|---|
| 220 | options->user_mode = USER_MODE_CERT;
|
|---|
| 221 | }
|
|---|
| 222 | else if (!stricmp(token, "ldap"))
|
|---|
| 223 | {
|
|---|
| 224 | options->user_mode = USER_MODE_LDAP;
|
|---|
| 225 | }
|
|---|
| 226 | else
|
|---|
| 227 | {
|
|---|
| 228 | _snprintf(error_msg, error_msg_len, "user-mode option must be list, certificate, or ldap\n");
|
|---|
| 229 | ret = 1;
|
|---|
| 230 | goto get_config_exit;
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 | else if (!stricmp(token, "user"))
|
|---|
| 234 | {
|
|---|
| 235 | if (options->user_mode == USER_MODE_NONE)
|
|---|
| 236 | {
|
|---|
| 237 | _snprintf(error_msg, error_msg_len, "user option: specify 'user-mode list' above user option\n");
|
|---|
| 238 | ret = 1;
|
|---|
| 239 | goto get_config_exit;
|
|---|
| 240 | }
|
|---|
| 241 | else if (options->user_mode != USER_MODE_LIST)
|
|---|
| 242 | {
|
|---|
| 243 | _snprintf(error_msg, error_msg_len, "user option: not legal with current user mode\n");
|
|---|
| 244 | ret = 1;
|
|---|
| 245 | goto get_config_exit;
|
|---|
| 246 | }
|
|---|
| 247 |
|
|---|
| 248 | if (parse_user() != 0)
|
|---|
| 249 | {
|
|---|
| 250 | _snprintf(error_msg, error_msg_len, "user option: parse error\n");
|
|---|
| 251 | ret = 1;
|
|---|
| 252 | goto get_config_exit;
|
|---|
| 253 | }
|
|---|
| 254 | }
|
|---|
| 255 | else if (!stricmp(token, "cert-dir-prefix"))
|
|---|
| 256 | {
|
|---|
| 257 | if (strlen(line) > 16)
|
|---|
| 258 | {
|
|---|
| 259 | strncpy(options->cert_dir_prefix, line + 16, MAX_PATH-2);
|
|---|
| 260 | options->cert_dir_prefix[MAX_PATH-2] = '\0';
|
|---|
| 261 | if (options->cert_dir_prefix[strlen(options->cert_dir_prefix)-1] != '\\')
|
|---|
| 262 | strcat(options->cert_dir_prefix, "\\");
|
|---|
| 263 | }
|
|---|
| 264 | else
|
|---|
| 265 | {
|
|---|
| 266 | _snprintf(error_msg, error_msg_len, "cert-dir-prefix option: parse error\n");
|
|---|
| 267 | ret = 1;
|
|---|
| 268 | goto get_config_exit;
|
|---|
| 269 | }
|
|---|
| 270 | }
|
|---|
| 271 | else if (!stricmp(token, "ca-path"))
|
|---|
| 272 | {
|
|---|
| 273 | if (strlen(line) > 8)
|
|---|
| 274 | {
|
|---|
| 275 | strncpy(options->ca_path, line + 8, MAX_PATH-2);
|
|---|
| 276 | options->ca_path[MAX_PATH-2] = '\0';
|
|---|
| 277 | }
|
|---|
| 278 | else
|
|---|
| 279 | {
|
|---|
| 280 | _snprintf(error_msg, error_msg_len, "ca-path option: parse error\n");
|
|---|
| 281 | ret = 1;
|
|---|
| 282 | goto get_config_exit;
|
|---|
| 283 | }
|
|---|
| 284 | }
|
|---|
| 285 | else if (!stricmp(token, "debug"))
|
|---|
| 286 | {
|
|---|
| 287 | options->debug = TRUE;
|
|---|
| 288 | }
|
|---|
| 289 | else
|
|---|
| 290 | _snprintf(error_msg, error_msg_len, "Unknown option %s\n", token);
|
|---|
| 291 | }
|
|---|
| 292 | }
|
|---|
| 293 |
|
|---|
| 294 | if (options->user_mode == USER_MODE_NONE)
|
|---|
| 295 | {
|
|---|
| 296 | _snprintf(error_msg, error_msg_len, "Must specify user-mode (list, certificate or ldap)\n");
|
|---|
| 297 | ret = 1;
|
|---|
| 298 | goto get_config_exit;
|
|---|
| 299 | }
|
|---|
| 300 |
|
|---|
| 301 | if (options->user_mode == USER_MODE_CERT &&
|
|---|
| 302 | strlen(options->ca_path) == 0)
|
|---|
| 303 | {
|
|---|
| 304 | _snprintf(error_msg, error_msg_len, "Must specify ca-path with certificate mode\n");
|
|---|
| 305 | ret = 1;
|
|---|
| 306 | }
|
|---|
| 307 |
|
|---|
| 308 | get_config_exit:
|
|---|
| 309 |
|
|---|
| 310 | close_config_file(config_file);
|
|---|
| 311 |
|
|---|
| 312 | return ret;
|
|---|
| 313 | }
|
|---|