X-Git-Url: https://git.saurik.com/apple/securityd.git/blobdiff_plain/ee396ef47db58c01c7ceaecfec60781c95ffeea1..4cd1cad0dea00daa03e1b54fdf2797a02373ad5b:/src/tokencache.cpp diff --git a/src/tokencache.cpp b/src/tokencache.cpp index c843340..0d3a747 100644 --- a/src/tokencache.cpp +++ b/src/tokencache.cpp @@ -65,27 +65,31 @@ static const char cacheDir[] = "cache"; // Note that the defaulted read functions do NOT write the default // to disk; they work fine in read-only disk areas. // -static uint32 getFile(const string &path, uint32 defaultValue) +static unsigned long getFile(const string &path, unsigned long defaultValue) { try { - FileDesc fd(path); - string s; fd.readAll(s); - uint32 value; sscanf(s.c_str(), "%ld", &value); - return value; + AutoFileDesc fd(path, O_RDONLY, FileDesc::modeMissingOk); + if (fd) { + string s; fd.readAll(s); + unsigned long value; sscanf(s.c_str(), "%lu", &value); + return value; + } } catch (...) { - return defaultValue; } + return defaultValue; } static string getFile(const string &path, const string &defaultValue) { try { - FileDesc fd(path); - string s; fd.readAll(s); - return s; + AutoFileDesc fd(path, O_RDONLY, FileDesc::modeMissingOk); + if (fd) { + string s; fd.readAll(s); + return s; + } } catch (...) { - return defaultValue; } + return defaultValue; } @@ -93,12 +97,12 @@ static void putFile(const string &path, uint32 value) { char buffer[64]; snprintf(buffer, sizeof(buffer), "%ld\n", value); - FileDesc(path, O_WRONLY | O_CREAT | O_TRUNC).writeAll(buffer); + AutoFileDesc(path, O_WRONLY | O_CREAT | O_TRUNC).writeAll(buffer); } static void putFile(const string &path, const string &value) { - FileDesc(path, O_WRONLY | O_CREAT | O_TRUNC).writeAll(value); + AutoFileDesc(path, O_WRONLY | O_CREAT | O_TRUNC).writeAll(value); } @@ -131,14 +135,7 @@ TokenCache::TokenCache(const char *where) makedir(path(configDir), O_CREAT, 0700, securityd); makedir(path(tokensDir), O_CREAT, 0711, securityd); - // get the path for the SSID file. Don't call getFile unless the file exists (avoids exception overhead) - string idFilePath = path (lastSSIDFile); - struct stat st; - if (stat (idFilePath.c_str (), &st) == -1) { - mLastSubservice = 1; - } else { - mLastSubservice = getFile(idFilePath, 1); - } + mLastSubservice = getFile(path(lastSSIDFile), 1); // identify uid/gid for token daemons struct passwd *pw = getpwnam(TOKEND_UID);