]> git.saurik.com Git - apple/securityd.git/blob - src/reader.cpp
securityd-26232.tar.gz
[apple/securityd.git] / src / reader.cpp
1 /*
2 * Copyright (c) 2004 Apple Computer, 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 // reader - token reader objects
27 //
28 #include "reader.h"
29
30
31 //
32 // Construct a Reader
33 // This does not commence state tracking; call update to start up the reader.
34 //
35 Reader::Reader(TokenCache &tc, const PCSC::ReaderState &state)
36 : cache(tc), mToken(NULL)
37 {
38 mName = state.name(); // remember separate copy of name
39 mPrintName = mName; //@@@ how to make this readable? Use IOKit information?
40 secdebug("reader", "%p (%s) new reader", this, name().c_str());
41 }
42
43 Reader::~Reader()
44 {
45 secdebug("reader", "%p (%s) destroyed", this, name().c_str());
46 }
47
48
49 //
50 // Killing a reader forcibly removes its Token, if any
51 //
52 void Reader::kill()
53 {
54 if (mToken)
55 removeToken();
56 NodeCore::kill();
57 }
58
59
60 //
61 // State transition matrix for a reader, based on PCSC state changes
62 //
63 void Reader::update(const PCSC::ReaderState &state)
64 {
65 // set new state
66 IFDEBUG(unsigned long oldState = mState.state());
67 mState = state;
68 mState.name(mName.c_str()); // (fix name pointer, unchanged)
69
70 try {
71 if (state.state(SCARD_STATE_UNAVAILABLE)) {
72 // reader is unusable (probably being removed)
73 secdebug("reader", "%p (%s) unavailable (0x%lx)",
74 this, name().c_str(), state.state());
75 if (mToken)
76 removeToken();
77 } else if (state.state(SCARD_STATE_EMPTY)) {
78 // reader is empty (no token present)
79 secdebug("reader", "%p (%s) empty (0x%lx)",
80 this, name().c_str(), state.state());
81 if (mToken)
82 removeToken();
83 } else if (state.state(SCARD_STATE_PRESENT)) {
84 // reader has a token inserted
85 secdebug("reader", "%p (%s) card present (0x%lx)",
86 this, name().c_str(), state.state());
87 //@@@ is this hack worth it (with notifications in)??
88 if (mToken && CssmData(state) != CssmData(pcscState()))
89 removeToken(); // incomplete but better than nothing
90 //@@@ or should we call some verify-still-the-same function of tokend?
91 //@@@ (I think pcsc will return an error if the card changed?)
92 if (!mToken)
93 insertToken();
94 } else {
95 secdebug("reader", "%p (%s) unexpected state change (0x%lx to 0x%lx)",
96 this, name().c_str(), oldState, state.state());
97 }
98 } catch (...) {
99 secdebug("reader", "state update exception (ignored)");
100 }
101 }
102
103
104 void Reader::insertToken()
105 {
106 RefPointer<Token> token = new Token();
107 token->insert(*this);
108 mToken = token;
109 addReference(*token);
110 secdebug("reader", "%p (%s) inserted token %p",
111 this, name().c_str(), mToken);
112 }
113
114
115 void Reader::removeToken()
116 {
117 secdebug("reader", "%p (%s) removing token %p",
118 this, name().c_str(), mToken);
119 assert(mToken);
120 mToken->remove();
121 removeReference(*mToken);
122 mToken = NULL;
123 }
124
125
126 //
127 // Debug dump support
128 //
129 #if defined(DEBUGDUMP)
130
131 void Reader::dumpNode()
132 {
133 PerGlobal::dumpNode();
134 Debug::dump(" [%s] state=0x%lx", name().c_str(), mState.state());
135 }
136
137 #endif //DEBUGDUMP