<div dir="ltr"><div><div><div><div><div><div><div>Hello Damien,<br><br></div>I am not sure that XPC will help me here. Maybe I need to describe the problem I try to solve :-)<br><br></div>I am working on a tokend component. This tokend component is part of CDSA architecture and is used to interact with a smart card.<br></div>CDSA is deprecated by Apple since OS X Lion in 2011 [1] but is still usable and have not yet been replaced by Apple by something else.<br><div><div><br></div><div>The tokend component is executed by the user &quot;_tokend&quot;. I can interact with it using the Keychain Access application or Safari or Mail.<br></div>When needed a component (I guess part of CDSA) asks the user to enter the smart card secret PIN code. The PIN code is then transferred to my tokend component so I can send it to the smart card.<br></div><div>This simple use case works fine.<br></div><div><br></div><div>In some cases (depending on the smart card configuration) my tokend needs to ask the user for the PIN again (or for a different PIN value).<br></div>My tokend component is running as user _tokend, not as the logged in user and has no access to the Cocoa framework to display a dialog box. I tried to use Cocoa from the tokend but the GUI is not behaving correctly.<br><br></div>So I need a way to communicate from a process running as _tokend to a process with access to the graphical interface, a process started as the logged in user.<br><br></div>My solution using a UNIX socket and launchd works fine. An application is started as the logged in user when needed and this application can display windows and interact with the user.<br></div><div>The limitation is that my solution does not work when 2 users are logged in at the same time, either using fast user switching or over the network.<br><br></div><div><br></div>For what I read about XPC it has been designed as an isolation mechanism. An XPC service has very limited privileges. I imagine that using Cocoa is not part of the allowed privileges. Exact?<br><br></div>Bye<br><div><div><div><div><div><div><br><div><div>[1] <a href="http://ludovicrousseau.blogspot.com/2011/08/mac-os-x-lion-and-tokend.html">http://ludovicrousseau.blogspot.com/2011/08/mac-os-x-lion-and-tokend.html</a><br><div><div><div class="gmail_extra"><br><div class="gmail_quote">2016-02-25 17:27 GMT+01:00 Damien Sorresso <span dir="ltr">&lt;<a href="mailto:dsorresso@apple.com" target="_blank">dsorresso@apple.com</a>&gt;</span>:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word">Ludovic,<div><br></div><div>If possible, I would recommend using XPC for this purpose. Rather than managing the permissions of a socket on-disk, the MachServices names registered by your agent would be in user-specific namespaces such that this collision never happens.</div><div><br></div><div>If that is not possible, then I would recommend taking a look at the SecureSocketForKey option. Instead of specifying the path name upfront, launchd will put the socket at a random path and then set that path as the value for an environment variable (which you name in the value for SecureSocketForKey). launchd then assures that all jobs within the same context as your agent get that environment variable so they can rendezvous with your agent. So for example, if your socket dictionary was</div><div><br></div><div>&lt;key&gt;Listeners&lt;/key&gt;</div><div>&lt;dict&gt;</div><div><span style="white-space:pre-wrap">        </span>&lt;key&gt;SecureSocketForKey&lt;/key&gt;</div><div><span style="white-space:pre-wrap">        </span>&lt;string&gt;MY_KEY&lt;/string&gt;</div><div>&lt;/dict&gt;</div><div><br></div><div>other processes would be able to just do a getenv(&quot;MY_KEY&quot;) to get the path to the socket and connect to it. This should achieve the user-based isolation you want with sockets. Though as I said before, XPC is a much more natural mechanism for IPC on OS X, so use that if it&#39;s a practical option for you.</div><span class=""><font color="#888888"><div>-damien</div></font></span><div><br><div><blockquote type="cite"><div><div class="h5"><div>On 25 Feb, 2016, at 05:43, Ludovic Rousseau &lt;<a href="mailto:ludovic.rousseau@gmail.com" target="_blank">ludovic.rousseau@gmail.com</a>&gt; wrote:</div><br></div></div><div><div><div class="h5"><div dir="ltr"><div><div><div><div><div>Hello,<br><br></div>I am using launchd to create a Unix domain socket /tmp/foobar.socket so that launchd starts my agent when someone connects to the sockets.<br><br></div>It works fine except when Fast User Switching is involved.<br><br><div>My problem is quite similar to the problem &quot;Listening socket and 
fast user switching&quot; [1] discussed on this mailing list in 2008. The difference is that I am using a
 UNIX domain socket and not a TCP socket.<br></div><br><br></div>The problematic scenario is as follows:<br></div>1. User A logs in<br>2. User A can connect to the socket<br><br>3. User B logs in using the Fast User Switching<br>4. User B can connect to the socket<br>5. User B logs out<br></div><br><div>6. User A comes back and fast user switch to his session. Since user A<br>   does not _logs in_ again the socket is not changed/recreated and is still<br>   only usable by user B<br><br></div><div>At this step user A gets:<br>$ telnet /tmp/foobar.socket<br>Trying /tmp/foobar.socket...<br>/tmp/foobar.socket: Connection refused<br clear="all"></div><div><div><div><div><div><div><br></div><div>The socket is still present in /tmp/ but it belongs to user B and is no more usable but user A.<br><br></div><div>What I would like is the ability for launchd to (re)configure the Unix domain socket each time a user authenticates to the console, either the first time or using the fast user switching method.<br><br><br></div><div>- I use a UNIX domain socket so that only local processes can talk to the socket and use my agent. I don&#39;t want to open my agent to other computer on the network.<br></div><div>- I use launchd to start my agent on request only. That is a very nice feature. I can&#39;t detect the user switching since my agent code is, most of the time, not running.<br></div><div>- I can&#39;t use a different socket name per user since the client of my agent does not know who is currently logged. My client application is talking to the socket configured for whoever is logged at that time.<br></div><br><br><div>My /Library/LaunchAgents/com.foobar.plist file contains:<br><br>&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;<br>&lt;!DOCTYPE plist PUBLIC &quot;-//Apple//DTD PLIST 1.0//EN&quot; &quot;<a href="http://www.apple.com/DTDs/PropertyList-1.0.dtd" target="_blank">http://www.apple.com/DTDs/PropertyList-1.0.dtd</a>&quot;&gt;<br>&lt;plist version=&quot;1.0&quot;&gt;<br>&lt;dict&gt;<br>    &lt;key&gt;Label&lt;/key&gt;<br>    &lt;string&gt;com.foobar&lt;/string&gt;<br>    &lt;key&gt;ProgramArguments&lt;/key&gt;<br>    &lt;array&gt;<br>        &lt;string&gt;/usr/local/lib/foobar_exe&lt;/string&gt;<br>    &lt;/array&gt;<br>    &lt;key&gt;Sockets&lt;/key&gt;<br>    &lt;dict&gt;<br>        &lt;key&gt;Listeners&lt;/key&gt;<br>        &lt;dict&gt;<br>            &lt;key&gt;SockPathName&lt;/key&gt;<br>            &lt;string&gt;/tmp/foobar.socket&lt;/string&gt;<br>        &lt;/dict&gt;<br>    &lt;/dict&gt;<br>    &lt;key&gt;ProcessType&lt;/key&gt;<br>    &lt;string&gt;Interactive&lt;/string&gt;<br>&lt;/dict&gt;<br>&lt;/plist&gt;<br><br><br></div><div>Does launchd provides a solution to my problem?<br></div><div><br>Should I open a bug at Apple to ask for this new feature in launchd?<br><br></div><div>Thanks<br></div><div><br></div>[1] <a href="https://lists.macosforge.org/pipermail/launchd-dev/2008-May/000298.html" target="_blank">https://lists.macosforge.org/pipermail/launchd-dev/2008-May/000298.html</a><br><div><br></div><div>-- <br><div> Dr. Ludovic Rousseau</div>
</div></div></div></div></div></div></div></div></div><span class="">
_______________________________________________<br>launchd-dev mailing list<br><a href="mailto:launchd-dev@lists.macosforge.org" target="_blank">launchd-dev@lists.macosforge.org</a><br><a href="https://lists.macosforge.org/mailman/listinfo/launchd-dev" target="_blank">https://lists.macosforge.org/mailman/listinfo/launchd-dev</a><br></span></div></blockquote></div><br></div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"> Dr. Ludovic Rousseau</div>
</div></div></div></div></div></div></div></div></div></div></div></div>