Annotation of www/porting.html, Revision 1.13
1.1 marc 1: <html>
2: <head>
3: <meta http-equiv="Content-Type"
4: content="text/html; charset=iso-8859-1">
5: <meta name="resource-type"
6: content="document">
7: <meta name="description"
8: CONTENT="How to make an OpenBSD port">
9: <meta name="keywords"
10: content="openbsd,ports">
11: <meta name="distribution"
12: content="global">
13: <meta name="copyright"
14: content="This document copyright 1997 by the OpenBSD project">
15: <title>Building an OpenBSD port</title>
16: <link rev="made" HREF="mailto:www@openbsd.org">
17: </head>
18: <body text="#000000" bgcolor="#FFFFFF" link="#23238E">
1.6 pauls 19: <img height=30 width=141 src=images/smalltitle.gif alt="[OpenBSD]" >
1.1 marc 20:
1.3 marc 21: <h2><font color=#e00000>Building an OpenBSD port</font></h2>
1.1 marc 22:
23: So you've just compiled your favorite software package on your
24: OpenBSD machine and you want to share your effort by turning
25: it into a standard port. What to do?
26: <p>
1.9 marc 27: First look at the porting information on this page. Then check
28: out the referenced documents, especially the OpenBSD porting
29: checklist.
1.1 marc 30: <p>
1.9 marc 31: Test, then re-test, and finally test again!
32: <p>
33: Submit the port. Create a gzipped tarball of the port directory.
34: You can then either place it on a public FTP or HTTP server, sending
35: its address to <a href=mailto:ports@openbsd.org>ports@openbsd.org</a>
36: or send the port mime encoded to the same address. Pick whichever
37: method works best for you.
1.1 marc 38: <p>
39: <h3><font color=#0000e0>Available Porting Information</font></h3>
40: <ul>
41: <li>The file <code>/usr/share/mk/bsd.port.mk</code>. This is the
42: system ports makefile included at the end of each individual
43: port makefile. Read the comments at the start of the makefile.
44: They do a good job of describing the available make options.
45: <li>The
1.13 ! art 46: <a href="http://www.netbsd.org/Documentation/software/packages.html">
! 47: NetBSD Package System</a> documentation. This document describes
! 48: NetBSD's version of the FreeBSD ports system and is quite helpful.
1.8 marc 49: <li>Section 19.2.5 of the
1.1 marc 50: <a href="http://www.freebsd.org/handbook/porting.html">FreeBSD
51: Handbook</a>. This is the FreeBSD porting bible.
1.9 marc 52: <li>OpenBSD porting <a href="checklist.html">checklist</a>.
1.1 marc 53: <li>The OpenBSD ports mailing list,
54: <a href="mailto:ports@openbsd.org">ports@OpenBSD.ORG</a>.
55: </ul>
56: <h3><font color=#0000e0>OpenBSD Porting Policy</font></h3>
57: <ul>
58: <li>OpenBSD does NOT use /usr/local/etc/rc.d.<br>
1.7 espie 59: <code>/usr/local</code> is often shared between several machines
1.8 marc 60: thanks to NFS. For this reason, configuration files that are specific
61: to a given machine can't be stored under <code>/usr/local</code>,
62: <code>/etc</code> is the central repository for per machine
63: configuration files. Moreover, OpenBSD policy is to never update
64: files under <code>/etc</code> automatically. Ports that need some
65: specific boot setup should advise the administrator about what to do
66: instead of blindly installing files.
1.1 marc 67: <li>OpenBSD does NOT compress man pages.
68: <li>OpenBSD does NOT require <code>-lcrypt</code>.<br>
69: DES encryption is part of the standard <code>libc</code>.
1.10 espie 70: <li>OpenBSD is strongly security-oriented. You should read and understand
71: this page's <a href="#security">security section</a>.
72: <li>Be sure to add the <code>$</code><code>OpenBSD$</code> CVS tag to
73: the Makefile. If importing a port from another system be sure to
74: leave their tag in the Makefile, too. However, replace the FreeBSD
75: <code>$</code><code>Id$</code> tag with the
76: <code>$</code><code>FreeBSD$</code> tag.
77: <li>The goal is to get all ported applications to support OpenBSD. To
78: achieve this goal you <strong>must</strong> feed any OpenBSD patches
79: back to the application maintainer.
80: </ul>
81: <a name=security>
82: <h3><font color=#0000e0>Security recommendations</font></h3>
83: There are many security problems to worry about. If
1.2 marc 84: you are not absolutely sure of what you are doing please request
1.1 marc 85: help from the <a href="mailto:ports@openbsd.org">ports</a> mailing
86: list.
1.10 espie 87:
88: <ul>
89: <li>Do <emph>not</emph> use alpha or beta code when preparing a port. Use the
90: latest regular or patch release.
91:
1.1 marc 92: <li>Any software to be installed as a server should be scanned
93: for buffer overflows, especially unsafe use of
94: <code>strcat/strcpy/strcmp/sprintf</code>. In general,
95: <code>sprintf</code> should be replaced with <code>snprintf</code>.
1.10 espie 96:
97: <li>Never use filenames when you need security. There are numerous race
98: conditions where you don't have proper control. For instance, an attacker
99: who already has user privileges on your machines may replace files in
100: <code>/tmp</code> with symbolic links to more strategic files, such as
101: <code>/etc/passwd</code>. For instance, the bsd linker warns about
102: <code>mktemp</code> uses. <strong>These must be fixed</strong>.
103: This is not quite as simple as <code>s/mktemp/mkstemp/g</code>.
104:
105: <li>Just because you can read it doesn't mean you should. A well-known hole
106: of this nature was the <code>startx</code> problem. As a setuid program,
107: you could launch startx with any file as a script. If the file was not
108: a valid shell script, a syntax error message would follow, along with the
109: first line of the offending file, without any further permission check.
110: Pretty handy to grab the first line of a shadow passwd file, considering
1.12 deraadt 111: these often start with root entry. Do not open your file, and then do
112: an <code>fstat</code> on the open descriptor to check if you should have
113: been able to open it (or the attacked will play with /dev/rst0 and rewind
114: your tape) -- open it with the correct uid/gid/grouplist set.
1.10 espie 115:
116: <li>Don't use anything that forks a shell in setuid programs before dropping
117: your privileges. This includes <code>popen</code> and <code>system</code>.
118: Use <code>fork</code>, <code>pipe</code> and <code>execve</code> instead.
119:
120: <li>Pass open descriptors instead of filenames to other programs to avoid race
121: conditions. Even if a program does not accept file descriptors, you can
122: still use <code>/dev/fd/0</code>.
123:
124: <li>Access rights are attached to file descriptors. If you need setuid rights
125: to open a file, open that file, then drop your privileges. You can still
126: access the open descriptor, but you have less to worry about. This is
127: double-edged: even after dropping privileges, you should still watch out
128: for those descriptors.
129:
130: <li>Avoid root setuid as much as you can. Basically, root can do anything,
131: but root rights are very rarely needed, except maybe to create socket ports with
132: a number under 1024. It is arguably better to keep that under <code>inetd</code>
133: control and just add the relevant entries to <code>inetd.conf</code>.
134: You must know the appropriate magic for writing daemons to achieve that.
135: It could be argued that you have no business writing setuid programs if you don't
136: know how to do that.
137:
138: <li>Use setgid instead of setuid. Apart from those specific files needed by setgid
139: programs, most files are not group-writable. Hence, a security problem in a setgid
140: program won't compromise your system as much: with only group rights, the worst
141: an intruder will be able to do is hack a games score table or some such.
142: See the <code>xkobo</code> port for an instance of such a change.
143:
144: <li>Don't trust group-writable files. Even though they are audited, setgid programs
145: are not perceived as important potential security holes. Hence stuff they can tamper
146: with shouldn't be considered sensitive information.
147:
148: <li>Don't trust your environment ! This involves simple things such as your <code>PATH</code>
149: (never use <code>system</code> with an unqualified name), but also more subtle items
150: such as your locale, timezone, termcap, and so on. Be aware of transitivity: even though you're
151: taking full precautions, programs you call directly won't necessarily. <strong>Never</strong>
152: use <code>system</code> in privileged programs, build your command line, a controlled environment,
153: and call <code>execvp</code> directly. The <code>perlsec</code> man page is a good tutorial on
154: such problems.
155:
156: <li>Never used setuid shell-scripts. These are inherently insecure. Wrap them into some C code
157: that ensures a proper environment. On the other hand, OpenBSD features secure perl scripts.
158:
159: <li>Beware the dynamic loader. If you are running setuid, it will only use trusted libraries
160: that were scanned with ldconfig. Setgid is not enough.
161:
162: <li>Dynamic libraries are tricky. C++ code sets a similar problem. Basically, libraries may take
163: some action based upon your environment before your main program even gets to check its setuid
164: status. OpenBSD <code>issetugid</code> addresses this problem, from the library writer point
165: of view. Don't try to port libraries unless you understand this issue thoroughly.
166: </ul>
167: <h3><font color=#0000e0>Generic porting hints</font></h3>
168: <ul>
169: <li><code>__OpenBSD__</code> should be used sparingly, if at all.
170: Constructs that look like
171: <pre>
172: #if defined(__NetBSD__) || defined(__FreeBSD__)
173: </pre>
174: are often inappropriate. Don't add blindly <code>__OpenBSD__</code>
175: to it. Instead, try to figure out what's going on, and what actual
176: feature is needed. Manual pages are often useful, as they include
177: historic comments, stating when a particular feature was incorporated
178: into BSD. Checking the numeric value of <code>BSD</code> against known
179: releases is often the right way. See
180: <a href="http://www.netbsd.org/Documentation/netbsd/Packages.txt">the NetBSD package guide</a>
181: for more information.
182: <li>Defining <code>BSD</code> is a bad idea. Try to include <code>sys/param.h</code>.
183: This not only defines <code>BSD</code>, it also gives it a proper value.
184: The right code fragment should look like:
185: <pre>
186: #if (defined(__unix__) || defined(unix)) && !defined(USG)
187: #include <sys/param.h>
188: #endif
189: </pre>
190: <li>Test for features, not for specific OSes. In the long run, it is much
191: better to test whether <code>tcgetattr</code> works than whether you're running
192: under BSD 4.3 or later, or SystemVR4. These kind of tests just confuse the
193: issue. The way to go about it is, for instance, to test for one particular
194: system, set up a slew of <code>HAVE_TCGETATTR</code> defines, then proceed to
195: the next system. This technique separates features tests from specific OSes.
196: In a hurry, another porter can just add the whole set of <code>-DHAVE_XXX</code>
197: defines to the Makefile. One may also write or add to a configure script to check for
198: that feature and add it automatically. As an example not to follow, check nethack 3.2.2
199: source: it assumes loads of things based on the system type. Most of these assumptions
200: are obsolete and no longer reflect reality: POSIX functions are more useful than older
201: BSD versus SystemV differences, to the point that some traditional bsd functions are
202: now only supported through compatibility libraries.
203:
204: <li>Avoid include files that include other includes that... First, because
205: this is inefficient. Your project will end up including a file that includes
206: everything. Also, because it makes some problems difficult to correct. It
207: becomes harder to <em>not</em> include one particular file at a given point.
208:
209: <li>Avoid bizarre macro tricks. Undefining a macro that was defined by a
210: header file is a bad idea. Defining macros to get some specific behavior
211: from an include file is also a bad idea: compilation modes should be global.
212: If you want POSIX behavior, say so, and <code>#define POSIX_C_SOURCE</code>
213: throughout the whole project, not when you feel like it.
214:
215: <li>Don't ever write system function prototypes. All modern systems,
216: OpenBSD included, have a standard location for these prototypes. Likely
217: places include <code>unistd.h</code>, <code>fcntl.h</code> or <code>termios.h</code>.
218: The man page frequently states where the prototype lies. You might need
219: another slew of <code>HAVE_XXX</code> macros to procure the right file.
220: Don't worry about including the same file twice, include files have guards
221: that prevent all kinds of nastiness.<br>
222: If some broken system needs you to write the prototype, don't impose
223: on all other systems. Roll-your-own prototypes will break because they may
224: use types that are equivalent on your system, but are not portable to other
225: systems (<code>unsigned long</code> instead of <code>size_t</code>), or get some
226: <code>const</code> status wrong. Also, some compilers, such as OpenBSD's own gcc,
227: are able to do a better job with some very frequent functions such as
228: <code>strlen</code> if you include the right header file.
229:
230: <li>Don't use the name of a standard system function for anything else.
231: If you want to write your own function, give it its own name, and call that
232: function everywhere. If you wish to revert to the default system function,
233: you just need to comment your code out, and define your own name to the
234: system function. Don't do it the other way round. Code should look like this
235: <pre>
236: /* prototype part */
237: #ifdef USE_OWN_GCVT
238: char *foo_gcvt(double number, size_t ndigit, char *buf);
239: #else
240: /* include correct file */
241: #include <stdlib.h>
242: /* use system function */
243: #define foo_gcvt gcvt
244: #endif
245:
246: /* definition part */
247: #ifdef USE_OWN_GCVT
248: char *foo_gcvt(double number, size_t ndigit, char *buf)
249: {
250: /* proper definition */
251: }
252:
253: /* typical use */
254: s = foo_gcvt(n, 15, b);
255: </pre>
1.1 marc 256: </ul>
257: <h3><font color=#0000e0>Other Helpful Hints</font></h3>
258: <ul>
259: <li>Do not assume <code>/usr/local/bin</code> or
260: <code>/usr/X11R6/bin</code> is in the installers path. A good
261: way to verify this is to create and install your port while
262: running as <code>root</code> with only <code>/bin</code> and
263: <code>/usr/bin</code> in your path.
264: <li>Do NOT generate shared libraries for <code>${MACHINE_ARCH} ==
265: alpha</code>
266: <li>In OpenBSD <code>curses.h/libcurses/libtermlib</code> are the
267: ``new curses''. Change:<br>
268: <code>ncurses.h ==> curses.h</code><br>
269: <code>-lncurses ==> -lcurses</code><br>
1.11 millert 270: ``old (BSD) curses'' is available by defining <code>_USE_OLD_CURSES_</code>
271: before including <code>curses.h</code> (usually in a Makefile) and
272: linking with <code>-lcurses</code>.
273: <li>In OpenBSD, terminal discipline has been upgraded from the older BSD
274: <code>sgtty</code> to the newer POSIX <code>tcgetattr</code> family.
1.10 espie 275: Avoid the older style in new code. Some code may define <code>tcgetattr</code>
1.11 millert 276: to be a synonym for the older <code>sgtty</code>, but this is at best a stopgap
1.10 espie 277: measure on OpenBSD. The <code>xterm</code> source code is a very good example of
278: what not to do.
279: Try to get your system functionality right: you want a type that remembers
280: the state of your terminal (possible typedef), you want a function that
281: extracts the current state, and a function that sets the new state.
282: Functions that modify this state are more difficult, as they tend to vary
283: depending upon the system. Also, don't forget that you will have to handle
284: cases where you are not connected to the terminal, and that you need to
285: handle signals: not only termination, but also getting put in the background
286: you should leave your terminal in a sane state. Do your tests under an
287: older shell, such as sh, which does not reset the terminal in any way at
288: program's termination.
1.11 millert 289: <li>The newer termcap/terminfo and curses, as included with OpenBSD, include standard sequences
1.10 espie 290: for controlling color terminals. You should use these if possible, reverting
291: to standard ANSI color sequences on other systems. However, some terminal descriptions
292: have not been updated yet, and you may need to be able to specify these sequences manually.
293: This is the way vim5.1 handles it. This is not strictly necessary. Except for privileged
294: programs, it is generally possible to override a termcap definition through the
295: <code>TERMCAP</code> variable and get it to work properly.
296: <li>Signal semantics are tricky, and vary from one system to another. Use <code>sigaction</code>
297: to ensure a specific semantics, along with other system calls referenced in its manpage.
1.1 marc 298: </ul>
299: <hr>
1.6 pauls 300: <a href="index.html"><img height=24 width=24 src=back.gif border=0 alt=OpenBSD></a>
1.1 marc 301: <a href=mailto:www@openbsd.org>www@openbsd.org</a>
1.13 ! art 302: <br><small>$OpenBSD: porting.html,v 1.12 1998/08/18 02:53:36 deraadt Exp $</small>
1.1 marc 303: </body>
304: </html>