1/*
2 * Copyright (c) 2004-2008,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// pcscmonitor - use PCSC to monitor smartcard reader/card state for securityd
27//
28#ifndef _H_PCSCMONITOR
29#define _H_PCSCMONITOR
30
31#include "server.h"
32#include "tokencache.h"
33#include "reader.h"
34#include "token.h"
35#include <security_utilities/pcsc++.h>
36#include <security_utilities/coderepository.h>
37#include <set>
38
39
40//
41// A PCSCMonitor uses PCSC to monitor the state of smartcard readers and
42// tokens (cards) in the system, and dispatches messages and events to the
43// various related players in securityd. There should be at most one of these
44// objects active within securityd.
45//
46class PCSCMonitor : private Listener, private MachServer::Timer {
47public:
48	enum ServiceLevel {
49		forcedOff,					// no service under any circumstances
50		externalDaemon				// use externally launched daemon if present (do not manage pcscd)
51	};
52
53	PCSCMonitor(Server &server, const char* pathToCache, ServiceLevel level = externalDaemon);
54
55protected:
56	Server &server;
57	TokenCache& tokenCache();
58
59protected:
60    // Listener
61    void notifyMe(Notification *message);
62
63	// MachServer::Timer
64	void action();
65
66public: //@@@@
67	void startSoftTokens();
68	void loadSoftToken(Bundle *tokendBundle);
69
70private:
71	ServiceLevel mServiceLevel;	// level of service requested/determined
72
73	std::string mCachePath;		// path to cache directory
74	TokenCache *mTokenCache;	// cache object (lazy)
75
76	typedef map<string, RefPointer<Reader> > ReaderMap;
77	typedef set<RefPointer<Reader> > ReaderSet;
78	ReaderMap mReaders;		// presently known PCSC Readers (aka slots)
79
80	class Watcher : public Thread {
81	public:
82		Watcher(Server &server, TokenCache &tokenCache, ReaderMap& readers);
83
84	protected:
85		void action();
86
87	private:
88		Server &mServer;
89		TokenCache &mTokenCache;
90		PCSC::Session mSession;		// PCSC client session
91		ReaderMap& mReaders;
92	};
93};
94
95
96#endif //_H_PCSCMONITOR
97