acc.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. // SPDX-FileCopyrightText: Copyright 2018 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include <algorithm>
  4. #include <array>
  5. #include "common/common_types.h"
  6. #include "common/fs/file.h"
  7. #include "common/fs/path_util.h"
  8. #include "common/logging/log.h"
  9. #include "common/polyfill_ranges.h"
  10. #include "common/string_util.h"
  11. #include "common/swap.h"
  12. #include "core/constants.h"
  13. #include "core/core.h"
  14. #include "core/core_timing.h"
  15. #include "core/file_sys/control_metadata.h"
  16. #include "core/file_sys/patch_manager.h"
  17. #include "core/hle/service/acc/acc.h"
  18. #include "core/hle/service/acc/acc_aa.h"
  19. #include "core/hle/service/acc/acc_su.h"
  20. #include "core/hle/service/acc/acc_u0.h"
  21. #include "core/hle/service/acc/acc_u1.h"
  22. #include "core/hle/service/acc/async_context.h"
  23. #include "core/hle/service/acc/errors.h"
  24. #include "core/hle/service/acc/profile_manager.h"
  25. #include "core/hle/service/glue/glue_manager.h"
  26. #include "core/hle/service/ipc_helpers.h"
  27. #include "core/hle/service/server_manager.h"
  28. #include "core/loader/loader.h"
  29. namespace Service::Account {
  30. // Thumbnails are hard coded to be at least this size
  31. constexpr std::size_t THUMBNAIL_SIZE = 0x24000;
  32. static std::filesystem::path GetImagePath(const Common::UUID& uuid) {
  33. return Common::FS::GetYuzuPath(Common::FS::YuzuPath::NANDDir) /
  34. fmt::format("system/save/8000000000000010/su/avators/{}.jpg", uuid.FormattedString());
  35. }
  36. static constexpr u32 SanitizeJPEGSize(std::size_t size) {
  37. constexpr std::size_t max_jpeg_image_size = 0x20000;
  38. return static_cast<u32>(std::min(size, max_jpeg_image_size));
  39. }
  40. class IManagerForSystemService final : public ServiceFramework<IManagerForSystemService> {
  41. public:
  42. explicit IManagerForSystemService(Core::System& system_, Common::UUID)
  43. : ServiceFramework{system_, "IManagerForSystemService"} {
  44. // clang-format off
  45. static const FunctionInfo functions[] = {
  46. {0, nullptr, "CheckAvailability"},
  47. {1, nullptr, "GetAccountId"},
  48. {2, nullptr, "EnsureIdTokenCacheAsync"},
  49. {3, nullptr, "LoadIdTokenCache"},
  50. {100, nullptr, "SetSystemProgramIdentification"},
  51. {101, nullptr, "RefreshNotificationTokenAsync"}, // 7.0.0+
  52. {110, nullptr, "GetServiceEntryRequirementCache"}, // 4.0.0+
  53. {111, nullptr, "InvalidateServiceEntryRequirementCache"}, // 4.0.0+
  54. {112, nullptr, "InvalidateTokenCache"}, // 4.0.0 - 6.2.0
  55. {113, nullptr, "GetServiceEntryRequirementCacheForOnlinePlay"}, // 6.1.0+
  56. {120, nullptr, "GetNintendoAccountId"},
  57. {121, nullptr, "CalculateNintendoAccountAuthenticationFingerprint"}, // 9.0.0+
  58. {130, nullptr, "GetNintendoAccountUserResourceCache"},
  59. {131, nullptr, "RefreshNintendoAccountUserResourceCacheAsync"},
  60. {132, nullptr, "RefreshNintendoAccountUserResourceCacheAsyncIfSecondsElapsed"},
  61. {133, nullptr, "GetNintendoAccountVerificationUrlCache"}, // 9.0.0+
  62. {134, nullptr, "RefreshNintendoAccountVerificationUrlCache"}, // 9.0.0+
  63. {135, nullptr, "RefreshNintendoAccountVerificationUrlCacheAsyncIfSecondsElapsed"}, // 9.0.0+
  64. {140, nullptr, "GetNetworkServiceLicenseCache"}, // 5.0.0+
  65. {141, nullptr, "RefreshNetworkServiceLicenseCacheAsync"}, // 5.0.0+
  66. {142, nullptr, "RefreshNetworkServiceLicenseCacheAsyncIfSecondsElapsed"}, // 5.0.0+
  67. {150, nullptr, "CreateAuthorizationRequest"},
  68. {160, nullptr, "RequiresUpdateNetworkServiceAccountIdTokenCache"},
  69. {161, nullptr, "RequireReauthenticationOfNetworkServiceAccount"},
  70. };
  71. // clang-format on
  72. RegisterHandlers(functions);
  73. }
  74. };
  75. // 3.0.0+
  76. class IFloatingRegistrationRequest final : public ServiceFramework<IFloatingRegistrationRequest> {
  77. public:
  78. explicit IFloatingRegistrationRequest(Core::System& system_, Common::UUID)
  79. : ServiceFramework{system_, "IFloatingRegistrationRequest"} {
  80. // clang-format off
  81. static const FunctionInfo functions[] = {
  82. {0, nullptr, "GetSessionId"},
  83. {12, nullptr, "GetAccountId"},
  84. {13, nullptr, "GetLinkedNintendoAccountId"},
  85. {14, nullptr, "GetNickname"},
  86. {15, nullptr, "GetProfileImage"},
  87. {21, nullptr, "LoadIdTokenCache"},
  88. {100, nullptr, "RegisterUser"}, // [1.0.0-3.0.2] RegisterAsync
  89. {101, nullptr, "RegisterUserWithUid"}, // [1.0.0-3.0.2] RegisterWithUidAsync
  90. {102, nullptr, "RegisterNetworkServiceAccountAsync"}, // 4.0.0+
  91. {103, nullptr, "RegisterNetworkServiceAccountWithUidAsync"}, // 4.0.0+
  92. {110, nullptr, "SetSystemProgramIdentification"},
  93. {111, nullptr, "EnsureIdTokenCacheAsync"},
  94. };
  95. // clang-format on
  96. RegisterHandlers(functions);
  97. }
  98. };
  99. class IAdministrator final : public ServiceFramework<IAdministrator> {
  100. public:
  101. explicit IAdministrator(Core::System& system_, Common::UUID)
  102. : ServiceFramework{system_, "IAdministrator"} {
  103. // clang-format off
  104. static const FunctionInfo functions[] = {
  105. {0, nullptr, "CheckAvailability"},
  106. {1, nullptr, "GetAccountId"},
  107. {2, nullptr, "EnsureIdTokenCacheAsync"},
  108. {3, nullptr, "LoadIdTokenCache"},
  109. {100, nullptr, "SetSystemProgramIdentification"},
  110. {101, nullptr, "RefreshNotificationTokenAsync"}, // 7.0.0+
  111. {110, nullptr, "GetServiceEntryRequirementCache"}, // 4.0.0+
  112. {111, nullptr, "InvalidateServiceEntryRequirementCache"}, // 4.0.0+
  113. {112, nullptr, "InvalidateTokenCache"}, // 4.0.0 - 6.2.0
  114. {113, nullptr, "GetServiceEntryRequirementCacheForOnlinePlay"}, // 6.1.0+
  115. {120, nullptr, "GetNintendoAccountId"},
  116. {121, nullptr, "CalculateNintendoAccountAuthenticationFingerprint"}, // 9.0.0+
  117. {130, nullptr, "GetNintendoAccountUserResourceCache"},
  118. {131, nullptr, "RefreshNintendoAccountUserResourceCacheAsync"},
  119. {132, nullptr, "RefreshNintendoAccountUserResourceCacheAsyncIfSecondsElapsed"},
  120. {133, nullptr, "GetNintendoAccountVerificationUrlCache"}, // 9.0.0+
  121. {134, nullptr, "RefreshNintendoAccountVerificationUrlCacheAsync"}, // 9.0.0+
  122. {135, nullptr, "RefreshNintendoAccountVerificationUrlCacheAsyncIfSecondsElapsed"}, // 9.0.0+
  123. {140, nullptr, "GetNetworkServiceLicenseCache"}, // 5.0.0+
  124. {141, nullptr, "RefreshNetworkServiceLicenseCacheAsync"}, // 5.0.0+
  125. {142, nullptr, "RefreshNetworkServiceLicenseCacheAsyncIfSecondsElapsed"}, // 5.0.0+
  126. {143, nullptr, "GetNetworkServiceLicenseCacheEx"},
  127. {150, nullptr, "CreateAuthorizationRequest"},
  128. {160, nullptr, "RequiresUpdateNetworkServiceAccountIdTokenCache"},
  129. {161, nullptr, "RequireReauthenticationOfNetworkServiceAccount"},
  130. {200, nullptr, "IsRegistered"},
  131. {201, nullptr, "RegisterAsync"},
  132. {202, nullptr, "UnregisterAsync"},
  133. {203, nullptr, "DeleteRegistrationInfoLocally"},
  134. {220, nullptr, "SynchronizeProfileAsync"},
  135. {221, nullptr, "UploadProfileAsync"},
  136. {222, nullptr, "SynchronizaProfileAsyncIfSecondsElapsed"},
  137. {250, nullptr, "IsLinkedWithNintendoAccount"},
  138. {251, nullptr, "CreateProcedureToLinkWithNintendoAccount"},
  139. {252, nullptr, "ResumeProcedureToLinkWithNintendoAccount"},
  140. {255, nullptr, "CreateProcedureToUpdateLinkageStateOfNintendoAccount"},
  141. {256, nullptr, "ResumeProcedureToUpdateLinkageStateOfNintendoAccount"},
  142. {260, nullptr, "CreateProcedureToLinkNnidWithNintendoAccount"}, // 3.0.0+
  143. {261, nullptr, "ResumeProcedureToLinkNnidWithNintendoAccount"}, // 3.0.0+
  144. {280, nullptr, "ProxyProcedureToAcquireApplicationAuthorizationForNintendoAccount"},
  145. {290, nullptr, "GetRequestForNintendoAccountUserResourceView"}, // 8.0.0+
  146. {300, nullptr, "TryRecoverNintendoAccountUserStateAsync"}, // 6.0.0+
  147. {400, nullptr, "IsServiceEntryRequirementCacheRefreshRequiredForOnlinePlay"}, // 6.1.0+
  148. {401, nullptr, "RefreshServiceEntryRequirementCacheForOnlinePlayAsync"}, // 6.1.0+
  149. {900, nullptr, "GetAuthenticationInfoForWin"}, // 9.0.0+
  150. {901, nullptr, "ImportAsyncForWin"}, // 9.0.0+
  151. {997, nullptr, "DebugUnlinkNintendoAccountAsync"},
  152. {998, nullptr, "DebugSetAvailabilityErrorDetail"},
  153. };
  154. // clang-format on
  155. RegisterHandlers(functions);
  156. }
  157. };
  158. class IAuthorizationRequest final : public ServiceFramework<IAuthorizationRequest> {
  159. public:
  160. explicit IAuthorizationRequest(Core::System& system_, Common::UUID)
  161. : ServiceFramework{system_, "IAuthorizationRequest"} {
  162. // clang-format off
  163. static const FunctionInfo functions[] = {
  164. {0, nullptr, "GetSessionId"},
  165. {10, nullptr, "InvokeWithoutInteractionAsync"},
  166. {19, nullptr, "IsAuthorized"},
  167. {20, nullptr, "GetAuthorizationCode"},
  168. {21, nullptr, "GetIdToken"},
  169. {22, nullptr, "GetState"},
  170. };
  171. // clang-format on
  172. RegisterHandlers(functions);
  173. }
  174. };
  175. class IOAuthProcedure final : public ServiceFramework<IOAuthProcedure> {
  176. public:
  177. explicit IOAuthProcedure(Core::System& system_, Common::UUID)
  178. : ServiceFramework{system_, "IOAuthProcedure"} {
  179. // clang-format off
  180. static const FunctionInfo functions[] = {
  181. {0, nullptr, "PrepareAsync"},
  182. {1, nullptr, "GetRequest"},
  183. {2, nullptr, "ApplyResponse"},
  184. {3, nullptr, "ApplyResponseAsync"},
  185. {10, nullptr, "Suspend"},
  186. };
  187. // clang-format on
  188. RegisterHandlers(functions);
  189. }
  190. };
  191. // 3.0.0+
  192. class IOAuthProcedureForExternalNsa final : public ServiceFramework<IOAuthProcedureForExternalNsa> {
  193. public:
  194. explicit IOAuthProcedureForExternalNsa(Core::System& system_, Common::UUID)
  195. : ServiceFramework{system_, "IOAuthProcedureForExternalNsa"} {
  196. // clang-format off
  197. static const FunctionInfo functions[] = {
  198. {0, nullptr, "PrepareAsync"},
  199. {1, nullptr, "GetRequest"},
  200. {2, nullptr, "ApplyResponse"},
  201. {3, nullptr, "ApplyResponseAsync"},
  202. {10, nullptr, "Suspend"},
  203. {100, nullptr, "GetAccountId"},
  204. {101, nullptr, "GetLinkedNintendoAccountId"},
  205. {102, nullptr, "GetNickname"},
  206. {103, nullptr, "GetProfileImage"},
  207. };
  208. // clang-format on
  209. RegisterHandlers(functions);
  210. }
  211. };
  212. class IOAuthProcedureForNintendoAccountLinkage final
  213. : public ServiceFramework<IOAuthProcedureForNintendoAccountLinkage> {
  214. public:
  215. explicit IOAuthProcedureForNintendoAccountLinkage(Core::System& system_, Common::UUID)
  216. : ServiceFramework{system_, "IOAuthProcedureForNintendoAccountLinkage"} {
  217. // clang-format off
  218. static const FunctionInfo functions[] = {
  219. {0, nullptr, "PrepareAsync"},
  220. {1, nullptr, "GetRequest"},
  221. {2, nullptr, "ApplyResponse"},
  222. {3, nullptr, "ApplyResponseAsync"},
  223. {10, nullptr, "Suspend"},
  224. {100, nullptr, "GetRequestWithTheme"},
  225. {101, nullptr, "IsNetworkServiceAccountReplaced"},
  226. {199, nullptr, "GetUrlForIntroductionOfExtraMembership"}, // 2.0.0 - 5.1.0
  227. {200, nullptr, "ApplyAsyncWithAuthorizedToken"},
  228. };
  229. // clang-format on
  230. RegisterHandlers(functions);
  231. }
  232. };
  233. class INotifier final : public ServiceFramework<INotifier> {
  234. public:
  235. explicit INotifier(Core::System& system_, Common::UUID)
  236. : ServiceFramework{system_, "INotifier"} {
  237. // clang-format off
  238. static const FunctionInfo functions[] = {
  239. {0, nullptr, "GetSystemEvent"},
  240. };
  241. // clang-format on
  242. RegisterHandlers(functions);
  243. }
  244. };
  245. class IProfileCommon : public ServiceFramework<IProfileCommon> {
  246. public:
  247. explicit IProfileCommon(Core::System& system_, const char* name, bool editor_commands,
  248. Common::UUID user_id_, ProfileManager& profile_manager_)
  249. : ServiceFramework{system_, name}, profile_manager{profile_manager_}, user_id{user_id_} {
  250. static const FunctionInfo functions[] = {
  251. {0, &IProfileCommon::Get, "Get"},
  252. {1, &IProfileCommon::GetBase, "GetBase"},
  253. {10, &IProfileCommon::GetImageSize, "GetImageSize"},
  254. {11, &IProfileCommon::LoadImage, "LoadImage"},
  255. };
  256. RegisterHandlers(functions);
  257. if (editor_commands) {
  258. static const FunctionInfo editor_functions[] = {
  259. {100, &IProfileCommon::Store, "Store"},
  260. {101, &IProfileCommon::StoreWithImage, "StoreWithImage"},
  261. };
  262. RegisterHandlers(editor_functions);
  263. }
  264. }
  265. protected:
  266. void Get(HLERequestContext& ctx) {
  267. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  268. ProfileBase profile_base{};
  269. UserData data{};
  270. if (profile_manager.GetProfileBaseAndData(user_id, profile_base, data)) {
  271. ctx.WriteBuffer(data);
  272. IPC::ResponseBuilder rb{ctx, 16};
  273. rb.Push(ResultSuccess);
  274. rb.PushRaw(profile_base);
  275. } else {
  276. LOG_ERROR(Service_ACC, "Failed to get profile base and data for user=0x{}",
  277. user_id.RawString());
  278. IPC::ResponseBuilder rb{ctx, 2};
  279. rb.Push(ResultUnknown); // TODO(ogniK): Get actual error code
  280. }
  281. }
  282. void GetBase(HLERequestContext& ctx) {
  283. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  284. ProfileBase profile_base{};
  285. if (profile_manager.GetProfileBase(user_id, profile_base)) {
  286. IPC::ResponseBuilder rb{ctx, 16};
  287. rb.Push(ResultSuccess);
  288. rb.PushRaw(profile_base);
  289. } else {
  290. LOG_ERROR(Service_ACC, "Failed to get profile base for user=0x{}", user_id.RawString());
  291. IPC::ResponseBuilder rb{ctx, 2};
  292. rb.Push(ResultUnknown); // TODO(ogniK): Get actual error code
  293. }
  294. }
  295. void LoadImage(HLERequestContext& ctx) {
  296. LOG_DEBUG(Service_ACC, "called");
  297. IPC::ResponseBuilder rb{ctx, 3};
  298. rb.Push(ResultSuccess);
  299. const Common::FS::IOFile image(GetImagePath(user_id), Common::FS::FileAccessMode::Read,
  300. Common::FS::FileType::BinaryFile);
  301. if (!image.IsOpen()) {
  302. LOG_WARNING(Service_ACC,
  303. "Failed to load user provided image! Falling back to built-in backup...");
  304. ctx.WriteBuffer(Core::Constants::ACCOUNT_BACKUP_JPEG);
  305. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  306. return;
  307. }
  308. const u32 size = SanitizeJPEGSize(image.GetSize());
  309. std::vector<u8> buffer(size);
  310. if (image.Read(buffer) != buffer.size()) {
  311. LOG_ERROR(Service_ACC, "Failed to read all the bytes in the user provided image.");
  312. }
  313. ctx.WriteBuffer(buffer);
  314. rb.Push<u32>(size);
  315. }
  316. void GetImageSize(HLERequestContext& ctx) {
  317. LOG_DEBUG(Service_ACC, "called");
  318. IPC::ResponseBuilder rb{ctx, 3};
  319. rb.Push(ResultSuccess);
  320. const Common::FS::IOFile image(GetImagePath(user_id), Common::FS::FileAccessMode::Read,
  321. Common::FS::FileType::BinaryFile);
  322. if (!image.IsOpen()) {
  323. LOG_WARNING(Service_ACC,
  324. "Failed to load user provided image! Falling back to built-in backup...");
  325. rb.Push(SanitizeJPEGSize(Core::Constants::ACCOUNT_BACKUP_JPEG.size()));
  326. } else {
  327. rb.Push(SanitizeJPEGSize(image.GetSize()));
  328. }
  329. }
  330. void Store(HLERequestContext& ctx) {
  331. IPC::RequestParser rp{ctx};
  332. const auto base = rp.PopRaw<ProfileBase>();
  333. const auto user_data = ctx.ReadBuffer();
  334. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid=0x{}",
  335. Common::StringFromFixedZeroTerminatedBuffer(
  336. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  337. base.timestamp, base.user_uuid.RawString());
  338. if (user_data.size() < sizeof(UserData)) {
  339. LOG_ERROR(Service_ACC, "UserData buffer too small!");
  340. IPC::ResponseBuilder rb{ctx, 2};
  341. rb.Push(Account::ResultInvalidArrayLength);
  342. return;
  343. }
  344. UserData data;
  345. std::memcpy(&data, user_data.data(), sizeof(UserData));
  346. if (!profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  347. LOG_ERROR(Service_ACC, "Failed to update user data and base!");
  348. IPC::ResponseBuilder rb{ctx, 2};
  349. rb.Push(Account::ResultAccountUpdateFailed);
  350. return;
  351. }
  352. IPC::ResponseBuilder rb{ctx, 2};
  353. rb.Push(ResultSuccess);
  354. }
  355. void StoreWithImage(HLERequestContext& ctx) {
  356. IPC::RequestParser rp{ctx};
  357. const auto base = rp.PopRaw<ProfileBase>();
  358. const auto user_data = ctx.ReadBuffer();
  359. const auto image_data = ctx.ReadBuffer(1);
  360. LOG_DEBUG(Service_ACC, "called, username='{}', timestamp={:016X}, uuid=0x{}",
  361. Common::StringFromFixedZeroTerminatedBuffer(
  362. reinterpret_cast<const char*>(base.username.data()), base.username.size()),
  363. base.timestamp, base.user_uuid.RawString());
  364. if (user_data.size() < sizeof(UserData)) {
  365. LOG_ERROR(Service_ACC, "UserData buffer too small!");
  366. IPC::ResponseBuilder rb{ctx, 2};
  367. rb.Push(Account::ResultInvalidArrayLength);
  368. return;
  369. }
  370. UserData data;
  371. std::memcpy(&data, user_data.data(), sizeof(UserData));
  372. Common::FS::IOFile image(GetImagePath(user_id), Common::FS::FileAccessMode::Write,
  373. Common::FS::FileType::BinaryFile);
  374. if (!image.IsOpen() || !image.SetSize(image_data.size()) ||
  375. image.Write(image_data) != image_data.size() ||
  376. !profile_manager.SetProfileBaseAndData(user_id, base, data)) {
  377. LOG_ERROR(Service_ACC, "Failed to update profile data, base, and image!");
  378. IPC::ResponseBuilder rb{ctx, 2};
  379. rb.Push(Account::ResultAccountUpdateFailed);
  380. return;
  381. }
  382. IPC::ResponseBuilder rb{ctx, 2};
  383. rb.Push(ResultSuccess);
  384. }
  385. ProfileManager& profile_manager;
  386. Common::UUID user_id{}; ///< The user id this profile refers to.
  387. };
  388. class IProfile final : public IProfileCommon {
  389. public:
  390. explicit IProfile(Core::System& system_, Common::UUID user_id_,
  391. ProfileManager& profile_manager_)
  392. : IProfileCommon{system_, "IProfile", false, user_id_, profile_manager_} {}
  393. };
  394. class IProfileEditor final : public IProfileCommon {
  395. public:
  396. explicit IProfileEditor(Core::System& system_, Common::UUID user_id_,
  397. ProfileManager& profile_manager_)
  398. : IProfileCommon{system_, "IProfileEditor", true, user_id_, profile_manager_} {}
  399. };
  400. class ISessionObject final : public ServiceFramework<ISessionObject> {
  401. public:
  402. explicit ISessionObject(Core::System& system_, Common::UUID)
  403. : ServiceFramework{system_, "ISessionObject"} {
  404. // clang-format off
  405. static const FunctionInfo functions[] = {
  406. {999, nullptr, "Dummy"},
  407. };
  408. // clang-format on
  409. RegisterHandlers(functions);
  410. }
  411. };
  412. class IGuestLoginRequest final : public ServiceFramework<IGuestLoginRequest> {
  413. public:
  414. explicit IGuestLoginRequest(Core::System& system_, Common::UUID)
  415. : ServiceFramework{system_, "IGuestLoginRequest"} {
  416. // clang-format off
  417. static const FunctionInfo functions[] = {
  418. {0, nullptr, "GetSessionId"},
  419. {11, nullptr, "Unknown"}, // 1.0.0 - 2.3.0 (the name is blank on Switchbrew)
  420. {12, nullptr, "GetAccountId"},
  421. {13, nullptr, "GetLinkedNintendoAccountId"},
  422. {14, nullptr, "GetNickname"},
  423. {15, nullptr, "GetProfileImage"},
  424. {21, nullptr, "LoadIdTokenCache"}, // 3.0.0+
  425. };
  426. // clang-format on
  427. RegisterHandlers(functions);
  428. }
  429. };
  430. class EnsureTokenIdCacheAsyncInterface final : public IAsyncContext {
  431. public:
  432. explicit EnsureTokenIdCacheAsyncInterface(Core::System& system_) : IAsyncContext{system_} {
  433. MarkComplete();
  434. }
  435. ~EnsureTokenIdCacheAsyncInterface() = default;
  436. void LoadIdTokenCache(HLERequestContext& ctx) {
  437. LOG_WARNING(Service_ACC, "(STUBBED) called");
  438. IPC::ResponseBuilder rb{ctx, 2};
  439. rb.Push(ResultSuccess);
  440. }
  441. protected:
  442. bool IsComplete() const override {
  443. return true;
  444. }
  445. void Cancel() override {}
  446. Result GetResult() const override {
  447. return ResultSuccess;
  448. }
  449. };
  450. class IManagerForApplication final : public ServiceFramework<IManagerForApplication> {
  451. public:
  452. explicit IManagerForApplication(Core::System& system_,
  453. const std::shared_ptr<ProfileManager>& profile_manager_)
  454. : ServiceFramework{system_, "IManagerForApplication"},
  455. ensure_token_id{std::make_shared<EnsureTokenIdCacheAsyncInterface>(system)},
  456. profile_manager{profile_manager_} {
  457. // clang-format off
  458. static const FunctionInfo functions[] = {
  459. {0, &IManagerForApplication::CheckAvailability, "CheckAvailability"},
  460. {1, &IManagerForApplication::GetAccountId, "GetAccountId"},
  461. {2, &IManagerForApplication::EnsureIdTokenCacheAsync, "EnsureIdTokenCacheAsync"},
  462. {3, &IManagerForApplication::LoadIdTokenCache, "LoadIdTokenCache"},
  463. {130, &IManagerForApplication::GetNintendoAccountUserResourceCacheForApplication, "GetNintendoAccountUserResourceCacheForApplication"},
  464. {150, nullptr, "CreateAuthorizationRequest"},
  465. {160, &IManagerForApplication::StoreOpenContext, "StoreOpenContext"},
  466. {170, nullptr, "LoadNetworkServiceLicenseKindAsync"},
  467. };
  468. // clang-format on
  469. RegisterHandlers(functions);
  470. }
  471. private:
  472. void CheckAvailability(HLERequestContext& ctx) {
  473. LOG_DEBUG(Service_ACC, "(STUBBED) called");
  474. IPC::ResponseBuilder rb{ctx, 3};
  475. rb.Push(ResultSuccess);
  476. rb.Push(false); // TODO: Check when this is supposed to return true and when not
  477. }
  478. void GetAccountId(HLERequestContext& ctx) {
  479. LOG_DEBUG(Service_ACC, "called");
  480. IPC::ResponseBuilder rb{ctx, 4};
  481. rb.Push(ResultSuccess);
  482. rb.PushRaw<u64>(profile_manager->GetLastOpenedUser().Hash());
  483. }
  484. void EnsureIdTokenCacheAsync(HLERequestContext& ctx) {
  485. LOG_WARNING(Service_ACC, "(STUBBED) called");
  486. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  487. rb.Push(ResultSuccess);
  488. rb.PushIpcInterface(ensure_token_id);
  489. }
  490. void LoadIdTokenCache(HLERequestContext& ctx) {
  491. LOG_WARNING(Service_ACC, "(STUBBED) called");
  492. ensure_token_id->LoadIdTokenCache(ctx);
  493. }
  494. void GetNintendoAccountUserResourceCacheForApplication(HLERequestContext& ctx) {
  495. LOG_WARNING(Service_ACC, "(STUBBED) called");
  496. std::vector<u8> nas_user_base_for_application(0x68);
  497. ctx.WriteBuffer(nas_user_base_for_application, 0);
  498. if (ctx.CanWriteBuffer(1)) {
  499. std::vector<u8> unknown_out_buffer(ctx.GetWriteBufferSize(1));
  500. ctx.WriteBuffer(unknown_out_buffer, 1);
  501. }
  502. IPC::ResponseBuilder rb{ctx, 4};
  503. rb.Push(ResultSuccess);
  504. rb.PushRaw<u64>(profile_manager->GetLastOpenedUser().Hash());
  505. }
  506. void StoreOpenContext(HLERequestContext& ctx) {
  507. LOG_DEBUG(Service_ACC, "called");
  508. profile_manager->StoreOpenedUsers();
  509. IPC::ResponseBuilder rb{ctx, 2};
  510. rb.Push(ResultSuccess);
  511. }
  512. std::shared_ptr<EnsureTokenIdCacheAsyncInterface> ensure_token_id{};
  513. std::shared_ptr<ProfileManager> profile_manager;
  514. };
  515. // 6.0.0+
  516. class IAsyncNetworkServiceLicenseKindContext final
  517. : public ServiceFramework<IAsyncNetworkServiceLicenseKindContext> {
  518. public:
  519. explicit IAsyncNetworkServiceLicenseKindContext(Core::System& system_, Common::UUID)
  520. : ServiceFramework{system_, "IAsyncNetworkServiceLicenseKindContext"} {
  521. // clang-format off
  522. static const FunctionInfo functions[] = {
  523. {0, nullptr, "GetSystemEvent"},
  524. {1, nullptr, "Cancel"},
  525. {2, nullptr, "HasDone"},
  526. {3, nullptr, "GetResult"},
  527. {4, nullptr, "GetNetworkServiceLicenseKind"},
  528. };
  529. // clang-format on
  530. RegisterHandlers(functions);
  531. }
  532. };
  533. // 8.0.0+
  534. class IOAuthProcedureForUserRegistration final
  535. : public ServiceFramework<IOAuthProcedureForUserRegistration> {
  536. public:
  537. explicit IOAuthProcedureForUserRegistration(Core::System& system_, Common::UUID)
  538. : ServiceFramework{system_, "IOAuthProcedureForUserRegistration"} {
  539. // clang-format off
  540. static const FunctionInfo functions[] = {
  541. {0, nullptr, "PrepareAsync"},
  542. {1, nullptr, "GetRequest"},
  543. {2, nullptr, "ApplyResponse"},
  544. {3, nullptr, "ApplyResponseAsync"},
  545. {10, nullptr, "Suspend"},
  546. {100, nullptr, "GetAccountId"},
  547. {101, nullptr, "GetLinkedNintendoAccountId"},
  548. {102, nullptr, "GetNickname"},
  549. {103, nullptr, "GetProfileImage"},
  550. {110, nullptr, "RegisterUserAsync"},
  551. {111, nullptr, "GetUid"},
  552. };
  553. // clang-format on
  554. RegisterHandlers(functions);
  555. }
  556. };
  557. class DAUTH_O final : public ServiceFramework<DAUTH_O> {
  558. public:
  559. explicit DAUTH_O(Core::System& system_, Common::UUID) : ServiceFramework{system_, "dauth:o"} {
  560. // clang-format off
  561. static const FunctionInfo functions[] = {
  562. {0, nullptr, "EnsureAuthenticationTokenCacheAsync"},
  563. {1, nullptr, "LoadAuthenticationTokenCache"},
  564. {2, nullptr, "InvalidateAuthenticationTokenCache"},
  565. {3, nullptr, "IsDeviceAuthenticationTokenCacheAvailable"},
  566. {10, nullptr, "EnsureEdgeTokenCacheAsync"},
  567. {11, nullptr, "LoadEdgeTokenCache"},
  568. {12, nullptr, "InvalidateEdgeTokenCache"},
  569. {13, nullptr, "IsEdgeTokenCacheAvailable"},
  570. {20, nullptr, "EnsureApplicationAuthenticationCacheAsync"},
  571. {21, nullptr, "LoadApplicationAuthenticationTokenCache"},
  572. {22, nullptr, "LoadApplicationNetworkServiceClientConfigCache"},
  573. {23, nullptr, "IsApplicationAuthenticationCacheAvailable"},
  574. {24, nullptr, "InvalidateApplicationAuthenticationCache"},
  575. };
  576. // clang-format on
  577. RegisterHandlers(functions);
  578. }
  579. };
  580. // 6.0.0+
  581. class IAsyncResult final : public ServiceFramework<IAsyncResult> {
  582. public:
  583. explicit IAsyncResult(Core::System& system_, Common::UUID)
  584. : ServiceFramework{system_, "IAsyncResult"} {
  585. // clang-format off
  586. static const FunctionInfo functions[] = {
  587. {0, nullptr, "GetResult"},
  588. {1, nullptr, "Cancel"},
  589. {2, nullptr, "IsAvailable"},
  590. {3, nullptr, "GetSystemEvent"},
  591. };
  592. // clang-format on
  593. RegisterHandlers(functions);
  594. }
  595. };
  596. void Module::Interface::GetUserCount(HLERequestContext& ctx) {
  597. LOG_DEBUG(Service_ACC, "called");
  598. IPC::ResponseBuilder rb{ctx, 3};
  599. rb.Push(ResultSuccess);
  600. rb.Push<u32>(static_cast<u32>(profile_manager->GetUserCount()));
  601. }
  602. void Module::Interface::GetUserExistence(HLERequestContext& ctx) {
  603. IPC::RequestParser rp{ctx};
  604. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  605. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  606. IPC::ResponseBuilder rb{ctx, 3};
  607. rb.Push(ResultSuccess);
  608. rb.Push(profile_manager->UserExists(user_id));
  609. }
  610. void Module::Interface::ListAllUsers(HLERequestContext& ctx) {
  611. LOG_DEBUG(Service_ACC, "called");
  612. ctx.WriteBuffer(profile_manager->GetAllUsers());
  613. IPC::ResponseBuilder rb{ctx, 2};
  614. rb.Push(ResultSuccess);
  615. }
  616. void Module::Interface::ListOpenUsers(HLERequestContext& ctx) {
  617. LOG_DEBUG(Service_ACC, "called");
  618. ctx.WriteBuffer(profile_manager->GetOpenUsers());
  619. IPC::ResponseBuilder rb{ctx, 2};
  620. rb.Push(ResultSuccess);
  621. }
  622. void Module::Interface::GetLastOpenedUser(HLERequestContext& ctx) {
  623. LOG_DEBUG(Service_ACC, "called");
  624. IPC::ResponseBuilder rb{ctx, 6};
  625. rb.Push(ResultSuccess);
  626. rb.PushRaw<Common::UUID>(profile_manager->GetLastOpenedUser());
  627. }
  628. void Module::Interface::GetProfile(HLERequestContext& ctx) {
  629. IPC::RequestParser rp{ctx};
  630. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  631. LOG_DEBUG(Service_ACC, "called user_id=0x{}", user_id.RawString());
  632. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  633. rb.Push(ResultSuccess);
  634. rb.PushIpcInterface<IProfile>(system, user_id, *profile_manager);
  635. }
  636. void Module::Interface::IsUserRegistrationRequestPermitted(HLERequestContext& ctx) {
  637. LOG_WARNING(Service_ACC, "(STUBBED) called");
  638. IPC::ResponseBuilder rb{ctx, 3};
  639. rb.Push(ResultSuccess);
  640. rb.Push(profile_manager->CanSystemRegisterUser());
  641. }
  642. void Module::Interface::InitializeApplicationInfo(HLERequestContext& ctx) {
  643. LOG_DEBUG(Service_ACC, "called");
  644. IPC::ResponseBuilder rb{ctx, 2};
  645. rb.Push(InitializeApplicationInfoBase());
  646. }
  647. void Module::Interface::InitializeApplicationInfoRestricted(HLERequestContext& ctx) {
  648. LOG_WARNING(Service_ACC, "(Partial implementation) called");
  649. // TODO(ogniK): We require checking if the user actually owns the title and what not. As of
  650. // currently, we assume the user owns the title. InitializeApplicationInfoBase SHOULD be called
  651. // first then we do extra checks if the game is a digital copy.
  652. IPC::ResponseBuilder rb{ctx, 2};
  653. rb.Push(InitializeApplicationInfoBase());
  654. }
  655. Result Module::Interface::InitializeApplicationInfoBase() {
  656. if (application_info) {
  657. LOG_ERROR(Service_ACC, "Application already initialized");
  658. return Account::ResultApplicationInfoAlreadyInitialized;
  659. }
  660. // TODO(ogniK): This should be changed to reflect the target process for when we have multiple
  661. // processes emulated. As we don't actually have pid support we should assume we're just using
  662. // our own process
  663. const auto launch_property =
  664. system.GetARPManager().GetLaunchProperty(system.GetApplicationProcessProgramID());
  665. if (launch_property.Failed()) {
  666. LOG_ERROR(Service_ACC, "Failed to get launch property");
  667. return Account::ResultInvalidApplication;
  668. }
  669. switch (launch_property->base_game_storage_id) {
  670. case FileSys::StorageId::GameCard:
  671. application_info.application_type = ApplicationType::GameCard;
  672. break;
  673. case FileSys::StorageId::Host:
  674. case FileSys::StorageId::NandUser:
  675. case FileSys::StorageId::SdCard:
  676. case FileSys::StorageId::None: // Yuzu specific, differs from hardware
  677. application_info.application_type = ApplicationType::Digital;
  678. break;
  679. default:
  680. LOG_ERROR(Service_ACC, "Invalid game storage ID! storage_id={}",
  681. launch_property->base_game_storage_id);
  682. return Account::ResultInvalidApplication;
  683. }
  684. LOG_WARNING(Service_ACC, "ApplicationInfo init required");
  685. // TODO(ogniK): Actual initialization here
  686. return ResultSuccess;
  687. }
  688. void Module::Interface::GetBaasAccountManagerForApplication(HLERequestContext& ctx) {
  689. LOG_DEBUG(Service_ACC, "called");
  690. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  691. rb.Push(ResultSuccess);
  692. rb.PushIpcInterface<IManagerForApplication>(system, profile_manager);
  693. }
  694. void Module::Interface::IsUserAccountSwitchLocked(HLERequestContext& ctx) {
  695. LOG_DEBUG(Service_ACC, "called");
  696. FileSys::NACP nacp;
  697. const auto res = system.GetAppLoader().ReadControlData(nacp);
  698. bool is_locked = false;
  699. if (res != Loader::ResultStatus::Success) {
  700. const FileSys::PatchManager pm{system.GetApplicationProcessProgramID(),
  701. system.GetFileSystemController(),
  702. system.GetContentProvider()};
  703. const auto nacp_unique = pm.GetControlMetadata().first;
  704. if (nacp_unique != nullptr) {
  705. is_locked = nacp_unique->GetUserAccountSwitchLock();
  706. } else {
  707. LOG_ERROR(Service_ACC, "nacp_unique is null!");
  708. }
  709. } else {
  710. is_locked = nacp.GetUserAccountSwitchLock();
  711. }
  712. IPC::ResponseBuilder rb{ctx, 3};
  713. rb.Push(ResultSuccess);
  714. rb.Push(is_locked);
  715. }
  716. void Module::Interface::InitializeApplicationInfoV2(HLERequestContext& ctx) {
  717. LOG_WARNING(Service_ACC, "(STUBBED) called");
  718. IPC::ResponseBuilder rb{ctx, 2};
  719. rb.Push(ResultSuccess);
  720. }
  721. void Module::Interface::GetProfileEditor(HLERequestContext& ctx) {
  722. IPC::RequestParser rp{ctx};
  723. Common::UUID user_id = rp.PopRaw<Common::UUID>();
  724. LOG_DEBUG(Service_ACC, "called, user_id=0x{}", user_id.RawString());
  725. IPC::ResponseBuilder rb{ctx, 2, 0, 1};
  726. rb.Push(ResultSuccess);
  727. rb.PushIpcInterface<IProfileEditor>(system, user_id, *profile_manager);
  728. }
  729. void Module::Interface::ListQualifiedUsers(HLERequestContext& ctx) {
  730. LOG_DEBUG(Service_ACC, "called");
  731. // All users should be qualified. We don't actually have parental control or anything to do with
  732. // nintendo online currently. We're just going to assume the user running the game has access to
  733. // the game regardless of parental control settings.
  734. ctx.WriteBuffer(profile_manager->GetAllUsers());
  735. IPC::ResponseBuilder rb{ctx, 2};
  736. rb.Push(ResultSuccess);
  737. }
  738. void Module::Interface::ListOpenContextStoredUsers(HLERequestContext& ctx) {
  739. LOG_DEBUG(Service_ACC, "called");
  740. ctx.WriteBuffer(profile_manager->GetStoredOpenedUsers());
  741. IPC::ResponseBuilder rb{ctx, 2};
  742. rb.Push(ResultSuccess);
  743. }
  744. void Module::Interface::StoreSaveDataThumbnailApplication(HLERequestContext& ctx) {
  745. IPC::RequestParser rp{ctx};
  746. const auto uuid = rp.PopRaw<Common::UUID>();
  747. LOG_WARNING(Service_ACC, "(STUBBED) called, uuid=0x{}", uuid.RawString());
  748. // TODO(ogniK): Check if application ID is zero on acc initialize. As we don't have a reliable
  749. // way of confirming things like the TID, we're going to assume a non zero value for the time
  750. // being.
  751. constexpr u64 tid{1};
  752. StoreSaveDataThumbnail(ctx, uuid, tid);
  753. }
  754. void Module::Interface::StoreSaveDataThumbnailSystem(HLERequestContext& ctx) {
  755. IPC::RequestParser rp{ctx};
  756. const auto uuid = rp.PopRaw<Common::UUID>();
  757. const auto tid = rp.Pop<u64_le>();
  758. LOG_WARNING(Service_ACC, "(STUBBED) called, uuid=0x{}, tid={:016X}", uuid.RawString(), tid);
  759. StoreSaveDataThumbnail(ctx, uuid, tid);
  760. }
  761. void Module::Interface::StoreSaveDataThumbnail(HLERequestContext& ctx, const Common::UUID& uuid,
  762. const u64 tid) {
  763. IPC::ResponseBuilder rb{ctx, 2};
  764. if (tid == 0) {
  765. LOG_ERROR(Service_ACC, "TitleID is not valid!");
  766. rb.Push(Account::ResultInvalidApplication);
  767. return;
  768. }
  769. if (uuid.IsInvalid()) {
  770. LOG_ERROR(Service_ACC, "User ID is not valid!");
  771. rb.Push(Account::ResultInvalidUserId);
  772. return;
  773. }
  774. const auto thumbnail_size = ctx.GetReadBufferSize();
  775. if (thumbnail_size != THUMBNAIL_SIZE) {
  776. LOG_ERROR(Service_ACC, "Buffer size is empty! size={:X} expecting {:X}", thumbnail_size,
  777. THUMBNAIL_SIZE);
  778. rb.Push(Account::ResultInvalidArrayLength);
  779. return;
  780. }
  781. // TODO(ogniK): Construct save data thumbnail
  782. rb.Push(ResultSuccess);
  783. }
  784. void Module::Interface::TrySelectUserWithoutInteraction(HLERequestContext& ctx) {
  785. LOG_DEBUG(Service_ACC, "called");
  786. // A u8 is passed into this function which we can safely ignore. It's to determine if we have
  787. // access to use the network or not by the looks of it
  788. IPC::ResponseBuilder rb{ctx, 6};
  789. if (profile_manager->GetUserCount() != 1) {
  790. rb.Push(ResultSuccess);
  791. rb.PushRaw(Common::InvalidUUID);
  792. return;
  793. }
  794. const auto user_list = profile_manager->GetAllUsers();
  795. if (std::ranges::all_of(user_list, [](const auto& user) { return user.IsInvalid(); })) {
  796. rb.Push(ResultUnknown); // TODO(ogniK): Find the correct error code
  797. rb.PushRaw(Common::InvalidUUID);
  798. return;
  799. }
  800. // Select the first user we have
  801. rb.Push(ResultSuccess);
  802. rb.PushRaw(profile_manager->GetUser(0)->uuid);
  803. }
  804. Module::Interface::Interface(std::shared_ptr<Module> module_,
  805. std::shared_ptr<ProfileManager> profile_manager_,
  806. Core::System& system_, const char* name)
  807. : ServiceFramework{system_, name}, module{std::move(module_)}, profile_manager{std::move(
  808. profile_manager_)} {}
  809. Module::Interface::~Interface() = default;
  810. void LoopProcess(Core::System& system) {
  811. auto module = std::make_shared<Module>();
  812. auto profile_manager = std::make_shared<ProfileManager>();
  813. auto server_manager = std::make_unique<ServerManager>(system);
  814. server_manager->RegisterNamedService("acc:aa",
  815. std::make_shared<ACC_AA>(module, profile_manager, system));
  816. server_manager->RegisterNamedService("acc:su",
  817. std::make_shared<ACC_SU>(module, profile_manager, system));
  818. server_manager->RegisterNamedService("acc:u0",
  819. std::make_shared<ACC_U0>(module, profile_manager, system));
  820. server_manager->RegisterNamedService("acc:u1",
  821. std::make_shared<ACC_U1>(module, profile_manager, system));
  822. ServerManager::RunServer(std::move(server_manager));
  823. }
  824. } // namespace Service::Account