1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
15 #include <gnutls/gnutls.h>
16 #include <gnutls/x509.h>
17 #include "transport.h"
21 #pragma comment(lib, "libgnutls-13.lib")
24 /* $ModDesc: Provides SSL support for clients */
25 /* $CompileFlags: pkgconfincludes("gnutls","/gnutls/gnutls.h","") */
26 /* $LinkerFlags: rpath("pkg-config --libs gnutls") pkgconflibs("gnutls","/libgnutls.so","-lgnutls") */
27 /* $ModDep: transport.h */
28 /* $CopyInstall: conf/key.pem $(CONPATH) */
29 /* $CopyInstall: conf/cert.pem $(CONPATH) */
31 enum issl_status { ISSL_NONE, ISSL_HANDSHAKING_READ, ISSL_HANDSHAKING_WRITE, ISSL_HANDSHAKEN, ISSL_CLOSING, ISSL_CLOSED };
33 bool isin(const std::string &host, int port, const std::vector<std::string> &portlist)
35 if (std::find(portlist.begin(), portlist.end(), "*:" + ConvToStr(port)) != portlist.end())
38 if (std::find(portlist.begin(), portlist.end(), ":" + ConvToStr(port)) != portlist.end())
41 return std::find(portlist.begin(), portlist.end(), host + ":" + ConvToStr(port)) != portlist.end();
44 /** Represents an SSL user's extra data
46 class issl_session : public classbase
54 gnutls_session_t sess;
59 class CommandStartTLS : public Command
63 CommandStartTLS (InspIRCd* Instance, Module* mod) : Command(Instance,"STARTTLS", 0, 0, true), Caller(mod)
65 this->source = "m_ssl_gnutls.so";
68 CmdResult Handle (const std::vector<std::string> ¶meters, User *user)
70 /* changed from == REG_ALL to catch clients sending STARTTLS
71 * after NICK and USER but before OnUserConnect completes and
72 * give a proper error message (see bug #645) - dz
74 if (user->registered != REG_NONE)
76 user->WriteNumeric(691, "%s :STARTTLS is not permitted after client registration has started", user->nick.c_str());
80 if (!user->GetIOHook())
82 user->WriteNumeric(670, "%s :STARTTLS successful, go ahead with TLS handshake", user->nick.c_str());
83 user->AddIOHook(Caller);
84 Caller->OnRawSocketAccept(user->GetFd(), user->GetIPString(), user->GetServerPort());
87 user->WriteNumeric(691, "%s :STARTTLS failure", user->nick.c_str());
94 class ModuleSSLGnuTLS : public Module
96 std::vector<std::string> listenports;
98 issl_session* sessions;
100 gnutls_certificate_credentials x509_cred;
101 gnutls_dh_params dh_params;
104 std::string certfile;
107 std::string sslports;
113 CommandStartTLS* starttls;
117 ModuleSSLGnuTLS(InspIRCd* Me)
120 ServerInstance->Modules->PublishInterface("BufferedSocketHook", this);
122 sessions = new issl_session[ServerInstance->SE->GetMaxFds()];
124 gnutls_global_init(); // This must be called once in the program
127 // Needs the flag as it ignores a plain /rehash
128 OnModuleRehash(NULL,"ssl");
130 // Void return, guess we assume success
131 gnutls_certificate_set_dh_params(x509_cred, dh_params);
132 Implementation eventlist[] = { I_On005Numeric, I_OnRawSocketConnect, I_OnRawSocketAccept,
133 I_OnRawSocketClose, I_OnRawSocketRead, I_OnRawSocketWrite, I_OnCleanup,
134 I_OnBufferFlushed, I_OnRequest, I_OnUnloadModule, I_OnRehash, I_OnModuleRehash,
135 I_OnPostConnect, I_OnEvent, I_OnHookUserIO };
136 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
138 starttls = new CommandStartTLS(ServerInstance, this);
139 ServerInstance->AddCommand(starttls);
142 virtual void OnRehash(User* user)
144 ConfigReader Conf(ServerInstance);
150 for(int index = 0; index < Conf.Enumerate("bind"); index++)
152 // For each <bind> tag
153 std::string x = Conf.ReadValue("bind", "type", index);
154 if(((x.empty()) || (x == "clients")) && (Conf.ReadValue("bind", "ssl", index) == "gnutls"))
156 // Get the port we're meant to be listening on with SSL
157 std::string port = Conf.ReadValue("bind", "port", index);
158 std::string addr = Conf.ReadValue("bind", "address", index);
162 // normalize address, important for IPv6
164 irc::sockets::sockaddrs bin;
165 if (irc::sockets::aptosa(addr.c_str(), portint, &bin))
166 irc::sockets::satoap(&bin, addr, portint);
169 irc::portparser portrange(port, false);
171 while ((portno = portrange.GetToken()))
176 listenports.push_back(addr + ":" + ConvToStr(portno));
178 for (size_t i = 0; i < ServerInstance->ports.size(); i++)
179 if ((ServerInstance->ports[i]->GetPort() == portno) && (ServerInstance->ports[i]->GetIP() == addr))
180 ServerInstance->ports[i]->SetDescription("ssl");
181 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Enabling SSL for port %ld", portno);
183 if (addr != "127.0.0.1")
184 sslports.append((addr.empty() ? "*" : addr)).append(":").append(ConvToStr(portno)).append(";");
186 catch (ModuleException &e)
188 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: FAILED to enable SSL on port %ld: %s. Maybe it's already hooked by the same port on a different IP, or you have an other SSL or similar module loaded?", portno, e.GetReason());
194 if (!sslports.empty())
195 sslports.erase(sslports.end() - 1);
198 virtual void OnModuleRehash(User* user, const std::string ¶m)
205 ConfigReader Conf(ServerInstance);
207 std::string confdir(ServerInstance->ConfigFileName);
208 // +1 so we the path ends with a /
209 confdir = confdir.substr(0, confdir.find_last_of('/') + 1);
211 cafile = Conf.ReadValue("gnutls", "cafile", 0);
212 crlfile = Conf.ReadValue("gnutls", "crlfile", 0);
213 certfile = Conf.ReadValue("gnutls", "certfile", 0);
214 keyfile = Conf.ReadValue("gnutls", "keyfile", 0);
215 dh_bits = Conf.ReadInteger("gnutls", "dhbits", 0, false);
217 // Set all the default values needed.
224 if (certfile.empty())
225 certfile = "cert.pem";
230 if((dh_bits != 768) && (dh_bits != 1024) && (dh_bits != 2048) && (dh_bits != 3072) && (dh_bits != 4096))
233 // Prepend relative paths with the path to the config directory.
234 if ((cafile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(cafile)))
235 cafile = confdir + cafile;
237 if ((crlfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(crlfile)))
238 crlfile = confdir + crlfile;
240 if ((certfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(certfile)))
241 certfile = confdir + certfile;
243 if ((keyfile[0] != '/') && (!ServerInstance->Config->StartsWithWindowsDriveLetter(keyfile)))
244 keyfile = confdir + keyfile;
250 // Deallocate the old credentials
251 gnutls_dh_params_deinit(dh_params);
252 gnutls_certificate_free_credentials(x509_cred);
257 if((ret = gnutls_certificate_allocate_credentials(&x509_cred)) < 0)
258 ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to allocate certificate credentials: %s", gnutls_strerror(ret));
260 if((ret = gnutls_dh_params_init(&dh_params)) < 0)
261 ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to initialise DH parameters: %s", gnutls_strerror(ret));
263 if((ret =gnutls_certificate_set_x509_trust_file(x509_cred, cafile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
264 ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to set X.509 trust file '%s': %s", cafile.c_str(), gnutls_strerror(ret));
266 if((ret = gnutls_certificate_set_x509_crl_file (x509_cred, crlfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
267 ServerInstance->Logs->Log("m_ssl_gnutls",DEBUG, "m_ssl_gnutls.so: Failed to set X.509 CRL file '%s': %s", crlfile.c_str(), gnutls_strerror(ret));
269 if((ret = gnutls_certificate_set_x509_key_file (x509_cred, certfile.c_str(), keyfile.c_str(), GNUTLS_X509_FMT_PEM)) < 0)
271 // If this fails, no SSL port will work. At all. So, do the smart thing - throw a ModuleException
272 throw ModuleException("Unable to load GnuTLS server certificate (" + certfile + ", key: " + keyfile + "): " + std::string(gnutls_strerror(ret)));
275 // This may be on a large (once a day or week) timer eventually.
279 void GenerateDHParams()
281 // Generate Diffie Hellman parameters - for use with DHE
282 // kx algorithms. These should be discarded and regenerated
283 // once a day, once a week or once a month. Depending on the
284 // security requirements.
288 if((ret = gnutls_dh_params_generate2(dh_params, dh_bits)) < 0)
289 ServerInstance->Logs->Log("m_ssl_gnutls",DEFAULT, "m_ssl_gnutls.so: Failed to generate DH parameters (%d bits): %s", dh_bits, gnutls_strerror(ret));
292 virtual ~ModuleSSLGnuTLS()
294 gnutls_dh_params_deinit(dh_params);
295 gnutls_certificate_free_credentials(x509_cred);
296 gnutls_global_deinit();
297 ServerInstance->Modules->UnpublishInterface("BufferedSocketHook", this);
301 virtual void OnCleanup(int target_type, void* item)
303 if(target_type == TYPE_USER)
305 User* user = (User*)item;
307 if (user->GetIOHook() == this)
309 // User is using SSL, they're a local user, and they're using one of *our* SSL ports.
310 // Potentially there could be multiple SSL modules loaded at once on different ports.
311 ServerInstance->Users->QuitUser(user, "SSL module unloading");
314 if (user->GetExt("ssl_cert"))
317 user->GetExt("ssl_cert", tofree);
319 user->Shrink("ssl_cert");
324 virtual void OnUnloadModule(Module* mod, const std::string &name)
328 for(unsigned int i = 0; i < listenports.size(); i++)
330 for (size_t j = 0; j < ServerInstance->ports.size(); j++)
331 if (listenports[i] == (ServerInstance->ports[j]->GetIP()+":"+ConvToStr(ServerInstance->ports[j]->GetPort())))
332 ServerInstance->ports[j]->SetDescription("plaintext");
337 virtual Version GetVersion()
339 return Version("$Id$", VF_VENDOR, API_VERSION);
343 virtual void On005Numeric(std::string &output)
345 if (!sslports.empty())
346 output.append(" SSL=" + sslports);
347 output.append(" STARTTLS");
350 virtual void OnHookUserIO(User* user, const std::string &targetip)
352 if (!user->GetIOHook() && isin(targetip,user->GetServerPort(),listenports))
354 /* Hook the user with our module */
355 user->AddIOHook(this);
359 virtual const char* OnRequest(Request* request)
361 ISHRequest* ISR = (ISHRequest*)request;
362 if (strcmp("IS_NAME", request->GetId()) == 0)
366 else if (strcmp("IS_HOOK", request->GetId()) == 0)
368 const char* ret = "OK";
371 ret = ISR->Sock->AddIOHook((Module*)this) ? "OK" : NULL;
373 catch (ModuleException &e)
379 else if (strcmp("IS_UNHOOK", request->GetId()) == 0)
381 return ISR->Sock->DelIOHook() ? "OK" : NULL;
383 else if (strcmp("IS_HSDONE", request->GetId()) == 0)
385 if (ISR->Sock->GetFd() < 0)
388 issl_session* session = &sessions[ISR->Sock->GetFd()];
389 return (session->status == ISSL_HANDSHAKING_READ || session->status == ISSL_HANDSHAKING_WRITE) ? NULL : "OK";
391 else if (strcmp("IS_ATTACH", request->GetId()) == 0)
393 if (ISR->Sock->GetFd() > -1)
395 issl_session* session = &sessions[ISR->Sock->GetFd()];
398 if ((Extensible*)ServerInstance->SE->GetRef(ISR->Sock->GetFd()) == (Extensible*)(ISR->Sock))
400 VerifyCertificate(session, (BufferedSocket*)ISR->Sock);
406 else if (strcmp("GET_FP", request->GetId()) == 0)
408 if (ISR->Sock->GetFd() > -1)
410 issl_session* session = &sessions[ISR->Sock->GetFd()];
413 Extensible* ext = ISR->Sock;
415 if (ext->GetExt("ssl_cert",certinfo))
416 return certinfo->GetFingerprint().c_str();
424 virtual void OnRawSocketAccept(int fd, const std::string &ip, int localport)
426 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
427 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
430 issl_session* session = &sessions[fd];
432 /* For STARTTLS: Don't try and init a session on a socket that already has a session */
436 gnutls_init(&session->sess, GNUTLS_SERVER);
438 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
439 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
440 gnutls_dh_set_prime_bits(session->sess, dh_bits);
442 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
444 gnutls_certificate_server_set_request(session->sess, GNUTLS_CERT_REQUEST); // Request client certificate if any.
446 Handshake(session, fd);
449 virtual void OnRawSocketConnect(int fd)
451 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
452 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
455 issl_session* session = &sessions[fd];
457 gnutls_init(&session->sess, GNUTLS_CLIENT);
459 gnutls_set_default_priority(session->sess); // Avoid calling all the priority functions, defaults are adequate.
460 gnutls_credentials_set(session->sess, GNUTLS_CRD_CERTIFICATE, x509_cred);
461 gnutls_dh_set_prime_bits(session->sess, dh_bits);
462 gnutls_transport_set_ptr(session->sess, (gnutls_transport_ptr_t) fd); // Give gnutls the fd for the socket.
464 Handshake(session, fd);
467 virtual void OnRawSocketClose(int fd)
469 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
470 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds()))
473 CloseSession(&sessions[fd]);
475 EventHandler* user = ServerInstance->SE->GetRef(fd);
477 if ((user) && (user->GetExt("ssl_cert")))
480 user->GetExt("ssl_cert", tofree);
482 user->Shrink("ssl_cert");
486 virtual int OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult)
488 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
489 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
492 issl_session* session = &sessions[fd];
497 CloseSession(session);
501 if (session->status == ISSL_HANDSHAKING_READ)
503 // The handshake isn't finished, try to finish it.
505 if(!Handshake(session, fd))
507 errno = session->status == ISSL_CLOSING ? EIO : EAGAIN;
508 // Couldn't resume handshake.
512 else if (session->status == ISSL_HANDSHAKING_WRITE)
519 // If we resumed the handshake then session->status will be ISSL_HANDSHAKEN.
521 if (session->status == ISSL_HANDSHAKEN)
523 int ret = gnutls_record_recv(session->sess, buffer, count);
531 // Client closed connection.
533 CloseSession(session);
536 else if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
543 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
544 "m_ssl_gnutls.so: Error while reading on fd %d: %s",
545 fd, gnutls_strerror(ret));
547 CloseSession(session);
550 else if(session->status == ISSL_CLOSING)
556 virtual int OnRawSocketWrite(int fd, const char* buffer, int count)
558 /* Are there any possibilities of an out of range fd? Hope not, but lets be paranoid */
559 if ((fd < 0) || (fd > ServerInstance->SE->GetMaxFds() - 1))
562 issl_session* session = &sessions[fd];
563 const char* sendbuffer = buffer;
567 CloseSession(session);
571 session->outbuf.append(sendbuffer, count);
572 sendbuffer = session->outbuf.c_str();
573 count = session->outbuf.size();
575 if (session->status == ISSL_HANDSHAKING_WRITE || session->status == ISSL_HANDSHAKING_READ)
577 // The handshake isn't finished, try to finish it.
578 Handshake(session, fd);
579 errno = session->status == ISSL_CLOSING ? EIO : EAGAIN;
585 if (session->status == ISSL_HANDSHAKEN)
587 ret = gnutls_record_send(session->sess, sendbuffer, count);
591 CloseSession(session);
595 if(ret != GNUTLS_E_AGAIN && ret != GNUTLS_E_INTERRUPTED)
597 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
598 "m_ssl_gnutls.so: Error while writing to fd %d: %s",
599 fd, gnutls_strerror(ret));
600 CloseSession(session);
609 session->outbuf = session->outbuf.substr(ret);
615 /* Who's smart idea was it to return 1 when we havent written anything?
616 * This fucks the buffer up in BufferedSocket :p
618 return ret < 1 ? 0 : ret;
621 bool Handshake(issl_session* session, int fd)
623 int ret = gnutls_handshake(session->sess);
627 if(ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
629 // Handshake needs resuming later, read() or write() would have blocked.
631 if(gnutls_record_get_direction(session->sess) == 0)
633 // gnutls_handshake() wants to read() again.
634 session->status = ISSL_HANDSHAKING_READ;
638 // gnutls_handshake() wants to write() again.
639 session->status = ISSL_HANDSHAKING_WRITE;
646 ServerInstance->Logs->Log("m_ssl_gnutls", DEFAULT,
647 "m_ssl_gnutls.so: Handshake failed on fd %d: %s",
648 fd, gnutls_strerror(ret));
649 CloseSession(session);
650 session->status = ISSL_CLOSING;
657 // Handshake complete.
658 // This will do for setting the ssl flag...it could be done earlier if it's needed. But this seems neater.
659 EventHandler *extendme = ServerInstance->SE->GetRef(fd);
662 if (!extendme->GetExt("ssl"))
663 extendme->Extend("ssl", "ON");
666 // Change the seesion state
667 session->status = ISSL_HANDSHAKEN;
669 // Finish writing, if any left
676 virtual void OnPostConnect(User* user)
678 // This occurs AFTER OnUserConnect so we can be sure the
679 // protocol module has propagated the NICK message.
680 if (user->GetIOHook() == this && (IS_LOCAL(user)))
682 ssl_cert* certdata = VerifyCertificate(&sessions[user->GetFd()],user);
683 if (sessions[user->GetFd()].sess)
685 std::string cipher = gnutls_kx_get_name(gnutls_kx_get(sessions[user->GetFd()].sess));
686 cipher.append("-").append(gnutls_cipher_get_name(gnutls_cipher_get(sessions[user->GetFd()].sess))).append("-");
687 cipher.append(gnutls_mac_get_name(gnutls_mac_get(sessions[user->GetFd()].sess)));
688 user->WriteServ("NOTICE %s :*** You are connected using SSL cipher \"%s\"", user->nick.c_str(), cipher.c_str());
691 ServerInstance->PI->SendMetaData(user, TYPE_USER, "ssl", "ON");
693 ServerInstance->PI->SendMetaData(user, TYPE_USER, "ssl_cert", certdata->GetMetaLine().c_str());
697 void MakePollWrite(int fd)
699 //OnRawSocketWrite(fd, NULL, 0);
700 EventHandler* eh = ServerInstance->SE->GetRef(fd);
702 ServerInstance->SE->WantWrite(eh);
705 virtual void OnBufferFlushed(User* user)
707 if (user->GetIOHook() == this)
709 issl_session* session = &sessions[user->GetFd()];
710 if (session && session->outbuf.size())
711 OnRawSocketWrite(user->GetFd(), NULL, 0);
715 void CloseSession(issl_session* session)
719 gnutls_bye(session->sess, GNUTLS_SHUT_WR);
720 gnutls_deinit(session->sess);
723 session->outbuf.clear();
724 session->sess = NULL;
725 session->status = ISSL_NONE;
728 ssl_cert* VerifyCertificate(issl_session* session, Extensible* user)
730 if (!session->sess || !user)
734 const gnutls_datum_t* cert_list;
736 unsigned int cert_list_size;
737 gnutls_x509_crt_t cert;
739 unsigned char digest[MAXBUF];
740 size_t digest_size = sizeof(digest);
741 size_t name_size = sizeof(name);
742 ssl_cert* certinfo = new ssl_cert;
744 user->Extend("ssl_cert",certinfo);
746 /* This verification function uses the trusted CAs in the credentials
747 * structure. So you must have installed one or more CA certificates.
749 ret = gnutls_certificate_verify_peers2(session->sess, &status);
753 certinfo->error = std::string(gnutls_strerror(ret));
757 certinfo->invalid = (status & GNUTLS_CERT_INVALID);
758 certinfo->unknownsigner = (status & GNUTLS_CERT_SIGNER_NOT_FOUND);
759 certinfo->revoked = (status & GNUTLS_CERT_REVOKED);
760 certinfo->trusted = !(status & GNUTLS_CERT_SIGNER_NOT_CA);
762 /* Up to here the process is the same for X.509 certificates and
763 * OpenPGP keys. From now on X.509 certificates are assumed. This can
764 * be easily extended to work with openpgp keys as well.
766 if (gnutls_certificate_type_get(session->sess) != GNUTLS_CRT_X509)
768 certinfo->error = "No X509 keys sent";
772 ret = gnutls_x509_crt_init(&cert);
775 certinfo->error = gnutls_strerror(ret);
780 cert_list = gnutls_certificate_get_peers(session->sess, &cert_list_size);
781 if (cert_list == NULL)
783 certinfo->error = "No certificate was found";
787 /* This is not a real world example, since we only check the first
788 * certificate in the given chain.
791 ret = gnutls_x509_crt_import(cert, &cert_list[0], GNUTLS_X509_FMT_DER);
794 certinfo->error = gnutls_strerror(ret);
798 gnutls_x509_crt_get_dn(cert, name, &name_size);
801 gnutls_x509_crt_get_issuer_dn(cert, name, &name_size);
802 certinfo->issuer = name;
804 if ((ret = gnutls_x509_crt_get_fingerprint(cert, GNUTLS_DIG_MD5, digest, &digest_size)) < 0)
806 certinfo->error = gnutls_strerror(ret);
810 certinfo->fingerprint = irc::hex(digest, digest_size);
813 /* Beware here we do not check for errors.
815 if ((gnutls_x509_crt_get_expiration_time(cert) < ServerInstance->Time()) || (gnutls_x509_crt_get_activation_time(cert) > ServerInstance->Time()))
817 certinfo->error = "Not activated, or expired certificate";
820 gnutls_x509_crt_deinit(cert);
825 void OnEvent(Event* ev)
827 GenericCapHandler(ev, "tls", "tls");
832 Module* server = ServerInstance->Modules->Find("m_spanningtree.so");
833 ServerInstance->Modules->SetPriority(this, I_OnPostConnect, PRIORITY_AFTER, &server);
837 MODULE_INIT(ModuleSSLGnuTLS)