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